ok I have to tables they are along the same lines, but one that lists all stores that sell goods and one that is products that we sell.
Think of it like Fruit and Veg totally different.
What I need to work out is if there is 7 fruit and we need 8 listings then go and get a random veg and show it in the same results.
Here is what our query currently looks like. you will notice we can send a $count which we send as 8 but we may want to increase to 10 or even make it 4.
public function realcashoffers($state,$count)
{
$this->state = $state;
$this->number = $count;
//print count($this->JSONselect("business_stores","*",NULL,NULL),1);
print $this->JSONselect("approved_business, business_stores, Real_Cash_Offers"," *, group_concat(offer ORDER BY offer ASC SEPARATOR ',') as offers"," approved_business.id = business_stores.business_id AND Real_Cash_Offers.business_id = approved_business.id AND Real_Cash_Offers.storeid = business_stores.storeid AND business_stores.state = '{$this->state}'","GROUP BY id ORDER BY RAND(), approved_business.id DESC LIMIT {$this->number} ");
}
this->JSONselect goes to
//JSON select
public function JSONselect($table,$options,$where,$orderby)
{
$options = empty($options) ? "*" : $options;
$where = empty($where) ? "1=1" : $where;
$orderby = empty($orderby) ? "" : $orderby;
$qry = "SELECT $options FROM $table WHERE $where $orderby ";
//print $qry;
$result = mysql_query($qry) or die(json_encode(array("error",mysql_error())));
while(($row = mysql_fetch_assoc($result))){ $resultArray[] = $row; }
//print json_encode($resultArray);
return count($resultArray) < 1 ? print "[".json_encode(array("error"=>"sorry"))."]" : json_encode($resultArray);
}
If I understand correctly I think what your looking for is something along the lines of this;
Update the main function to determine if there were enough results and call the secondary query if not
public function realcashoffers($state,$count)
{
$this->state = $state;
$this->number = $count;
$result = $this->JSONselect("approved_business, business_stores, Real_Cash_Offers"," *, group_concat(offer ORDER BY offer ASC SEPARATOR ',') as offers"," approved_business.id = business_stores.business_id AND Real_Cash_Offers.business_id = approved_business.id AND Real_Cash_Offers.storeid = business_stores.storeid AND business_stores.state = '{$this->state}'","GROUP BY id ORDER BY RAND(), approved_business.id DESC LIMIT {$this->number} ");
$remaining = count($result) - $count;
if ($remaining) {
$result = array_merge($result, $this->JSONselect(.. enter secondary call here using $remaining as the limit..);
}
$this->JSONprint($result);
}
Update JSONselect to return the results instead of being responsible for printing them as well
public function JSONselect($table,$options,$where,$orderby)
{
$resultArray = array();
$options = empty($options) ? "*" : $options;
$where = empty($where) ? "1=1" : $where;
$orderby = empty($orderby) ? "" : $orderby;
$qry = "SELECT $options FROM $table WHERE $where $orderby ";
//print $qry;
$result = mysql_query($qry) or die(json_encode(array("error",mysql_error())));
while(($row = mysql_fetch_assoc($result))){ $resultArray[] = $row; }
//print json_encode($resultArray);
return $resultArray;
}
create JSONprint that will print the results returned
protected function JSONprint($resultArray) {
return count($resultArray) < 1 ? print "[".json_encode(array("error"=>"sorry"))."]" : json_encode($resultArray);
}
Related
I'm working on a system that has several server-side datatables but i facing issues with 2 joins when i try to order de columns.
I receive the following message when try to sort the columns:
Query error: Column 'notes' in order clause is ambiguous - Invalid query: SELECT *
FROM `tbl_project`
LEFT JOIN `tbl_client` ON `tbl_project`.`client_id`=`tbl_client`.`client_id`
LEFT JOIN `tbl_account_details` ON `tbl_project`.`created_by` = `tbl_account_details`.`user_id`
LEFT JOIN `tbl_notes` ON `tbl_project`.`notes` = `tbl_notes`.`notes_id`
WHERE `tbl_project`.`client_id` = '100'
ORDER BY `notes` DESC
LIMIT 10
This is the code with my query:
$id = $this->input->post("client_id");
$client_details = get_row('tbl_client', array('client_id' => $id));
$draw = intval($this->input->post("draw"));
$start = intval($this->input->post("start"));
$length = intval($this->input->post("length"));
$order = $this->input->post("order");
$search= $this->input->post("search");
$search = $search['value'];
$col = 0;
$dir = "";
if(!empty($order))
{
foreach($order as $o)
{
$col = $o['column'];
$dir= $o['dir'];
}
}
if($dir != "desc" && $dir != "desc")
{
$dir = "desc";
}
$valid_columns = array(
0=>'project_id',
1=>'client',
2=>'fullname',
3=>'notes',
4=>'origen',
5=>'end_date',
6=>'project_status',
7=>'action',
);
if(!isset($valid_columns[$col]))
{
$order = null;
}
else
{
$order = $valid_columns[$col];
}
if($order !=null)
{
$this->db->order_by($order, $dir);
}
$searchQuery = "";
if($search != ''){
$searchQuery = " (tbl_project.project_id like'%".$search."%' OR tbl_project.end_date like'%".$search."%' OR tbl_project.project_status like'%".$search."%' OR tbl_notes.notes like'%".$search."%' OR tbl_notes.eco like'%".$search."%' OR tbl_account_details.origen like'%".$search."%' OR tbl_client.name like'%".$search."%') ";
}
$this->db->select('*');
$this->db->from('tbl_project');
$this->db->join('tbl_client', 'tbl_project.client_id=tbl_client.client_id','left');
$this->db->join('tbl_account_details', 'tbl_project.created_by = tbl_account_details.user_id','left');
$this->db->join('tbl_notes', 'tbl_project.notes = tbl_notes.notes_id','left');
$this->db->where('tbl_project.client_id', $client_details->client_id);
if($searchQuery != '')
$this->db->where($searchQuery);
$this->db->limit($length,$start);
$cita = $this->db->get()->result();
For some reason the ORDER BY is not set as tbl_notes.notes
Any suggestion on how to fix this?
Thanks in advance
EDIT: i have added more code so there is more visibility of the process
The error occurs, because your column name is not unique, it exists in more than one table.
append the table name of the searched column to your query to make it unique:
for example in this line:
$this->db->order_by('my_table_name.'.$order, $dir);
that would generate something like
ORDER BY `my_table_name.notes` DESC
edit: or in case you have to address columns from several different tables you could change your $valid_columns array:
$valid_columns = array(
0=>'my_table_name1.project_id',
1=>'my_table_name2.client',
2=>'my_table_name2.fullname',
3=>'my_table_name3.notes',
// etc.
);
and maintain the remaining original code.
I have some if statements in my code.
e.g:
if($option[0]->posts == 1 && $option[0]->pages == 1){
$results = $wpdb->get_results( 'SELECT * FROM '.$wpdb->prefix.'posts WHERE post_status="publish" AND (post_type="page" OR post_type="post") ORDER BY post_title ASC', OBJECT );
}
if($option[0]->pages == 1 && $option[0]->posts == 0){
$results = $wpdb->get_results( 'SELECT * FROM '.$wpdb->prefix.'posts WHERE post_status="publish" AND post_type="page" ORDER BY post_title ASC', OBJECT );
}
if($option[0]->pages == 0 && $option[0]->posts == 1){
$results = $wpdb->get_results( 'SELECT * FROM '.$wpdb->prefix.'posts WHERE post_status="publish" AND post_type="post" ORDER BY post_title ASC', OBJECT );
}
a bit pseudo code of the code above:
if foo = 1 and bar = 1 -> return foo and bar
if foo = 0 and bar = 1 -> return only bar
if foo = 1 and bar = 0 -> return only foo
if foo = 0 and bar = 0 -> return false
You see:
00
10
01
11
00
If I insert another variable I'll get a lot of more opportunities and that is realy bad. Because I'll get realy big if statements.
Can somebody tells me another opportunitie to achive the same result?
Thank you.
I'd do it like this:
$sql_condition = '( 1=2 '; // one fake-condition, just to make it possible to start with 'OR' later
foreach($option[0] as $key => $value) { // iterate through all possible conditions
if($value===1) { // maybe exclude $keys that should not be used here
$sql_condition.=' OR post_type="'.$key.'"';
}
}
$sql_condition.=')';
$results = $wpdb->get_results( 'SELECT * FROM '.$wpdb->prefix.'posts WHERE post_status="publish" AND '.$sql_condition.' ORDER BY post_title ASC', OBJECT );
Please try this code :
$sub_query = $operator = '';
if($option[0]->posts == 1)
{
$sub_query = 'post_type="page"';
$operator = ' OR';
}
if($option[0]->pages == 1)
{
$sub_query .= $operator.' post_type="post"';
}
if(empty($sub_query))
{
return false;
}
else
{
$results = $wpdb->get_results( 'SELECT * FROM '.$wpdb->prefix.'posts WHERE post_status="publish" AND ('.$sub_query.') ORDER BY post_title ASC', OBJECT );
}
Create an array($arr) and set the key like "0,0" and value like "$sql";
and your code will be like this:
$tempKey = $option[0]->pages . "," . $option[0]->posts;
if(isset($arr[$tempKey]) {
$results = $wpdb->get_results($arr[$tempKey]);
}
So when you want to add more pages and posts all you will do is to change the arr.
$types = [];
if ($option[0]->posts)
$types[] = '"post"';
if ($option[0]->pages)
$types[] = '"page"';
if (!$types)
return null;
$results = $wpdb->get_results( 'SELECT * FROM '.$wpdb->prefix.'posts WHERE post_status="publish" AND (post_type IN ('. implode(',', $types) .')) ORDER BY post_title ASC', OBJECT );
Is here any possible to write single return for both $strValues and $strThumbImages i need to return $strValues for every time but returning $strThumbImages is just when it is available..
function doSelectBranchRecords($objArray,$ImageId = NULL)
{
global $global_config;
$strWhereClause = '';
if ($objArray['frmNameSearch']) {
$strWhereClause.= " AND A.branch_ident = '".$objArray['frmNameSearch']."' ";
}
if ($objArray['frmLoanSearch']) {
$strWhereClause.= " AND A.loan_ident = '".$objArray['frmLoanSearch']."' ";
}
if ($objArray['frmBeneficiarySearch']) {
$strWhereClause.= " AND A.beneficiary_idents = '".$objArray['frmBeneficiarySearch']."' ";
}
if ($objArray['frmDateSearch']) {
$strDate = explode("-", $objArray['frmDateSearch']);
$strAccountstarted = $strDate[2].'-'.$strDate[1].'-'.$strDate[0];
$strWhereClause.= " AND A.account_started = '".$strAccountstarted."' ";
/*printArray($strWhereClause); exit;*/
}
if ($ImageId) {
$strThumbImages = $global_config["SiteGlobalUploadPath"].$ImageId;
return $strThumbImages;
}
$strSqlSelect = "SELECT A.*,B.branch_name FROM tbl_companydetails as A,tbl_branchdetails as B where A.branch_ident=B.branch_id $strWhereClause order by company_id DESC";
$strValues = SelectQry($strSqlSelect);
return $strValues;
}
A php function can't return more than one variable. You could get both results from the function by returning an array with either one or two keys in it, then do a check in your calling code to see what has been set. So the end of your function becomes:
...
}
$output = array();
if ($ImageId) {
$strThumbImages = $global_config["SiteGlobalUploadPath"].$ImageId;
$output['thumbImages'] = $strThumbImages;
}
$strSqlSelect = "SELECT A.*,B.branch_name FROM tbl_companydetails as A,tbl_branchdetails as B where A.branch_ident=B.branch_id $strWhereClause order by company_id DESC";
$strValues = SelectQry($strSqlSelect);
$output['strValues'] = $strValues;
return $output;
}
#Simon and #Suchit are right in that you cannot return more than one variable. But in php it is very straightforwarded to return a list with multiple items like so:
function getXYZ(){
return array(4,5,6);
}
list($x,$y,$z) = getXYZ();
So in your case this could look like so:
$strThumbImages = null;
if ($ImageId) {
$strThumbImages = $global_config["SiteGlobalUploadPath"].$ImageId;
}
$strSqlSelect = "SELECT A.*,B.branch_name FROM tbl_companydetails as A,tbl_branchdetails as B where A.branch_ident=B.branch_id $strWhereClause order by company_id DESC";
$strValues = SelectQry($strSqlSelect);
return array($strValues, $strThumbImages);
As above #Simon Brahan said you can not return more than one variable.
so you can use array with different keys.but you should also check if $ImageId is set and not null.
if (isset($ImageId) && !is_null($ImageId)) {
$strThumbImages = $global_config["SiteGlobalUploadPath"].$ImageId;
$output['thumbImages'] = $strThumbImages;
}
$strSqlSelect = "SELECT A.*,B.branch_name FROM tbl_companydetails as A,tbl_branchdetails as B where A.branch_ident=B.branch_id $strWhereClause order by company_id DESC";
$strValues = SelectQry($strSqlSelect);
$output['strValues'] = $strValues;
return $output;
I want to select some news excluded the first 4 and I got a error
Invalid argument supplied for foreach:
Model:
public function get_all_news_home()
{
$exclude = array();
$this->load->database();
$last_news_query = $this->db->query("SELECT * FROM News WHERE Type = 1 AND Ready='Y' ORDER BY Date DESC LIMIT 4");
$last_news = $this->db->query($last_news_query);
$last_news = ($last_news->num_rows()) ? $last_news->result_array() : NULL;
foreach ($last_news as $ln)
{
array_push($exclude,array('id' => $ln['ID']));
}
$newsIds = implode(',', array_values($exclude));
$all_news = $this->db->query("SELECT * FROM News WHERE News.Ready = 'Y' AND News.ID NOT IN ('$newsIds') ORDER BY Date DESC LIMIT 12");
if($all_news->num_rows())
{
$all_news = $all_news->result_array();
}
else
{
$all_news = NULL;
}
return $all_news;
}
Controller:$this->data["all_news"] = &$this->site_news->get_all_news_home();
change this. you should check for null condition before passing it to foreach.
public function get_all_news_home()
{
$exclude = array();
$this->load->database();
$last_news_query = $this->db->query("SELECT * FROM News WHERE Type = 1 AND Ready='Y' ORDER BY Date DESC LIMIT 4");
$last_news = $this->db->query($last_news_query);
$last_news = ($last_news->num_rows()) ? $last_news->result_array() : NULL;
$all_news = NULL;
if($last_news!=NULL){
foreach ($last_news as $ln)
{
array_push($exclude,array('id' => $ln['ID']));
}
$newsIds = implode(',', array_values($exclude));
$all_news = $this->db->query("SELECT * FROM News WHERE News.Ready = 'Y' AND News.ID NOT IN ('$newsIds') ORDER BY Date DESC LIMIT 12");
if($all_news->num_rows())
{
$all_news = $all_news->result_array();
}
}
return $all_news;
}
I think this simple function should work nicely for you. Please use this simple function and let us know what happened.
public function get_all_news_home()
{
$this->load->database();
$all_news = $this->db->query("SELECT * FROM News WHERE Type = 1 AND Ready = 'Y' ORDER BY Date DESC LIMIT 4, 12");
if($all_news->num_rows())
{
$all_news = $all_news->result_array();
}
else
{
$all_news = NULL;
}
return $all_news;
}
$exclude = array();
$this->load->database();
$last_news = $this->db->query("SELECT * FROM News WHERE Type = 1 AND Ready='Y' ORDER BY Date DESC LIMIT 4");
//$last_news = $this->db->query($last_news_query);
//remove this line you are re-executing the query.
$last_news = ($last_news->num_rows()) ? $last_news->result_array() : NULL;
//before foreach add if condition
if (!is_null($last_news)) {
//add your code
}
Adding if condition before foreach is always a good practice. I hope this would help you.
I have a problem in a class I can't solve, the loop breaks in getAlojamiento() when I call getImagenes() inside the while. In this case getAlojamiento() returns only one row, it should return all the rows. Here are the methods involved:
//THIS GETS THE ROWS FROM A TABLE AND RETURN AN ARRAY, IF $id IS SET, CHECKS IF id_asoc == $id
public function getImagenes($table, $id=false){
$return = '';
//checks if id id == true
if($id){
$sql="SELECT * FROM $table WHERE id_asoc = '$id' ORDER BY id DESC";
}else{
$id = '';
$sql="SELECT * FROM $table ORDER BY id DESC";
}
//this make the sql request (returns an array)
if(!$this->MySQLQuery($sql)){
$return = false;
}else{
if($this->dbNumberRows == 0){ //cheks if there are results
$return = false;
}else{ //if has results makes and fills an array
$items = array();
while($f = mysql_fetch_object($this->dbResults)){
$items[$f->id]['id'] = $f->id;
$items[$f->id]['id_asoc'] = $f->id_asoc;
$items[$f->id]['comentario'] = htmlentities($f->comentario);
$items[$f->id]['nombre'] = htmlentities($f->nombre);
}
$return = $items;
}
}
return $return;
}
//THIS GETS THE ROWS FROM A TABLE AND RETURN AN ARRAY
public function getAlojamiento($id=false){
$return = '';
//checks if id id == true
if($id){
$sql="SELECT * FROM tb_alojamiento WHERE id = '$id' LIMIT 1";
}else{
$id = '';
$sql="SELECT * FROM tb_alojamiento ORDER BY titulo ASC";
}
//this make the sql request (returns an array)
if(!$this->MySQLQuery($sql)){
$return = false;
}else{
if($this->dbNumberRows == 0){ //cheks if there are results
$return = false;
}else{ //if has results makes and fills an array
$items = array();
while($f = mysql_fetch_object($this->dbResults)){
$imagenes_arr = $this->getImagenes('tb_alo_imagenes', $f->id); //this is causing the break
$items[$f->id]['id'] = $f->id;
$items[$f->id]['titulo'] = $f->titulo;
$items[$f->id]['telefono'] = $f->telefono;
$items[$f->id]['descripcion'] = $f->descripcion;
$items[$f->id]['email'] = $f->email;
$items[$f->id]['url'] = $f->url;
$items[$f->id]['direccion'] = $f->direccion;
$items[$f->id]['ubicacion'] = $f->ubicacion;
$items[$f->id]['coordenadas'] = $f->coordenadas;
$items[$f->id]['disponibilidad'] = $f->disponibilidad;
$items[$f->id]['tarifas'] = $f->tarifas;
$items[$f->id]['servicios'] = $f->servicios;
$items[$f->id]['theme'] = $f->theme;
$items[$f->id]['premium'] = $f->premium;
$items[$f->id]['img_ppal_id'] = $f->img_ppal_id;
$items[$f->id]['imagenes'] = $imagenes_arr;
}
$return = $items;
}
}
return $return;
}
The problem is that you are using $this->dbResults for both queries. When you call getImagenes you are clobbering the dbResults in getAlojamiento.
A simple solution to avoid having the first mysql query affect other queries is to complete it first:
//first loop over the result set from first query
while($f = mysql_fetch_object($this->dbResults))
{
//note: no call to $this->getImagenes here!
$items[$f->id]['id'] = $f->id;
$items[$f->id]['titulo'] = $f->titulo;
....
}
//only now you fetch the images in a second pass
foreach( $items as $id => $item )
{
$items[$id]['imagenes'] = $this->getImagenes('tb_alo_imagenes', $id);
}
//return the complete $items array
$return = $items;
while($f = mysql_fetch_object($this->dbResults)){
$items[$f->id]['id'] = $f->id;
$items[$f->id]['id_asoc'] = $f->id_asoc;
$items[$f->id]['comentario'] = htmlentities($f->comentario);
$items[$f->id]['nombre'] = htmlentities($f->nombre);
}
$return[] = $items;
You're setting the array to be the single $items array. You need to add it to the $return array.