[SOLVED] PHP & mySQL populating HTML table
04-24-08, 10:14 AM
Newbie Coder
Join Date: Mar 2008
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] PHP & mySQL populating HTML table
using this code
PHP Code:
<?php
$value_array = array( 'item_type' ,
'title' ,
'brand' ,
'model' ,
'serial_no' ,
'replacement_cost' ,
'quantity' ,
'kit_content' ,
'main_location' ,
'current_location' ,
'notes' );
$inventory = mysql_fetch_array ( get_all_inventory ());
foreach ( $inventory as $row ){
$output = "<tr>" ;
foreach( $value_array as $cell ){
$output .= "<td> { $row [ $cell ]</ td > ";
};
$output .= " </ tr > ";
}
echo $output ;
?>
But all it seems to do is output the letter 'U' for one row of cells, rather than the content of my table
here is the code for the function get_all_inventory();
04-24-08, 10:22 AM
Wannabe Coder
Join Date: Apr 2008
Location: Leeds, UK
Posts: 116
Thanks: 0
Thanked 0 Times in 0 Posts
There needs to be a closing } not sure it wil fix it;
04-24-08, 10:28 AM
Newbie Coder
Join Date: Mar 2008
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
oops
and no, didn't fix it!!
is it the method that is flawed?
04-24-08, 10:43 AM
Wannabe Coder
Join Date: Apr 2008
Location: Leeds, UK
Posts: 116
Thanks: 0
Thanked 0 Times in 0 Posts
Just fetching the array once will just output one value so something like this usually:
PHP Code:
<?php $value_array = array( 'item_type' , 'title' , 'brand' , 'model' , 'serial_no' , 'replacement_cost' , 'quantity' , 'kit_content' , 'main_location' , 'current_location' , 'notes' ); $result = get_all_inventory (); while( $inventory = mysql_fetch_array ( $result )); { foreach( $value_array as $cell ) $output .= "<td> { $inventory [ $cell ]} </td>" ; }; $output .= "</tr>" ; } echo $output ; ?>
That should output all values. Although you will need to add the table header to output before the while loop
Edit:Changed the code since I think get_all_inventory() will reset the result all the time... causing an infinite loop!
Last edited by de.monkeyz; 04-24-08 at 10:50 AM .
04-24-08, 10:50 AM
Newbie Coder
Join Date: Mar 2008
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
used this (just a little doctored version of yours) and it hangs Safari
04-24-08, 10:52 AM
Wannabe Coder
Join Date: Apr 2008
Location: Leeds, UK
Posts: 116
Thanks: 0
Thanked 0 Times in 0 Posts
Yeah it's an infinite loop, I changed my code in the prev post coz I realised that
04-24-08, 10:54 AM
Newbie Coder
Join Date: Mar 2008
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
where is the change? I can't see why its an infinite loop?!?
04-24-08, 10:59 AM
Newbie Coder
Join Date: Mar 2008
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
right, used this and it worked
PHP Code:
<?php $value_array = array( 'item_type' , 'title' , 'brand' , 'model' , 'serial_no' , 'replacement_cost' , 'quantity' , 'kit_content' , 'main_location' , 'current_location' , 'notes' ); $result = get_all_inventory (); while( $inventory = mysql_fetch_array ( $result )) { $output = "<tr>" ; foreach( $value_array as $cell ){ $output .= "<td> { $inventory [ $cell ]} </td>" ; }; $output .= "</tr>" ; echo $output ; } ?>
04-24-08, 11:09 AM
Wannabe Coder
Join Date: Apr 2008
Location: Leeds, UK
Posts: 116
Thanks: 0
Thanked 0 Times in 0 Posts
So no problems then? The reason it would be an infinite loop is because you would keep called get_all_inventory each time, and restart the data from entry 1, so you just get the first item over and over
04-24-08, 11:12 AM
Newbie Coder
Join Date: Dec 2007
Location: The burning depths of hell
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
hehe, that's never wanted in a script, especially if it's got a massive max runtime
__________________
The Demon
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off