Solved

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

Posted on 2008-06-12
11
269 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
 

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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 …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now