?
Solved

How to combine data of 3 or more elements in array and print it in one row

Posted on 2008-06-12
11
Medium Priority
?
281 Views
Last Modified: 2008-06-15
PHP

How to combine data of 5 elements in array and print it in one row
I have this code in my code snippet

in my code i get this output

ouput

coles | "1.00000"|coles| "5.00000"| coles| "2.00000"... and so on

from above clearly state that one "coles" repeat itself to print out different value of AMOUNT.

how to make it to print out as follow

coles | "1.00000"| "5.00000"|"2.00000"... and so on
that no coles is repeated and amount is printed in the same row



function GetAmount()
{
$ItemID = array(1234,6545,5468,9856,"no stock",4561,5466);
$arrayElement[3] = "no stock";
$supermarketID = 21;
 
 $i = 0;
 $j = 0;
 
 for ($j = 0; $j < sizeof($ItemID); $j++)
  {
 if (($ItemID[$j] != $arrayElement[3]) 
	{
	 $query = "SELECT getitem_std_amount( \"$supermarketID\",  \"ItemID[$j]\") AS AMOUNT";
     
	 $result = mysql_query($query);
     if(!$result) die("Query didn't work. " . mysql_error());
    
	 while($row = mysql_fetch_array($result))
	      {
          $this->GetAmountReturn[$i] .= '"'.$row['AMOUNT'].'"'; 
          $i++;
	      }	 
   	}  
	else
    {
      $this->GetAmountReturn[$i] .=  "\"Full Time Employees\" |";
  	  $i++;
	}
   }

Open in new window

0
Comment
Question by:firekiller15
  • 5
  • 5
11 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 1000 total points
ID: 21775893
Try:
function GetAmount()
{
$ItemID = array(1234,6545,5468,9856,"no stock",4561,5466);
$arrayElement[3] = "no stock";
$supermarketID = 21;
 
 $i = 0;
 $j = 0;
 
 for ($j = 0; $j < count($ItemID); $j++)
  {
 if (($ItemID[$j] != $arrayElement[3]) 
      {
       $query = "SELECT getitem_std_amount( \"$supermarketID\",  \"ItemID[$j]\") AS AMOUNT";
     
       $result = mysql_query($query);
     if(!$result) die("Query didn't work. " . mysql_error());
    $temp="";
       while($row = mysql_fetch_array($result))
            {
          $temp .= '"'.$row['AMOUNT'].'"'; 
          $i++;
            } 
                  $this->GetAmountReturn[$i] .= $temp;
         }  
      else
    {
      $this->GetAmountReturn[$i] .=  "\"Full Time Employees\" |";
          $i++;
      }
   }

Open in new window

0
 

Author Comment

by:firekiller15
ID: 21775947
>>   $temp .= '"'.$row['AMOUNT'].'"';
          $i++;
do i still need $i++?
0
 

Author Comment

by:firekiller15
ID: 21775951
why the output become empty?
Array ( [0] => "" ) Array ( [0] => "" [1] => "" ) Array ( [0] => "" [1] => "" [2] => "" ) Array ( [0] => "" [1] => "" [2] => "" [3] => "" ) Array ( [0] => "" [1] => "" [2] => "" [3] => "" [4] => "" ) Array ( [0] => "" [1] => "" [2] => "" [3] => "" [4] => "" [5] => "" ) Array ( [0] => "" [1] => "" [2] => "" [3] => "" [4] => "" [5] => "" [6] => "" ) Array
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:firekiller15
ID: 21775959
you code looks weird as

$temp="";
       while($row = mysql_fetch_array($result))
            {
          $temp .= '"'.$row['AMOUNT'].'"';
          $i++;
            }
                  $this->GetAmountReturn[$i] .= $temp;
         }  
$this->GetAmountReturn is outside the loop . can it get the data from mysql_fetch_array?
0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 1000 total points
ID: 21775992
actually, that
$i++;

should not be there at all. Instead change:
$this->GetAmountReturn[$i] .= $temp;

to:
$this->GetAmountReturn[$i++] .= $temp;
function GetAmount()
{
$ItemID = array(1234,6545,5468,9856,"no stock",4561,5466);
$arrayElement[3] = "no stock";
$supermarketID = 21;
 
 $i = 0;
 $j = 0;
 
 for ($j = 0; $j < count($ItemID); $j++)
  {
 if (($ItemID[$j] != $arrayElement[3]) 
      {
       $query = "SELECT getitem_std_amount( \"$supermarketID\",  \"ItemID[$j]\") AS AMOUNT";
     
       $result = mysql_query($query);
     if(!$result) die("Query didn't work. " . mysql_error());
    $temp="";
       while($row = mysql_fetch_array($result))
            {
          $temp .= '"'.$row['AMOUNT'].'"'; 
            } 
                  $this->GetAmountReturn[$i++] .= $temp;
         }  
      else
    {
      $this->GetAmountReturn[$i] .=  "\"Full Time Employees\" |";
          $i++;
      }
   }

Open in new window

0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 1000 total points
ID: 21776010
also, try using mysql_fetch_assoc instead of mysql_fetch_array
0
 

Author Comment

by:firekiller15
ID: 21776253
what is the difference btw them?
0
 

Author Comment

by:firekiller15
ID: 21776270
the output result of my code your yours seems like thesame
what is the purpose of your code?
why need to set a variable $temp to empty string?
0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 1000 total points
ID: 21777756
>>what is the difference btw them?
If your record returns only one column/field named "AMOUNT" and assuming your first record contais the value 3.000, then
when you do:
$row = mysql_fetch_array($result);
then $row gets/sees:
Array( AMOUNT=>'3.000', 0=>'3.000');

You get an associative index/"named index" AND a numeric index. When you use mysql_fetch_assoc you do NOT get the numeric index. Since you are not using the numeric index, I suggest you get rid of it. No need to allocate/consume memory for something you are not going to use.

>>the output result of my code your yours seems like thesame
I suppose this means that it is still not working as expected. Correct?

>>what is the purpose of your code?
a. execute a query for every $ItemID that does NOT equal "no stock"
b. grab the query results (which may have more than one record) and save/collect the values in a variable named $temp
c. Once all the values for the current id have been "collected" THEN save them to $this->GetAmountReturn[$i++] ;

If the code is not working, my follow-up question is what is in:
$this->GetAmountReturn[$i] initially? Is it empty or does it always have the value "coles"? If not, where is "coles" coming from?
0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 1000 total points
ID: 21777795
See if this works:
function GetAmount()
{
$ItemID = array(1234,6545,5468,9856,"no stock",4561,5466);
$arrayElement[3] = "no stock";
$supermarketID = 21;
 
 $i = 0;
 $j = 0;
 
 for ($j = 0; $j < sizeof($ItemID); $j++)
  {
 if (($ItemID[$j] != $arrayElement[3]) 
	{
	 $query = "SELECT getitem_std_amount( \"$supermarketID\",  \"ItemID[$j]\") AS AMOUNT";
     
	 $result = mysql_query($query);
     if(!$result) die("Query didn't work. " . mysql_error());
    
	 while($row = mysql_fetch_array($result))
	      {
          $this->GetAmountReturn[$i] .= '"'.$row['AMOUNT'].'"'; 
          $i++;
	      }	 
   	}  
	else
    {
      $this->GetAmountReturn[$i] .=  "\"Full Time Employees\" |";
  	  $i++;
	}
   }
 
 
function joinSUPERMARKETNAMEnAMOUNT()
{
//SUPERMARKETNAME select value is from another function
 
 foreach($this->GetSupermarketReturn_value as $datas)
	{
           $data1 = "{$datas} | " . implode(" | ", $this->GetAmountReturn ); 
	   print_r( $data1);
  }  
}

Open in new window

0
 
LVL 29

Expert Comment

by:fibo
ID: 21785506
Fire killer15,

This question seems to duplicate your other question
http://www.experts-exchange.com/Q_23481937.html

You would probably get better input from experts if not asking them in parallel.

And if they are duplicate, how are you going to allocate points to both questions?
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question