Solved

Why output array is not in correct order

Posted on 2008-06-13
2
195 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

615 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