display results on query basis in Codeigniter with Pagination - codeigniter

I have a table of products. I need to display all the products. I am fetching results on different conditions.
create table `products` (
`id` double ,
`category_id` double ,
`subcategory_id` double ,
`product_name` varchar (765),
`product_description` varchar (765),
`product_viewed` varchar (765),
`sale_wanted` tinyint (2),
`added_date` datetime ,
`updated_date` datetime ,
);
I need to diplay the results like this
1. The latest products (use of added date)
2. Most Wanted (Sorting by sale_wanted 1 for sale , 2 for wanted)
3. Most Viewed (Sorting by product_viewed)
4. Sorting by Specific Subcategory
All the results should display with pagination. This is all right if i first get the result. But if i walk with pagination links all the condition data is lost and the query fetches the results without any condition. How can i manage This situation. Please i dont need Code i need hints and suggestions. The other thing is that i am using Codeigniter's pagination class.
EDITED
Here is my Model Method i am using
public function getProductsList($per_page=5,$page=0)
{
$info = $this->input->post();
if(isset($info['type']))
{
$type = $info['type'];
if($type == 'most_wanted'){
$where = " AND sale_wanted = 1";
$order_by = " ORDER BY ldc.added_date desc";
}else if($type == 'most_viewed'){
$where = " ";
$order_by = " ORDER BY ldc.product_viewed desc";
}else{
$where = " ";
$order_by = " ORDER BY ldc.added_date desc";
}
}else if(isset($info['sale_wanted']) AND isset($info['subcategory_id'])){
$sale_wanted = $info['sale_wanted'];
$subcategory_id = $info['subcategory_id'];
$where = " AND sale_wanted = $sale_wanted AND ldc.subcategory_id = $subcategory_id";
$order_by = " ORDER BY ldc.added_date desc";
}else if(isset($info['keyword'])){
$keyword = $info['keyword'];
$search_type = $info['search_type'];
$where = " AND ldc.$search_type like '$keyword%'";
$order_by = " ";
}else{
$where = " ";
$order_by = " ";
}
$num = 0;
if($page != 0){
$num = ($page * $per_page) - $per_page;
}
$sql_query = "
SELECT
ldc.id,
ldc.product_name,
ldc.product_viewed,
DATE_FORMAT(ldc.added_date, '%m/%d/%Y') as added_date,
ifnull(dc.name,'Unknown') as category,
dpi.product_image
FROM default_products AS ldc
LEFT JOIN default_manufacturers as dm ON dm.id = ldc.manufacturer
LEFT JOIN default_category as dc ON dc.category_id = ldc.category_id
LEFT JOIN ((select product_id , product_image from default_product_images group by product_id) as dpi)
ON dpi.product_id = ldc.id
WHERE approved = 1
$where
$order_by
LIMIT $num,$per_page
";
$query = $this->db->query($sql_query);
return $query->result();
}

i would highly recommend these two Free tutorials on codeigniter pagination -
video tutorials, working sample code ( might need to update code to CI 2.1 ), and even some helpful info in the comments.
CodeIgniter from Scratch: Displaying & Sorting Tabular Data
http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-displaying-sorting-tabular-data/
CodeIgniter from Scratch: Search Results without Query Strings
http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-search-results-without-query-strings-2/

Related

Codeigniter update_batch in Core PHP

everyone.
In the codeigniter there is update_batch function by using it we are able to bulk update multiple rows.
$this->db->update_batch('table_name', $update_data_array, 'where_condition_field_name');
I want similar functionality in core PHP in one function or in one file. Is there any workaround?
Is there any way to extract update_batch function from Codeigniter in one file/function?
I tried to extract this function but it is very lengthy process and there will be many files / functions should be extracted.
Please help me in this regard
Thanks in advance
You can also insert multiple rows into a database table with a single insert query in core php.
(1)One Way
<?php
$mysqli = new mysqli("localhost", "root", "", "newdb");
if ($mysqli == = false) {
die("ERROR: Could not connect. ".$mysqli->connect_error);
}
$sql = "INSERT INTO mytable (first_name, last_name, age)
VALUES('raj', 'sharma', '15'),
('kapil', 'verma', '42'),
('monty', 'singh', '29'),
('arjun', 'patel', '32') ";
if ($mysqli->query($sql) == = true)
{
echo "Records inserted successfully.";
}
else
{
echo "ERROR: Could not able to execute $sql. "
.$mysqli->error;
}
$mysqli->close();
? >
(2)Second Way
Let's assume $column1 and $column2 are arrays with same size posted by html form.
You can create your sql query like this:-
<?php
$query = 'INSERT INTO TABLE (`column1`, `column2`) VALUES ';
$query_parts = array();
for($x=0; $x<count($column1); $x++){
$query_parts[] = "('" . $column1[$x] . "', '" . $column2[$x] . "')";
}
echo $query .= implode(',', $query_parts);
?>
You can easily construct similar type of query using PHP.
Lets use array containing key value pair and implode statement to generate query.
Here’s the snippet.
<?php
$coupons = array(
1 => 'val1',
2 => 'va2',
3 => 'val3',
);
$data = array();
foreach ($coupons AS $key => $value) {
$data[] = "($key, '$value')";
}
$query = "INSERT INTO `tbl_update` (id, val) VALUES " . implode(', ', $data) . " ON DUPLICATE KEY UPDATE val = VALUES(val)";
$this->db->query($query);
?>
Alternatively, you can use CASE construct in UPDATE statement. Then the query would be something
like:
UPDATE tbl_coupons
SET code = (CASE id WHEN 1 THEN 'ABCDE'
WHEN 2 THEN 'GHIJK'
WHEN 3 THEN 'EFGHI'
END)
WHERE id IN(1, 2 ,3);

