Jump to content

return values in 3 colums


flemingmike

Recommended Posts

<?php //Connect to the database prior to this code ?>
<table width="200px" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
         <tr>
            <td width="33%" align="center" bgcolor="#00005F"><strong>col</strong></td>
            <td width="33%" align="center" bgcolor="#00005F"><strong>col2</strong></td>
            <td width="33%" align="center" bgcolor="#00005F"><strong>col3</strong></td>
        </tr>
<?php
	$sql="SELECT `col`, `col2`, `col3` FROM `table` WHERE 1=1;"; //Change the 1=1 to why you want to pull from the database because of.
	$result=mysql_query($sql, $link);
                while($rows = mysql_fetch_array($result)){
        ?>
        <tr>
            <td <?php echo $rows['col']; ?></td>
            <td <?php echo $rows['col2']; ?></td>
            <td><?php echo $rows['col3']; ?></td>
        </tr>
        <?php } ?>
      </table>

Link to comment
Share on other sites

I believe what he is trying to achive is to have records displayed in a grid where each row in the grid will display three records - not one record per row with three fields. So, row one displayes records 1-3, row 2 displayes records 4-6, etc.

 

Here is some pseudo code

//Variable to determine number of records in a row
$recordsPerRow = 3;

$query = "SELECT * FROM table";
$result = mysql_query($query);

$recIdx = 0;
while($row = mysql_fetch_assoc($result))
{
    //Increment the record index
    $recIdx++;

    //Open new row if first record in row
    if($recIdx%$recordsPerRow==1)
    {
        echo "<tr>\n";
    }

    //Display the record
    echo "<td>{$row['somevalue']}</td>\n";

    //Close row if last record in row
    if($recIdx%$recordsPerRow==0)
    {
        echo "</tr>\n";
    }
}

//Close last row if not already done
if($recIdx%$recordsPerRow!=0)
{
    echo "</tr>\n";
}

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.