I'm trying to do a filter search functionality in codeigniter. I have a table named products and my system has the functionality to filter these products by category and by date. I have a mysql code in mind which looks something like this:
SELECT * from products WHERE product_category='Cloth'
INTERSECT
SELECT * from products WHERE ('insert date logic here')
So it should return records (via id) from the same table named products. However, there's no INTERSECT in mysql so I don't know how to do this. Any help would be greatly appreciated!
This is my code just for the part of the product category
$this->db->limit($limit,$start);
$query = $this->db->query('SELECT * from product_advertised WHERE quantity > 0 AND prodcatid='.$prodcats[0].' LIMIT '.$start.','.$limit);
if(sizeof($prodcats > 1)) {
$query_str = "SELECT * FROM product_advertised WHERE quantity>0 AND ";
$str="";
for($i = 0;$i < sizeof($prodcats);$i++) {
if($i != sizeof($prodcats)-1) {
$str = $str. "prodcatid=".$prodcats[$i]." OR ";
}
else {
$str = $str. "prodcatid=".$prodcats[$i]." LIMIT ".$start.",".$limit;
}
}
$query_str .= $str;
$query = $this->db->query($query_str);
}
if($query->num_rows() > 0) {
foreach($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
Why wouldn't you just use and?
SELECT *
from products
WHERE product_category = 'Cloth' AND
('insert date logic here');
Related
Get maximum value of row in mysql using codeigniter.
i have following mysql data
I need to get the details based on total_payment of each student.
like this
i tried code below
$student_id_dis = $this->db->query('SELECT DISTINCT(student_id) FROM student_fees')->result_array();
$fee_cat_id_dis = $this->db->query('SELECT DISTINCT(fee_category_id) FROM student_fees')->result_array();
$this->db->select(['student_fees.*', 'fee_categories.fee_category_name as fee_name', 'fee_categories.amount as fee_amount']);
$this->db->join('student', 'student_fees.student_id = student.student_id');
$this->db->join('fee_categories', 'student_fees.fee_category_id = fee_categories.fee_categories_id');
$where = '';
for ($i = 0; $i < count($student_id_dis); $i++) {
if (isset($fee_cat_id_dis[$i]['fee_category_id'])) {
$where .='total_paid = (SELECT max(stdp.total_paid)
FROM student_fees stdp
WHERE stdp.fee_category_id = ' . $fee_cat_id_dis[$i]['fee_category_id'] . ')';
}
$this->db->where($where);
$this->db->get('student_fees')->result_array();
}
try this
select * from student_fees where student_fees_id in
(select student_fees_id from
where total_paid =max(total_paid) group by fee_category_id)
Earlier this day a asked a question about an update query. But now i want to select some things ( and it is working ) but I also want to order them and put a limit on it.
This is the code to select all the food :
public function getFood($id)
{
$id = (int)$id;
$rowset = $this->tableGateway->select(array('kindOfFood_id' => $id));
$row = $rowset->current();
if (!$row) {
throw new \Exception("Could not find row $id");
}
return $row;
}
But how can i do this :
Select * from KindOfFood ==> order by kindOfFood_votes DESC ?
I saw on the documentation you can do something like this, but it doesn't work with me?
$rowset = $artistTable->select(function (Select $select) {
$select->where->like('name', 'Brit%');
$select->order('name ASC')->limit(2);
});
Are you looking to return only single row or multiple rows.
Try this for multiple rows -
use Zend\Db\Sql\Select; //at the top of the page among other use statements.
public function getFood($id)
{
$id = (int) $id;
$select = new Select(TABLE_NAME); //CHANGE TABLE_NAME as per needs
$select->where('kindOfFood_id = ' . $id);
$select->order('kindOfFood_votes DESC');
$resultSet = $this->tableGateway->selectWith($select); //Will get array of rows.
//$row = $rowset->current(); THIS IS FOR RETURNING ONLY SINGLE ROW NOT ALL ROWS
if (!$resultSet) {
throw new \Exception("Could not find rows with food id - $id");
}
return $resultSet;
}
Can access the returned resultSet via loop. Eg: foreach
foreach($resultSet as $row) {
echo $row->kindOfFood_id; //or something
}
Note:
If you need only
Select * from KindOfFood order by kindOfFood_votes DESC
then remove the $select->where('kindOfFood_id = ' . $id); line from above.
This is my query
select count(payment_id) from payment where booking_id='$booking_id';
When i run this query in phpmyadmin i get count value 2 but i want access that value..
$sql="select count(payment_id) from payment where booking_id='$booking_id'";
$query = $this->db->query($sql);
echo $query->num_rows();
if($query->num_rows() > 0){
do something....
}
If count is 1 then do something and if count is greater than 1 then do something...
Try this:
$sql="select count(payment_id) as count_value from payment where booking_id='$booking_id'";
$query = $this->db->query($sql);
foreach ($query->result() as $row)
{
$count = $row->count_value;
}
if($count> 0){
do something....
}
count('coulme name') means, It always return result as a single row
Count() always returns 1 row. you need to do
$row = $query->result();
and then access the value of count from there...
This is a model function you can do it inside or return the count from the function and do the actions in your controller.
function count_payer($booking_id) {
$this->db->select ( 'payment_id' );
$this->db->from ( 'payment' );
$this->db->where ( 'booking_id', $booking_id );
$query = $this->db->get ();
$value = $query->num_rows ();
if ($value == 1) {
// do something and return
} else {
// do something else and return
}
}
If you want payment id then the query would be:
select sql_calc_found_rows payment_id from payment where booking_id='$booking_id';
select FOUND_ROWS();
I have done a bit off searching around for an answer, but most results are either not clear enough or I find it hard to implement in my current pattern... What I wish to achieve is having a query to select all products from the products table matching a category ID from the category table, But now i wish to also get products that are sub categories of the said parent category. I am using Doctrine 2 with codeigniter and my function so far looks like this
function searchForProducts( $offset, $limit ) {
$search = $this->input->get('search');
$category = (int)$this->input->get('category');
$supplier = (int)$this->input->get('supplier');
for( $i = 0; $i < 2; $i++ ) {
$select = ($i == 0) ? 'count(p)' : 'p';
$qb = $this->em->createQueryBuilder();
$qb ->select($select)
->from(self::ENTITY, 'p');
if( $i != 0) {
$qb ->setFirstResult( (int)$offset )
->setMaxResults( (int)$limit );
}
if( $search ) {
$qb ->where( "p.title LIKE :search" )
->orWhere( "p.sku LIKE :search" )
->setParameter('search', "%$search%");
}
if( $category ) {
$qb ->andWhere( "p.category = ?1" )
->setParameter(1, $category);
}
if( $supplier ) {
$qb ->andWhere( "p.supplier = ?2" )
->setParameter(2, $supplier);
}
if( $i == 0 ) {
$this->totalRows = $qb->getQuery()->getSingleScalarResult();
} else {
return $qb->getQuery()->getResult();
}
}
}
I also don't think it would be practical to get all products then do it from application level as I'm using pagination and products could become quite large.
When you do Product -> Category -> Child Categories -> Product then the resulting Products will not be returned in the same column. Thus Doctrine will hydrate them in the same manor. Which is no good if you want to use pagination on all the products.]
Your best bet is to use a native SQL with result set mapping. Then your query should use a UNION, so that the main Product lines up in the same columns as the child Products
To answer my own question, I decided to take the easy way out, and considering i would only have around 40 categories max, i don't think this should be much of a problem.
In my categories model class i created 2 static methods,
static function getCategoryWithChildren( $id = NULL ) {
$obj = new self;
$parent = $obj->getCategory( $id );
$list = array();
self::iterateCategoriesChildren($parent, $list);
return array_reverse($list);
}
static function iterateCategoriesChildren( $category, &$array ) {
if( $category->hasChildren() ) {
foreach( $category->getChildren() as $child ) {
self::iterateCategoriesChildren($child, $array);
}
}
$array[] = $category;
}
So pretty much this will get the parent and iterate all the children and assign each 1 to the flat array.
Then from my searchProducts method i just added a bit more under the $category check.
if( $category ) {
if( $this->settings->get('product_child_categories') ) {
$categories = Categories_model::getCategoryWithChildren($category);
foreach($categories as $_category) {
$qb ->orWhere( "p.category = " . $_category->getID() );
}
} else {
$qb ->andWhere( "p.category = ?1" )
->setParameter(1, $category);
}
}
May not be best or practical, but it works and does what I need for the time being.
I am using some filters to display the products. Filters like colors, price and stuff.
Link : http://www.applechain.com/products/iPod.php
I use this query
$sql = "Select *
from tbl_product
where device='iPhone'
and (color='$c1'
or color='$c2'
or color='$c3'
or color='$c4'
or color='$c5'
or color='$c6'
or color='$c7'
or color='$c8'
or color='$c9'
or color='$c10'
) and (storage='$cp1'
or storage='$cp2'
or storage='$cp3'
or storage='$cp4'
or storage='$cp5'
) and (f_unlock='$factory')
and (warranty='$warranty')
and (price >= '$price1'
and price <= '$price2'
)
order by product_id desc";
I am using the AND condition for main parameters. Now how do I display the result if my only two parameters gets satisfied. How to achieve that if color and storage parameters gets satisfied,it shows result based on them only irrespective of whether the others are selected or not.
Simple. Remove the other conditions from the query.
BTW, you can rewrite that query this way...
SELECT *
FROM tbl_product
WHERE device = 'iPhone'
AND color IN ('$c1','$c2','$c3','$c4','$c5','$c6','$c7','$c8','$c9','$c10')
AND storage IN ('$cp1','$cp2','$cp3','$cp4','$cp5')
AND f_unlock = '$factory'
AND warranty = '$warranty'
AND price BETWEEN '$price1' and '$price2'
ORDER
BY product_id DESC;
You could build your query based on what is set, normally an ORM tool would do this for you.
As it's probably too late to start using a PHP framework, you should do something like:
if(isset($colours))
{
$sub_query = concatenateOrClauses($colours, 'color');
$main_query .= $sub_query;
}
private function concatenateOrClauses($values, $name)
{
$query_string = "";
for($i = 0; $i < sizeof($values); $i++)
{
if($i == 0)
{
$query_string = $name."=".$values[$i];
}
else
{
$query_string = $query_string." OR ".$name."=".$values[$i];
}
}
return $query_string;
}