将查询结果拆分为3个集群

时间:2014-12-30 13:21:41

标签: php mysql

我需要在3个集群中分离查询的结果:第1-3行,第4-6行,第7-9行,并将它们放在变量return1,return2,return3 resp中。我怎样才能有效地做到这一点?

$numrows = mysql_num_rows($sResult1);

$return1 = '';
$return2 = '';
$return3 = '';
for ($i = 0; $i < $numrows; $i++) {
    $row1 = mysql_fetch_array($sResult1);   
    $return1 .= '<a title="' . $row1['resort'] . ' @ ' . $row1['lud'] . '" class="item" href="/review/' . $row1['resort_us'] . '/' . $row1['cam_id'] . '"><img class="item__image" id="c' . $row1['cam_id'] . '" src="/xs1/' . $row1['cam_id'] . '.jpg" alt="' . $row1['resort'] . '" /><h2 class="item__title">' . $row1['resort'] . '</h2></a>';
}               

echo $return1;

1 个答案:

答案 0 :(得分:2)

也许这可能会有所帮助:

(已更新 - 在floor之前添加了功能intval

$numrows = mysql_num_rows($sResult1);

$return[] = array();

$return[0] = $return[1] = $return[2] = '';

for ($i = 0; $i < $numrows; $i++)
{
$row1 = mysql_fetch_array($sResult1);   
$return[intval(floor($i/3))] .= '<a title="' . $row1['resort'] . ' @ ' . $row1['lud'] . '" class="item" href="/review/' . $row1['resort_us'] . '/' . $row1['cam_id'] . '"><img class="item__image" id="c' . $row1['cam_id'] . '" src="/xs1/' . $row1['cam_id'] . '.jpg" alt="' . $row1['resort'] . '" /><h2 class="item__title">' . $row1['resort'] . '</h2></a>';
}               

echo $return1;

由于mysql_已弃用且(根据我们的经验)可能会出现问题,因此请考虑(如果可能)转移到其他内容,例如mysqlipdo