Jump to content

[SOLVED] while + insert after every second run?


blueman378

Recommended Posts

hi guys i have this code

<?php
function GetAction(){
global $database;
    $q = "SELECT * FROM " . Games . "
          WHERE category='Action / Adventure'
          ORDER BY `gName` ASC
         ";   
    $result = $database->query($q) or die("Error: " . mysql_error());
    /* Error occurred, return given name by default */
    $num_rows = mysql_numrows($result);
if( $num_rows == 0 ){
      return '<font size="2">Games not found!</font>';
    }
        /* Display table contents */
    $content = "<table border=\"0\" cellspacing=\"10\" ><tr>
";
    while( $row = mysql_fetch_assoc($result) ) {
        $content .= "
<td style='width: 10%;' valign='top'><a href='showgame.php?game=$row[gName]'><img border='1' src='thumbs_ins/$row[gThumb]'></a>
    </td><td style='width: 40%;' valign='top'><b>
<a href='showgame.php?game=$row[gName]'>$row[gName]</a></b><br>$row[gDescription]</td>
";
    }

    $content .= "</tr></table><br>\n";
    return $content;
}

// You can
echo GetAction();
?>

 

but that means that if i have 12 results i get

[] [] [] [] [] [] [] [] [] [] [] []

but what i am looking for is

[] []

[] []

[] []

[] []

[] []

[] []

so i would want to insert a </tr><tr> after every second run, any ideas?

Link to comment
Share on other sites

i would look at xhtml b4 i do any php, <br><b><font color=color><font size4> are all so no no,

 

But yeah just add a <tr> :

 


     $content .= "
<td style='width: 10%;' valign='top'><a href='showgame.php?game=$row[gName]'><img border='1' src='thumbs_ins/$row[gThumb]'></a>
    </td><td style='width: 40%;' valign='top'><b>
<a href='showgame.php?game=$row[gName]'>$row[gName]</a></b><br>$row[gDescription]</td>
";
    }

    $content .= "</tr><tr></td></table><br>\n";

 

Sould work.

Link to comment
Share on other sites

You're going to want to run an incrementation on your while loop (like an $x++) and check every loop through of whether the remainder (x % 2) == 1.  If so, </td></tr>...else just </td>...conversely, you'll have to switch between <tr><td> and just <td> at the start of each while loop.

 

It's easily done...just did it earlier today actually...just can't pull up the code at the moment.

Link to comment
Share on other sites

I use this construct:

 

$j = 1
echo "<tr>";
for($i = 0; $i < something; $i++)
{
    // echo your <td></td> here with the applicable data inside the tags
    if ($j % 2 = 0)
    {
        echo "</tr><tr>";
    }
    $j += 1;
}
echo "</tr>";[code]

This sets an opening <tr> tag at the start, then fills in one <td></td> tag, goes back to the start of the loop, echos another <td></td> then echoes a closing </tr> tag, and then another opening <tr> tag. Finally after the loop is finished, it echoes a closing </tr> tag to finish everything off.

[/code]

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.