I have the following function which fetches some data from a MySQL table:
function readQuestion ($quizType, $questionId) {
$data = array();
$query = $this->dbConnection->query("SELECT * FROM $quizType WHERE id = $questionId");
foreach ($query as $row) {
var_dump($row);
};
echo $data
}
How can I push all the returned data into an array, where each member is indexed by a number?
Do I need to use echo or return at the end? They seem to have the same effect.
EDIT: Is this the correct way of returning results of the query? I am passing it to the front-end.
$questionData = $controller->readQuestion($quizType, $questionId);
return $questionData;
In general your function must looks like:
function readQuestion ($quizType, $questionId) {
$data = array();
$query = $this->dbConnection->query("SELECT * FROM $quizType WHERE id = $questionId");
return $query;
}
But, you have to look what is inside $query variable. If it is array - just return this array, if not - maybe it is some iterator, hence you have to check it and try to find method like toArray or something like that... Otherwise, you have to do something like:
$data = [];
foreach ($query as $row) {
$data[] = $row;
};
return $data;
Now you can use this function like:
var_dump(readQuestion($quizType, $questionId));
Take a look up here on how to secure your data.
Anyway, as I said in the comment you cannot use echo on an Array. And you can't do anything with the output of var_dump.
Change var_dump($row); for $data[] = $row;
and change echo $data; for return $data;
function readQuestion ($quizType, $questionId) {
$data = array();
$query = $this->dbConnection->query("SELECT * FROM $quizType WHERE id = $questionId");
foreach ($query as $row) {
$data=$row;
};
return $result;
}
Related
I'm having troubles with my function and how to retrieve the data. Here is what i got.
public function getImages()
{
$array = array();
//Test query
$query = $this->connect()->query("SELECT * from user");
while ($row = $query->fetch()) {
$array[] = $row;
return $array;
}
}
Now I'm calling this function but i can not use foreach to access the array.
include "header.html";
include "autoload.php";
spl_autoload_register('my_autoloader');
$users = new User;
$users->getImages();
foreach ($users as $value) {
echo $value["username"];
}
What am I doing wrong? I'm only getting one result but there are many in my database. Or if i call the $array in foreach it says undefined.
A couple things. First, your function is only ever returning an array with one element in it. If you want to finish populating the array, don't return until after the loop:
while ($row = $query->fetch()) {
$array[] = $row;
}
return $array;
And second, you're trying to iterate over the object which has the function, not the value returned from the function. Get the return value and iterate over that:
$userDAO = new User;
$users = $userDAO->getImages();
foreach ($users as $value) {
echo $value["username"];
}
You just need to put the return statement out of the while loop.
public function getImages() {
$array = array(); //Test query
$query = $this->connect()->query("SELECT * from user");
while ($row = $query->fetch()) {
$array[] = $row;
}
return $array;
}
Good I'm starting with php and codeigniter, I have the following problem I'm running an array from my model, which is the result of a query, how can I read one by one my records in the controller?
function getCustomers() {
$sql = "SELECT * FROM customers";
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$i = 0;
foreach($query->result() as $row) {
$img[$i]['id'] = $row->id;
$img[$i]['name'] = $row->name;
$img[$i]['Location'] = $row->Location;
$img[$i]['telephone'] = $row->telephone;
$i++;
}
return $img;
}
}
You can return array to the controller by loading the model into the controller like this.
$this->load->model('yourmodel');
$array = $this->yourmodel->yourmodelsmethod();
First you have to return this array to your controller like
$this->load->model('yourmodel');
$array['test'] = $this->yourmodel->yourmodelsmethod();
foreach($test as $key=>$value)
{
echo $value;
}
By doing this you can easily print your data from model in controller
Since the servers version is older than 5.3.0, I need to rewrite the following piece of function to do the same as it does now:
else {
$res = mysqli_query($_con, "SELECT * FROM house");
$row = mysqli_fetch_all($res, MYSQLI_ASSOC);
return $row;
}
In html I call it like this:
$results = getResults();
foreach ($results as $value) {
echo $value['Title']." / "; echo $value['Version'];
}
How can I call the results in my html the same way but with different function?
EDIT: I want to get all of the results from table "house" but without the use of function mysqli_fetch_all()
It's just a simple loop that calls mysql_fetch_array() and collects all the rows in an array.
function mysqli_fetch_all($res, $mode) {
$array = array();
while ($row = mysql_fetch_array($res, $mode)) {
$array[] = $row;
}
return $array;
}
I am doing my project in codeigniter. My issues is i will store value for 'game_aspect_details' in json format like
"{"game_aspect_details":[{"aspect_id":"1"},{"aspect_id":"4"}]}"
for this select query i will decode the json format and check that value in foreach.
$this->db->select('game_aspect_details');
$this->db->from('share_reviews');
$this->db->where('review_id',6);
$query = $this->db->get();
$result = $query->result();
$test = $result[0]->game_aspect_details;
$res = json_decode($test);
$result_array = array();
foreach ($res as $row)
{
$this->db->select('comments');
$this->db->from('review_ratings');
$this->db->where('game_aspect_id',$row->game_aspect_details); //here i need
$query1 = $this->db->get(); to check 1 and 4
$resultReviews['comments'] = $query1->result();
$result_array[] = $resultReviews;
}
print_r($res);
exit;
First this do see what you are getting in the $row within the foreach by using the print_r().
And I hope that you need to replace the below line
$this->db->where('game_aspect_id',$row->game_aspect_details);
With following line:
$this->db->where('game_aspect_id',$row['aspect_id']);
as $row is an array not an object.
EDITED:
foreach ($res as $rows)
{
foreach ($rows as $row)
{
........
$this->db->where('game_aspect_id',$row['aspect_id']); //here make change
.....
}
}
You are doing some things wrong, see the comments in this code:
...
//$test = $result[0]->game_aspect_details; // This does not work since $result is not decoded yet
$res = json_decode($result); // Changed to `$result` instead of `$test`
$res = $res->game_aspect_details; // Instead pick the `game_aspect_details` here, after the decode
$result_array = array();
foreach ($res as $row)
{
$this->db->select('comments');
$this->db->from('review_ratings');
$this->db->where('game_aspect_id',$row['aspect_id']); // Changed to `aspect_id`
$query1 = $this->db->get();
$resultReviews['comments'] = $query1->result();
$result_array[] = $resultReviews;
}
...
i searched all over the web or i just don't see it.
I want to do the following:
<?php
function abc(){
$sql = "SELECT * FROM table";
$result = mysql_fetch_assoc($sql);
$data = mysql_fetch_assoc($result);
}
?>
<? while(abc()) : ?>
<?=article_title()?>
<?=article_content()?>
<?=endwhile;?>
Could somebody give me a direction and/or example?
Thank you very much
PHP does not have generator/iterator functions, so you cannot do that.
You can however return an array and iterate over that array:
function abc() {
// ...
$rows = array();
while($row = mysql_fetch_assoc($result)) { $rows[] = $row; }
return $rows;
}
foreach(abc() as $row) {
// do something
}
If the functions you call need access to the row, pass it as an argument. Using global variables for it would be very bad/nasty
have you tried to do something like that?
<?php
function getData(){
//get the data from the database
//return an array where each component is an element found
}
$elems = getData();
foreach($elems as $e){
doSomethingWith($e);
}