i create a function in my model like :
public function getAllMenuByOnlyActionPage($actionlot){
$act = trim($actionlot);
$result = $this->tableGateway->select(function (Select $select) {
$select->where(array('status != ?' => 13))
->where(array('action' => $act))
->order('id ASC');
});
$results = array();
foreach ($result as $row) {
$results[] = $row;
}
return $results;
}
When i try to execute then its show me Notice: Undefined variable: act. I already see this link ZF2 tableGateway select but i want to set parameter for this function. Thanks
Try
$result = $this->tableGateway->select(function (Select $select) use ($act) {
$select->where(array('status != ?' => 13))
->where(array('action' => $act))
->order('id ASC');
});
Related
I wanting to be able to join two tables togeather.
How ever because I my forum table has column "name" and my forum_categories column "name"
I am not able to display both names.
On my select() if I use like $this->db->select('f.name, fc.name', false); it only displays name from forum_categories
array(1) { [0]=> array(1) { ["name"]=> string(17) "News & Discussion" } }
Question how can I get both names to show from both columns and
tables.
Note: I only want to be able to use $result['name'] in my foreach loop.
So the out put I would like it to be
General
News & Discussion
Lounge
I have looked at
CodeIgniter ActiveRecord field names in JOIN statement
codeigniter - select from 2 tables with same column name
Model
public function get_forums() {
$this->db->select('f.name, fc.name', false);
$this->db->from('forum as f');
// tried $this->db->join('forum_categories as fc', 'fc.forum_id = f.forum_id');
$this->db->join('forum_categories as fc', 'fc.forum_categories_id = f.forum_id');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return false;
}
}
Controller
<?php
class Forums extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$data['label'] = '';
$data['forums'] = array();
$results = $this->get_forums();
var_dump($results);
if (isset($results)) {
foreach ($results as $result) {
$data['forums'][] = array(
'name' => $result['name'], // Only want to use single variable.
);
}
}
$data['header'] = Modules::run('admin/common/header/index');
$data['footer'] = Modules::run('admin/common/footer/index');
$this->load->view('template/forum/list_forum_view', $data);
}
public function get_forums() {
$this->db->select('f.name, fc.name', false);
$this->db->from('forum as f');
$this->db->join('forum_categories as fc', 'fc.forum_categories_id = f.forum_id');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return false;
}
}
}
update
works fine with code below but would rather just use one lot of join()
public function get_forums() {
$this->db->select("*");
$this->db->from('forum');
$query = $this->db->get();
foreach ($query->result_array() as $f) {
$data[] = array(
'name' => $f['name']
);
$this->db->select("*");
$this->db->from('forum_categories');
$query = $this->db->get();
foreach ($query->result_array() as $fc) {
$data[] = array(
'name' => $fc['name']
);
}
}
return $data;
}
Try This , It Will Work .
public function get_forums() {
$this->db->select('f.name as forum_name, fc.name as forum_categories_name', false);
$this->db->from('forum f');
// tried $this->db->join('forum_categories fc', 'fc.forum_id = f.forum_id');
$this->db->join('forum_categories fc', 'fc.forum_categories_id = f.forum_id');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return false;
}
}
From what i see in your updated question. What you need is UNION and not JOIN. You can use get_compiled_select() to build both query before concat with UNION.
public function get_forums() {
$forum = $this->db->select('name')->get_compiled_select('forum');
$forum_categories = $this->db->select('name')->get_compiled_select('forum_categories');
$query = $this->db->query($forum.' UNION '.$forum_categories);
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return false;
}
}
Simply use
AS
keyword, because when you try to print it, that AS keyword give you optional name for your column.
Here i have two table which have same column name "name" so i have changed those with AS keyword.
Ex:
$this->db->select('tbl_category.id_category,tbl_category.name AS cat_name,
tbl_subject.id_subject,tbl_subject.name AS sub_name');
In result
Array ( [0] => Array ( [id_category] => 9 [cat_name] => OL [id_subject] => 13 [sub_name] => Science )
So it is simple solution from the SQL language
i first checked if there any same problems like mine i ddnt find anything.
all are sorting alphanumeric column mixed with numeric data.
here is my problem.
i have a table that contain column A datas like this.
WRG-01 WRG-39 WRG-22 WRG-45 WRG-43
need to sort that as
WRG-01 WRG-22 WRG-39 WRG-43 WRG-45
this is the code i using so far in codeigniter frame work
$data['products'] = $this->db->order_by('product_id', 'asc')->get('products');
in mysql i can use this query to get done my work
preg_replace("/[^\d]/", "",'product_id'), 'asc')
How to apply it to my above codeigniter code?
here is search funtion
public function search()
{
$data['title'] = 'Search Product';
$product_name = $this->input->get('product_name');
$product_id = $this->input->get('product_id');
$product_category = $this->input->get('product_category');
$secondCategory = $this->input->get('secondCategory');
$thirdCategory = $this->input->get('thirdCategory');
$data['category'] = $this->db->order_by('id', 'asc')->get_where('categories', ['parent' => 0]);
if($product_category != '')
{
$data['secondCategory'] = $this->db->get_where('categories', ['parent' => $product_category]);
}
if($secondCategory != '')
{
$data['thirdCategory'] = $this->db->get_where('categories', ['parent' => $secondCategory]);
}
if($product_name != '')
{
$this->db->like('product_name', $product_name);
}
if($product_id != '')
{
$this->db->where('product_id', $product_id);
}
if($product_category != '')
{
$this->db->where('product_category', $product_category);
}
if($secondCategory != '')
{
$this->db->where('secondCategory', $secondCategory);
}
if($thirdCategory != '')
{
$this->db->where('thirdCategory', $thirdCategory);
}
$data['products'] = $this->db->order_by('product_id' 'asc')->get('products');
theme('all_product', $data);
}
i can't use sql query here because products is result array from product table.
Use MySQL cast
cast(product_id as SIGNED)
or
cast(product_id as UNSIGNED)
Try query like that :-
select * from products cast(product_id as UNSIGNED) ASC|DESC
Try this
$query= $this->db->query("SELECT * FROM products WHERE ??==?? ORDER BY product_id ASC");
$result= $query->result_array();
return $result;
as default data will sort by Ascending Order
This is in model. So if you pass it to controller it will return data as Objective Array.
So in controller you can access
$result = $this->model_name->method_for_above_code();
$name = $result[0]['name'];
$id = $result[0]['id'];
if in View
$result['this_for_view'] = $this->model_name->method_for_above_code();
foreach ($this_for_view as $new_item) {
echo "Name is ".$new_item['name'];
echo "ID is ".$new_item['id'];
}
Can someone give me a hint what I am doing wrong?
public function getPaymentSumByTypeAndProject($project_id,$type) {
$type = (int) $type;
$project_id = (int) $project_id;
$rowset = $this->tableGateway->select(array('total_amount' => new Expression('SUM(payment.amount)')))->where(array('type' => $type, 'project_id' => $project_id));
$row = $rowset->toArray();
if (!$row) {
throw new \Exception("Busted :/");
}
return $rowset;
}
I want to make the same query:
SELECT SUM(amount) FROM payment WHERE type='$type' AND project_id ='$project_id';
Edit:
I made small progress, i have figured out how to sum whole column
public function getPaymentSumByTypeAndProject($project_id, $type) {
$type = (int) $type;
$project_id = (int) $project_id;
$resultSet = $this->tableGateway->select(function (Select $select) {
$select->columns(array(new \Zend\Db\Sql\Expression('SUM(amount) as amount')))->where('type="0"');
});
return $resultSet;
Maybe someone could help me to figure out how to add condition: "WHERE type='$type' AND project_id='$project_id'" ?
I know this is an old question, but I came across it and figured I'd throw in my two cents:
public function getPaymentSumByTypeAndProject($project_id, $type) {
// This TableGateway is already setup for the table 'payment'
// So we can skip the ->from('payment')
$sql = $this->tableGateway->getSql();
// We'll follow the regular order of SQL ( SELECT, FROM, WHERE )
// So the query is easier to understand
$select = $sql->select()
// Use an alias as key in the columns array instead of
// in the expression itself
->columns(array('amount' => new \Zend\Db\Sql\Expression('SUM(amount)')))
// Type casting the variables as integer can take place
// here ( it even tells us a little about the table structure )
->where(array('type' => (int)$type, 'project_id' => (int)$project_id));
// Use selectWith as a shortcut to get a resultSet for the above select
return $this->tableGateway->selectWith($select);
}
Also, an adapter can be retrieved from a table gateway like this:
$adapter = $this->tableGateway->getAdapter();
But you don't really need it anymore when you select using the above mentioned method.
Ok now this is working, tell me is this how it;s should be done?
public function getPaymentSumByTypeAndProject($project_id, $type) {
$type = (int) $type;
$project_id = (int) $project_id;
$adapter = $this->tableGateway->adapter;
$sql = new Sql($adapter);
$select = $sql->select();
$select->from('payment');
$select->where(array('type'=>$type,'project_id'=>$project_id));
$select->columns(array(new \Zend\Db\Sql\Expression('SUM(amount) as amount')));
$selectString = $sql->getSqlStringForSqlObject($select);
$resultSet = $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);
return $resultSet;
}
use This one
$select = $this->getSql()->select()
->columns(array('amount' => new \Zend\Db\Sql\Expression('SUM(amount)')))
->where("type ='$type'")
->where("project_id ='$project_id'");
$resultSet = $this->selectWith($select);
$row = $resultSet->current();
// echo $select->getSqlString();die; //check query use this line
if(!$row){
return False;
}
return $row->amount;
try to make like that instead of (int) $type
intval($type);
and
intval($project_id);
and in your sql
change your variables to
'".$type."'
AND
'".$project_id."'
I have the following model function and I was wanting to know how I could improve it and also pull the needed $row out when needed so that I dont get a PHP error.
How I am pulling the required data:
$data['companyName'] = $this->core_model->companyDetails('coreCompanyName');
Errors:
Undefined property: stdClass::$coreContactName
Undefined property: stdClass::$coreContactEmail
Model:
function companyDetails()
{
$this->db->select('coreCompanyName, coreContactName, coreContactEmail');
$this->db->from('core');
$whereData = array('coreCompanyName' => $companyName, 'coreContactName' => $companyContactName, 'coreContactEmail' => $companyContactEmail);
$this->db->where($whereData);
$query = $this->db->get();
$result = '';
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$result .= $row->coreCompanyName;
$result .= $row->coreContactName;
$result .= $row->coreContactEmail;
}
}
return $result;
}
It should be like this
$this->db->select('coreCompanyName, coreContactName, coreContactEmail');
not
$this->db->select('coreCompanyName','coreContactName','coreContactEmail');
See reference http://codeigniter.com/user_guide/database/active_record.html#select
To limit to a specific set of records, use the where() function. e.g.
$this->db->where('companyId', $companyId);
<?php
/**
*
* WHERE ARE : $companyName, $companyContactName, $companyContactEmail being defined ?
* WHY return 'companyName' WHEN You are quering based on the '$companyName' ???
* Your query wil fail with out it, that that's all your using ?
*
*/
function companyDetails() {
$query = $this->db->query("SELECT companyName FROM core WHERE coreCompanyName = ? AND coreContactName = ? AND coreContactEmail = ? LIMIT 1",
array($companyName, $companyContactName, $companyContactEmail));
return ($query->num_rows() == 1) ? $query->row()->companyName : FALSE;
}
Change:
$this->db->select('coreCompanyName','coreContactName','coreContactEmail');
To:
$this->db->select('coreCompanyName, coreContactName, coreContactEmail');
Now, you said this pulls all the data, but you aren't actually filtering the result. I wonder what the required data is
Im trying to get an max value with codeigniter from an table but it isnt working. This is the error i get:
Severity: 4096
Message: Object of class CI_DB_mysql_result could not be converted to
string
Filename: database/DB_active_rec.php
Line Number: 427
This is my function:
public function getPeriodeNummer($bedrijf_id) {
$this->db->select_max('id');
$this->db->where('bedrijf_id', $bedrijf_id);
$result = $this->db->get('rapporten');
$this->db->select('periode_nummer');
$this->db->where('rapporten_id', $result);
$query = $this->db->get('statistieken_onderhoud');
$data = $query + 1;
return $data;
}
What im trying to do is as followed:
Select the highest id where bedrijf_id = $bedrijf_id from rapporten.
Select the periode_nummer from statistieken_onderhoud where rapporten_id = the highest id i got from step 1.
Add 1 to the periode_nummer i got from step 2 and return that number.
Thanks in forward for your help!
Try
public function getPeriodeNummer($bedrijf_id) {
$this->db->select_max('id');
$this->db->where('bedrijf_id', $bedrijf_id);
$res1 = $this->db->get('rapporten');
if ($res1->num_rows() > 0)
{
$res2 = $res1->result_array();
$result = $res2[0]['id'];
$this->db->select('periode_nummer');
$this->db->where('rapporten_id', $result);
$query = $this->db->get('statistieken_onderhoud');
if ($query->num_rows() > 0)
{
$row = $query->result_array();
$data['query'] = 1 + $row[0]['periode_nummer'];
}
return $data['query'];
}
return NULL;
}
Try this:
$this->db->select_max('display_sequence');
$this->db->from('acl_menu');
$query = $this->db->get();
$r=$query->result();
Display Sequence is your column name & acl_menu is your table name.
You can't use an object as a string. Use this:
public function getPeriodeNummer($bedrijf_id) {
$this->db->select_max('id');
$this->db->where('bedrijf_id', $bedrijf_id);
$result = $this->db->get('rapporten');
$this->db->select('periode_nummer');
$this->db->where('rapporten_id', $result);
$query = $this->db->get('statistieken_onderhoud');
// fetch first row in object
$result = $query->row();
$data = $result + 1;
return $data;
}
$this->db->select_max('id', 'max_id');
$query = $this->db->get('video_processing');
return $query->row();
try the above:
I think the $query variable is holding a mysql result resource and it cannot be used as a String or in this case an Integer.
You could try this way:
$data = mysql_result($query,0) + 1;
Shortest:
$this->db->select_max('id', 'max_id')->get('video_processing')->row();