drfals1307 2013-06-10 05:02
浏览 28
已采纳

从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>';
}
  • 写回答

2条回答 默认 最新

  • douyi9787 2013-06-10 05:10
    关注
    <?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>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?