我需要在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;
答案 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_
已弃用且(根据我们的经验)可能会出现问题,因此请考虑(如果可能)转移到其他内容,例如mysqli
,pdo
。