dongzhonggua4229 2014-12-30 13:21
浏览 21
已采纳

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

I need to separate the results of a query in 3 clusters: rows 1-3, rows 4-6, rows 7-9 and place them in variables return1, return2, return3 resp. How can I do that efficiently?

$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条回答 默认 最新

  • doushou6480 2014-12-30 13:30
    关注

    Maybe this could help in any way:

    (updated - added function floor before 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;
    

    Since mysql_ are deprecated and (from our experience) could be problematic, consider maybe (if possible) moving to something else like mysqli, pdo.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度