• 0

[php]How to do a loop


Question

Ok - so i a database load of data, and i want to echo it into a table. so the table format would be along the lines of

<table width='100%' border='0'>
  <tr>
        <td>$date</td>
        <td>$action</td>
        <td>$username</td>
        <td>$uid</td>
        <td>$ip</td>
      </tr>
    </table>

question is how do i get it to echo each line of data coherently like that?!

i have this atm = but it dont work :)

<?
require_once 'vars.php';
//sql conn
mysql_connect($dbserver,$dbuser,$dbpass);
@mysql_select_db($db);
//grab hol dates




$gsql="SELECT * FROM log"; 
  $gresult=mysql_query($gsql);
  $num=mysql_num_rows($gresult);
  if ($num > 0) {
 	 echo "
 	 <html>
<title>User Logs</title>
<body BGCOLOR='#DCDDF2'>";
 	 while ($i < $num) {
    if ($result=mysql_query("SELECT * FROM log ORDER BY id")) {
  $row = mysql_fetch_array($result);
} 	 
    echo "
 	 
        <table width='100%' border='0'>
  <tr>
        <td>$row[date]</td>
        <td>$row[action]</td>
        <td>$row[username]</td>
        <td>$row[uid]</td>
        <td>$row[ip]</td>
      </tr>
    </table>";
    ++$i;
 	 }
 	 
  }




?>

thanks

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

when you do a mysql_query and then an array fetch you can use a ''while'' loop

something like

$result = mysql_query("SELECT * FROM log ORDER BY id");
while ($row = mysql_fetch_array($result)) {
 ? ?echo "<td>$row[date]</td>";
...
...
}

Link to comment
Share on other sites

  • 0

I don't think you can print an array element like that. Try replacing the <td> lines with lines like this:

<td>".$row[date]".</td>

You say it doesn't work ... what doesn't work? What does it say or not say?

Link to comment
Share on other sites

  • 0
I don't think you can print an array element like that. Try replacing the <td> lines with lines like this:

<td>".$row[date]".</td>

as a matter of fact you can have:

echo "<td>$row[date]</td>";

as Redmak said.

and the problem that Mr magoo had was that he did not have a loop and it probably wouldnt print more then 1 set.

also i would add:

extract($row); right after the loop is opened.

$query = mysql_query("select * FROM table", $db);
while($row = mysql_fetch_array($query)){ 
    extract($row);
}

Link to comment
Share on other sites

  • 0

hope this helps


<?php

require_once 'vars.php';

//sql connect

mysql_connect($dbserver,$dbuser,$dbpass);

@mysql_select_db($db);

//grab hol dates

$gsql="SELECT * FROM log ORDER BY id";

$gresult=mysql_query($gsql);

$html_output = "<html>\n<title>User Logs</title>\n<body BGCOLOR='#DCDDF2'>\n";

while ( $row = mysql_fetch_array( $gresult ) )

{

$html_output .= "<table width='100%' border='0'>\n";

$html_output .= "\t<tr>\n";

$html_output .= "\t<td>$row[date]</td>\n";

$html_output .= "\t<td>$row[action]</td>\n";

$html_output .= "\t<td>$row[username]</td%3

Link to comment
Share on other sites

  • 0

wow thanks for the help guys= thats sorted it a treat :D

ps- what does extract($row); do?

thanks v much again - MERRY EASTER!

:Z

Link to comment
Share on other sites

  • 0

extract - extracts the elements of a variable.

so in your case extract($row); automaticaly gives you the vars you need ($date, $username etc.)

Link to comment
Share on other sites

  • 0

sorry about my post earlier... i had some MAJOR packet loss last night and my post did get post fully... either that or neowin was down :huh: but here

hope this helps

&lt;?php

require_once 'vars.php';

//sql connect
mysql_connect($dbserver,$dbuser,$dbpass);
@mysql_select_db($db);

//grab hol dates

$gsql="SELECT * FROM log ORDER BY id";

$gresult=mysql_query($gsql);

$html_output = "&lt;html&gt;\n&lt;title&gt;User Logs&lt;/title&gt;\n&lt;body BGCOLOR='#DCDDF2'&gt;\n";

while ( $row = mysql_fetch_array( $gresult ) )
{

	$html_output .= "&lt;table width='100%' border='0'&gt;\n";
	$html_output .= "\t&lt;tr&gt;\n";
	$html_output .= "\t&lt;td&gt;" . $row[date] . "&lt;/td&gt;\n";
	$html_output .= "\t&lt;td&gt;" . $row[action] . "&lt;/td&gt;\n";
	$html_output .= "\t&lt;td&gt;" . $row[username] . "&lt;/td&gt;\n";
	$html_output .= "\t&lt;td&gt;" . $row[uid] . "&lt;/td&gt;\n";
	$html_output .= "\t&lt;td&gt;" . $row[ip] . "&lt;/td&gt;\n";
	$html_output .= "\t&lt;/tr&gt;\n";
	$html_output .= "&lt;/table&gt;\n";

}

$html_output .= "&lt;/body&gt;";

echo $html_output;

?&gt;

try to use "<?php ?>" more instead of "<? ?>" :)

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.