-1

I have a problem displaying the return on MySQL query into HTML Table. I've tried a lot of options but the return tables will not align, or will not get into a table. I have the following PHP code:

   <?php
   require_once('modules/config.php');
   $cerereSQL = 'SELECT * FROM `allowed`'; 
   $rezultat = mysqli_query($conexiune, $cerereSQL);
   while($rand = mysqli_fetch_array($rezultat))

   {
   echo "IP: " . $rand["ip"]. "<br>
   Hostname: " . $rand["host"]. "<br> 
   Port: " . $rand["port"]. "<br>
   UserAgent: " . $rand["ua"]. "<br>
   Referrer: " . $rand["ref"]. "<br>
   URL: " . $rand["url"]. "<br>
   Date/Time: " . $rand["dtime"]. "<br><br>";

   }   

   ?>

What can I do to return the query into a nice HTML table? Every time I tried to add into the PHP Echo function, the query will just go under the last one, even without
and aligned on middle.

Script47
  • 14,230
  • 4
  • 45
  • 66
unkn0wnx
  • 137
  • 3
  • 14

1 Answers1

1

Change your SQL a little, only get fields you need, like:

SELECT ip as IP, host as Hostname,port as Port ...  FROM

Then do

while($rand = mysqli_fetch_array($rezultat)){
    $resultset[]=$rand;
}
$html = "<table><tr><th>".implode('</th><th>',array_keys($resultset[0])).'</th></tr>';
foreach($resultset as $set){
    $html .= "<tr><td>".implode('</td><td>',$set).'</td></tr>';
}
print $html.'</table>';

...

JustOnUnderMillions
  • 3,741
  • 9
  • 12
  • Thanks a lot, this is the best by far. Still, it gives me some number over the top: 0 IP 1 HostName 2 Port 3 UA 4 Ref 5 URL 6 Dtime. What can I do to strip those numbers out? And to set a width for all "columns" and a border? Still, thank you very much! – unkn0wnx Aug 16 '16 at 14:18
  • Download Bootstrap Css and add class='table' to the table, then it looks nice :) – JustOnUnderMillions Aug 16 '16 at 14:19
  • Did as you said, and it does look nicer. Thanks a lot. Still, can you please help me out get those numbers out? 0 IP, 1 Hostname, 2 Port. So I can have only text. Also, can I set every "th" width? The IP's is very large, and the hostname is small, even though there's more text into hostname. – unkn0wnx Aug 16 '16 at 14:25
  • Also, it executes the query 2 times I think, the result is posted into the HTML table twice, like: ip ip, hostname hostname, port port. – unkn0wnx Aug 16 '16 at 14:31
  • 1
    `0 ip` comes from `mysqli_fetch_array($rezultat))` use `mysqli_fetch_assoc($rezultat))` and it will fix this too: `query 2 times`, because with `_array` you get the data twice index and assoc eg `[0=>'ipaddrs','ip'=>'ipaddrs']` – JustOnUnderMillions Aug 17 '16 at 15:15