Page 1 of 1
foreach PHP code help
Posted: Sat Dec 12, 2009 9:25 am
by stevenm187

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)
I believe is possible but my PHP skills are not up there yet...can somebody help?
Re: foreach PHP code help
Posted: Sat Dec 12, 2009 10:27 am
by cpetercarter
Code: Select all
$list = array();
foreach ($items as $item) {
if (!isset($item['subcategory2'])) {
$list[$item['subcategory2']] = 1;
} else {
$list[$item['subcategory2']] ++;
}
}
foreach ($list as $name => $value) {
echo $name . "(".$value.")\n";
}
Re: foreach PHP code help
Posted: Sat Dec 12, 2009 12:39 pm
by stevenm187
That looks like what I need I am implementing it now.
I have to tweak it a bit because i use this PHP API for filemaker which is the database software I am using.
So my record set looks actually like this
Code: Select all
$sublist_find = $Rackspace->newFindCommand('List Pages Layout');
$sublist_findCriterions = array('VehicleCategory'=>$_GET['VehicleCategory'],);
foreach($sublist_findCriterions as $key=>$value) {
$sublist_find->AddFindCriterion($key,$value);
}
fmsSetPage($sublist_find,'sublist',10);
$sublist_result = $sublist_find->execute();
if(FileMaker::isError($sublist_result)) fmsTrapError($sublist_result,"error.php");
fmsSetLastPage($sublist_result,'sublist',10);
$sublist_row = current($sublist_result->getRecords());
// FMStudio v1.0 - do not remove comment, needed for DreamWeaver support ?>
So to call on the subcatergory2 field:
$sublist_row->getField('SubCategory2')
Re: foreach PHP code help
Posted: Sat Dec 12, 2009 1:31 pm
by stevenm187
In my ignorance i thought something like this might work
Code: Select all
$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";
}
but is not i get this error Fatal error: Cannot use object of type FileMaker_Record as array in C:\inetpub\wwwroot\discounttruckaccessories\vlist.php on line 236
line 236 = if (!isset($sublit_row['SubCategory2'])) {
