PDA

View Full Version : getting info from and presenting my array content


CraigR
05-06-2011, 06:17 AM
My client sells machine parts, each type having a different set of specifications.

I wish to display the item specs for each item on the item detail page

I have set my sql select statement to load all of the fields from the spec table, (the table queried depends on the item type).

This returns all of the specs for the product I need, returning a single row.

As each spec table has a different number of fields, I thought the best approach would be to store the recordset row in an array, with the field names as the keys, and the data as values.

This means I can have one page for my item detail, regardless of the item type.

So far so good.


I can do a print_r on the array and see my data, what I cannot do is loop through the array and show the values in a tabular form

Jason Byrnes
05-06-2011, 06:59 AM
this code looks correct to me:
<?php
foreach ($specificationsarray as $header => $value) {
echo "Header: $header; Value: $value<br />\n";
}
?>

What exactly is the problem you are having getting the array to show the values in a tabular form?

what is the result of the <?php print_r($specificationsarray); ?> code? This will give me some idea of the data you are working with.

Your foreach loop[ looks like it should do what you are asking about, so I guess I need clarification on what you are expecting from the foreach loop that it is not giving.

CraigR
05-06-2011, 07:01 AM
the result of print_r is as follows, (for example, product id number 2)

Array ( [2] => Array ( [AnchorID] => 2 [ItemID] => 2 [StockNumber] => CSA-60 [ThreadClass2A] => 8-32 [APM015] => .875 [BPM03] => .700 [CPM01] => .080 [DPM01] => .095 [EPM005] => .070 [FPM01] => .16 [GPM01] => .07 [Material] => Steel ) )

I think the problem may be that the array is multidimensional ?? , but this was the closest i got after a few tries.

Jason Byrnes
05-06-2011, 07:27 AM
OK, I see.


a recordset is already an array. you dont need to convert it by using a loop.

change:

do {
$specificationsarray[$row_rsspecifications['ItemID']] = $row_rsspecifications;
} while ($row_rsspecifications = mysql_fetch_assoc($rsspecifications));

to:

$specificationsarray = $row_rsspecifications;

CraigR
05-06-2011, 07:31 AM
Thats it!

I'm overcomplicating things as usual.

Cheers