Jump to content

foreach PHP code help


This topic is 5271 days old. Please don't post here. Open a new topic instead.

Recommended Posts

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?

Link to comment
Share on other sites

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 ;

}  

Link to comment
Share on other sites

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
Link to comment
Share on other sites

This topic is 5271 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

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