rubbertoad Posted April 3, 2009 Share Posted April 3, 2009 Hi everyone. I'm learning PHP and I am trying to display the content of a table. It works except it always skips the first row. This is the code I am using: $fields_num = mysql_num_fields($rsTable); echo "<table border='1'><tr>"; // printing table headers for($i=0; $i<$fields_num; $i++) { $field = mysql_field_name($rsTable,$i); echo "<td>$field</td>"; } echo "</tr>"; while ($get_info = mysql_fetch_row($rsTable)){ echo $get_info; echo "<tr>\n"; foreach ($get_info as $field) echo "\t<td><font face=arial size=1/>$field</font></td>\n"; echo "</tr>\n"; } echo "</table>\n"; What am I doing wrong? Thanks a bunch Roo Link to comment https://forums.phpfreaks.com/topic/152467-solved-displaying-table-content-skips-first-row/ Share on other sites More sharing options...
Yesideez Posted April 3, 2009 Share Posted April 3, 2009 Why are you echo'ing out the field name? I'd advise that to be set inside the code. <table> <tr><td>Set It Here</td></tr> <?php $query=mysql_query("SELECT * FROM table WHERE clause"); while ($row=mysql_fetch_assoc($query)) { echo '<tr>'; foreach ($row as $r) { echo '<td>'.$r.'</td>'; } echo '</tr>'; } ?> </table> Link to comment https://forums.phpfreaks.com/topic/152467-solved-displaying-table-content-skips-first-row/#findComment-800746 Share on other sites More sharing options...
rubbertoad Posted April 3, 2009 Author Share Posted April 3, 2009 Thanks for the reply. I fixed it and tried the code you suggested. Even with your code, the page skips the first row in the table. I'm really confused. Rob Link to comment https://forums.phpfreaks.com/topic/152467-solved-displaying-table-content-skips-first-row/#findComment-800752 Share on other sites More sharing options...
Psycho Posted April 3, 2009 Share Posted April 3, 2009 Why are you echo'ing out the field name? I'd advise that to be set inside the code. It could be that he is using this code to display the results of various database queries - such as for debugging purposes. If not, then I agree, hard code the column names. You could do somthing like this: if (mysql_num_rows($query)) { echo "<table border=\"1\">\n"; $first_record = true; while($record = mysql_fetch_assoc($query)) { //Print table headers if first record if ($first_record) { $first_record = false; echo "<tr>"; foreach($record as $label => $value) { echo "<td>$label</td>"; } echo "</tr>\n"; } //Print table contents echo "<tr>"; foreach($record as $value) { echo "<td>$value</td>"; } echo "</tr\n>"; } } Link to comment https://forums.phpfreaks.com/topic/152467-solved-displaying-table-content-skips-first-row/#findComment-800755 Share on other sites More sharing options...
rubbertoad Posted April 3, 2009 Author Share Posted April 3, 2009 The page finds the table it will be displaying by receiving the name of the table via a variable. I then want to display a table that shows the name of the fields at the top and the content of each row below. I am able to show the field names, but the content below start on the second row instead of the first row for some weird reason. Link to comment https://forums.phpfreaks.com/topic/152467-solved-displaying-table-content-skips-first-row/#findComment-800758 Share on other sites More sharing options...
rubbertoad Posted April 3, 2009 Author Share Posted April 3, 2009 here is the code now, but it still shows the fields at the top correctly and then it skips the first row :-\: echo "<table border='1'><tr>"; // printing table headers for($i=0; $i<$fields_num; $i++) { $field = mysql_field_name($rsTable,$i); echo "<td>$field</td>"; } echo "</tr>"; while ($row=mysql_fetch_row($rsTable)) { echo '<tr>'; foreach ($row as $r) { echo "<td>$r</td>"; } echo '</tr>'; } echo "</table>\n"; ?> Link to comment https://forums.phpfreaks.com/topic/152467-solved-displaying-table-content-skips-first-row/#findComment-800760 Share on other sites More sharing options...
Psycho Posted April 3, 2009 Share Posted April 3, 2009 Then you have a line of code somewhere that is using up that first record. Show the code that starts from the actual query to where you actually start displaying the records. I revised your code above slightly and there is no reasons it shouldn't work. This worked fine for me if (mysql_num_rows($rsTable)) { //Open the table echo "<table border=\"1\">\n"; //Print the table headers echo "<tr>"; for($i=0; $i< mysql_num_fields($rsTable); $i++) { echo "<td>".mysql_field_name($rsTable, $i)."</td>"; } echo "<tr>\n"; //Print the records while($record = mysql_fetch_assoc($rsTable)) { //Print table contents echo "<tr>"; foreach($record as $value) { echo "<td>$value</td>"; } echo "</tr>\n"; } //Close the table echo "</table>\n"; } Link to comment https://forums.phpfreaks.com/topic/152467-solved-displaying-table-content-skips-first-row/#findComment-800761 Share on other sites More sharing options...
rubbertoad Posted April 3, 2009 Author Share Posted April 3, 2009 here it is. I have not incorporated your changes yet, but this is the current code. Thanks again for all your help <?php // get table name from previous page $tablename = $_GET['tname']; ?> <?php require_once('page/pageconn.php'); if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } mysql_select_db($database_connCooking, $connCooking); $query_rsTableDisplay = "SELECT * FROM {$tablename} ORDER BY id ASC"; $rsTable = mysql_query($query_rsTableDisplay, $connCooking) or die(mysql_error()); $row_rsTable = mysql_fetch_assoc($rsTable); $totalRows_rsTableDisplay = mysql_num_rows($rsTable); $fields_num = mysql_num_fields($rsTable); echo "<h1>Table: {$tablename}</h1>"; echo "<table border='1'><tr>"; // printing table headers for($i=0; $i<$fields_num; $i++) { $field = mysql_field_name($rsTableDisplay,$i); echo "<td>$field</td>"; } echo "</tr>"; while ($row=mysql_fetch_row($rsTableDisplay)) { echo '<tr>'; foreach ($row as $r) { echo "<td>$r</td>"; } echo '</tr>'; } echo "</table>\n"; ?> </body> </html> <?php mysql_free_result($rsTable); ?> Link to comment https://forums.phpfreaks.com/topic/152467-solved-displaying-table-content-skips-first-row/#findComment-800768 Share on other sites More sharing options...
rubbertoad Posted April 3, 2009 Author Share Posted April 3, 2009 I just tried your code and it still skips the first row!!! on both tables I'm testing it on! I'm sure I'm doing something wrong, but I can't see it!!! I took out all the extra stuff at the beginning (function...), but nothing... Link to comment https://forums.phpfreaks.com/topic/152467-solved-displaying-table-content-skips-first-row/#findComment-800773 Share on other sites More sharing options...
rubbertoad Posted April 3, 2009 Author Share Posted April 3, 2009 I edited out $row_rsTable... and $totalRows_rsTable... and that fixed it. Can someone explain it to me? This is a real puzzle. I feel like such a noob!!! Link to comment https://forums.phpfreaks.com/topic/152467-solved-displaying-table-content-skips-first-row/#findComment-800775 Share on other sites More sharing options...
PFMaBiSmAd Posted April 3, 2009 Share Posted April 3, 2009 $row_rsTable = mysql_fetch_assoc($rsTable); fetches a row from the result set. Computers only do exactly what their code tells them to do. If you have a line of code that fetches a row from the result set and does not use that row, a row will be missing. Link to comment https://forums.phpfreaks.com/topic/152467-solved-displaying-table-content-skips-first-row/#findComment-800781 Share on other sites More sharing options...
rubbertoad Posted April 3, 2009 Author Share Posted April 3, 2009 Ok, I understand.. Doh!! Now I have another question. If I want to capture the value of one of the cells in a row while it's looping (for instance the first cell is called id and i woul dlike to get it's value as it's looping so I can have a link in each row that contains the value of that cell) is there a way to do that? Thanks, Link to comment https://forums.phpfreaks.com/topic/152467-solved-displaying-table-content-skips-first-row/#findComment-800792 Share on other sites More sharing options...
rubbertoad Posted April 4, 2009 Author Share Posted April 4, 2009 I figured it out. Link to comment https://forums.phpfreaks.com/topic/152467-solved-displaying-table-content-skips-first-row/#findComment-800882 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.