0

I am having some issues gettig the desired output from the below loops. My desired output is an array that looks like the following.

    Array
    (
        [0] => Array
            (
                [0] => 10013
                [1] => 8
                [2] => 2
                [3] => 6
                [4] => Array
                    (
                        [0] => Array
                            (
                                [0] => Jerk Chicken
                                [1] => Drink
                                [2] => 8
                                [3] => 2
                                [4] => 6
                            )

                    )

            )

        [1] => Array
            (
                [0] => 10107
                [1] => 28
                [2] => 28
                [3] => 0
                [4] => Array
                    (
                        [0] => Array
                            (
                                [0] => Konig Pilsener
                                [1] => Tobacco
                                [2] => 3.5
                                [3] => 3.5
                                [4] => 0
                            )

                    )

            )

        [2] => Array
            (
                [0] => 10259
                [1] => 18
                [2] => 18
                [3] => 0
                [4] => Array
                    (
                        [0] => Array
                            (
                                [0] => Trailer Grog
                                [1] => Tobacco
                                [2] => 7
                                [3] => 7
                                [4] => 0
                            )

                    )

            )

        [3] => Array
            (
                [0] => 10375
                [1] => 8
                [2] => 2
                [3] => 6
                [4] => Array
                    (
                        [0] => Array
                            (
                                [0] => Steak Sandwhich
                                [1] => Drink
                                [2] => 8
                                [3] => 2
                                [4] => 6
                            )

                    )

            )

    )

The only problem is it should be showing me more values not those above that I am getting.

I should see this: http://awesomescreenshot.com/0321af5cc4

Thanks for any help. I assure you I have tried so many variations of below and yet to no avail.

// all passing through correctly
global $actual; 
global $potentialarray;
global $compsarray;
global $CheckName;
}

unset($temparray);
unset($temparray2);

$i=0;
foreach ($CheckName as $value) {
$v=0;

$query8 = "SELECT * FROM Comps_Item_Name WHERE DOB="."'". $from_date."'"." AND CheckID=" . $value ." ORDER BY CheckID AND ITMNAME";
$result8 = mysql_query($query8) or die(mysql_error());
//$var= mysql_num_rows($result8);

    $row1[] = mysql_fetch_array($result8,MYSQL_ASSOC);


    //print_r($row1);
    //echo'<br>';
foreach ($row1 as $itemvalue){

    //Item Name
    $itemname = $itemvalue['ITMNAME'];
    $catname = $itemvalue['CatName'];   
    $saleprice = $itemvalue['SalePrice'];
    $discount1 = $itemvalue['Discount'];
    //Actual retail value of check
    $actual1= $itemvalue['SalePrice'] - $itemvalue['Discount']; 

    $temparray[$i][$v] = array($itemname, $catname, $saleprice, $discount1, $actual1);
    $v++;

}
echo '<pre>';
        print_r($temparray[$i]);
        echo '</pre>';


$temparray2[$i] = array($CheckName[$i], $potentialarray[$i], $compsarray[$i], $actual[$i], $temparray[$i]);
$i++;
unset($row1);
}

//  $DesiredOutput = array($CheckName, $potential[$i], $comps[$i], $actual[$i], array($itemname[$i][$v], $catnam[$i][$v], $saleprice[$i][$v], $discount1[$i][$v], $actual1[$i][$v]));
unset($itemname);
unset($catname);
unset($saleprice);
unset($discount1);
unset($actual1);

        echo '<pre>';
        print_r($temparray2);
        echo '</pre>';

Any thoughts?? Cheers.

8
  • Note: unset accepts a variable amount of arguments. You're overusing it though. Commented May 18, 2013 at 10:22
  • Your question is close (not the same) to this one: stackoverflow.com/questions/12771708/… Commented May 18, 2013 at 10:23
  • Shouldn't the $i declaration be in one of the loops? Commented May 18, 2013 at 10:24
  • You could use 1 query rather than putting it in a loop and doing multiple queries, but using IN Commented May 18, 2013 at 10:24
  • I used unset() to insure that variables were reset/zeroed as I was getting larger lists that seemed to replicate. Do I not need them? Commented May 18, 2013 at 10:25

1 Answer 1

0

The problem seems $row1[] = mysql_fetch_array($result8,MYSQL_ASSOC); only gives you the first row. Try:

 $row1 = array();
 while($row = mysql_fetch_array($result8,MYSQL_ASSOC) ) {$row1[]=$row;}

See also:

<?
error_reporting(E_ALL);
ini_set('error_reporting','on');
$CheckName=array('test1','test2');

for($i=0; $i<count($CheckName); $i++) 
{


/*$query8 = "SELECT * FROM Comps_Item_Name WHERE DOB="."'". $from_date."'"." AND CheckID=" . $value ." ORDER BY CheckID AND ITMNAME";
$result8 = mysql_query($query8) or die(mysql_error());
//$var= mysql_num_rows($result8);

    $row1[] = mysql_fetch_array($result8,MYSQL_ASSOC);*/

$row1 = array();  
$row1[] = array('ITMNAME'=>'cola','CatName'=>'drink'); 
$row1[] = array('ITMNAME'=>'beer','CatName'=>'drink');
$v=0;
foreach ($row1 as $itemvalue){

    //Item Name
    $itemname = $itemvalue['ITMNAME'];
    $catname = $itemvalue['CatName'];   


    $temparray[$i][$v] = array($itemname, $catname);
    $v++;

}
$temparray2[$i] = array($CheckName[$i], $temparray[$i]);

}


echo '<pre>';
print_r($temparray2);
echo '</pre>';
Sign up to request clarification or add additional context in comments.

1 Comment

Bass = Genius! Thanks so very much.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.