TYPO3 Extbase: combine multiple queries - php

I want to select only $limit records from the table with the category $cat.
foreach($cats as $cat) {
$query = $this->createQuery();
$query->matching($query->equals('type', $cat));
$query->setLimit((int)$limit);
$result[] = $query->execute();
}
Is it possible to combine the query result so one query result?

I think your query is something like ,
"SELECT * FROM table_name WHERE type IN ('".$cats."')"
Here $cats is an array of categories like,
$cats = array("cat1", "cat2", "cat3");
Then the extabse query will be,
$query = $this->createQuery();
$query->matching($query->in('type', $cats));
$query->setLimit((int)$limit);
$result = $query->execute();
In the above query in() checks if a single-valued property exists in a multi-value operand.
For more details refer : http://docs.typo3.org/typo3cms/ExtbaseFluidBook/6-Persistence/3-implement-individual-database-queries.html

Thank you, I found another way that worked for me:
public function findAllLimitedByCategory($limit, $category){
$cats = explode(",",$category);
$result = array();
foreach($cats as $cat) {
$query = $this->createQuery();
$query->matching($query->equals('type', $cat));
$query->setLimit((int)$limit);
$result[] = $query->execute()->toArray();
}
$out = array();
foreach($result as $r)
$out = array_merge($out, $r);
return $out;
}
...but I have to sort the array by php with this solution

Related

How to get all results from while loop,from function

So i have a problem getting all of the results from my while loop within a function.When i print the results of the function its just 1 result instead of 5-10 which i need.There is the code:
function display_post_edits($post_id,$post_name){
global $connect;
$result = array();
$rev = sanitize('revision');
$post_name = '$post_id-revision-v1'; //sanitize this soon!!
$query = mysqli_query($connect,"SELECT post_author,post_date FROM posts WHERE post_type ='$rev' AND post_name = '$post_id-revision-v1'");
while ($row = mysqli_fetch_assoc($query)){
$result[]= $row;
}
return $result;
}
//outside of function
$display_edits = display_post_edits(get_post_id($_POST['subject']),$_POST['subject']);
print_r($display_edits);
So i am trying to get all of the results from the while loop and attach it to $display_edits and when i need for example post date only i will use $display_edits['post_date'].
There are some of my tries so far:
foreach($result as $row){
return implode($result[0]);
}
Returns 1 results because $return is printing arrays like http://prntscr.com/gby0xu (array in array) so i need to define a index.Same thing with implode i need to define index.
while ($row = mysqli_fetch_assoc($query)){
$result = array(
'post_author' =>$row'post_author',
'post_date' =>$row'post_date'
);
}
Prints only 1 results again...
If you need anything more,or i missed something to give here please say what and i will add it !
Thanks in advance !
You foreach should be like this:
foreach($result as $row){
return implode($row[0]);
}
It should use $row and not $result

Why does Mysql query only give me the last result of a query instead of all results?

