dongsuo0517 2018-08-22 01:30
浏览 47
已采纳

即时通讯试图在PHP中制作一个循环以下的循环

im trying to make a loop in php that spits out the below

$sqlons = "SELECT * FROM products WHERE ons = 1";
$ons = $db->query($sqlons);
$sqlpopa = "SELECT * FROM products WHERE popa = 1";
$popa = $db->query($sqlpopa);
$sqlnewr = "SELECT * FROM products WHERE newr = 1";
$newr = $db->query($sqlnewr);
$sqlpopm = "SELECT * FROM products WHERE popm = 1";
$popm = $db->query($sqlpopm);
$sqlpopm2 = "SELECT * FROM products WHERE popm = 2";
$popm2 = $db->query($sqlpopm2);
$sqlfeata = "SELECT * FROM products WHERE feata = 1";
$feata = $db->query($sqlfeata);
$sqlfeata2 = "SELECT * FROM products WHERE feata = 2";
$feata2 = $db->query($sqlfeata2);
$sqlbests = "SELECT * FROM products WHERE bests = 1";
$bests = $db->query($sqlbests);
$sqlbests2 = "SELECT * FROM products WHERE bests = 2";
$bests2 = $db->query($sqlbests2);
$sqlbests3 = "SELECT * FROM products WHERE bests = 3";
$bests3 = $db->query($sqlbests3);
$sqlsea = "SELECT * FROM products WHERE sea = 1";
$sea = $db->query($sqlsea);

i've currently done this

$vart = array("sqlons", "sqlpopa", "sqlnewr", "sqlpopm", "sqlfeata", "sqlbests", "sqlsea");
$vart2 = array("ons", "popa", "newr", "popm", "feata", "bests", "sea");
$var = array("ons", "popa", "newr", "popm", "popm", "feata", "feata", "bests", "bests", "bests", "sea");
$var2 = array("1", "1", "1", "1", "2", "1", "2", "1", "2", "3", "1");
foreach($var as $v){
    foreach($var2 as $v2){
        foreach($vart as $t){
            "$".$t = "SELECT * FROM products WHERE $v = $v2";
            foreach($vart2 as $t2){
            "$".$t2 = $db->query("$".$t);
            }
        }
    }
}

but i get an undefined variable such a "Undefined variable: ons in C:\xampp\htdocs\AniBuy\pages\index.php on line 90"

please help me! :)

sorry this might of helped if i added the html/php code

<?php while($product = mysqli_fetch_assoc($ons)) : ?>
                        <div class="<?= $product['class']; ?>">
                            <div class="grid-col-1">
                                <img class="featured-image img-responsive" src="<?= $product['img']; ?>">
                            </div>
                            <div class="grid-col-2">
                                <h2 class="featured-heading"><?= $product['title']; ?></h2>
                                <div class="grid-col-3">
                                    <div class="span-text"><?= $product['about']; ?><br></div><div class="buy-now"><a href="#">BUY NOW</a></div>
                                </div>
                            </div>
                        </div>
                    <?php endwhile; ?>

there is more then just one there are multiple of theses that spit out the img title ect each from the phpmyadmin database

 <?php while($product = mysqli_fetch_assoc($popa)) : ?>
                            <div class="<?= $product['class']; ?>">
                            <div class="buy-now buy"><a href="#">BUY NOW</a></div>
                                <div h4 class="read-more" onclick="rmModal(<?= $product['id'];?>)"><a href="#rmModal-<?= $product['title'];?>.php">READ MORE</a></div>
                                <div class="featured-image"><img class="featured-image img-pop-anime img-responsive" src="<?= $product['img']; ?>"></div>
                            </div>
                            <?php endwhile; ?>
    <?php while($product = mysqli_fetch_assoc($newr)) : ?>
                        <div class="<?= $product['class']; ?>">
                            <div class="buy-now buy"><a href="#">BUY NOW</a></div>
                            <div h4 class="read-more" onclick="rmModal(<?= $product['id'];?>)"><a href="#rmModal-<?= $product['title'];?>.php">READ MORE</a></div>
                            <div class="grid-col-1"><img class="featured-image img-new" src="<?= $product['img']; ?>"></div>
                        </div>
                    <?php endwhile; ?>

so far i have tried this

$vars = array("ons" => 1, "popa" => 1, "newr" => 1);
 foreach ($vars as $key => $value) {
"$"."sql".$key = "SELECT * FROM products WHERE $key = $value";
"$".$key = $db->query("$"."sql".$key);
// echo "$"."sql".$key;
// echo $value;
// echo "$".$key;
   }

but it sill gives me the error mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, string given in C:\xampp\htdocs\AniBuy\pages\index.php on line 142

if anyone is interested in what the website looks like here's the url it only has html, css and js: https://54x1.github.io/AniBuy/pages/indexv2.html

here are the files with php and mysql i've included the sql export in side the anibuy folder btw its .../pages/index.php: https://drive.google.com/drive/folders/1mUYm2sH1bsxF3UaCxAvOIwTkubLCoVod?usp=sharing

  • 写回答

