简体   繁体   English

按品牌显示包含产品信息的关联数组

[英]display associative array containing product info brand by brand

I developed a code in php where i can get the product info from database and save in an associative array and then i can display the according to my layout.Now problem for me is like when i pick products from database table,its in bulk means if i pick 1000 records of suppose 3 brands.I want to display them like one product for one brand then for second and then for third and repeating the case according to the number of products.Means i want to display the product list one by one brand name. 我在php中开发了一个代码,可以从数据库中获取产品信息并保存在关联数组中,然后可以根据我的layout显示。现在的问题就像我从数据库表中选择产品时一样,它的批量意味着如果我选择了1000个假设的3个品牌的记录,我想将它们像一个产品展示给一个品牌,然后显示为第二个,然后显示为第三个,并根据产品的数量重复此案例。我想一一显示产品列表品牌。 Following is my array in php which im developing from PHP 以下是我从PHP开发的PHP数组

$firstsql='SELECT * from xml where ('.implode('AND',$parts1).') ';

$firstsql3=mysql_query($firstsql);
$first=array();  
while($row = mysql_fetch_array($firstsql3)) {

$first[]=$row;
}

And then im iterating through this array using foreach loop only.if i have 300 records for first brand,200 for second and 500 for third.I want to iterate in such a way that first product should be from brand 1,second from brand2,third from brand3. 然后仅使用foreach循环遍历此数组。如果我有第一个品牌的300条记录,第二个品牌的200条记录,第三个品牌的500条记录,我想以这样的方式进行迭代:第一个产品应来自品牌1,第二个产品应来自brand2,第三品牌。

Please guide me on how to do this... 请指导我如何做...

Code for array displaying format 数组显示格式的代码

<?php
foreach($countArray as $array)
{
?>

<div>
<a href="index23.php?name=<?php $array['IMAGEURL']?>"><img src="<?php echo $array['IMAGEURL']?>"/></a><br />
<h3><?php echo $array['BRAND']?></h3>
</div>

Tried the following code but didnt work properly 尝试了以下代码,但无法正常工作

<?php
 $firstsql='SELECT * from xml ';
print($firstsql);
        $firstsql3=mysql_query($firstsql);
        $first=array();  
        while($row = mysql_fetch_array($firstsql3)) {

            $first[]=$row;
        }

        $brand1 = array();
        $brand2 = array();
        $brand3 = array();

        //seperate the product by brand
        foreach($first as $v){
            if( $v['brand'] == 1 ){
                $brand1[] = $v;
            } else if( $v['brand'] == 2 ){
                $brand2[] = $v;
            } else if( $v['brand'] == 3 ){
                $brand3[] = $v;
            }
        }

        //count the number of product for each brand
        $brand1_c = count($brand1);
        $brand2_c = count($brand2);
        $brand3_c = count($brand3);
echo $brand1_c;
echo $brand2_c;
        //initialize the final product array
        $final = array();

        //get the highest count from each brand products
        $highest_number = max($brand1_c, $brand2_c, $brand3_c);

        //on basis of highest count go with for loop
        for( $i=1; $i<$highest_number; $i++  ){
            //check that array key exist or not brand1
            if (array_key_exists($i, $brand1)) {
                //add the product details in final product array
                $final[] = $brand1[$i];
            }

            //check that array key exist or not for brand2
            if (array_key_exists($i, $brand2)) {
                //add the product details in final product array
                $final[] = $brand2[$i];
            }                   

            //check that array key exist or not for brand3
            if (array_key_exists($i, $brand3)) {
                //add the product details in final product array
                $final[] = $brand3[$i];
            }
        }
        foreach($final as $array)
        {
        $i;
        ?>
        <div>
        <img src="<?php echo $array['IMAGEURL'] ?>"/>
        </div>
        <?php
        $i++;
        }
        ?>

TRY THIS...... 尝试这个......

        $firstsql='SELECT * from xml where ('.implode('AND',$parts1).') ';

        $firstsql3=mysql_query($firstsql);
        $first=array();  
        while($row = mysql_fetch_array($firstsql3)) {

            $first[]=$row;
        }

        $brand1 = array();
        $brand2 = array();
        $brand3 = array();

        //seperate the product by brand
        foreach($first as $v){
            if( $v['brand'] == 1 ){
                $brand1[] = $v;
            } else if( $v['brand'] == 2 ){
                $brand2[] = $v;
            } else if( $v['brand'] == 3 ){
                $brand3[] = $v;
            }
        }

        //count the number of product for each brand
        $brand1_c = count($brand1);
        $brand2_c = count($brand2);
        $brand3_c = count($brand3);

        //initialize the final product array
        $final = array();

        //get the highest count from each brand products
        $highest_number = max($brand1_c, $brand2_c, $brand3_c);

        //on basis of highest count go with for loop
        for( $i=1; $i<$highest_number; $i++  ){
            //check that array key exist or not brand1
            if (array_key_exists($i, $brand1)) {
                //add the product details in final product array
                $final[] = $brand1[$i];
            }

            //check that array key exist or not for brand2
            if (array_key_exists($i, $brand2)) {
                //add the product details in final product array
                $final[] = $brand2[$i];
            }                   

            //check that array key exist or not for brand3
            if (array_key_exists($i, $brand3)) {
                //add the product details in final product array
                $final[] = $brand3[$i];
            }
        }

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

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