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

标签 php mysql arrays

我在 php 中开发了一个代码,我可以从数据库中获取产品信息并将其保存在关联数组中,然后我可以根据我的布局显示该信息。现在对我来说问题就像当我从数据库表中选择产品时,它批量意味着如果我选择假设 3 个品牌的 1000 条记录。我想将它们显示为一个品牌的一种产品,然后是第二个品牌,然后是第三个品牌,并根据产品数量重复这种情况。意味着我想显示产品列表一一品牌名称。 以下是我用 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,第二个来自品牌2,第三个来自品牌3。

请指导我如何执行此操作...

数组显示格式代码

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

最佳答案

试试这个......

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

关于php - 按品牌显示包含产品信息的关联数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23212783/

相关文章:

PHP/MySQL 好友请求系统

mysql - 使用 MySql 将最大记录存储在 query_cache 中

c - 为什么我的子字符串前有奇怪的字符?

PHP 文件和数组

php - 如何使用sql JOIN查询显示多个表中的所有数据?

php:在空白处将字符串分成两半,直到结果部分都不长于 x 个单词

php - 您可以在 PHP 中从内存中的图像创建 GD 图像吗?

php - 使用多个选择器删除一条mysql记录

java - JDBC 和 MySQL DATE 相差一个 - 如何更正

c++ - 如何获取多维结构数组的长度?