Forum Moderators: coopster

Message Too Old, No Replies

php with HTML table

         

rscrsc

8:42 pm on Mar 5, 2006 (gmt 0)

10+ Year Member



Hi I was wondering if someone could please help me out. I have a php page that query MYSQL database for certain information. I was stuck with trying to print the tables that names that correspond with the query results. Since I was unable to I create the tables myself and had them print. The problem is that when the results spit out on the screen the results are ontop of the tables instead of below each column. How to I force the results to print both below and if possible in each column that it corresponds to.

Here is what I mean

currently

192.168.1.50 192.168.1.1 test_sig 01-25-2006
src_ip dst_ip sig_name date

The results should be below the column names

src_ip dst_ip
192.168.1.50 192.168.1.1 ect...

is there also a way to make them fit or show up under each column?

thanks so much for the help in advance

here is my code.

<table border="1" width="75%" cellpadding="2" cellspacing="2">
<th align="center">signature</th>
<th align="center">dst_ip</th>
<th align="center">src_ip</th>

<?
$host = "localhost";
$user = "#*$!x";
$pass = "xxxxx";
$dbname = "xxxxx";

$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);
//Query that is being run
$query= "select * from signature where sig_name= '" . $_POST['IP'] . "'";
//this will try to spit out the previous sql query
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
//this is a line break before the spit output
while ($line = mysql_fetch_row($result) )
{
echo $line[0]."\t".$line[1]."\t".$line[2]."\t".$line[3]."\t".$line[4]."<br>";
}
?>

dreamcatcher

10:42 pm on Mar 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




<table border="1" width="75%" cellpadding="2" cellspacing="2">
<tr>
<td align="center">signature</td>
<td align="center">dst_ip</td>
<td align="center">src_ip</td>
<td align="center">date</td>
</tr>
<?php

<?
$host = "localhost";
$user = "#*$!x";
$pass = "#*$!xx";
$dbname = "xxxxx";

$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);
//Query that is being run
$query= "select * from signature where sig_name= '" . $_POST['IP'] . "'";
//this will try to spit out the previous sql query
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
//this is a line break before the spit output

while ($line = mysql_fetch_row($result) )
{
echo '<tr>';
echo '<td align="center">'.$line[0].'</td>';
echo '<td align="center">'.$line[1].'</td>';
echo '<td align="center">'.$line[2].'</td>';
echo '<td align="center">'.$line[3].'</td>';
echo '</tr>';
}

?>
</table>

dc

Habtom

10:22 am on Mar 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Will this actually do the job?
while ($line = mysql_fetch_row($result) )
{
echo '<tr>';
echo '<td align="center">'.$line[0].'</td>';
echo '<td align="center">'.$line[1].'</td>';
echo '<td align="center">'.$line[2].'</td>';
echo '<td align="center">'.$line[3].'</td>';
echo '</tr>';
}

I would put it this way:
while ($line = mysql_fetch_row($result) )
{
echo "<tr>";
echo "<td align=center>$line[0]</td>";
echo "<td align=center>$line[1]</td>";
echo "<td align=center>$line[2]</td>";
echo "<td align=center>$line[3]</td>";
echo "</tr>";
}

Does the difference make sense?

Habtom

dreamcatcher

12:09 pm on Mar 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



They are both the same.

jatar_k

5:43 pm on Mar 6, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> They are both the same.

they both do exactly the same thing

>> Does the difference make sense?

well just to confuse and clarify at the same time, I would do this

while ($line = mysql_fetch_row($result)) {
echo '<tr>';
echo '<td align="center">',$line[0],'</td>';
echo '<td align="center">',$line[1],'</td>';
echo '<td align="center">',$line[2],'</td>';
echo '<td align="center">',$line[3],'</td>';
echo '</tr>';
}

difference, this does not force the echo to concatenate the line before outputting it. I wouldn't use the double quoted version, again too much work before outputting and you should also put in the quotes around "center" as dc and I have.

more info on output
Benchmarking PHP text output [webmasterworld.com]

inveni0

5:53 pm on Mar 6, 2006 (gmt 0)

10+ Year Member



I would do:

while ($line = mysql_fetch_row($result) )
{
echo "<tr>";
echo "<td align='center'>".$line[0]."</td>";
echo "<td align='center'>".$line[1]."</td>";
echo "<td align='center'>".$line[2]."</td>";
echo "<td align='center'>".$line[3]."</td>";
echo "</tr>";
}

Using an apostrophe to enclose "center" will prevent the need to escape your quotes.