Jump to content

Recommended Posts

bit confused here, im trying to echo out multiple results from a database and returning them in a table. Each new row in the database leads should lead to a new row in the HTML table.

 

However I can only get it so the first row from the database goes into the html table as it should, the rest go outside it. Any ideas?

 

$query = "SELECT * FROM 070208_new";

$result = mysql_query($query)
or die ("This entry hasn't been found.");

    while($row = mysql_fetch_array($result))
{
    $log_id = $row['log_id'];
$time = $row['time'];
$s_ip = $row['s-ip'];
$cs_method = $row['cs-method'];
$cs_uri_stem = $row['cs-uri-stem'];
$cs_uri_query = $row['cs-uri-query'];
$port = $row['port'];
$cs_username = $row['cs-username'];
$c_ip = $row['c-ip'];
$user_agent = $row['cs(User-Agent)'];
$sc_status = $row['sc-status'];
$sc_substatus = $row['sc-substatus'];
$sc_win32_status = $row['sc-win32-status'];
$sc_bytes = $row['sc-bytes'];
$cs_bytes = $row['cs_bytes'];
	  echo "<tr>
    <td>$log_id</td>
    <td>$time</td>
    <td>$s_ip</td>
    <td>$cs_method</td>
    <td>$cs_uri_stem</td>
    <td>$cs_uri_query</td>
    <td>$port</td>
    <td>$cs_username</td>
    <td>$c_ip</td>
    <td>$user_agent</td>
    <td>$sc_status</td>
    <td>$sc_substatus</td>
    <td>$sc_win32_status</td>
    <td>$sc_bytes</td>
    <td>$cs_bytes</td>
  </tr>";
echo "</table>";
}

Link to comment
https://forums.phpfreaks.com/topic/41459-solved-echoing-multiple-html-tables/
Share on other sites

$query = "SELECT * FROM 070208_new";

$result = mysql_query($query) or die ("This entry hasn't been found.");
echo '<table>';
while($row = mysql_fetch_array($result))
{
    $log_id = $row['log_id'];



$time = $row['time'];



$s_ip = $row['s-ip'];



$cs_method = $row['cs-method'];



$cs_uri_stem = $row['cs-uri-stem'];



$cs_uri_query = $row['cs-uri-query'];



$port = $row['port'];



$cs_username = $row['cs-username'];



$c_ip = $row['c-ip'];



$user_agent = $row['cs(User-Agent)'];



$sc_status = $row['sc-status'];



$sc_substatus = $row['sc-substatus'];



$sc_win32_status = $row['sc-win32-status'];



$sc_bytes = $row['sc-bytes'];



$cs_bytes = $row['cs_bytes'];




  echo "<tr>
    <td>$log_id</td>
    <td>$time</td>
    <td>$s_ip</td>
    <td>$cs_method</td>
    <td>$cs_uri_stem</td>
    <td>$cs_uri_query</td>
    <td>$port</td>
    <td>$cs_username</td>
    <td>$c_ip</td>
    <td>$user_agent</td>
    <td>$sc_status</td>
    <td>$sc_substatus</td>
    <td>$sc_win32_status</td>
    <td>$sc_bytes</td>
    <td>$cs_bytes</td>
  </tr>";



}echo "</table>";

I don't see your opening table tag, but your closing table tag is within the loop - it needs to be after the loop. Also, there's no need to create those variables if you are only going to use them once:

 

$query = "SELECT * FROM 070208_new";

$result = mysql_query($query)
or die ("This entry hasn't been found.");

if (mysql_num_rows($result)==0) {
 echo "There were no results.";
} else {

 echo "<table>";
 while($row = mysql_fetch_array($result)) {
    echo "<tr>
     <td>$row['log_id']</td>
     <td>$row['time']</td>
     <td>$row['s-ip']</td>
     <td>$row['cs-method']</td>
     <td>$row['cs-uri-stem']</td>
     <td>$row['cs-uri-query']</td>
     <td>$row['port']</td>
     <td>$row['cs-username']</td>
     <td>$row['c-ip']</td>
     <td>$row['cs(User-Agent)']</td>
     <td>$row['sc-status']</td>
     <td>$row['sc-substatus']</td>
     <td>$row['sc-win32-status']</td>
     <td>$row['sc-bytes']</td>
     <td>$row['cs_bytes']</td>
     </tr>";
 }
 echo "</table>";
}

first, there's no need to rename all your variables. second, this is how you do it:

<?php
        $sql = "SELECT * FROM your_table";
        $query = mysql_query($sql);

        echo "<table border=\"1\">\n";
        while($row = mysql_fetch_array($query)){
                echo "<tr><td>". $row['col1'] ."</td><td>". $row['col2'] ."</td><td>". $row['col3'] ."</td></tr>\n";
        }
        echo "</table>\n";
?>

 

and the reason why your first row is populated but the rest are not is because you've got your </table> tag INSIDE the while loop.

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.