Solved

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

Posted on 2008-06-12
11
276 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 250 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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 250 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 250 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 250 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 250 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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.

827 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