Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OOP PHP Array Values

Status
Not open for further replies.

scripter50

IS-IT--Management
Dec 9, 2002
35
US
I'm have trouble getting values from an array returned from a php class. Code example:

<?php

require_once("include/NameDetails.php");

class TestQueries {

var $CE_Config_DB = NULL;

function getNameInfo($searchType, $searchValue) {
$resultArray = array();

$query = "SELECT * ";
$query .= "FROM table ";

if ($result = $this->CE_Config_DB->query($query)) {
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$nameDetails = new NameDetails();
$nameDetails->firstName = $row['firsName'];
$nameDetails->lastName = $row['lastName'];
$nameDetails->age = $row['age'];
$resultArray[] = $nameDetails;
}
}
}
return $resultArray;
}

}

?>

# call to the class:

<?php

include("include/TestQueries.php");

$testQueries = new TestQueries();
$nameData = $bvoipQueries->getNameInfo("name", "Fred");

# the array that is returned looks like this:

Array
(
[0] => NameDetails Object
(
[firstName] => Frank
[lastName] => Smith
[age] => 62
)
)

My issue is how to get the values out of the array??? It's the object that is throwing me?

If I try:

if(is_array($nameData)) {
foreach($nameData as $key=>$value) {
if($key == "firstName") {
$firstName = $value;
echo $firstName;
}
}
}

The error I get is:
"Catchable fatal error: Object of class NameDetails could not be converted to string in .....
 
I can't see how your class works at all as you are setting the property $CE_Config_DB to null.

and I don't see where the results come from as you have told us nothing of the property $bvoipQueries.

but assuming you have some way of successfully retrieving the values the way you iterate the result array is like so:

Code:
$fields = array('firstName', 'lastName', 'age');
if(is_array($nameData)) :
   foreach($nameData as $key=>$value):
       foreach($fields as $field):
           echo "$field = " . $value->$field . "<br/>";
       endforeach;
   endforeach;
endif;

(nb. not tested)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top