如何在php中使用变量外循环

时间:2017-08-21 04:45:49

标签: php while-loop

<ul class="resp-tabs-list">
    <?php
    $get_products="SELECT * FROM `categories` ORDER BY 'id' LIMIT 0,4 ";
    $run_products=mysqli_query($con, $get_products);
    while($row_products=mysqli_fetch_array($run_products)){

        //get data  from database
        $category_id=$row_products['cat_id'];
        $product_title=$row_products['cat_title'];
        ?>
        <li> <?php echo $product_title;?></li>
        <?php
    }
    ?>
</ul>

//Outside variable call by $category_id 

<?php   
$get_top_pro="SELECT * FROM `products` WHERE product_cat= '$category_id' 
LIMIT 0,8";
$run_top_pro=mysqli_query($con, $get_top_pro);                                  
while($row_top_products=mysqli_fetch_array($run_top_pro)){                                  
    $get_top_pro_id=$row_top_products['product_id'];                                    
    $get_top_pro_title=$row_top_products['product_title'];                                  
    $get_top_pro_image=$row_top_products['product_image'];

我想传递$category_id变量来从特定类别中获取8个产品。

2 个答案:

答案 0 :(得分:0)

您需要将ID保存在数组中,然后再使用它来获取特定的ID类别。

<?php
$get_products="SELECT * FROM `categories` ORDER BY 'id' LIMIT 0,4 ";
$run_products=mysqli_query($con, $get_products);
$category_id = array(); //array which you will save the ids
while($row_products=mysqli_fetch_array($run_products)){

    //get data  from database
    $category_id[] = $row_products['cat_id']; //each id is saved into category_id array
    $product_title=$row_products['cat_title'];
    ?>
    <li> <?php echo $product_title;?></li>
    <?php
}
?>

或者您可以使用array_push()将值插入数组。

<?php
$get_products="SELECT * FROM `categories` ORDER BY 'id' LIMIT 0,4 ";
$run_products=mysqli_query($con, $get_products);
$category_id = array(); //array which you will save the ids
while($row_products=mysqli_fetch_array($run_products)){

    //get data  from database
    array.push($category_id, $row_products['cat_id']); //each id is saved into category_id array
    $product_title=$row_products['cat_title'];
    ?>
    <li> <?php echo $product_title;?></li>
    <?php
}
?>

然后,使用特定的ID来获取类别:

<?php  
  foreach ($category_id as $category) {
         $get_top_pro="SELECT * FROM `products` WHERE product_cat= '$category' LIMIT 0,8";
         $run_top_pro=mysqli_query($con, $get_top_pro);                                  

         while($row_top_products=mysqli_fetch_array($run_top_pro)){                                  
               $get_top_pro_id=$row_top_products['product_id'];                                    
               $get_top_pro_title=$row_top_products['product_title'];                                  
               $get_top_pro_image=$row_top_products['product_image'];
         }
  }

答案 1 :(得分:0)

你可以使用数组。

<ul class="resp-tabs-list">
        <?php
        $catArray   = array();
        $get_products="SELECT * FROM `categories` ORDER BY 'id' LIMIT 0,4 ";
        $run_products=mysqli_query($con, $get_products);
        while($row_products=mysqli_fetch_array($run_products)){

            //get data  from database
            $catArray[]=$row_products['cat_id'];///save all cat_id in an array 
            $product_title=$row_products['cat_title'];
            ?>
            <li> <?php echo $product_title;?></li>
            <?php
        }
        ?>
    </ul>

    //Outside variable call by $category_id 

    <?php 
    for ($i==0;$i<count($catArray);$i++)//loop through  all the values of an array
    $get_top_pro="SELECT * FROM `products` WHERE product_cat= '$catArray[$i]' 
    LIMIT 0,8";
    $run_top_pro=mysqli_query($con, $get_top_pro);                                  
    while($row_top_products=mysqli_fetch_array($run_top_pro)){                                  
        $get_top_pro_id=$row_top_products['product_id'];                                    
        $get_top_pro_title=$row_top_products['product_title'];                                  
        $get_top_pro_image=$row_top_products['product_image'];
    }