john smith 1924 Veteran Posted April 19, 2003 Veteran Share Posted April 19, 2003 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 More sharing options...
0 Redmak Administrators Posted April 19, 2003 Administrators Share Posted April 19, 2003 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 More sharing options...
0 Quboid Posted April 19, 2003 Share Posted April 19, 2003 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 More sharing options...
0 rayper Posted April 19, 2003 Share Posted April 19, 2003 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 More sharing options...
0 sinatosk Posted April 19, 2003 Share Posted April 19, 2003 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 More sharing options...
0 john smith 1924 Veteran Posted April 19, 2003 Author Veteran Share Posted April 19, 2003 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 More sharing options...
0 Redmak Administrators Posted April 20, 2003 Administrators Share Posted April 20, 2003 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 More sharing options...
0 sinatosk Posted April 20, 2003 Share Posted April 20, 2003 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 <?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>\n"; $html_output .= "\t<td>" . $row[uid] . "</td>\n"; $html_output .= "\t<td>" . $row[ip] . "</td>\n"; $html_output .= "\t</tr>\n"; $html_output .= "</table>\n"; } $html_output .= "</body>"; echo $html_output; ?> try to use "<?php ?>" more instead of "<? ?>" :) Link to comment Share on other sites More sharing options...
0 john smith 1924 Veteran Posted April 20, 2003 Author Veteran Share Posted April 20, 2003 hey zionath and redmak - thank you both very much :p Link to comment Share on other sites More sharing options...
Question
john smith 1924 Veteran
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
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