Been Here Before

14 posts by 2 authors in: Forums > CMS Builder
Last Post: April 10, 2018   (RSS)

By Mohaukla - April 5, 2018

I have created a list of categories and inserted it into a list of documents/links.

So my loading code looks like this:

  // load records from 'resource_links_docs'
  list($resource_links_docsRecords, $resource_links_docsMetaData) = getRecords(array(
    'tableName'   => 'resource_links_docs',
    'loadUploads' => true,
    'allowSearch' => false,
  ));

So I want to layout the doc/link lists by category:

First Category
Doc
Link
Link
Doc
Etc...

Second Category
Doc
Doc
Link
Doc
Etc...

And So on

So I have these variables:

<?php echo htmlencode($record['title']) ?>
      <?php echo join(', ', $record['category:values']); ?>
      <?php echo join(', ', $record['category:labels']); ?>
      <?php echo htmlencode($record['link']) ?>
       <?php foreach ($record['document'] as $index => $upload): ?>
        <?php echo htmlencode($upload['urlPath']) ?>
       <?php endforeach ?>

And would like to set it up like this:

<foreach...>
<h4><category:labels></h4>
 <if upload then...> 
   <?php foreach ($record['document'] as $index => $upload): ?>
     <a href="<?php echo htmlencode($upload['urlPath']) ?>"><p><?php echo htmlencode($upload['filename']) ?></p></a>
   <?php endforeach ?>    
 <else>
  <a href="<?php echo htmlencode($record['link']) ?>" target="_blank"><p><?php echo htmlencode($record['title']) ?></p></a>
<?php endforeach ?>

So I know this has been done by me before but I can not remember and can not seem to find the correct example to work off of.

Thanks for your help with this.

Michael

Michael Moyers



Owner of Just Rite Design Inc. A growing network of professionals in web design, graphic design, flash development, programming, and audio & video productions.



"Due to budget constraints, the Light at the end of the tunnel will be temporarily out!"

By leo - April 6, 2018

Hi Michael,

If you want to group the records by their category, you can use cmsb library function array_groupBy($array, $key). In your case you can call it like this: $records = array_groupBy($resource_links_docsRecords, 'category'); That will give you the record array that's grouped by their category, and do 2 foreach loop to get the result.

Hope that answers your question!

Thanks,

Leo - PHP Programmer (in training)
interactivetools.com

By Mohaukla - April 6, 2018

So what does that look like in my situation here.

I am not experienced enough to see that work out in code.

Thanks
Your help is greatly appreciated.

Michael

Michael Moyers



Owner of Just Rite Design Inc. A growing network of professionals in web design, graphic design, flash development, programming, and audio & video productions.



"Due to budget constraints, the Light at the end of the tunnel will be temporarily out!"

By leo - April 6, 2018

Hi Michael,

If you want an array of records that are grouped by their categories, call $records = array_groupBy($resource_links_docsRecords, 'category:label');

And your page will be something similar to this structure:

<?
foreach($records as $categoryLabel => $recordsByCategory){
  echo $categoryLabel; // HTML for the category label
  foreach($recordsByCategory as $record){
    showme($record); // HTML for each record
  }
}
?>

Hope that helps!

Leo - PHP Programmer (in training)
interactivetools.com

By Mohaukla - April 6, 2018

Ok

I have broken out what you did so that I can have more control over how I display it

But I'm getting errors and I am hoping it's because of how I am adding the strings.

   <? foreach($records as $categoryLabel => $recordsByCategory): // HTML for the category label?>
    <h4><p><?php echo ($categoryLabel['lables']) ?></p></h4>
   <? foreach($recordsByCategory as $record): ?>
    <ul>
<li><?php echo ($record['title']) // HTML for each record ?></li>
    </ul>
   <? endforeach ?>
   <hr/>
   <? endforeach ?>

The errors I'm getting back are:

Warning: Illegal string offset 'lables' in /home/homevaluere/public_html/resourcesTEST.php on line 34

  • Warning: Illegal string offset 'title' in /home/homevaluere/public_html/resourcesTEST.php on line 37 R
  • Notice: Undefined index: title in /home/homevaluere/public_html/resourcesTEST.php on line 37
  • Notice: Undefined index: title in /home/homevaluere/public_html/resourcesTEST.php on line 37
  • AND SO ON .....

I am closer than I was  ...

What is the problem? (Page attached)

Michael

Michael Moyers



Owner of Just Rite Design Inc. A growing network of professionals in web design, graphic design, flash development, programming, and audio & video productions.



"Due to budget constraints, the Light at the end of the tunnel will be temporarily out!"
Attachments:

resourcesTEST.php 3K

By leo - April 9, 2018

Hi Michael,

The label is just $categoryLabel not $categoryLabel['label']. And seems either your record doesn't have a 'title' field or it doesn't have a 'category' field. That gives you the second error. Fix those and you should be good!

Leo - PHP Programmer (in training)
interactivetools.com

By leo - April 10, 2018

Hi Michael,

Try changing your array_group function to this: array_groupBy($resource_links_docsRecords, 'category:label'); and it should be good!

Leo - PHP Programmer (in training)
interactivetools.com

By Mohaukla - April 10, 2018

OK 

One step closer.

We got rid of the errors but it seems to skip over your code altogether.

Here is what I have: (the main chunks LOL)

  // load records from 'resource_links_docs'
  list($resource_links_docsRecords, $resource_links_docsMetaData) = getRecords(array(
    'tableName'   => 'resource_links_docs',
    'loadUploads' => true,
    'allowSearch' => false,
  ));
  
  $records = array_groupBy($resource_links_docsRecords, 'category:label');



    <h1>Leo's Code</h1>
   <? foreach($records as $categoryLabel => $recordsByCategory): // HTML for the category label?>
    <h4><p><?php echo ($categoryLabel) ?></p></h4>
   <? foreach($recordsByCategory as $record): ?>
    <ul>
<li><?php echo ($record['title']) // HTML for each record ?></li>
    </ul>
   <? endforeach ?>
   <hr/>
   <? endforeach ?>

    <h1>Regular Viewer Code</h1>
    <?php foreach ($resource_links_docsRecords as $record): ?>
    
      Record Number: <?php echo htmlencode($record['num']) ?><br/>
      Title: <?php echo htmlencode($record['title']) ?><br/>
      Category (values): <?php echo join(', ', $record['category:values']); ?><br/>
      Category (labels): <?php echo join(', ', $record['category:labels']); ?><br/>
      Link: <?php echo htmlencode($record['link']) ?><br/>

      <hr/>
    <?php endforeach ?>

    <?php if (!$resource_links_docsRecords): ?>
      No records were found!<br/><br/>
    <?php endif ?>

You can see the result here:

http://homevaluerealestate.biz/resourcesTEST.php

Thanks 

Michael

Michael Moyers



Owner of Just Rite Design Inc. A growing network of professionals in web design, graphic design, flash development, programming, and audio & video productions.



"Due to budget constraints, the Light at the end of the tunnel will be temporarily out!"

By leo - April 10, 2018

Hi Michael,

Try this: array_groupBy($resource_links_docsRecords, 'category:label', true);

Leo - PHP Programmer (in training)
interactivetools.com