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

时间:2014-04-22 07:06:46

标签: php mysql arrays

我在php中开发了一个代码,我可以从数据库中获取产品信息并保存在关联数组中,然后我可以根据我的布局显示。现在我的问题就像当我从数据库表中选择产品时,批量意味着如果我选择1000个假设3个品牌的记录。我想将它们显示为一个品牌的产品,然后是第二个,然后是第三个,并根据产品的数量重复案例。我想显示产品列表一个个品牌名称。 以下是我在PHP中使用PHP开发的数组

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

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

$first[]=$row;
}

然后我只使用foreach循环迭代这个数组。如果我有第一个品牌的300条记录,第二个类型的200条记录和第三条形式的500条记录。我想以这样的方式进行迭代:第一个产品应该来自品牌1,第二个来自brand2,第三名来自brand3。

请指导我如何做到这一点......

数组显示格式代码

<?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>

尝试了以下代码,但没有正常工作

<?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++;
        }
        ?>

1 个答案:

答案 0 :(得分:0)

尝试这个......

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