how to use order by random in codeigniter

While Searching I want to show the paid customers firstly, and then rest of customers list
but the problem is I want to show by random
for Example. paid customers should not mix with others.
Can anyone tell what will be query?
please help me!
I am using codeigniter
Example:
function randomval()
{
$this->db->order_by('id', 'RANDOM');
$this->db->limit(1);
$query = $this->db->get('tblname');
return $query->result_array();
}
Mysql query for your need if i understood well
SELECT `featured`,group_concat(`id` order by rand() ) as `id` FROM `dbc_posts` where `status` = 1 GROUP By `featured` ORDER BY `featured` DESC
now with php
$results = $this->db->query("SELECT `featured`,group_concat(`id` order by rand() ) as `id` FROM `dbc_posts` where `status` = 1 GROUP By `featured` ORDER BY `featured` DESC")->result_array();
$paid = $results[0];//featured = 1
// comma seprated ids of the paid people e.g :- 3,7,1,26,92 are available in
$paidusers = $results[0]["id"];
//seprate them by
$paidusers = explode(",",$paidusers);
foreach($paidusers as $paiduser)
{
$row = $this->db->get_where("dbc_posts", array("id"=> $paiduser))->row();
print_r($row );
echo "<br>";
}
// do same for unpaid
$unpaid = $results[1];//featured = 0
$unpaidusers = $results[1]["id"];
//seprate them by
$unpaidusers = explode(",",$unpaidusers);
foreach($unpaidusers as $unpaiduser)
{
$row = $this->db->get_where("dbc_posts", array("id"=> $unpaiduser))->row();
print_r($row );
echo "<br>";
}
Ask me if anything goes wrong
Set featured = 1 for paid and featured = 0 for unpaid customers in database. Then use the query
mysql_query("SELECT * FROM dbc_posts WHERE status = 1 ORDER BY featured DESC, RAND() LIMIT 1");

Doctrine create subquery with bound params

I am trying to construct a query that will basically pull all entries whose id's have 2 particular entries in another table so I am trying the below:
$query = $this->createQuery('e');
$subquery1 = $query->createSubquery('sea')
->select('sea.entry_id')
->from('Table sea')
->addWhere('sea.form_element_id = ?', $element)
->addWhere('sea.answer = ?', $answer)
->getDQL();
$subquery2 = $query->createSubquery('sea2')
->select('sea2.entry_id')
->from('Table sea2')
->addwhere('sea2.form_element_id = ?', $element2)
->addWhere('sea2.answer = ?', $answer2)
->getDQL();
$query->addWhere('e.id IN ?', $subquery1)
->addWhere('e.id IN ?', $subquery2);
return $query->execute();
However this is gives me an error on bound params.
What is the correct ways of constructing such subqueries?
NOTE that if I dont bind the params in the subqueries it works fine.
$nestedQuery = " id IN (SELECT sea.entry_id from table sea where sea.form_element_id = ? and sea.answer = ?) "
. " and id IN (SELECT sea2.entry_id from table sea2 where sea2.form_element_id = ? and sea2.answer = ?)";
return $this->findBySql($nestedQuery, array($param1, $param2, $param3, $param4));
That obviously returns a doctrine collection but you can do getFirst or loop through the returned objects or even use the Hydrator to get an array!

can put alias in fulltext query?

