Solved

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

Posted on 2008-06-12
11
272 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

914 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

16 Experts available now in Live!

Get 1:1 Help Now