Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Why output array is not in correct order

Posted on 2008-06-13
2
Medium Priority
?
202 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
2 Comments
 
LVL 4

Accepted Solution

by:
afzz earned 375 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 375 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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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 count occurrences of each item in an array.
Suggested Courses

885 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