Solved

Why output array is not in correct order

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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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 …

746 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

11 Experts available now in Live!

Get 1:1 Help Now