Return list of object from a Class
-
Hi all, I am new to PHP development after a reasonable experience with the .NET. I have just created a class to return list of categories from the category table and display on my home page. Below is my code snippets:
link = mysqli_connect($this->host, $this->username, $this->password);
if (!$this->link) {
// todo: display error
}
if (!mysqli_set_charset($this->link, 'utf8')) {
// todo: display error
}
if (!mysqli_select_db($this->link, $this->dbName)) {
// todo: display error
}
}public function getAllCategories() { $sql = mysqli\_query($this->link, "CALL getCategories()"); while ($row = mysqli\_fetch\_array($sql)){ $row\['description'\] . '
';
}
return $row;
}
public function executeQuery ($query) {
if (!$this->query($query)) {
// todo: display error
}
}
}?>
on my home.php page, I have:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><script type="text/javascript" src="script/ref/jquery-1.6.2.min.js" /></script> <title></title> </head> <body>
-
Hi all, I am new to PHP development after a reasonable experience with the .NET. I have just created a class to return list of categories from the category table and display on my home page. Below is my code snippets:
link = mysqli_connect($this->host, $this->username, $this->password);
if (!$this->link) {
// todo: display error
}
if (!mysqli_set_charset($this->link, 'utf8')) {
// todo: display error
}
if (!mysqli_select_db($this->link, $this->dbName)) {
// todo: display error
}
}public function getAllCategories() { $sql = mysqli\_query($this->link, "CALL getCategories()"); while ($row = mysqli\_fetch\_array($sql)){ $row\['description'\] . '
';
}
return $row;
}
public function executeQuery ($query) {
if (!$this->query($query)) {
// todo: display error
}
}
}?>
on my home.php page, I have:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><script type="text/javascript" src="script/ref/jquery-1.6.2.min.js" /></script> <title></title> </head> <body>
Your
getAllCategories
function will return NULL, because it is returning the last value returned bymysqli_fetch_array($sql)
. You can return an array of results this way:$results = array();
while($row = mysqli_fetch_array($sql)){
$results[] = $row['description'];
}
return $results;or a string:
$results = '';
while($row = mysqli_fetch_array($sql)){
$results .= $row['description'] . '<br />';
}
return $results;You also have
$cat[] = $con->getAllCategories();
- this will add whatever the function returns as a new entry in the $cat array, which probably is not what you want. -
Your
getAllCategories
function will return NULL, because it is returning the last value returned bymysqli_fetch_array($sql)
. You can return an array of results this way:$results = array();
while($row = mysqli_fetch_array($sql)){
$results[] = $row['description'];
}
return $results;or a string:
$results = '';
while($row = mysqli_fetch_array($sql)){
$results .= $row['description'] . '<br />';
}
return $results;You also have
$cat[] = $con->getAllCategories();
- this will add whatever the function returns as a new entry in the $cat array, which probably is not what you want.Thank Graham; Your response was correct. So on my home.php page, this is what I finaly did:
$con = new Connection();
$cat = $con->getAllCategories();
foreach ($cat as $c) {
echo $c['description'] . '
';
}But the result of the above was the first character of each record per row. How do I fix this?
-
Thank Graham; Your response was correct. So on my home.php page, this is what I finaly did:
$con = new Connection();
$cat = $con->getAllCategories();
foreach ($cat as $c) {
echo $c['description'] . '
';
}But the result of the above was the first character of each record per row. How do I fix this?
This is a bit confusing, but I've worked it out. You've already pulled the "description" field out of the row in the getAllCategories function - so now
echo $c['description'] ...
is accessing the string$c
, and not an associative array. The square brackets are referring to the characters of the string, so 'description' is being evaluated as 0 and you are getting the first character in the string. Try using this instead:echo $c . '<br />';
-
This is a bit confusing, but I've worked it out. You've already pulled the "description" field out of the row in the getAllCategories function - so now
echo $c['description'] ...
is accessing the string$c
, and not an associative array. The square brackets are referring to the characters of the string, so 'description' is being evaluated as 0 and you are getting the first character in the string. Try using this instead:echo $c . '<br />';
Thanks. I am very glad for your response. Now, I am getting it right. Please don't be annoyed. Thanks once again.