Jump to content

for while function didn't show data completely


gongpex

Recommended Posts

Hello everyone, I tried to show all of my data using while and for function like this code :

<?$sel = mysql_query("select * from book");$bk_array = mysql_fetch_array($sel);$b_id = $bk_array[id];$b_title = $bk_array[bk_title];$b_price = $bk_array[bk_price];$b_img = $bk_array[bk_img];$b_list = mysql_num_rows($sel); // total product?><?$x=0;while($product=mysql_fetch_row($sel)){$a[$x][0] = $product[0]; //id$a[$x][1] = $product[5]; //title$a[$x][2] = $product[11]; //price$a[$x][3] = $product[13]; //img$x++;}?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Our Product</title><link href="admin/adm-index.css" rel="stylesheet" type="text/css" /></head><body><?for($y=0;$y<$b_list;$y++){echo"<div align=\"center\"><ul type=\"square\">  <li><div align=\"center\"><img src=\"".$a[$y][3]."\" width=\"auto\" height=\"auto\" /></div></li>  <li>Book Title : ".$a[$y][1]."</li>  <li>Book Price : <strong>".$a[$y][2]."</strong></li></ul><br /></div>";}?></body></html>

When I check using command like this :

<?echo"".$a[1][1]."";?>

it can display data But when I check using :

<?echo"".$a[0][1]."";?>

It won't show anything, Usually for function always display data start from 0, but on my case it display data start from 1, What I must do? Please someone help me Thanks

Link to comment
Share on other sites

Hello everyone, I tried to show all of my data using while and for function like this code :
<?$sel = mysql_query("select * from book");$bk_array = mysql_fetch_array($sel);$b_id = $bk_array[id];$b_title = $bk_array[bk_title];$b_price = $bk_array[bk_price];$b_img = $bk_array[bk_img];$b_list = mysql_num_rows($sel); // total product?><?$x=0;while($product=mysql_fetch_row($sel)){$a[$x][0] = $product[0]; //id$a[$x][1] = $product[5]; //title$a[$x][2] = $product[11]; //price$a[$x][3] = $product[13]; //img$x++;}?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Our Product</title><link href="admin/adm-index.css" rel="stylesheet" type="text/css" /></head><body><?for($y=0;$y<$b_list;$y++){echo"<div align=\"center\"><ul type=\"square\">  <li><div align=\"center\"><img src=\"".$a[$y][3]."\" width=\"auto\" height=\"auto\" /></div></li>  <li>Book Title : ".$a[$y][1]."</li>  <li>Book Price : <strong>".$a[$y][2]."</strong></li></ul><br /></div>";}?></body></html>

When I check using command like this :

<?echo"".$a[1][1]."";?>

it can display data But when I check using :

<?echo"".$a[0][1]."";?>

It won't show anything, Usually for function always display data start from 0, but on my case it display data start from 1, What I must do? Please someone help me Thanks

i dont understand the way you code, but if your using mysql_fetch_array then your pulling information from the database, so you going to need the table of which your pulling the information from, and the columns names
Link to comment
Share on other sites

<li>Book Title : ".$a[$y][1]."</li> you should use a <?php ?> to echo out the . $a[$y][1] . in the html code

Link to comment
Share on other sites

but if your using mysql_fetch_array then your pulling information from the database, so you going to need the table of which your pulling the information from, and the columns names
Table? Did you mean : <table></table> (html table?) please help Thanks
Link to comment
Share on other sites

You should initialize $a and each sub-array:

$x=0;$a = array();while($product=mysql_fetch_row($sel)){  $a[$x] = array();  ...

Also, when you call mysql_fetch_array here: $sel = mysql_query("select * from book");$bk_array = mysql_fetch_array($sel); You are moving the pointer past the first record. So the while loop will not get that first record because you already got it. Since you're not using any of these variables anywhere else: $b_id = $bk_array[id];$b_title = $bk_array[bk_title];$b_price = $bk_array[bk_price];$b_img = $bk_array[bk_img]; It would probably be best to just remove all of those lines and the first call to mysql_fetch_array, so that the while loop loops over all rows. Otherwise, you can use mysql_data_seek to reset the row pointer so that it starts at the first row again: http://www.php.net/manual/en/function.mysql-data-seek.php

Link to comment
Share on other sites

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...