This is FULLTEXT query, searching food from restaurants-menu
$query = "SELECT *, MATCH(food_row) AGAINST('cheese' IN BOOLEAN MODE) AS yyy
FROM table_menu
WHERE MATCH(food_row) AGAINST('cheese' IN BOOLEAN MODE)
AND restaurant IN (SELECT id FROM table_restaurant
WHERE country IN ('FR') AND town='paris')";
I want to modify. Considering Max distance = 5km. this is working.
latitude and longitude are of user (google geocode), lat and lng of restaurants in mysql (they are in table_restaurant)
$xxx = " (((acos(sin((".$latitude."*pi()/180)) *
sin((`lat`*pi()/180))+cos((".$latitude."*pi()/180)) *
cos((`lat`*pi()/180)) * cos(((".$longitude."- `lng`)
*pi()/180))))*180/pi())*60*1.1515*1.609344) ";
$query .= " AND ($xxx <= 5 )"; // 5 = the max distance
yes working, but I can't write each restaurant distance in the output.
I don't know how insert 'AS distance' If not FULLTEXT, would be easy.
$query = "SELECT *, $xxx as distance FROM table2 WHERE ... ORDER BY distance ";
output: distance = row['distance']
how can I do? Sorry for my english, tanks
I assume the reason you're modifying the query is because the distance test is optional.
Try it like this:
if ($max_dist) {
$dist = " (((acos(sin((".$latitude."*pi()/180)) *
sin((`lat`*pi()/180))+cos((".$latitude."*pi()/180)) *
cos((`lat`*pi()/180)) * cos(((".$longitude."- `lng`)
*pi()/180))))*180/pi())*60*1.1515*1.609344) ";
$having = "HAVING distance < $max_dist";
} else {
$dist = " NULL ";
$having = '';
}
$query = "SELECT m.*, MATCH(food_row) AGAINST('cheese' IN BOOLEAN MODE) AS yyy,
$dist AS distance
FROM table_menu m
JOIN table_restaurant r ON m.restaurant = r.id
WHERE MATCH(food_row) AGAINST('cheese' IN BOOLEAN MODE)
AND country IN ('FR') AND town='paris'
$having";

How to speed this query up?

I'm using yii framework to create a website takes care of poems and words and stuff...
the problem is that the database has more than 250,000 record of messages and more than 500,000 records of keywords and relationships :S
what I'm trying to say that I want to make an optimal query to get all related messages of a specific message depending on key-tags..
what I did so far is caching the query, but it's still slow when opening it for the first time and that's a big problem!
here is my code:
public function getRelatedMessages()
{
$id = $this->id;
$dependency = "select `messages`.id,count(id) as tag_count from messages left join `messages_tags` on `messages`.`id` = `messages_tags`.mid
where `messages_tags`.`tagid` in (select `tags`.`id` from tags left join `messages_tags` on tags.id = `messages_tags`.tagid
where `messages_tags`.`mid` = {$id}) and id <> {$id} group by id order by tag_count desc";
$dependency = Messages::model()->cache(900)->findAllBySql($dependency);
foreach ($dependency as $dependency) {
$dependency1[] = $dependency['id'];
}
$dependency = implode(", ", $dependency1);
$sql = "select * from messages where id in ({$dependency}) limit 4";
$relateds = Messages::model()->findAllBySql($sql);
$db = array();
foreach($relateds as $related) {
$db[] = $related;
}
if(!$relateds || count($db)<4) {
$limit = 4-count($db);
$sql = "select `messages`.id, `messages`.url_key, `messages`.photo_url, `messages`.message from messages order by rand(), likes desc, comments desc, impressions desc, reported asc limit {$limit};";
$relateds = Messages::model()->cache(900)->findAllBySql($sql);
foreach($relateds as $related) {
$db[] = $related;
}
}
return $db;
}
the code above selects only 4 records from the related messages after filtering them and ordering them and stuff.. the problem is with "order by" but I need it :S
sorry if that was too long..
thank you :)
You could improve the first query by not using findAllBySql() and instantiating all the ActiveRecord classes. You don't need them, so use plain sql query like this (query stays the same but code around has changed):
// ...
$dependency = $this->dbConnection->createCommand("
select `messages`.id, count(id) as tag_count
from messages left join `messages_tags` on `messages`.`id` = `messages_tags`.mid
where
`messages_tags`.`tagid` in
(select `tags`.`id` from tags left join `messages_tags` on tags.id = `messages_tags`.tagid where `messages_tags`.`mid` = {$id})
and id <> {$id}
group by id order by tag_count desc")->queryColumn();
$dependency = implode(", ", $dependency);
$sql = "select * from messages where id in ({$dependency}) limit 4";
$relateds = Messages::model()->findAllBySql($sql);
// ...

Resources