Solved

Why output array is not in correct order

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
php subtract from date 2 29
curl parse data from site 20 44
How do use '  ' within this code? 4 29
geting data from the array list 6 17
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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…
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 look for a specific file type in a local or remote server directory using PHP.

726 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