SQL循环输出问题

时间:2013-05-18 10:19:25

标签: php arrays loops

我遇到了一些问题,从下面的循环得到了所需的输出。我想要的输出是一个如下所示的数组。

    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
                            )

                    )

            )

    )

唯一的问题是它应该向我展示更多的价值而不是我正在获得的价值。

我应该看到:http://awesomescreenshot.com/0321af5cc4

感谢您的帮助。我向你保证,我已经尝试过以下这么多的变化而且无济于事。

// 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>';

有什么想法? 欢呼声。

1 个答案:

答案 0 :(得分:0)

问题似乎$row1[] = mysql_fetch_array($result8,MYSQL_ASSOC);只给你第一行。尝试:

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

另见:

<?
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>';