5条回答 默认 最新

  • dqcj32855 2018-08-22 06:01
    关注

    The error that was triggered on your side, it's weird because when I run your code on my side, I did not get it.

    In the first array add all your fields names and values where ons is the field name and 1 is the value for the where condition. Do the same for the others.

    $key = fields names in database

    $value = value of field name

    // As you have different values for bests, 1, 2, 3 and for others
    // If we used bests as index ex: array('bests' => 2, 'bests' => 3). The data
    // will be overwritten by the next bests as they have the same index.
    // 'fieldname_value' where fieldname = ons and value = 1
    $vars    = array('ons_1', 'bests_1', 'bests_2', 'bests_3'); 
    $results = array();
    foreach ($vars as $key => $value) {
    
    // Here I break the value which is ex: ons_1 into 
    // an array(ons, 1). This will repeat for the other values.
    $values = explode('_', $value);
    
    $sql = "SELECT * FROM products WHERE $values[0] = $values[1]"; // $values[0] = fieldname $values[1] = value_of_fieldname
    
    $result = mysqli_query($db, $sql);
    
    if (mysqli_num_rows($result) > 0) {
        // output data of each row
        while($row = mysqli_fetch_assoc($result)) {
            $results[$value][] = $row; // Add all rows for fieldnames (ex: ons_1, bests_1, bests_2) inside $result array
        }
    } 
    } 
    
    /**
     *
     * First var_dump($results); to get a clearer view of what's going on below
     *
     * The array $results contain an array of arrays 
     * Ex: $results = array ('ons_1' => array( 0 => array('class' => your_class), 'bests_1' => array( 0 => array('class' => your_class)) 
     *
     * This if line checks whether the index exist isset($results['ons_1']) and if it is not empty !empty($results['ons_1'])
     * before doing the foreach loop. It would be unecessary to do a foreach loop that will trigger an error if ons_1 does
     * not exist and has no data. This is why this check is done.
     * 
     * The foreach loop on the other hand will loop through all the data present in 'ons_1' array. var_dump($result['ons_1']); to 
     * have an idea of what's going ons. 
     *
     */
    
    ?>
    
    <div class="grid-container container-fluid ">
        <div id="home-page" class="home-page">
    
         <h2 class="featured-heading-2">On Sale Now!</h2>
            <div class="home-page-onsale">
                <?php if (isset($results['ons_1']) && !empty($results['ons_1'])) { ?>
    
                    <?php foreach ($results['ons_1'] as $key => $product) { ?>
                            <div class="<?php echo $product['class']; ?>">
                            <div class="buy-now buy"><a href="#">BUY NOW</a></div>
                            <div h4 class="read-more" onclick="rmModal(<?php echo $product['id']; ?>)"><a href="#rmModal-<?php echo $product['title']; ?>.php">READ MORE</a></div>
                            <div class="grid-col-1"><img class="featured-image img-new" src="<?php echo $product['img']; ?>"></div>
                            </div>
                    <?php } ?>
                <?php }?>
            </div>
    
            <div class="breakpoint-3">
                        <div class="home-page-best-anime">
                            <h2 id="home-page-best-anime bpshow" class="featured-heading-2">Best Sellers</h2>
                            <div class="home-page-best-anime-row-1">
                                <?php if (isset($results['bests_1']) && !empty($results['bests_1'])) { ?>
    
                                    <?php foreach ($results['bests_1'] as $key => $product) { ?>
                                            <div class="<?php echo $product['class']; ?>">
                                            <div class="buy-now buy"><a href="#">BUY NOW</a></div>
                                            <div h4 class="read-more" onclick="rmModal(<?php echo $product['id']; ?>)"><a href="#rmModal-<?php echo $product['title']; ?>.php">READ MORE</a></div>
                                            <div class="grid-col-1"><img class="featured-image img-new" src="<?php echo $product['img']; ?>"></div>
                                            </div>
                                    <?php } ?>
                                <?php }?>
                            </div>
                            <div class="home-page-best-anime-row-2">
                                <?php if (isset($results['bests_2']) && !empty($results['bests_2'])) { ?>
    
                                    <?php foreach ($results['bests_2'] as $key => $product) { ?>
                                            <div class="<?php echo $product['class']; ?>">
                                            <div class="buy-now buy"><a href="#">BUY NOW</a></div>
                                            <div h4 class="read-more" onclick="rmModal(<?php echo $product['id']; ?>)"><a href="#rmModal-<?php echo $product['title']; ?>.php">READ MORE</a></div>
                                            <div class="grid-col-1"><img class="featured-image img-new" src="<?php echo $product['img']; ?>"></div>
                                            </div>
                                    <?php } ?>
                                <?php }?>
                            </div>
                            <div class="home-page-best-anime-row-3">
                                <?php if (isset($results['bests_3']) && !empty($results['bests_3'])) { ?>
    
                                    <?php foreach ($results['bests_3'] as $key => $product) { ?>
                                            <div class="<?php echo $product['class']; ?>">
                                            <div class="buy-now buy"><a href="#">BUY NOW</a></div>
                                            <div h4 class="read-more" onclick="rmModal(<?php echo $product['id']; ?>)"><a href="#rmModal-<?php echo $product['title']; ?>.php">READ MORE</a></div>
                                            <div class="grid-col-1"><img class="featured-image img-new" src="<?php echo $product['img']; ?>"></div>
                                            </div>
                                    <?php } ?>
                                <?php }?>
                            </div>
                        </div>
    
        </div>
        </div>
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加