Creating a PHP array for display in an HTML table -


i have database records ip address , session id of users hit web-site. want display html table of top ten occuring ip addresses , number of distinct sessions associated each ip address.

so start query:

$sql="select ip, country, city, count(*) count hits group ip order count desc limit 10";

and then, each of resulting 10 rows use while loop query same database extract number of distinct session id's associated each address thus:

$sql="select distinct session hits ip='$ipaddress'"

my challenge build, go through while loop, array (with same structure mysqli query result) on can subsequently use foreach loop output html table. here's full code:

$con = mysqli_connect("localhost","user","password","dbase"); $sql="select ip, country, city, count(*) count hits group ip order count desc limit 10"; $result = mysqli_query($con,$sql); $data = array(); while ($row = mysqli_fetch_assoc($result)) {      $ipaddress = $row["ip"];      $sql="select distinct session hits ip='$ipaddress'";      $res = mysqli_query($con,$sql);      $distinctsessions = mysqli_num_rows($res);      $data[] =$ipaddress.",".$distinctsessions; }; $colnames = array_keys(reset($data)); foreach($data $row) {     echo "<tr>";     foreach($colnames $colname) {     echo "<td>".$row[$colname]."</td>";     }    echo "</tr>"; } echo "</table>"; mysqli_free_result($result); mysqli_free_result($res); mysqli_close($con);  

i realise mistake lies in line:

$data[] =$ipaddress.",".$distinctsessions;

and isn't correct structure can't work out is. grateful advice.

no need use foreach use below code

$con = mysqli_connect("localhost","user","password","dbase"); $sql="select ip, country, city, count(*) count hits group ip order count desc limit 10"; $result = mysqli_query($con,$sql); $data = array(); while ($row = mysqli_fetch_assoc($result)) {      $ipaddress = $row["ip"];      $sql="select distinct session hits ip='$ipaddress'";      $res = mysqli_query($con,$sql);      $distinctsessions = mysqli_num_rows($res);  ?>      <tr>           <td><?php echo $ipaddress; ?></td>           <td><?php echo $distinctsessions; ?></td>      </tr>   <?php }          echo "</table>";          mysqli_free_result($result);          mysqli_free_result($res);          mysqli_close($con);    ?> 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -