mysql where condition not working? - php

I have written my php code in codeigniter but when I set $limit = 1, still i get records that have seen = 1 why??
i have written a mysql query in pure sql but the problem remains !
here is my code :
function getComment($limit)
{
while( !$this->checkFuncState('getComment') );
$this->lockFunc('getComment');
$grade = $this->session->userdata('grade');
$this->db->select('*');
$this->db->select('m_comments.id AS `ComId`');
$this->db->select('m_comments.approved AS `Comappr`');
$this->db->select('m_comments.content AS `ComContent`');
$this->db->select('m_users.hid AS `UserHid`');
$this->db->from('m_comments');
$this->db->join('m_users','m_comments.user_id = m_users.id');
if( $grade == "0" || $grade == "1" )
$this->db->where("( m_comments.approved = 0 || m_comments.approved = -15 )");
else
$this->db->where('m_comments.approved','0');
$this->db->where('m_comments.seen','0');
$this->db->limit($limit);
$q = $this->db->get();
$this->setComsSeen($q);
$this->unlockFunc('getComment');
return $q;
}

Related

Codeigniter returns wrong "pagination" results

I have this helper method which job should be to prepare pagination data in order to retrieve it on controller...
Basically this is the code which happens in helper
if (!isset($_GET['page'])) {
$_GET['page'] = 1;
}
if (!isset($_GET['per_page'])) {
$_GET['per_page'] = 5;
}
$results = $ci->$model->get('', $_GET['per_page'], $_GET['page']);
And this is my model which should return data
public function get($tableName = "", $limit = null, $start = null)
{
if ($tableName == "") {
$tableName = $this->table;
}
if ($limit != null && $start != null) {
// problem is here with limit and start which returns wrong data
$this->db->limit($limit, $start);
$query = $this->db->get($tableName);
var_dump($query->result());
die();
}
} else {
$query = $this->db->get($tableName);
}
return $query->result();
}
Problem is that data returned from model isn't correct and i can't figure out how to get properly data based on page number and items per page....
So in my case if i request data with paramas $_GET['page'] = 1 and $_GET['per_page'] = 5 it will return 5 records, but starting with record 2 till record 6. So my question is how to properly request give me let say 5 records on starting page 1 and then give me another 5 records on page 2 ETC....
If you need any additional information please let me know and i will provide. Thank you
The problem lies within your $start variable. You should remember that when using getting the first the 5 records, you should use an offset 0 instead 1. Counting starts from 0 remember :)
The code should be
public function get($tableName = "", $limit = null, $start = null)
{
if ($tableName == "") {
$tableName = $this->table;
}
if ($limit != null && $start != null) {
// problem is here with limit and start which returns wrong data
//$this->db->limit($limit, $start);
// use this instead
$this->db->limit($limit, ( $start - 1 ) * $limit );
$query = $this->db->get($tableName);
var_dump($query->result());
die();
}
} else {
$query = $this->db->get($tableName);
}
return $query->result();
}
This is working example try to do like this: https://www.formget.com/pagination-in-codeigniter/

Wordpress Paginate with additional Parameters

The code below works great for paginating my custom post search results:
if( isset($_GET['pagination']) && $_GET['pagination'] != 'false'){
$arguments['posts_per_page'] = -1;
$pagination = 'true';
} else {
$arguments['posts_per_page'] = 9;
$pagination = 'false';
$arguments['order'] = 'ASC';
$arguments['meta_key'] = 'code_auto';
$arguments['orderby'] = 'meta_value';
}
However, if someone has filtered the results using this:
if( isset($_GET['sort_price'])){
$sort_price = $_GET['sort_price'];
if($sort_price == 'ASC'){
$arguments['order'] = 'ASC';
$arguments['meta_key'] = 'price-6';
$arguments['orderby'] = 'meta_value';
} else {
$sort_price = '';
$arguments['order'] = 'DESC';
$arguments['orderby'] = 'DATE';
}
}
The results paginate but don't include the sort_price query. Can anyone give any rewrites to include both arguments as i have tried many times but cant quite get it. Thank you!

SQL unable to insert data into database

