Display query result in a table

后端 未结 4 1482
谎友^
谎友^ 2021-01-22 20:01

I have a MySQL query with over 50 return results. Now I need to display the results in a table with 3 rows and 3 columns.

Something like this:



        
相关标签:
4条回答
  • 2021-01-22 20:26

    try something like:

     echo "<table>";
     while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
     $name   = $row['name'];
     $address = $row['address'];
     $content = $row['content'];
     echo "<tr><td>".$name."</td><td>".$address."</td><td>".$content."</td></tr>";
     }
     echo "</table>";
    

    this example will print in table all the result of the query. if you want to limit only to some results, so limit the sql query. for example:

     $q = "SELECT name, address, content FROM mytable limit 50"; 
    

    to get each content,name, address in TD, and 3 of mycontent(content, name, address) in a TR try this:

    $c= mysql_query("select COUNT(name) from mytable");
    $n=mysql_fetch_array($c);
    $maxname= $n[0];
    $i=0;
    $tr=0;
    echo "<table>";
    while ($tr < $maxname)
    { 
    echo "<tr>";
    while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC) and $i<$tr) {
    $name   = $row['name'];
    $address = $row['address'];
    $content = $row['content'];
    echo "<td>".$name." | ".$address." | ".$content."</td>";
    $i++;
    }
    echo "</tr>";
    $tr= $tr+3;
    }
    echo "</table>";
    
    0 讨论(0)
  • 2021-01-22 20:46

    You can store in a variable if u r planning to use it in a function or you can just print it. Either way...

    $q = "SELECT name, address, content FROM mytable"; 
    $r = mysqli_query($dbc, $q); 
    
    $str = "<table>";
    
    while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
        $str .= "<tr>";
        $str .= "<td>" . $row['name'] . "</td>";
        $str .= "<td>" . $row['address'] . "</td>";
        $str .= "<td>" . $row['content'] . "</td>";
        $str .= "</tr>";
    }
    
    $str .= "</table>"
    
    echo $str;
    

    There you go!

    0 讨论(0)
  • 2021-01-22 20:49

    use a for-loop to iterate your $mycontent-array:

    EDIT: I was made aware that $mycontent is built differently than I first assumed:

    $h = "<table>"; // we are going to build the table in $h
    
    $maxcount = count($mycontent); // get the number of values within the array 
    
    for ($i = 0;$i < $maxcount;$i++) { // counting $i up from 0 to $maxcount -1 ...
    
       $h.= "<tr><td>{$mycontent[$i]}</td></tr>"; // build row
    
    }
    
    $h.= "</table>"; // close the table
    echo $h; // echo it 
    

    ---> live demo: http://3v4l.org/g1E1T

    0 讨论(0)
  • 2021-01-22 20:50

    Try something like this. It places your values in an easy to use associative array. Then you just loop through.

    <?php
    while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
        $values[] = array(
            'name' => $row['name'],
            'address' => $row['address'],
            'content' => $row['content']
        );
    }
    ?>
    <table>
    <?php
    foreach($values as $v){
        echo '
        <tr>
            <td>'.$v['name'].'</td>
            <td>'.$v['address'].'</td>
            <td>'.$v['content'].'</td>
        </tr>
        ';
    }
    ?>
    </table>
    
    0 讨论(0)
提交回复
热议问题