CSS
-Tricks
treehouse :
what would you like to learn today?
Web Design
Web Development
iOS Development
Show search box
Search
Search in:
All
Articles
Forums
Snippets
Videos
✕
Home
Forums
Snippets
Gallery
Videos
Almanac
Demos
Lodge
Navigation 'n' Search
Forums
Illustration by Nick Sirotich
Forums
»
PHP Problems
[Solved] Iterating through an associative array from SQL query
D3mon
Permalink to comment
#
March 2012
Hi guys,
I have an associative array (arrInfo) that I can't seem to get the data from. Here's the print_r output as an example:
Array ( [0] => Array ( [label_text] => Area of coverage [data] => 160 m� ) [1] => Array ( [label_text] => Dimensions [data] => 352 x 277 x 112 (mm) ) [2] => Array ( [label_text] => Finish [data] => Aluminium ) [3] => Array ( [label_text] => Lamp [data] => EcoLite energy saving 13 watt (�2) ) [4] => Array ( [label_text] => Lead length [data] => 1.1m ) [5] => Array ( [label_text] => Mounting [data] => Wall mount, ceiling suspend or free stand ) [6] => Array ( [label_text] => Power input [data] => 230Vac 50Hz (standard UK mains supply) ) [7] => Array ( [label_text] => Warranty period [data] => 1 year ) )
I need to iterate this array to create a HTML definition list (DL) with the DD term as the [label_text] and the DT as the [data value] for each row.
The code I have right now is:
foreach($arrInfo as $row) {
echo ''.$row['label_text'].''.$row['data'].'';
}
but it only returns the last row of the array (arrInfo)?
D3mon
Permalink to comment
#
March 2012
OK. Scratch that - it was working fine. I was building an output string instead of echo'ing and discovered that I was overwriting the string with each loop! DUH. :)
kgscott284
Permalink to comment
#
March 2012
lol, I was going to tell you that but you figured it out! Good work ;)
Add a Comment
I have an associative array (arrInfo) that I can't seem to get the data from. Here's the print_r output as an example:
Array ( [0] => Array ( [label_text] => Area of coverage [data] => 160 m� ) [1] => Array ( [label_text] => Dimensions [data] => 352 x 277 x 112 (mm) ) [2] => Array ( [label_text] => Finish [data] => Aluminium ) [3] => Array ( [label_text] => Lamp [data] => EcoLite energy saving 13 watt (�2) ) [4] => Array ( [label_text] => Lead length [data] => 1.1m ) [5] => Array ( [label_text] => Mounting [data] => Wall mount, ceiling suspend or free stand ) [6] => Array ( [label_text] => Power input [data] => 230Vac 50Hz (standard UK mains supply) ) [7] => Array ( [label_text] => Warranty period [data] => 1 year ) )
I need to iterate this array to create a HTML definition list (DL) with the DD term as the [label_text] and the DT as the [data value] for each row.
The code I have right now is:
foreach($arrInfo as $row) {
echo ''.$row['label_text'].''.$row['data'].'';
}
but it only returns the last row of the array (arrInfo)?