0

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>';
}
Flatlyn
  • 2,040
  • 6
  • 40
  • 69
  • 1
    Why not use increment of 2? for ($i = 0; $i < $rowCount; $i+=2) for the first div, and for ($i = 1; $i < $rowCount; $i+=2) for the second div? – Not Amused Jun 10 '13 at 05:05
  • If you're (still) using `mysql_*` functions, the first thing you should do is to turn the result handle into a traversable that can iterate over each row. Then you can *easily* do such things. See [PHP file cannot enter some part of code](http://stackoverflow.com/q/11575531/367456) and [How to successfully rewrite old mysql-php code with deprecated mysql_* functions?](http://stackoverflow.com/q/10919277/367456) which is suggesting PDO that is more easy to use *and* provides a Traversable on results out of the box. – hakre Jun 10 '13 at 07:57

2 Answers2

0

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>
Orangepill
  • 24,500
  • 3
  • 42
  • 63
0
<?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>";
Not Amused
  • 942
  • 2
  • 10
  • 28
  • Not sure I understand how to work this when using `while($row = mysql_fetch_array($result)` or am I just missing something – Flatlyn Jun 10 '13 at 05:22
  • the example above is used in the case you already populated the array with the records in the while loop. $row = array(); while($row[] = mysql_fetch_array($result)); then you execute the code above, and `echo` your data however you want. Hope this made sense. – Not Amused Jun 10 '13 at 05:26
  • I think your answer is right but I can't seem to get it to work. The problem I'm having is getting the data out the array. I need to get out several things from the row (title, body, time etc). I've added a explanation to question. – Flatlyn Jun 10 '13 at 05:37
  • extracting data from the array-- If you want associative extraction then: `$row[$i]['columnName']`; If you want indexed extraction then `$row[$i][$index];` where `$index`= the index for the desired column. – Not Amused Jun 10 '13 at 05:40
  • MORE: If you want to get an associative array then use, mysql_fetch_array($result, MYSQL_ASSOC); If you want to get an Indexed array then use mysql_fetch_array($result, MYSQL_NUM); And if you want both, then use mysql_fetch_array($result, MYSQL_BOTH); – Not Amused Jun 10 '13 at 05:44
  • You sir, deserve more than a green tick. – Flatlyn Jun 10 '13 at 05:45