Jump to content

Recommended Posts

hello again,

 

I have a loop through my db looking for products:

<?php 
if ($numProduct > 0 ) {

$i = 0;
while ($row = dbFetchAssoc($result)) {
//lots of output for product info etc

 

I want to group my products by the item they belong to - so I thought I would add a foreach loop:

<?php 
if ($numProduct > 0 ) {

$i = 0;
while ($row = dbFetchAssoc($result)) {
foreach() {
//info about the item (name, etc)

//all that product info I got before
}

 

My query looks like:

SELECT i.Name as ItemName, pd.ProductId, pd.Name, pd.PriceRetail, pd.ImagetnURL, PackageType, QtyInStock
	FROM product pd, item i, productpackaging pdp, packaging pack
	WHERE pd.ItemId = i.ItemId 
	AND pd.ProductId = pdp.ProductId
	AND pack.PackageId = pd.PackageId
	AND Pcode = 'RT'
	ORDER BY ItemName pd.Name

 

could some one help me with the foreach set up??

Link to comment
https://forums.phpfreaks.com/topic/64844-help-with-foreach/
Share on other sites

<?php
$arProductsByItem = array();
if ($numProduct > 0 ) {
  $i = 0;
  while ($row = dbFetchAssoc($result)) {
    $arProductsByItem[$row['ItemName']] = $row;
  } 	
}
// example of later output
if (count($arProductsByItem) > 0) {
  reset($arProductsByItem);
  while(list($Item,$arProd) = each($arProductsByItem)) {
    echo 'Item: '.$Item.' - Products: ';

    $prod = array();
    reset($arProd);
    while(list(,$row) = each($arProd)) {
      $prod[] = $row['Name'];
    } 
    echo Implode(", ", $prod);
  }
}
?>

Link to comment
https://forums.phpfreaks.com/topic/64844-help-with-foreach/#findComment-323566
Share on other sites

Thanks for the help!

 

Can you explain what the

    $prod = array();
    reset($arProd);
    while(list(,$row) = each($arProd)) {
      $prod[] = $row['Name'];
    } 
    echo Implode(", ", $prod);

is doing - and why it only outputs the first character of the field ??

 

My display should look something like,

- img -

Item: $ItemName 

product1 $Name : $PriceRetail

product2 " ": " "

etc,

Link to comment
https://forums.phpfreaks.com/topic/64844-help-with-foreach/#findComment-323608
Share on other sites

If you don't want to wait for reply..

http://www.php.net/manual/en/index.php

 

    $prod = array();
    // creates new empty array

    reset($arProd);
    // set array position to the first element

    while(list(,$row) = each($arProd)) {
      // this will take element from $arProd array on current position and push position to the next element

      $prod[] = $row['Name'];
      // add value from the $row array under the key 'Name' to the $prod array

    } 
    echo Implode(", ", $prod);
    // print out all values in the $prod array split by ', ' string

 

You can't use your while statement cause it will be an infinitive loop

 

To print out data as you've mentioned use my previous construct with this output

if (count($arProductsByItem) > 0) {
  reset($arProductsByItem);
  while(list($Item,$arProd) = each($arProductsByItem)) {
    echo 'Item: '.$Item.'<br />';

    reset($arProd);
    while(list(,$row) = each($arProd)) {
      echo 'product'.$i.' '.$row['Name'].' : '.$row['PriceRetail'].'<br />';
      ++$i;
    }
    echo '<br />';
  }
}

Link to comment
https://forums.phpfreaks.com/topic/64844-help-with-foreach/#findComment-323895
Share on other sites

Thanks Again for the help.

 

This is not exactly what I need - maybe because of my db query?

 

Your code spits out a record for each field in my query that I SELECTED so it prints this:

product0 C : C

product1 2 : 2

product2 2 : 2

product3 C : C

product4 0 : 0

product5 c : c

product6 9 : 9

product7 2 : 2

 

??

Link to comment
https://forums.phpfreaks.com/topic/64844-help-with-foreach/#findComment-323911
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.