?
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
?
279 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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

765 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