开发者

How to iterate over an array of arrays

开发者 https://www.devze.com 2023-03-28 21:50 出处:网络
I\'m hoping someone can help. I\'m sure its just a simple one that I just can\'t work out for some reason.

I'm hoping someone can help.

I'm sure its just a simple one that I just can't work out for some reason.

Basically I have up a class that handles all my database functions (connect, select, insert, update).

In the select function I am returning an array.

public function getAll($table, $cols, $where, $limit, $order) {
    // Set the query variables
    if($cols == '') {
        $cols = '*';
    }
    if($开发者_如何学Gowhere!='') {
        $where = ' WHERE '.$where;
    }
    if($limit!= '') {
        $limit = ' LIMIT '.$limit;
    }
    if($order!='') {
        $order = ' ORDER BY '.$order;
    }

    // Make the query string 
    $sql = 'SELECT '.$cols.' FROM '.$table.$where.$order.$limit;

    //echo $sql;

    // Set the query
    $news_qry = mysql_query($sql);

    // Set the array 
    $rows = array();

    // Run a loop through the results
    while($item = mysql_fetch_object($news_qry)) 
    {
        // Add each row to an array.
        $rows[] = $item;
    }
    return $rows;       
}   

This function is working as I can print an array. See below:

Array ( [Gallery_id] => 1 [Gallery_Name] => Test [Gallery_FolderName] => Test Folder )

But when I go to use the object -

$arr_GalleryInfo = $dataObj->getAll('tbl_Gallery', '', '', '', '');

Within the for each loop (see below) I only get the first letter of the result from the database.

 <?php
        foreach ($arr_GalleryInfo[0] as $arrGallery) 
        {
    ?>
            <tr>
                <td>
                     <?php echo $arrGallery['Gallery_Name']; ?>          
                </td>

                <td>
                    <?php echo $arrGallery; ?>   
                </td>

                <td>
                    <?php echo $arrGallery; ?>    
                </td>
            </tr>
    <?php
        }
    ?>

Any help would be great.

Thanks.


Replace:

foreach ($arr_GalleryInfo[0] as $arrGallery) 
{
  etc...

with:

foreach ($arr_GalleryInfo as $arrGallery)         
{
  etc...


Well, your big issue is that you're trying to iterate over the 0-index of an array.

foreach ($arr_GalleryInfo[0] as $arrGallery) // get rid of the `[0]`.

That will make it so that you actually get some legit iteraction, but there are some other things which are gotchas that you're about to hit.

 // this will output `Array`. You want $artGallery['Gallery_FolderName']
 // or $artGallery['Gallery_id']
 echo $arrGallery; 

Of course, you could avoid that whole second issue with a nested loop:

foreach ($arr_GalleryInfo as $arrGallery) {
   echo '<tr>';
   foreach($arrGallery as $val ) echo "<td>$val</td>";
   echo '</tr>';
}

If $news_qry = mysql_query($sql); fails, you'll have nothing to warn you if something breaks. You should make it: $news_qry = mysql_query($sql) or die(mysql_error());

And, of course, you should use mysql_real_escape_string on all of your db inputs.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号