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>

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";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.