我用php开发了一个代码,我可以从数据库中获取产品信息并保存在一个关联数组中,然后我可以根据我的layout.Now问题来显示,就像当我从数据库表中挑选产品时,它的批量意味着如果我挑选了3个品牌的1000条记录,我想要显示它们,就像一个品牌的一个产品,然后是第二个品牌,然后是第三个品牌,然后根据products.Means的编号重复这种情况,我想要一个接一个地显示产品列表。下面是我用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循环only.if遍历这个数组,第一个品牌有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++;
}
?>发布于 2014-04-22 15:25:10
试试这个......
$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];
}
}https://stackoverflow.com/questions/23212783
复制相似问题