I'm attempting to loop through an array and use the rows(values) as the conditions for my SQL query but I can't seem to get it loop through the rows. It only outputs the data for the first row where the form_name = ' V243823' then stops. i need all the rows, so 3 arrays total to be returned.
Campus Forms Array
[0] => Array
(
[PQ_Lookup] => V243823
[RL_Lookup] => B3823RL
[MA_Lookup] => F356823
)
Query
foreach( $campus_forms[0] as $key => $row )
{
$this->db->select('form_deadline,form_url,form_fullname');
$this->db->from('form_deadlines');
$this->db->where('form_name', $row);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$campus_forms = $query->result_array();
return $campus_forms;
}
}
In first loop you are returning for this reason it doesn't execute next loop.Please return your data after foreach loop.You can hold your data in an array in every loop.You can do it in this way:
$form_data_array = array();
foreach( $campus_forms[0] as $key => $row )
{
$this->db->select('form_deadline,form_url,form_fullname');
$this->db->from('form_deadlines');
$this->db->where('form_name', $row);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$campus_forms = $query->result_array();
// return $campus_forms;
$form_data_array[] = $campus_forms;
}
}
return $form_data_array;
Related
I have a array that contains user ids and i want to get each user's data and put all user ids and datas in different arrays like this :
array 1 : ['user 1' => ['user data'] ]
array 2 : ['user 2' => ['user data'] ]
I already wrote this code but gives me wrong data
foreach ($users->userids as $user){
$commentusers=[];
$nodeusers=[];
$postQuery = InstaPost::select('post.id','userid')->where('userid',$user)->get();
if (count($postQuery) > 0 ){
foreach ($postQuery as $item){
$postArr[] = $item->post['id'];
}
foreach ($postArr as $key => $value){
$commentquery = comment::select('comment.owner.id','comment.owner.username','postid')->where('postid',$value)->get()->unique('comment.owner.id')->toArray();
if (count($commentquery) > 0){
foreach ($commentquery as $eachpost){
$commentusers[] = $eachpost['comment']['owner']['username'];
}
}
$thread_comment = DB::table('Comments')->select('comment.edge_threaded_comments.edges.node.owner.username','comment.edge_threaded_comments.edges.node.owner.id','postid')->where('postid','=',$value)->where('comment.edge_threaded_comments.count','>',0 )->get()->unique('comment.edge_threaded_comments.edges.node.owner.id')->toArray();
foreach ($thread_comment as $eachthread){
$edges = $eachthread['comment']['edge_threaded_comments']['edges'];
foreach ($edges as $edge){
$nodeusers[] = $edge['node']['owner']['username'];
}
}
}
}
$result = array_merge($nodeusers,$commentusers);
$result = array_values(array_unique($result));
$merge[$user] = $result;
}
as you can see in picture first array is a correct result but second array duplicates first array with it's own data
this is a result when i'm just getting 1 user's data and everything is correct.
This code is in model...
i want to return these two values in controller.....please help me if you know
Array ( [disease_name] => magraines ) Array ( [disease_name] => brain cancer )
when i do
return print_r($qq);
then i get one value but i need two value that one value is
Array ( [disease_name] => magraines ) 1
function getDiseaseInfo()
{
$spytoms = $_GET['syptoms'];
foreach ($spytoms as $ss)
{
$query = $this->db->query("SELECT d.disease_name FROM diseases d,syptoms s ,syptoms_disease sd
WHERE
'$ss' = s.syptom_name AND
s.s_id = sd.s_id_fk
AND sd.d_id_fk IN (d.d_id)
");
$qq = $query->row_array();
print_r($qq);
}
}
From your model return $qq only.
you are returning return print_r($qq) thats not the correct way.
print_r will print the entire array.
if you want to return your array values to your controller you have to return it like return $qq;
Update 1
I think you are getting last row's values,if im right you have to follow below steps,
You have to introduce a new array variable above your foreach
and assign your query array values to this newly created array
and you have to return that newly created array
function getDiseaseInfo()
{
$spytoms = $_GET['syptoms'];
$tmpArray = array();
foreach ($spytoms as $ss)
{
$query = $this->db->query("SELECT d.disease_name FROM diseases d,syptoms s ,syptoms_disease sd WHERE '$ss' = s.syptom_name AND s.s_id = sd.s_id_fk AND sd.d_id_fk IN (d.d_id) ");
$qq = $query->row_array();
$tmpArray[] = $qq;
}
return $tmpArray;
}
Have a PHP factory with a sub class that queries either one or more unrelated tables in a database. The class takes a table name as a parameter and returns an array object.
When more than one table is required, I would like a way to delimit each tables result set in to its own array. The class would then return a multi-dim array.
I would prefer to not instantiate another instance of the factory. Here is the current query/result code block. I've left out all the other non-essential code for brevity
// if array has more than one table to query,
// run queries on each table
$count = count($tname);
if ($count>1) {
foreach ($tname as $value) { //each table name in array
/* $query = "SELECT s.* FROM $value s"; tried with table alias */
$query = "SELECT * FROM $value";
if ($stmt = self::$_conn->prepare($query)) {
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$result[] = $row;
}
} else {
return false;
}
}
return $result;
// else if only one table to query
} else {
$string = $tname; //table name
$query = "SELECT * FROM $string";
if ($stmt = self::$_conn->prepare($query)) {
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$result[] = $row;
}
return $result;
} else {
return false;
}
}
With more than one table, would return something like:
Array
(
[0] => Array
(
[team_id] => 3
[team_name] => Maverics
)
[1] => Array
(
[team_id] => 4
[team_name] => Stallions
)
[3] => Array
(
[fld_id] => 1
[fld_name] => 6v6-1
)
[4] => Array
(
[fld_id] => 2
[fld_name] => 8v8-2
)
)
Where 0,1 are from one table and 3,4 are from another.
Thank you in advance
Figured it out... Simple actually, just didn't see it before I asked the questions. I changed:
$result[] = $row;
to
$result[$value][] = $row; //line 11 in code block example
This now returns the array with each table name as the array name for the result set.
Thanks anyway,
I've been working on a OOP method that is supposed to return the rows of a MySQL query. I have to have the data array in the format:
$rows = array('row'=> rownum, 'fld1'=> fldval .... 'fldn' => fldval);
What I have encountered are the two problems of either:
returns
$rows = array('0'=>fldval, 'fld1'=> fldval .... 'n'=> fldval, 'fldn' => fldval);
or single row of
$rows = array('fld1'=> fldval .... 'fldn' => fldval);
Little frustrated as every PHP mysql function I have tried has some sort to goofy crap flaw and will not do a straight out process.
I assume there is a good example somewhere, that can get me past the crap limitations, but haven't found anything useful yet!
I've tried all of the following:
$row = mysql_result($db_res,$n);
$row = mysql_fetch_array($db_res);
$row = mysql_fetch_assoc($db_res);
$row = mysql_fetch_object($db_res);
$row = mysql_fetch_row($db_res);
None have worked successfully! For getting out the bogus "numeric" array entries. I wrote:
foreach ($row as $k => $v)
if (is_numeric($k)) { continue; }
$result[$k] = $v;
} // end foreach $row
$row = array_push($row, 'row'=>$rownum, $result);
Hoping someone has a link.
$list = array();
$query = "SELECT value FROM table";
$resource = mysql_query($query);
while($row = mysql_fetch_assoc($resource))
{
$list['fld' . (1 + count($list))] = $row['value'];
}
$list = array('row' => count($list)) + $list;
if table have 3 row, the code above is going to give you a array like:
array(
'row' => 3,
'fld1' => 12,
'fld2' => 34,
'fld3' => 56
);
I have got data from mysql_fetch_assoc stored in to an array using this command
if(mysql_num_rows($data) > 1){
while($row = mysql_fetch_assoc($data)){
$ndata[] = $row;
}
} else {
$ndata = mysql_fetch_assoc($data);
}
Now when I use count on $ndata, it retuns 1; although it is empty.
When I run mysql_num_rows on the returned data it retuns 0 rows. But when I convert the data to $ndata and then run count on that it returns 1. I want it to return the number of rows.
Thanks
Can someone please explain why is there a problem and how to fix it?
Your logic is wrong: you don't test for the case that mysql_num_rows($data) == 0. If this is the case, your code executes the same path as when the number of rows is 1 ($ndata = mysql_fetch_assoc($data);). But there are no more rows to return (there are no rows at all), so mysql_fetch_assoc returns false. And count(false) returns 1, because that's how count works.
Do it this way:
$rows = mysql_num_rows($data);
if($rows == 0) {
return null;
}
else if($rows == 1){
$ndata = mysql_fetch_assoc($data);
} else {
while($row = mysql_fetch_assoc($data)){
$ndata[] = $row;
}
}
You can either return null or array() in the first if branch; these are the only two values for which count returns 0.
I think mysql_fetch_assoc() is returning false (see doc) and count(false) returns 1.
$data = mysql_query($query);
while($row = mysql_fetch_assoc($data)){
$ndata[] = $row;
}
if(count($ndata) == 1) $ndata = $ndata[0];
Runing query that returns 1 row and
$data = mysql_query("SELECT * FROM online LIMIT 1");
print_r($ndata);
Result
Array ( [timestamp] => 1301826108 [ip] => 213.186.122.2)
Runing query with multiple rows...
$data = mysql_query("SELECT * FROM online");
print_r($ndata);
Result
Array
(
[0] => Array
(
[timestamp] => 1301848228
[ip] => 67.195.112.29
)
[1] => Array
(
[timestamp] => 1301826108
[ip] => 213.186.122.2
)
[2] => Array
(
[timestamp] => 1301825465
[ip] => 77.88.28.246
)
[3] => Array
(
[timestamp] => 1301763579
[ip] => 69.171.224.251
)
)
It seems I finally got what you need (However, question remains extremely vague and it's hard to tell for sure). But anyway.
$ndata = array();
while($row = mysql_fetch_assoc($data)){
$ndata[] = $row;
}
Now you would get desired result from count($ndata). It will return 0 for no rows, 1 for 1 row and whatever number if more rows returned. Bingo!
But if you're still planning to get different kinds of arrays, I should warn you against doing it this way.
Always state explicitly what kind of result you want to get:
$ndata = array();
if ($type=="row"){
$ndata = mysql_fetch_assoc($data);
} elseif ($type=="array"){
while($row = mysql_fetch_assoc($data)){
$ndata[] = $row;
}
}