首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按品牌展示包含产品信息的关联数组

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

Stack Overflow用户
提问于 2014-04-22 15:06:46
回答 1查看 110关注 0票数 0

我用php开发了一个代码,我可以从数据库中获取产品信息并保存在一个关联数组中,然后我可以根据我的layout.Now问题来显示,就像当我从数据库表中挑选产品时,它的批量意味着如果我挑选了3个品牌的1000条记录,我想要显示它们,就像一个品牌的一个产品,然后是第二个品牌,然后是第三个品牌,然后根据products.Means的编号重复这种情况,我想要一个接一个地显示产品列表。下面是我用php编写的数组,它是从PHP开发的。

代码语言:javascript
复制
$firstsql='SELECT * from xml where ('.implode('AND',$parts1).') ';

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

$first[]=$row;
}

然后我使用foreach循环only.if遍历这个数组,第一个品牌有300条记录,第二个品牌有200条记录,第三个品牌有500条记录。我想这样迭代,第一个产品应该来自品牌1,第二个产品来自brand2,第三个产品来自brand3。

请指导我怎么做……

数组显示格式代码

代码语言:javascript
复制
<?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>

尝试了以下代码,但无法正常工作

代码语言:javascript
复制
<?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++;
        }
        ?>
EN

回答 1

Stack Overflow用户

发布于 2014-04-22 15:25:10

试试这个......

代码语言:javascript
复制
        $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];
            }
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23212783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档