Jump to content

loop control


dropfaith

Recommended Posts

so i know this is an easy one i just cant recall how or what to search to make it work been a bit since ive coded php

right now it does everything needed but im trying to clean up the display a bit

the echo line just loops the tr and tds endlessly to the left and right What im going for

would be more like this

Current Result

<tr>

       <td>1</td>

       <td>2</td>

       <td>3</td>

       <td>4</td>

       <td>5</td>

       <td>6</td>

       <td>7</td>

       <td>8</td>

</tr>

 

Result i need

<tr>

       <td>1</td>

       <td>2</td>

       <td>3</td>

       <td>4</td>

</tr>

<tr>

       <td>5</td>

       <td>6</td>

       <td>7</td>

       <td>8</td>

</tr>

and so on

Code below

$sql = "SELECT * FROM hashtag";
if($result = mysqli_query($link, $sql)){
    if(mysqli_num_rows($result) > 0){
      
       while($row = mysqli_fetch_array($result)){
                
                echo "<td><h1>" . $row['text'] . "</h1><img src=" . $row['link'] . "></td>";
                
           
        }
         // Free result set
        mysqli_free_result($result);
    } else{
        echo "No records matching your query were found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

right now 

Link to comment
Share on other sites

I'm not clear on the criteria for emitting a new

.

 

With that said, if it's simply a fixed number of rows per table row, then you want to add a simple counter and then the modulus.

 

For example:

$i = 0;
echo '<tr>';
while($row = mysqli_fetch_array($result)){
    $i++;
    if ($i % 4 == 0) {
        // End/Start new Table Row
        echo '</tr>';
        echo '<tr>';
    }
    echo "<td><h1>" . $row['text'] . "</h1><img src=" . $row['link'] . "></td>"
}
// Close final TR
echo '</tr>';
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.