Add <tr> after every second <td> using

922 views Asked by At

I have PHP code which fetching images from database with <td> tags. I would like to display two pictures in row, any advice how to add <tr> after every second <td> using jQuery?

public function homepage_gall($panel){

    //Fetching photos from database,to create photo gallery called in homepage.php
    $sql="SELECT name FROM photos LIMIT 6";
    $this->query($sql);

    while($row=$this->result->fetch_array(MYSQLI_ASSOC)){
        $this->name=$row['name'];

    // Making different path,depends on place where function is called,panel.php or homepage.php
        if($panel){

        echo "<td><img src=\"../uploaded_images/".$this->name."\"/></td>";
                }
        else{


        echo "<td><img src=\"uploaded_images/".$this->name."\"/></td>";

         }
        }
   }
1

There are 1 answers

0
Thanasis Pap On BEST ANSWER

I don't know why you want to use jQuery on this but I would add a table and handle all tr and td in PHP as you already do. Replace your whole code with the following if you want that:

public function homepage_gall($panel) {
    //Fetching photos from database,to create photo gallery called in homepage.php
    $sql = "SELECT name FROM photos LIMIT 6";
    $this->query($sql);
    echo '<table><tr>';
    $i = 0;
    while ($row = $this->result->fetch_array(MYSQLI_ASSOC)) {
        $i++;
        $this->name=$row['name'];
        // Making different path,depends on place where function is called,panel.php or homepage.php
        if ($i % 2 === 0) {
            echo '</tr><tr>';
        }
        if ($panel) {
            echo "<td><img src=\"../uploaded_images/".$this->name."\"/></td>";
        } else {
            echo "<td><img src=\"uploaded_images/".$this->name."\"/></td>";
        }
    }
    echo '</tr></table>';
}