I have a query that is bringing back all rows from the table instead of the fields I have specified. Can you see any mistakes in this? I'm using codeigniter.
Thanks in advance!
unset($conditions);
$conditions['conditions'] = array("accountid"=>$this->sessionInfo['database_account_id'],
"DATE_FORMAT(salestart,'%Y-%m-%d')"=>$today,
"shop"=>"london"
);
$conditions['group_by'] = "item";
$conditions['fields'] = "accountid, item, count(uniqueid) as totalitems, sum(options) as totaloptions, colour";
$today_sales = $this->Database_Model->selectData("sales",$conditions);
My model is:
public function selectData($table,$condition=array()) {
if(isset($condition['fields'])){
$fields = $condition['fields'];
}
else{
$fields = "*";
}
$this->Database->select('*');
$this->Database->from($table);
if(isset($condition['conditions'])){
$this->Database->where($condition['conditions']);
}
if(isset($condition['group_by'])){
$this->Database->group_by($condition['group_by']);
}
if(isset($condition['order_by'])){
$this->Database->order_by($condition['order_by']);
}
if(isset($condition['where_in'])){
$where_in = $condition['where_in'];
foreach($where_in as $key =>$value){
$this->Database->where_in($key,$value);
}
}
if(isset($condition['joins'])){
$joins = $condition['joins'];
foreach($joins as $join){
$this->Database->join($join['table'], $join['joinWith'],$join['type']);
}
}
$query = $this->Database->get();
return $query->result_array();
}
Change this
$this->Database->select('*');
to this
$this->Database->select($fields);
I want to get query result from my model and use it in view. I don't know why but my result is empty. Can you look at this and tell me what I'm doing wrong?
Controller:
$peugeot = $this->input->post('peugeot');
$citroen = $this->input->post('Citroen C-Elysee');
$nissan= $this->input->post('Nissan Evalia');
$renault = $this->input->post('Renault Trafic 9-os');
$this->load->model('Edit_model');
$this->Edit_model->peugeot($peugeot);
if($peugeot)
{
$result = $this->Edit_model->peugeot($peugeot);
$data['result'] = $result;
}
Model:
public function peugeot($peugeot)
{
$this->db->like('model', $peugeot, 'after');
$query = $this->db->get('cars');
$result = $query->result();
return $result;
}
View:
if($peugeot)
{
print_r($result);
}
Kinda strange, but It works
I've changed my controller into this
$data['peugeot'] = $this->input->post('peugeot');
$data['citroen'] = $this->input->post('citroen');
$data['nissan'] = $this->input->post('nissan');
$data['renault'] = $this->input->post('renault');
$this->load->model('Edit_model');
if($data['peugeot'])
{
$this->Edit_model->peugeot($data['peugeot']);
$result = $this->Edit_model->peugeot($data['peugeot']);
$data['result'] = $result;
$this->load->view('content/editprocess',$data);
}
My Code :
$this->db->join('followers','followers.id_follower = post.id_account','LEFT');
$id_account = $this->session->userdata('id');
$where = ("followers.id_following=$id_account or post.id_account = $id_account");
$this->db->where($where);
Please help me
Something like this?
In you controller:
// Set variables
$page_data = array();
$id_account = $this->session->userdata('id');
// Get data
$this->db->from('post');
$this->db->join('followers','followers.id_follower = post.id_account','LEFT');
$this->db->where('id_following', $id_account);
$this->db->or_where('id_account', $id_account);
$page_data['followers'] = $this->db->get();
// Load views
$this->load->view('myview', $data);
In your view somewhere
<?php if (!empty($followers)) {
foreach ($followers as $item) {
echo $item['follower_id'];
}
} ?>
You may want to use short forms of php in your view, but you get the idea.
When I sen a request this page , I got an error like this
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 0
Filename: models/mproduk.php
Line Number: 116
This is my controller
public function update($id=''){
$data = array();
$data['size'] = $this->msize->tampil();
$data['eta']=$this->mproduk->berapakah_stoknya($id);
$data['cats'] = $this->mkategori->tampil();
$data['prd'] = $this->mproduk->get_produk_detail($id);
$data['human'] = $this->mhuman->tampil();
$html = array();
$html['header'] = $this->load->view('secuser/header',null,true);
$html['kiri'] = $this->load->view('secuser/kiri',null,true);
$html['content'] = $this->load->view('secuser/product/update',$data,true);
$this->load->view('secuser/template',$html);
}
This is my model
public function berapakah_stoknya($kodepro){
$this->load->model('msize');
$hahaha=$this->msize->tampil();
foreach($hahaha as $wkwkwkw){
$apa= $wkwkwkw['id'];
$sql= "select ukr_$apa from produk where kodeproduk = '".$kodepro. "'";
$ggg= $this->db->query($sql)->result_array();
$return[]=array('stok'=>$ggg[0]["ukr_$apa"],'ukuran'=>$wkwkwkw['ukuran_produk'],'id_'=>$apa);
}
return $return;
}
If I use error_reporting(0) the page will be looping and show to be some page
How can i fix this error?
Your DB query retrieves no results and therefore $ggg is empty and has no 0 index.
Change your model to check if any results were found. If found, add to $return array.
public function berapakah_stoknya($kodepro){
$this->load->model('msize');
$hahaha = $this->msize->tampil();
$return = array();
foreach($hahaha as $wkwkwkw){
$apa= $wkwkwkw['id'];
$sql= "select ukr_$apa from produk where kodeproduk = '".$kodepro. "'";
$ggg= $this->db->query($sql)->result_array();
if(count($ggg)>0){
$return[]=array('stok'=>$ggg[0]["ukr_$apa"],'ukuran'=>$wkwkwkw['ukuran_produk'],'id_'=>$apa);
}
}
return $return;
}
It seems your are expectin to retrieve only one row. So you could use row_array.
public function berapakah_stoknya($kodepro){
$this->load->model('msize');
$hahaha = $this->msize->tampil();
$return = array();
foreach($hahaha as $wkwkwkw){
$apa= $wkwkwkw['id'];
$sql= "select ukr_$apa from produk where kodeproduk = '".$kodepro. "'";
$query = $this->db->query($sql);
if($query->num_rows() > 0){
$ggg = $query->row_array();
$return[]=array('stok'=>$ggg["ukr_$apa"],'ukuran'=>$wkwkwkw['ukuran_produk'],'id_'=>$apa);
}
}
return $return;
}
I am developing a CMS which works on template page system in a different approach.
I have this object:
$structure = new stdClass;
$structure->homepage->news->method = 'get_articles_by_page_name';
$structure->homepage->news->lang_key = translate('home_news');
$structure->homepage->news->lang = $lang;
$structure->homepage->news->add_media = true;
$structure->homepage->news->media_type = 'ibs';
$structure->homepage->news->limit = '5';
$structure->homepage->news->order_by = 'a.logical_date';
$structure->homepage->news->asc_des = 'desc';
$structure->homepage->news->result_type = 'result';
This helps to get contents as following:
foreach ($structure as $page_template => $page_contents)
{
// Call Customized Content for Homepage
if($this->data['page_data']->page_view == $page_template) // homepage comes ok.
{
foreach ($page_contents as $view_var_name => $page_cdata)
{
$method = $page_cdata->method; // method names comes
$page_cdata = substr(implode(",",(array) $page_cdata),(strlen($method)+1)) . '\'';
//Returns as expected:
//**'Haberler','tr','1','ibs','5','a.logical_date','desc','result'**
$this->data[$view_var_name] = $this->publish->$method($page_cdata);
vdebug($page_cdata);
}
}
}
It suppose to call them model function of:
function get_articles_by_page_name( $lang_key='',$lang='en',$add_media=true,
media_type='ibs',$limit='0',$order_by='a.logical_date',$asc_desc='desc',$result_type='result')
However, there is a problem with. When I return to last worked query it says:
SELECT * FROM (`page`) JOIN `page_lang` ON `page`.`id_page` = `page_lang`.`id_page` WHERE `page_lang`.`title` = '\'News\',\'tr\',\'1\',\'ibs\',\'5\',\'a.logical_date\',\'desc\',\'result\''
It souldn't be like this. every thing between commas are parameters of the method function. What cause this, any idea?
Content of get_articles_by_page_name:
function get_articles_by_page_name ($lang_key='',$lang='tr',$add_media=true,$media_type='ibs',$limit='0',$order_by='a.logical_date',$asc_desc='desc',$result_type='result')
{
// Define variables
$id_page = '';
$result = '';
// Get Page Data
$page_name = $lang_key;
$get_page = $this->vayes->getJoined('page','page_lang','id_page','','',array('page_lang.title'=>$page_name),'row');
if($get_page)
{
$id_page = $get_page->id_page;
$result = $this->publish->get_articles($lang,$id_page,null,false,'',$order_by,$asc_desc,$limit,'result');
}
else
{
$result = array('No id_page specified');
}
return $result;
}
Content of get_articles:
function get_articles($lang='tr',$id_page,$id_article=null,$incl_media=true,$media_type='',$order_by='a.logical_date',$asc_desc='desc',$limit='0',$result_type='result')
{
$this->db->select('*');
$this->db->from('article a');
$this->db->join('article_lang b','b.id_article=a.id_article','left outer');
if($incl_media) {
$this->db->join('article_media c','c.id_article=b.id_article','left outer');
$this->db->join('media d','d.id_media=c.id_media','left outer');
}
if($id_article == null) { $this->db->where('a.id_page',$id_page); }
else /*------------->*/ { $this->db->where('a.id_article',$id_article); }
$this->db->where('b.lang',$lang);
$this->db->where('b.online',1);
if(($incl_media == true) AND $media_type != '' ) $this->db->where('c.usage',$media_type);
// Order Results
$this->db->order_by($order_by,$asc_desc);
// Limit Results
if ($limit) $this->db->limit($limit);
$query = $this->db->get();
if($query->num_rows() > 0)
{
$result = $query->$result_type();
$query->free_result();
return $result;
}
return false;
}
try stripslashes()
Attempting to use stripslashes on an array in 5.2.17 returns the string "Array", but in 5.3.6 it returns NULL. So using stripslashes() on an array you will need to do it recursively;
function stripslashes_deep($value)
{
$value = is_array($value) ?
array_map('stripslashes_deep', $value) :
stripslashes($value);
return $value;
}
// Example
$array = array("f\\'oo", "b\\'ar", array("fo\\'o", "b\\'ar"));
$array = stripslashes_deep($array);
// Output
print_r($array);