getting values
session_start();
$text = $_SESSION['text']; (it's an array with multiple values)
making the array suitable for the query
$text = array_filter($text);
$text = implode("','", $text);
$text = "'".$text."'";
run query
$sql = "SELECT id_category FROM eiofm_category_lang WHERE name IN (".$text.")";
$query = mysql_query($sql);
get query result
while($result = mysql_fetch_array($query))
{
var_dump($result);
}
EDIT
I got it working with a foreach loop but still don't know why the while loop didn't work.
This worked for me
foreach($text as $values){
$query = mysql_query("SELECT id_category FROM eiofm_category_lang WHERE link_rewrite LIKE '$values'");
while ($row = mysql_fetch_assoc($query))
{
array_push($array, $row['id_category']);
}}
But still don't know why the while loop didn't work

PhP (Codeigniter) Merging query results before foreach loop

I would like know if it is possible to merge sql queries like the following from codeigniters active record.
//get assigned contacts
$this->db->select('*, CONCAT(first_name, " " ,last_name) AS name', FALSE);
$this->db->from('customers_contacts');
$this->db->join('customers_accounts', 'customers_accounts.contact_id = customers_contacts.contact_id');
$this->db->like('CONCAT(first_name, " " ,last_name)', $q);
$results1 = $this->db->get();
//get non assigned contacts
$this->db->select('*, CONCAT(first_name, " " ,last_name) AS name', FALSE);
$this->db->from('customers_contacts');
$this->db->like('CONCAT(first_name, " " ,last_name)', $q);
$results2 = $this->db->get();
I tried using $query = array_merge($results1, $results2); but that does not work, I believe because ->get() is returning an array of objects.
So I got it to work by putting both through a foreach loop, and then merging the resulting arrays. But I need to do some conditionals that would be easier in one foreach loop than two.
You can use like below
$arr_results1 = $this->db->get()->result_array();
$arr_results2 = $this->db->get()->result_array();
var_dump(array_merge($arr_results1,$arr_results2));
Hope this will help you!
After a bit of reading I came up with the following and it works!
//first query here...
$results1 = $this->db->get()->result_array();
//second query here...
$results1 = $this->db->get()->result_array();
//then...
$query = array_merge($results1,$results2);
You must always do $results->result(); to get an array of rows after $results = $this->db->get();. note: $results->result(); is an array of objects.
For example
# ...
$results1 = $this->db->get();
# ...
$results2 = $this->db->get();
$results = array();
if ($results1->num_rows())
{
$results = array_merge($results, $results1->result());
}
if ($results2->num_rows())
{
$results = array_merge($results, $results2->result());
}
return $results;
This will return an array of objects (rows) and you iterate through data as usual:
foreach ($results as $row)
{
echo $row->id;
echo $row->column_name_1;
# and so on...
}

Codeigniter db select where as an array

I have this code i need to use to get results from a database, I'm on codeigniter. This is how the query looks like,
$this->db->select('*');
$this->db->where('broadcast_id', $bid);
$query = $this->db->get('ih_noticeboard');
$result = $query->result();
if($query->num_rows() < 1){
$data = "no feed ";
}else{
$data = $result;
}
the $bid in the where clause is got from this code,
$this->db->select('broadcast_id');
$this->db->where('broadcast_subscribers', $id);
$query = $this->db->get('ih_broadcastors ');
if($query->num_rows() < 1){
$data = "user not subscribed to any broadcasting";
}else{
$ress = $query->result();
foreach ($ress as $row){
$bid = $row->broadcast_id;
}`
Now the select uses only one $bid, how can I use the $bid as an array?
It's quite simple .
just use $this->db->where_in('broadcast_id', $bid);
instead if $this->db->where('broadcast_id', $bid);
https://ellislab.com/codeigniter/user-guide/database/active_record.html#select
Wouldn't it be better to have one query to get the feeds?
$this->db->select("ih_noticeboard.*");
$this->db->from("ih_noticeboard");
$this->db->join("ih_broadcastors", "ih_noticeboard.broadcast_id = ih_broadcasters.broadcast_id");
$this->db->where("ih_broadcastors.broadcast_subscribers", $id);
$result->db->get()->result();
Assuming you want to select in the table where
name='jane' and broadcast_id=2 #as an example
$bid=array(
'name'=>'jane',
'broadcast_id'=>'2'
)
$this->db->where($bid);
$this->db->from($table_name);
$query = $this->db->get()->result_array();
return $query;
Please notice that the $bid as array must be formated to suite the next database table to be queried. You can do this using foreach loop E.g
foreach ($ress as $row){
$bid = array(
'name'=>$row['name'],
'broadcast_id'=>$row['id']
);
}

How do I do a query for each item in an array in wordpress?

I'm a bit stuck.
I have done a query in wordpress to get a list of taxonomy ids and they have been put into an array:
which outputs an array of ids:
array
(123,
633,
992);
global $wpdb;
$results = $wpdb->get_results("SELECT meta_value FROM `usermeta` WHERE `meta_key` LIKE 'alertids'");
if(!empty($results)) {
echo $wpdb->get_results("SELECT name FROM `terms` WHERE `term_id` =$results");
}else
{
echo "<p>We couldn't find anything !</p>";
}
How do I do a query for every item in the array $results on the terms table.
I tried a foreach loop but ended up confusing myself.
Thanks!
Something like this should get you started
<?php
$myarray = array(123,633,992);
global $wpdb;
$return = array();
foreach ($myarray as $item){
$results = $wpdb->get_results("SELECT meta_value FROM 'usermeta' WHERE 'meta_key' LIKE '".$item."'");
if(!empty($results)) {
$return[] = $wpdb->get_results("SELECT name FROM `terms` WHERE `term_id` = '".$results."'");
}else{
return false;
}
}
if($return){
return $return;
} else {
return false;
}
?>
I found a much better solution was to do a join in the database and just get the values from the join.I don't know if this is the best way of doing it but it works well for me.
global $wpdb;
$results = $wpdb->get_results("SELECT usermeta.umeta_id, usermeta.user_id, usermeta.meta_key, usermeta.meta_value, terms.term_id, terms.name
FROM usermeta
JOIN terms ON terms.term_id = usermeta.meta_value
WHERE usermeta.meta_key = 'alert_ids'
AND usermeta.user_id =$user_ID");
foreach($results as $row)
{
echo name;
}

Categories