3

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($where!='') {
        $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.

2 Answers 2

11

Replace:

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

with:

foreach ($arr_GalleryInfo as $arrGallery)         
{
  etc...
Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.