Jump to content

[SOLVED] Displaying table content skips first row


rubbertoad

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

$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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.