亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從表中獲取所有結果,每行最多列出 6 個結果。如果少于 6 個結果存在,列出其他項目以填充行中

從表中獲取所有結果,每行最多列出 6 個結果。如果少于 6 個結果存在,列出其他項目以填充行中

PHP
白板的微信 2023-04-15 17:28:29
我的 MYSQL 表“用戶”中共有 8 個結果/用戶。我想每行顯示 6 個結果/用戶配置文件圖像,如下所示:1st Result.   2nd Result.  3rd Result.  4th Result.   5th Result.  6th Result7th Result.   8th Result.    No More Results..... 要求每行至少包含 6 個結果/個人資料圖片。如果沒有足夠的結果/個人資料圖片來完成一行,那么我會嘗試使用模板個人資料圖片“在此處宣傳您的個人資料”來填充空間。advertise here 模板圖像存儲在以下目錄中:<div><img src="data/profile/0/main/advert.jpg" alt="Profile" height="100%" width="100%"></div>';給出期望的結果:1st Result.   2nd Result.  3rd Result.  4th Result.   5th Result.  6th Result7th Result.   8th Result.    9 Ad Here.  10 Ad Here.  11 Ad Here.  12 Ad Here.這是我目前擁有的代碼,感謝用戶@MegaColorBoy 到目前為止幫助我編寫代碼。但是,代碼仍然沒有給出預期的結果。請有人幫助改進修改代碼,讓它給我我需要的結果。謝謝。代碼: <?php $sql = "SELECT * FROM users WHERE status = 'active' AND usertype = 'advertiser'";        $result = $conn->query($sql)->fetch_all(MYSQLI_ASSOC);        $limit = 6;         $chunks = array_chunk($result, $limit);        foreach($chunks as $chunk){        echo '<div id="category_case_holder">';        foreach($chunk as $chunkItem){        echo '<div id="prime"><a href="profile.php?id='.htmlspecialchars($chunkItem['user_id']).'"><img src="data/profile/'.htmlspecialchars($chunkItem['user_id']).'/main/profile.jpg" alt="Profile" height="100%" width="100%"></a></div>';        }        echo '</div>';        } ?> 
查看完整描述

3 回答

?
搖曳的薔薇

TA貢獻1793條經驗 獲得超6個贊

如果我理解你的問題,我想你應該試試這個:


<div id="category_case_holder">

<table>

<tr>

<?php 

  $sql = "SELECT * FROM users WHERE status = 'active' AND usertype = 'advertiser'";

  $result = $conn->query($sql)->fetch_all(MYSQLI_ASSOC);

  $resultNumber = count($result);

  for ($i = 1; $i <= $resultNumber; $i++) {

    echo "<td>";

    $key = $i - 1;

      if (isset($result[$key])) {

       $filename = "data/profile/$i/main/profile.jpg";

       if (file_exists($filename)) {

        echo '<div id="prime"><a href="profile.php?id='.htmlspecialchars($result[$key]['user_id']).'"><img src="data/profile/'.htmlspecialchars($result[$key]['user_id']).'/main/profile.jpg" alt="Profile" height="100%" width="100%"></a></div>';

        } else {

          echo '<div id="prime"><a href="profile.php?id='.htmlspecialchars($result[$key]['user_id']).'"><img src="data/profile/0/main/profile.jpg" alt="Profile" height="100%" width="100%"></a></div>';

        }

      } else {

        echo '<div id="prime"><img src="data/profile/0/main/advert.jpg" alt="Profile" height="100%" width="100%"></div>';

      }

    echo "</td>";

    if (($i % 6) == 0 && $i <= $resultNumber) {

      echo "</tr>";

      echo "<tr>";

    }

  }

?>

</tr>

</div>


查看完整回答
反對 回復 2023-04-15
?
蕪湖不蕪

TA貢獻1796條經驗 獲得超7個贊

以下是完全未經測試的,可能還沒有看到數據,也沒有關于個人資料圖像的答案,但總體目標是將結果數組分解成小塊——這是理想的選擇array_chunk。


<div id="category_case_holder">

    <?php


        $sql = "SELECT * FROM `users` WHERE `status` = 'active' AND `usertype` = 'advertiser'";

        $result = $conn->query( $sql )->fetch_all( MYSQLI_ASSOC );


        # a placeholder in the path will be substituted later using sprintf or printf

        $filepath = 'data/profile/%s/main/profile.jpg';


        if( !empty( $result ) ){


            # split the recordset array into chunks - 6 records long. 

            # Each chunk will be a row from recordset.

            $chunks=array_chunk( $result, 6 );


            foreach( $chunks as $chunk ){

                echo '<div>';

                foreach( $chunk as $i => $rs ){


                    $filename=file_exists( sprintf( $filepath, $i ) ) ? sprintf( $filepath, $rs['user_id'] ) : sprintf( $filepath, '0' );

                    # ID attributes MUST be unique.

                    # substitue placeholders for values from this row data


                    printf('

                        <div class="prime">

                            <a href="profile.php?id=%s">

                                <img src="%s" alt="Profile" height="100%" width="100%">

                            </a>

                        </div>',

                        htmlspecialchars( $rs['user_id'] ),

                        $filename

                    );

                }

                echo '</div>';

            }

        }else{

            echo '<div id="prime"><img src="data/profile/0/main/advert.jpg" alt="Profile" height="100%" width="100%"></div>';

        }

    ?>

</div>


查看完整回答
反對 回復 2023-04-15
?
繁星coding

TA貢獻1797條經驗 獲得超4個贊

您可以使用該array_chunk()方法輕松實現。


例子:


//set it to whatever limit you want.

$limit = 6;?


// this will divide the array into x number of chunks based on y limit.

$chunks = array_chunk($result, limit);


foreach($chunks as $chunk){

? ?echo '<div>';

? ?foreach($chunk as $chunkItem){

? ? ? // your stuff here

? ?}

? ?echo '</div>';

}?


查看完整回答
反對 回復 2023-04-15
  • 3 回答
  • 0 關注
  • 181 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號