Page 1 of 1

Mystery array

Posted: Thu Sep 30, 2010 2:47 pm
by kenoli
In the following code, $admin->editPage($id) returns an array from an
object method with column data from a row in a table.
Here's the method that returns the array:

Code: Select all

public function editPage ($id) { 
        global $db; 
        $query = "SELECT * FROM web_content WHERE id_content = '$id'"; 
        $result = mysql_query($query, $db->dbc) or die('Failed to retrieve 
page to edit.'); 
        while ($row=mysql_fetch_array($result)) { 
                $edit_values[] = $row; 
        } 
        return $edit_values; 
} 
The mystery is that, in the following code, the print_r statement
displays the data, including data for ['page_name'], indicating that
the array is being passed correctly.
However, $stuff['page_name'] returns nothing.

$

Code: Select all

stuff = $admin->editPage($id); 
echo "Page_name = " . $stuff['page_name'] . "<br/><br/>";  // returns: 
Page_name = 
echo '<pre>'; 
print_r ($stuff); // Returns the array with all of its values 
including page_name 
echo '</pre>'; 
It is not just the page_name element. No other values are returned
except in the print_r statement.
What's going on. I've racked my brain.
What stupid thing am I overlooking?
--Kenoli

Re: Mystery array

Posted: Thu Sep 30, 2010 2:55 pm
by John Cartwright
Can you post the print_r()'s in both instances -- where it is "failing" and where it is "working"?

A quick look at your code is you are building a numerically indiced array, but you are trying to access it as a named key.

You probably need to loop the results to get at the innard array, and thus it's elements.

Try:

Code: Select all

$stuffs = $admin->editPage($id);

foreach ($stuffs as $stuff) {
   echo "Page_name = " . $stuff['page_name'] . "<br/><br/>";  // returns: Page_name =
   echo '<pre>';
   print_r ($stuff); // Returns the array with all of its values including page_name
   echo '</pre>';
}

Re: Mystery array

Posted: Thu Sep 30, 2010 4:34 pm
by kenoli
Well, I solved the problem with help from another forum. Somehow, I was generating a multidimensional array with out noticing it.

Thanks,

--Kenoli

Re: Mystery array

Posted: Thu Sep 30, 2010 4:41 pm
by John Cartwright
kenoli wrote:Well, I solved the problem with help from another forum. Somehow, I was generating a multidimensional array with out noticing it.

Thanks,

--Kenoli
Which was exactly as I hinted, but alas, glad it's fixed :P