My SQL code is not inserting in the table. Does someone know what i do wrong?
SQL/PHP Code:
$ownerid = $input->FilterText($_GET['ownerId']);
$widgetid = $input->FilterText($_GET['ratingId']);
$rate = $input->FilterText($_GET['givenRate']);
}
if(is_numeric($ownerid) && is_numeric($widgetid) && is_numeric($rate)){
$myvote = $db->result($db->query("SELECT COUNT(*) FROM ".PREFIX."ratings WHERE raterid = '".$user->id."' AND userid = '".$ownerid."'"));
if($myvote < 1 && $ownerid != $user->id && $rate > 0 && $rate < 6){
$db->query("INSERT INTO cms_ratings (userid,rating,raterid) VALUES ('".$ownerid."','".$rate."','".$user->id."')");
}
Hope someone can help me with this problem. Its not inserting # database.

php parameters passed inside function is not working

In php/wordpress I have made a function. I want to pass some parameteres inside the function so that it will show result according to that. So far now my function code is like this
$user_id = get_current_user_id();
function check_user_access($role, $action = NULL ) {
if( $role == 'subscriber') {
if( $action = 'check_customer' ) {
$check_customer = $wpdb->get_var("SELECT COUNT(id) FROM `table1` WHERE `user_id` = $user_id");
return $check_customer;
}
if( $action = 'check_users' ) {
$check_users = $wpdb->get_var("SELECT COUNT(id) FROM `table2` WHERE `user_id` = $user_id");
return $check_users;
}
}
}
Now I am using this function like this
$role = 'subscriber';
$check_customers = check_user_access($role, $action = 'check_users' );
if( $check_users <=1 ) {
//do something;
}
if( $check_users > 1 ) {
//do something other;
}
But its showing the result of $action = 'check_customer'. Means its working for the first block condition. Can someone tell me how to solve this? Am I doing something wrong?
change your
if( $action = 'check_customer' ) {}
to
if( $action == 'check_customer' ) {}
= means Assignment Operator
== means Comparison Operator
refer - from here

Codeigniter Search Query doesnt work correctly

Basically works by displaying a form where the user can input 2 variables $no and $first but i can only get it to display a result when i enter both fields. I need it to work so when i enter 1 variable it will display results.
I thought i could use some sort of OR statement but i'm unsure how to implement it. Any help would be appreciated and i apologize if its not clear enough i'm fairly new when it comes to codeigniter.
Controller
public function query()
{
$no = $this->input->post('no');
$first = $this->input->post('first');
$this->load->model("test");
$data['query']=$this->test->query($no,$first);
$this->load->view('query',$data);
}
Model
function query($no, $first)
{
return $query = $this->db->get_where('table', array('no' => $no,
'first' => $first ))->result();
}
View
<?php foreach($query as $row): ?>
<tr>
<td><?php echo $row->no; ?></td>
<td><?php echo $row->date; ?></td>
<td><?php echo $row->first; ?></td>
</tr>
<?php endforeach; ?>
You can try preparing the where clause first
function query($no = "", $first = "")
{
$response = array();
if($first != "" || $no != ""){
$where = "";
if($no == "") $where = 'first = '.$first;
if($first == "") $where = 'no = '.$no;
if($first != "" && $no != "") $where = 'no = '.$no.' AND first = '.$first;
$fetch = $this->db->where($where)->from('table')->get();
if($fetch->num_rows() > 0) $response = $fetch->result_array();
}
return $response;
}
Hope I get the idea you want.
You need to separate out your WHERE arguments:
function query($no, $first)
{
$this->db->select();
$this->db->from('table');
$this->db->where('no', $no);
if (!empty($first))
{
$this->db->or_where('first', $first); // or use `and_where`
}
return $query = $this->db->get()->result();
}
Source: http://ellislab.com/codeigniter/user-guide/database/active_record.html
function query($no = '%', $first = '%')
{
$this->db->where("'no' = $no OR 'first' = $first");
$query = $this->db->get('table');
if($query->num_rows() >= 0)
{
$return $query->result();
}
}
If you feed the values a wild card as default values either can be empty (in fact both can be empty to return all results) then just build the entire where statement inside double quotes.
You really don't have to feed the wildcards if you would rather not return all results if the strings are empty, but you should be doing some checking somewhere for empty results or no return.

Categories