简体   繁体   English

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

[英]How to use variable outside while loop in php

<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'];

I want to pass the $category_id variable for fetching 8 products from the particular category. 我想传递$category_id变量,以从特定类别中获取8种产品。

You need to save the ids in array then later use it to get specific id category. 您需要将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
}
?>

OR you can use array_push() in inserting value to array. 或者,您可以在向数组插入值时使用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
}
?>

Then, to use the specific id to get categories: 然后,使用特定的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'];
         }
  }

you can use array. 您可以使用数组。

<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'];
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM