doushi1960 2016-04-28 21:07
浏览 60
已采纳

PHP:在两个div中拆分mySQLi数组?

I'm currently working on a page that calls a mySQL database to populate a list of members. However, I need that list to be split into two equal parts, so that half can float to the left and half to the right. However, I have beat this horse to death and still cannot figure out how to split the array.

Here's what I have currently:

<div class="holder">
    <?php
    $members = $db->query('SELECT * FROM tableContacts ORDER BY lastName ASC');
    $rowcount = mysqli_num_rows($members);
    if ( $rowcount > 0 ) { ?>
        <div class="members-left">
            <ul class="members">
                <?php while ($row = $members->fetch_assoc()) { ?>
                    <li class="member">
                        //SOME CONTENT HERE
                    </li>
                <?php } ?>
            </ul>
        </div>
    <?php } ?>
</div>

However, I want the output to look something like this (let's say my table has 10 members):

<div class="holder">
    <!--output first half of members from table: -->
    <div class="members-left">
        <ul class="members">
            <li class="member">...</li>
            <li class="member">...</li>
            <li class="member">...</li>
            <li class="member">...</li>
            <li class="member">...</li>
        </ul>
    </div>

    <!--output second half of members from table: -->
    <div class="members-right">
        <ul class="members">
            <li class="member">...</li>
            <li class="member">...</li>
            <li class="member">...</li>
            <li class="member">...</li>
            <li class="member">...</li>
        </ul>
    </div>

</div>

I've tried setting counters and using things like if($i <= $rowcount/2), but to no avail. Any help would be greatly appreciated—I'm very new to mySQL and have limited knowledge of PHP, so this one has me stumped.

  • 写回答

4条回答 默认 最新

  • dongyaobo9081 2016-04-28 21:26
    关注

    add a control variable like "$i". add +1 to $i every loop. and check if $i reached half of number of rows. if reached close ul and div and open new div and ul and set $i to 0

      <div class="holder">
        <?php
        $members = $db->query('SELECT * FROM tableContacts ORDER BY lastName ASC');
        $rowcount = mysqli_num_rows($members);
        $i=0;
        if ( $rowcount > 0 ) { ?>
            <div class="members-right">
                <ul class="members">
                    <?php while ($row = $members->fetch_assoc()) { ?>
                        <li class="member">
                            //SOME CONTENT HERE
                        </li>
                    <?php
                    $i++;
                     if($i>=($rowcount/2)){
                     echo '</ul></div>
    <div class="members-right">
            <ul class="members">'; 
    $i=0;
                        }
    
                       } ?>
                </ul>
            </div>
        <?php } ?>
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