Solved

Why my array output is wrong

Posted on 2008-06-12
8
178 Views
Last Modified: 2008-06-15
PHP

Why my array output is wrong

i have this code in code snippet

my result should be
supermarketname | Amount_of_candy | Amount_of_milo | AMount_of_milk |...
    coles       |    1.00000      |   5.00000         |    2.00000     |...
    target      |    5.00000      |  10.00000      |    3.00000     |...
...
   
but i get result as below
      
coles | "1.00000"|target| "5.00000"| parkson| "2.00000"... and so on

other words my amount become
coles | array[0] | target | array[1] | parkson | array[2] .. and so on

this is so wrong as you can see from my code GetAmount(()

coles should follow by array[0],array[1] and array[2]

but dont know why array[1] has go to target which is wrong
how to do this?

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());
    
     $temp = "";   	 
     while($row = mysql_fetch_array($result))
	      {
            $temp = '"'.$row['AMOUNT'].'"';
              } 
              $this->GetAmountReturn[$i++] .= $temp ; 
              }	 
   	}  
	else
    {
      $this->GetAmountReturn[$i] .=  "\"Full Time Employees\" |";
  	  $i++;
	}
   }
 
 Wishing you a very happy birthday and a year full of things you love 
function joinSUPERMARKETNAMEnAMOUNT()
{
//SUPERMARKETNAME select value is from another function
 
 foreach(($this->GetSupermarketNameReturn_value as $i => $SupermarketName)
	{
         $data1 = "{$SupermarketName} | {$this->GetAmountReturn[$i]}  "; 
	  print_r( $data1);
	 }
        }  
}

Open in new window

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
  • 4
  • 3
8 Comments
 
LVL 48

Expert Comment

by:hernst42
ID: 21776465
Try replacing

              $this->GetAmountReturn[$i++] .= $temp ;
              }  
        }  
        else
    {
      $this->GetAmountReturn[$i] .=  "\"Full Time Employees\" |";
          $i++;
        }
with

              $this->GetAmountReturn[$j] .= $temp ;
              }  
        }   else    {
      $this->GetAmountReturn[$j] .=  "\"Full Time Employees\" |";
        }
0
 

Author Comment

by:firekiller15
ID: 21776641
change it to $j doesnt make any different?
0
 
LVL 48

Expert Comment

by:hernst42
ID: 21776780
please post the modified code
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:firekiller15
ID: 21776850
function GetAmount()
{
$ItemID = array(1234,6545,5468,9856,"no stock",4561,5466);
$arrayElement[3] = "no stock";
$supermarketID = 21;
 
 
 
 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());
   
     $temp = "";         
     while($row = mysql_fetch_array($result))
            {
            $temp = '"'.$row['AMOUNT'].'"';
              }
              $this->GetAmountReturn[$j] .= $temp ;
              }       
         }  
      else
    {
      $this->GetAmountReturn[$j] .=  "\"Full Time Employees\" |";
          
      }
   }
 
 Wishing you a very happy birthday and a year full of things you love
function joinSUPERMARKETNAMEnAMOUNT()
{
//SUPERMARKETNAME select value is from another function
 
 foreach(($this->GetSupermarketNameReturn_value as $i => $SupermarketName)
      {
         $data1 = "{$SupermarketName} | {$this->GetAmountReturn[$i]}  ";
        print_r( $data1);
       }
        }  
}
 
0
 
LVL 48

Expert Comment

by:hernst42
ID: 21776909
ok slowly I get the datastructure. The replace with $j is wrong, replace the $j in those two lines with $supermarketID
0
 

Author Comment

by:firekiller15
ID: 21777092
is still the same
0
 
LVL 48

Expert Comment

by:hernst42
ID: 21777124
hm no clue and without knowing you datastructures it can get very complicated.
0
 
LVL 29

Accepted Solution

by:
fibo earned 250 total points
ID: 21785490
Looking at << $query = "SELECT getitem_std_amount( \"$supermarketID\",  \"ItemID[$j]\") AS AMOUNT";>>
and using the sample data you are testing with
$ItemID = array(1234,6545,5468,9856,"no stock",4561,5466);
$arrayElement[3] = "no stock";
$supermarketID = 21;

The first run will give SELECT getitem_std_amount( "21",  "1234") AS AMOUNT;
The second SELECT getitem_std_amount( "21",  "5454") AS AMOUNT;
and 5th one SELECT getitem_std_amount( "21",  "no stock") AS AMOUNT;

What is precisely the result of running $result = mysql_query($query) ?


0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Title # Comments Views Activity
Group By Function Required in PDF Output 21 65
MySQL - Adding an incremented value to a duplicate string 30 52
Undefined variable with $_POST in PHP 5 39
Joomla Editing 3 21
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

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