Skip to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

foreach PHP code help

Featured Replies

Here is what I am trying to do.

I am currently at a page where I have a recordset of items. Each of these items have a field called SubCategory2 which is the category they fall under.

I am trying to add all the SubCategory2s that are the same so I can display something like Ladder Racks(3), Ladder Racks being the subcategory and 3 being the number of items with that subcategory.

I think i could probably achieve this with a foreach statement my pseudo code would be something like this:

for each SubCategory2 as subcategory

add every subcategory that is exactly the same

then echo subcategory(#ofcategories that were added)

with help i ended up with this

<?php

$list = array();  

 foreach ($sublist_result->getRecords() as $sublit_row)  {

 

   if (!isset($sublit_row['SubCategory2']))  {

 

    $list[$sublit_row['SubCategory2']] = 1;

  

    } else  {

 

  $list[$sublit_row['SubCategory2']] ++;

 

  }

 

 }  

foreach ($list as $name => $value)  {



 echo $name . "(".$value.")n";

 

 } ?>

i get this error Fatal error: Cannot use object of type FileMaker_Record as array in C:inetpubwwwrootdiscounttruckaccessoriesvlist.php on line 236

line 236 = if (!isset($sublit_row['SubCategory2'])) {

I believe the logic is fine just implementing into the filemkaer PHP API is whats killing me. can anyone help?

You're trying to use the FileMaker record / row objects as arrays... they're not.

Every row is a record object with a series of methods that let you access the data on the row as well as other information e.g. info on the fields in the record set, the layout etc.

To properly loop through you would do something like [using your code example above]:


<?php

$list = array();  

foreach ($sublist_result->getRecords() as $sublit_row)  {

	if ($sublit_row->getField('SubCategory2') == "")  

		$list[$sublit_row->getField('SubCategory2')] = 1;

	else 

		$list[$sublit_row->getField('SubCategory2')] ++;

}  

?>





... mind you the logic in the code above doesn't really make too much sense. If your trying to count the numbers in sub categories then your if test is incorrect and should be something like this instead:





$list = array();  

foreach ($sublist_result->getRecords() as $rec)  {

	$cat = $rec->getField('SubCategory2');

	if( !isset($list[$cat]) ) 

		$list[$cat] = 1;

	else 

		$list[$cat]++;

} 





... or in short hand





$list = array();  

foreach ($sublist_result->getRecords() as $rec)  {

	$cat = $rec->getField('SubCategory2');

	$list[$cat] = (!isset($list[$cat])) ? 1 : $list[$cat] +1 ;

}  

  • Author

I see that makes much more sense.

I never really worked with arrays, I always tried to avoid them but now since I'm trying to do more complicated things I'm going to try my best to learn them.

I am truly greateful for all your help.

Edited by Guest

Create an account or sign in to comment

Important Information

By using this site, you agree to our Terms of Use.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.