davids_media Posted April 10, 2012 Share Posted April 10, 2012 I have a problem with some code at the moment <?php //Set number of columns to use $maxCols = 3; error_reporting(E_ALL ^ E_NOTICE); ini_set("display_errors", 1); require ('includes/config.inc.php'); include ('./includes/header.html'); require (MYSQL); include ('./includes/main.html'); if($id = isset($_GET['catID'])) { //Create and run query to get product category names for the selected cat ID $query = "SELECT `product`.`prodID`, `product`.`product`, `category`.`cat`, `product`.`prod_descr`, `category`.`cat_descr`, `product`.`price`, `product`.`image` FROM `product` LEFT JOIN `category` ON `product`.`catID` = `category`.`catID` WHERE `product`.`catID`='{$_GET['catID']}' ORDER BY `product`.`product`"; $r = mysqli_query($dbc, $query); $num_rows = mysqli_num_rows($r); $showHeader = true; echo "<div id='right'>"; while($row = mysqli_fetch_array($r)) { if($showHeader) { //Display category header echo "<h1>" . "<span>" . "# " . "</span>" . $row['cat'] . "<span>" . " #" . "</span>" . "</h1>"; echo "<h2>" . $row['cat_descr'] . "</h2>"; $showHeader = false; } //Open table echo "<table>"; //Set index var to track record count $recIdx = 0; while($row = mysqli_fetch_array($r)) { $recIdx++; //Open new row if needed if($recIdx % $maxCols == 1) { echo "<tr>"; } //Display product echo "<td>"; echo "<img src='db/images/".$row['image']."' height=150px width=150px /><br>"; echo "<li>" . "<a href='item.php?prodID={$row['prodID']}'>" . $row['product'] . "</a>" . "</li>"; echo "<span>" . "£". $row['price'] . "</span>"; echo "</td>\n"; //Close row if needed if($recIdx % $maxCols == 0) { echo "</tr>\n"; } } //Close last row if needed if($recIdx % $maxCols == 0) { echo "</tr>\n"; } //Close table & div echo "</table>"; echo "$num_rows Rows\n"; echo "</div>"; } } include ('./includes/footer.html'); ?> I want to retrieve all my records based from my query search, however, it ignores the first record in the set and goes straight into the second record. I have six records based on this search, however, only five records are displayed even though I have done a mysql_num_rows which returns a value to state that there are six records. how do i solve this problem Link to comment https://forums.phpfreaks.com/topic/260683-problem-with-mysqli_fetch_array-not-displaying-first-record-from-select-query/ Share on other sites More sharing options...
Muddy_Funster Posted April 10, 2012 Share Posted April 10, 2012 $recIdx = 0; while($row = mysqli_fetch_array($r)) { $recIdx++; You are incrementing $recIdx before displaying anything. Above that, if you move the $recIdx = 0; out before your first while loop, there will be no need for the second one. Link to comment https://forums.phpfreaks.com/topic/260683-problem-with-mysqli_fetch_array-not-displaying-first-record-from-select-query/#findComment-1336074 Share on other sites More sharing options...
davids_media Posted April 10, 2012 Author Share Posted April 10, 2012 I have just tried moving that out, but it has made absolutely no difference whatsoever Link to comment https://forums.phpfreaks.com/topic/260683-problem-with-mysqli_fetch_array-not-displaying-first-record-from-select-query/#findComment-1336076 Share on other sites More sharing options...
Muddy_Funster Posted April 10, 2012 Share Posted April 10, 2012 erm...what did you do exactly? Link to comment https://forums.phpfreaks.com/topic/260683-problem-with-mysqli_fetch_array-not-displaying-first-record-from-select-query/#findComment-1336080 Share on other sites More sharing options...
DavidAM Posted April 10, 2012 Share Posted April 10, 2012 while($row = mysqli_fetch_array($r)) { if($showHeader) { //Display category header $showHeader = false; } while($row = mysqli_fetch_array($r)) { You are never actually displaying the data from the FIRST fetch before you start a NEW loop with a second FETCH. There is no need for the nested while loop. Move the <TABLE> tag and everything else you want to do before the first row up into the if($showHeader). Then remove the second while() statement (and it's closing curly-brace). Note: your closing TABLE tag is going to have to come out of the loop, as well as the closing DIV Link to comment https://forums.phpfreaks.com/topic/260683-problem-with-mysqli_fetch_array-not-displaying-first-record-from-select-query/#findComment-1336082 Share on other sites More sharing options...
davids_media Posted April 10, 2012 Author Share Posted April 10, 2012 i have managed to get all rows back (i disposed of the second while loop), however, my table layout (see this post: http://www.phpfreaks.com/forums/index.php?topic=357406.0) has now messed up completely here is the code now <?php //Set number of columns to use $maxCols = 3; error_reporting(E_ALL ^ E_NOTICE); ini_set("display_errors", 1); require ('includes/config.inc.php'); include ('./includes/header.html'); require (MYSQL); include ('./includes/main.html'); if($id = isset($_GET['catID'])) { //Create and run query to get product category names for the selected cat ID $query = "SELECT `product`.`prodID`, `product`.`product`, `category`.`cat`, `product`.`prod_descr`, `category`.`cat_descr`, `product`.`price`, `product`.`image` FROM `product` LEFT JOIN `category` ON `product`.`catID` = `category`.`catID` WHERE `product`.`catID`='{$_GET['catID']}' ORDER BY `product`.`product`"; $r = mysqli_query($dbc, $query); $showHeader = true; echo "<div id='right'>"; while($row = mysqli_fetch_array($r)) { if($showHeader) { echo "<table>"; //Display category header echo "<h1>" . "<span>" . "# " . "</span>" . $row['cat'] . "<span>" . " #" . "</span>" . "</h1>"; echo "<h2>" . $row['cat_descr'] . "</h2>"; $showHeader = false; //Set index var to track record count $recIdx = 0; $recIdx++; //Open new row if needed if($recIdx % $maxCols == 1) { echo "<tr>"; } } //Display product echo "<td>"; echo "<img src='db/images/".$row['image']."' height=150px width=150px /><br>"; echo "<li>" . "<a href='item.php?prodID={$row['prodID']}'>" . $row['product'] . "</a>" . "</li>"; echo "<span>" . "£". $row['price'] . "</span>"; echo "</td>"; //Close row if needed if($recIdx % $maxCols == 0) { echo "</tr>"; } } //Close last row if needed if($recIdx % $maxCols == 0) { echo "</tr>"; } //Close table & div } echo "</table>"; echo "</div>"; include ('./includes/footer.html'); ?> Link to comment https://forums.phpfreaks.com/topic/260683-problem-with-mysqli_fetch_array-not-displaying-first-record-from-select-query/#findComment-1336086 Share on other sites More sharing options...
davids_media Posted April 10, 2012 Author Share Posted April 10, 2012 I've managed to sort thankfully, all i needed to do to retain my columns was to adjust some css inline display, thank you muddy_funster and david am for your help Link to comment https://forums.phpfreaks.com/topic/260683-problem-with-mysqli_fetch_array-not-displaying-first-record-from-select-query/#findComment-1336102 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.