我想使用PHP从数据库填充两列HTML布局。返回的记录将按日期顺序递减,我想填充列,以便第一个记录进入第一列,第二个记录进入第二列,然后返回第一列,依此类推。
我正在研究我应该采用MySQL结果并通过将其拆分为两个数组的理论,将第一个记录放在第一个数组中,第二个放在第二个数组中,依此类推,然后使用这些数组输出到列。
列定义如下;
<div id="leftcol">
</div>
<div id="rightcol">
</div>
修改
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>';
}
答案 0 :(得分:0)
这样做
$rightCol = "";
$leftCol = "";
$c = 0;
foreach($data as $box){
if ($c%2){
$leftCol.= $box;
} else {
$rightCol.= $box;
}
$c++;
}
然后为你的html做这个
<div id="leftcol">
<?= $leftCol ?>
</div>
<div id="rightcol">
<?= $rightCol ?>
</div>
答案 1 :(得分: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>";
?>
修改强>
这就是我提出的代码......我不确定它是否会立即起作用,因为我没有测试过它。但是我希望它能让你知道现在该怎么做。
<?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>";