Solved

Why output array is not in correct order

Posted on 2008-06-13
2
192 Views
Last Modified: 2008-06-15
PHP

i have this code in code snippet

my output code $data1 = "{$datas} | {$datas1}";
print_r( $data1);

in my output get amount i suppose to get 6 item display on the screen.
dont know why i only get one item display on the screen

the item here i refered is the ouput select data that i call by the condition of  1234,6545,5468,9856,"no stock",4561,5466
why only one item return to my screen?


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++] .=  "\"no stock\" |";
          $i++;
      }
   }
 
 
function joinSUPERMARKETNAMEnAMOUNT()
{
//SUPERMARKETNAME select value is from another function
 
 foreach(($this->GetSupermarketReturn_value as $datas)
      {
    foreach($this->GetAmountReturn as $datas1)
       {
           $data1 = "{$datas} | {$datas1}  ";
         print_r( $data1);
       }
        }  
}

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
2 Comments
 
LVL 4

Accepted Solution

by:
afzz earned 125 total points
ID: 21777107
try this
foreach($this->GetAmountReturn as $datas1)
       {
           $data1 = "{$datas}"." | "."{$datas1}  ";
         print_r( $data1);
       }

Open in new window

0
 
LVL 29

Assisted Solution

by:fibo
fibo earned 125 total points
ID: 21784767
Some questions about your initial code:
----------------
  while($row = mysql_fetch_array($result))
            {
          $this->GetAmountReturn[$i++] .= '"'.$row['AMOUNT'].'"';
          $i++;
            }      
         }  
      else
    {
      $this->GetAmountReturn[$i++] .=  "\"no stock\" |";
          $i++;
      }
   }
--------------------
1 - Because of the 2 $i++, at each loop $i is increased by 2, leaving empty homes in GetAmountReturn. Is that deliberate?
2 - I would think that because of
<<$data1 = "{$datas} | {$datas1}  ";>>, the | in
<<$this->GetAmountReturn[$i++] .=  "\"no stock\" |";>> should be
<<$this->GetAmountReturn[$i++] .=  "\"no stock\" ";>>
3 - I fear that the line <<$data1 = "{$datas} | {$datas1}  ";>> is structurally and semantically wrong. I would think Zend Studio would catch that.
This line is followed by print_r( $data1) which implies that $data1 is/ should be an array or some kind of single value.
BUT what exactly do you expect "{$datas} | {$datas1}  " to be? if {datas} is an array, what is the output?
You might consider just "linearizing" the arrays, eg by some type of implode, and putting that into a single value... maybe putting these single values in an array


0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

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.
This article discusses how to implement server side field validation and display customized error messages to the client.
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.
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 …

734 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