Group by category

7 posts by 2 authors in: Forums > CMS Builder
Last Post: June 21, 2013   (RSS)

By Toledoh - June 19, 2013

Hi Guys,

Can anyone see why I'm getting a a bunch of errors "Warning: in_array(): Wrong datatype for second argument in /home/toledoh1/public_html/_dashboard.php on line 40"  here?

// load records from 'categories'
list($categoriesRecords, $categoriesMetaData) = getRecords(array(
'tableName' => 'categories',
'loadUploads' => false,
'allowSearch' => false,
));

// load records from 'links'
list($linksRecords, $linksMetaData) = getRecords(array(
'tableName'   => 'links',
'loadUploads' => true,
'allowSearch' => false,
));


$categories = mysql_select ('categories');
//make the array key the 'num' field
$categoriesSorted = array_groupBy($categories,'num');


?>

<ul>
<?php foreach ($categoriesRecords as $category): ?>
<li>
<?php echo htmlencode($category['title']) ?>
<ul><?php foreach ($linksRecords as $record): ?>
<?php if(in_array($category['num'], $record['category'])): ?>
<li>
<a href="<?php echo $record['_link'] ?>"><?php echo htmlencode($record['title']) ?></a>
</li>
<?php endif ?>
<?php endforeach ?>
</ul>
</li>
<?php endforeach ?>

Cheers,

Tim (toledoh.com.au)

By gregThomas - June 20, 2013

Hi Tim,

I think the problem is that $record['category'] isn't an array. I think you need to check the category:values array instead:

 <?php if(in_array($category['num'], $record['category:values'])): ?>
  <li><a href="<?php echo $record['_link'] ?>"><?php echo htmlencode($record['title']) ?></a></li>
<?php endif ?>

This is assuming that the categories array is a multi value list.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By gregThomas - June 20, 2013

Hi Tim,

What happens if you do a showme on the linkRecords variable? Is it outputting any arrays for the categories field?

<?php 
// load records from 'categories'
list($categoriesRecords, $categoriesMetaData) = getRecords(array(
'tableName' => 'categories',
'loadUploads' => false,
'allowSearch' => false,
));

// load records from 'links'
list($linksRecords, $linksMetaData) = getRecords(array(
'tableName'   => 'links',
'loadUploads' => true,
'allowSearch' => false,
));


$categories = mysql_select ('categories');
//make the array key the 'num' field
$categoriesSorted = array_groupBy($categories,'num');

//Display the contents of the links section.
showme($linksRecords[0]); 
exit;

?>

This should display the first record of the links section, and show what content is being stored for the categories field. Could you post what is output from the function?

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Toledoh - June 20, 2013

Array ( [_filename] => Indie-Server [_link] => javascript:alert('Set Detail Page Url for this section in: Admin > Section Editors > Viewer Urls') [_tableName] => links [category] => 1 [category:label] => Server [content] => [createdByUserNum] => 1 [createdDate] => 2013-06-15 08:07:42 [num] => 1 [title] => Indie Server [updatedByUserNum] => 1 [updatedDate] => 2013-06-20 08:54:59 [url] => https://64.207.150.140:8443/login_up.php3 [createdBy.num] => 1 [createdBy.createdDate] => 2013-02-24 23:28:28 [createdBy.createdByUserNum] => 0 [createdBy.updatedDate] => 2013-02-24 23:28:28 [createdBy.updatedByUserNum] => 0 [createdBy.fullname] => Tim Forrest [createdBy.email] => tim@toledoh.com.au [createdBy.username] => toledoh [createdBy.lastLoginDate] => 2013-06-21 09:52:39 [createdBy.expiresDate] => 0000-00-00 00:00:00 [createdBy.neverExpires] => 1 [createdBy.isAdmin] => 1 [createdBy.disabled] => 0 [createdBy.accessList] => [createdBy._filename] => [createdBy._link] => javascript:alert('Set Detail Page Url for this section in: Admin > Section Editors > Viewer Urls') )

Cheers,

Tim (toledoh.com.au)

By gregThomas - June 21, 2013

Hi Tim,

The issue is that the category field looks to be single selection list as opposed to a multi selection list, so an array of selected items is not created by the getRecords function. I think you need to modify your code so that it just checks if the selected category items matches the records category num:

<?php

  // load records from 'categories'
  list($categoriesRecords, $categoriesMetaData) = getRecords(array(
  'tableName' => 'categories',
  'loadUploads' => false,
  'allowSearch' => false,
  ));

  // load records from 'links'
  list($linksRecords, $linksMetaData) = getRecords(array(
  'tableName'   => 'links',
  'loadUploads' => true,
  'allowSearch' => false,
  ));


  $categories = mysql_select ('categories');
  //make the array key the 'num' field
  $categoriesSorted = array_groupBy($categories,'num');


?>

<ul>
<?php foreach ($categoriesRecords as $category): ?>
<li>
<?php echo htmlencode($category['title']) ?>
<ul><?php foreach ($linksRecords as $record): ?>
<?php if($category['num'] == $record['category']): ?>
<li>
<a href="<?php echo $record['_link'] ?>"><?php echo htmlencode($record['title']) ?></a>
</li>
<?php endif ?>
<?php endforeach ?>
</ul>
</li>
<?php endforeach ?>

So if the category num matches the selection in the category field in the list section, the link will be displayed.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Toledoh - June 21, 2013

Arrhhh.  I was sure it was multi-select.  Sorry for wasting your time Greg!  and thanks.

Cheers,

Tim (toledoh.com.au)