从MySQL填充两列HTML [关闭]

I want to populate a two column HTML layout from a database using PHP. The records returned will be in date ordered descending, and I want to populate the columns so that the first record goes in column one and the next in column two then back to column one and so on.

I'm working on the theory that I should take the MySQL result and run through it splitting it into two arrays, by putting the first record in the first array and the second in the second array and so on then using those arrays to output to the columns.

The columns are defined as follows;

<div id="leftcol">

</div>

<div id="rightcol">

</div>

EDIT

while($row = mysql_fetch_array($result))
{
    $lcol = $lcol + 1;
    $vis = 0;
    $uri = substr($row[1], 0, strpos($row[1], "&"));
    $sql = "SELECT * FROM `readart` WHERE `url`=\"".$uri."\"";
    $result2 = mysql_query($sql);

    while($row2 = mysql_fetch_assoc($result2))
    {
        $vis = $row2['visits'];
    }

    $tit = myTruncate2($row[4], 91);
    echo '<div class="newitem">';
    echo '<img style="float:left;margin:6px;margin-right:20px;" src="newslogo/'.$row[2].'.png" width="40px" height="40px" />';
    echo '<span id="header">'.$tit.'</span><br>';
    //echo $row[6];
    echo '<span id="date">'.date("D, j F g:i A", $row[6]);
    if($vis > 0){echo ' - Viewed '.$vis.' times';}
    echo '</span><hr>';
    echo '<p id="textbody">';
    echo $row[5];
    echo '<br><br><a href="recordarticles.php?url='.$row[1].'" target="_blank">Read More</a>';
    echo '</p><br></div>';
}
<?php
$row = array();
$rowCount = $mysql_row_count($result);
while ($row[] = mysql_fetch_array($result));

echo "<div idi\"leftcol\">";
    for ($i = 0; $i < $rowCount; $i+=2)
        echo $row[$i]; //Change this to whatever you want to echo or however you want to echo it
echo "</div>";

echo "<div idi\"rightcol\">";
    for ($i = 1; $i < $rowCount; $i+=2)
        echo $row[$i]; //Change this to whatever you want to echo or however you want to echo it
echo "</div>";
?>

EDIT

Here's what i came up with your code... I'm not sure if it will work right away since i haven't tested it. But I hope it gives you an idea of how to do it now.

<?php
function echoData($right, $row, $rowCount)
{
    for ($i = $right; $i < $rowCount; $i += 2)
    {
        $lcol = $lcol + 1;
        $vis = 0;
        $uri = substr($row[$i][1], 0, strpos($row[$i][1], "&"));
        $sql = "SELECT * FROM `readart` WHERE `url`=\"".$uri."\"";
        $result2 = mysql_query($sql);

        while($row2 = mysql_fetch_assoc($result2))
        {
            $vis = $row2['visits'];
        }

        $tit = myTruncate2($row[$i][4], 91);
        echo '<div class="newitem">';
        echo '<img style="float:left;margin:6px;margin-right:20px;" src="newslogo/'.$row[$i][2].'.png" width="40px" height="40px" />';
        echo '<span id="header">'.$tit.'</span><br>';
        //echo $row[6];
        echo '<span id="date">'.date("D, j F g:i A", $row[$i][6]);
        if($vis > 0){echo ' - Viewed '.$vis.' times';}
        echo '</span><hr>';
        echo '<p id="textbody">';
        echo $row[$i][5];
        echo '<br><br><a href="recordarticles.php?url='.$row[$i][1].'" target="_blank">Read More</a>';
        echo '</p><br></div>';
    }
}

**Here you should put the query code**
$sql = "SELECT e.t.c e.t.c";
$result = mysql_query($sql);


$rowCount = mysql_num_rows(result);
$row = array();
while($row[] = mysql_fetch_array($result));

echo "<div id='leftside'>";
echoData(0, $row, $rowCount);
echo "</div>";

echo "<div id='rightside'>";
echoData(1, $row, $rowCount);
echo "</div>";

do this

 $rightCol = "";
 $leftCol = "";
 $c = 0;
 foreach($data as $box){
       if ($c%2){
            $leftCol.= $box;
       } else {
            $rightCol.= $box;
       }
       $c++;
 }

Then for your html do this

<div id="leftcol">
<?= $leftCol ?>
</div>

<div id="rightcol">
<?= $rightCol ?>
</div>