I'm using smarty pagination My pagination URL look like this
categories.php?id=1&next=3
Now {paginate_prev} & {paginate_next} is working properly
but {paginate_middle} is stripping ID parameter it is generating url like this
categories.php?next=3
My pagination code is here
function get_db_results() {
global $conn;
$id = htmlspecialchars((int)$_GET['id']);
if ($id){
$_query = sprintf('SELECT SQL_CALC_FOUND_ROWS * FROM newser where idblog = '.$conn->qstr($id).' and (main = '.$conn->qstr('0').' or main = '.$conn->qstr('2').') ORDER BY blogid DESC LIMIT %d,%d',
SmartyPaginate::getCurrentIndex(),SmartyPaginate::getLimit());
}
else
{
$_query = sprintf('SELECT SQL_CALC_FOUND_ROWS * FROM newser where (main = '.$conn->qstr('0').' or main = '.$conn->qstr('2').') ORDER BY blogid DESC LIMIT %d,%d',
SmartyPaginate::getCurrentIndex(),SmartyPaginate::getLimit());
}
$brecordSet = $conn->Execute($_query);
if(!$brecordSet)
print $conn->ErrorMsg();
else
while(!$brecordSet->EOF) {
$_data[] = $brecordSet->GetRowAssoc(false);
$brecordSet->MoveNext();
}
$_query = "SELECT FOUND_ROWS() as total";
$crecordSet = $conn->Execute($_query);
if(!$crecordSet)
print $conn->ErrorMsg();
else
$_row = $crecordSet->GetRowAssoc();
$total = $crecordSet->fields['total'];
SmartyPaginate::setTotal($total);
return #$_data;
$brecordSet->Close();
$crecordSet->Close();
}
require ('libs/SmartyPaginate.class.php');
SmartyPaginate::connect();
SmartyPaginate::setLimit(4);
SmartyPaginate::setUrl('categories.php');
$smarty->caching = $caching;
$smarty->assign('results',get_db_results());
SmartyPaginate::assign($smarty);
$id = htmlspecialchars((int)$_REQUEST['id']);
#$next = htmlspecialchars((int)$_REQUEST['next']);
$cid = $id.$next;
$smarty->display('categories.php',$cid);
Please help me understand if there is any problem with my code or it a bug with smarty {paginate_middle} which is not handeling multiple parameters
I don't use SmartyPaginate at all but it seems you should change at least this line:
SmartyPaginate::setUrl('categories.php');
into
SmartyPaginate::setUrl('categories.php?id=1');
Related
This is my code below:
public function listParkedAccounts($days = NULL)
{
global $db;
$days_db = (empty($days))?"7":$days;
$stmt = "CALL parked_accounts('.$days_db.')";
$result = $db->query($stmt,true,'Failed to fetch aged author record');
$aged_authors = mysqli_fetch_all($result,MYSQLI_ASSOC);
//mysql_free_result($result); tried this
$counter = 0;
$data = null;
foreach($aged_authors as $authors){
if(empty($authors['call_descp'])){
$stmt_notes = "SELECT description FROM notes WHERE description IS NOT NULL AND parent_id = '".$authors['call_id']."' AND parent_type = 'Calls' ORDER BY date_entered DESC LIMIT 1";
$notes_descp = $db->getOne($stmt_notes, TRUE, 'Error retrieving call notes');
$notes = (!empty($notes_descp))?$notes_descp[0]['description']:"No Notes";
}else{
$notes = (!empty($authors['call_descp']))?$authors['call_descp']:"No Notes";
}
$lead = 'stuff';
$data[$counter]['lead_id'] = $lead;
$data[$counter]['call_notes'] = $notes;
$counter++;
}
$size = sizeof($data);
$json_vals['draw'] = "1";
$json_vals['recordsTotal'] = $size;
$json_vals['recordsFiltered'] = $size;
$json_vals['data'] = ($size > 0)?$data:array();
return $json_vals;
}
My problem here is this error message:
Error retrieving call notes Query Failed: SELECT description FROM
notes WHERE description IS NOT NULL AND parent_id =
'123' AND parent_type = 'Calls' ORDER
BY date_entered DESC LIMIT 1: MySQL error 2014: Commands out of sync;
you can't run this command now
What I understood from reading this is that I need to free the results or store them but when I tried those I still got the same error message. Though I am not quite sure where I should be freeing the results or if I am even doing them correctly. This is my first time using the stored procedure.
When you call a stored procedure, there is a possibility that the stored procedure contains multiple result sets that you need to process. If you only process the first result set and go ahead and try to execute another query with the same connection, you will get the
"Commands out of sync"
error.
To fix the issue, instead of the $db->query + mysqli_fetch_all (btw try not to mix object oriented style and procedural style) you should use multi_query.
An example how to handle multiple result sets with rows is shown in the PHP documentation:
http://php.net/manual/en/mysqli.multi-query.php
I have changed few lines as following:
$notes_descpRes = $db->query($stmt_notes);
$notes_descp = $db->fetchByAssoc($notes_descpRes);
$notes = (!empty($notes_descp))?$notes_descp['description']:"No Notes";
Following is the complete code:
public function listParkedAccounts($days = NULL)
{
global $db;
$days_db = (empty($days))?"7":$days;
$stmt = "CALL parked_accounts('.$days_db.')";
$result = $db->query($stmt,true,'Failed to fetch aged author record');
$aged_authors = mysqli_fetch_all($result,MYSQLI_ASSOC);
//mysql_free_result($result); tried this
$counter = 0;
$data = null;
foreach($aged_authors as $authors){
if(empty($authors['call_descp'])){
$stmt_notes = "SELECT description FROM notes WHERE description IS NOT NULL AND parent_id = '".$authors['call_id']."' AND parent_type = 'Calls' ORDER BY date_entered DESC LIMIT 1";
$notes_descpRes = $db->query($stmt_notes);
$notes_descp = $db->fetchByAssoc($notes_descpRes);
$notes = (!empty($notes_descp))?$notes_descp['description']:"No Notes";
}else{
$notes = (!empty($authors['call_descp']))?$authors['call_descp']:"No Notes";
}
$lead = 'stuff';
$data[$counter]['lead_id'] = $lead;
$data[$counter]['call_notes'] = $notes;
$counter++;
}
$size = sizeof($data);
$json_vals['draw'] = "1";
$json_vals['recordsTotal'] = $size;
$json_vals['recordsFiltered'] = $size;
$json_vals['data'] = ($size > 0)?$data:array();
return $json_vals;
}
Give it a try.
I have a code that works when I use it on a page but Im trying to make this a function. I cant get it to work, it seems like the variables $customer and $system arent being sent through to the code. Even if I type it in Raw. Any idea whats wrong? $Customer is the name of the customer, $system can be 'Source' or 'Target'.
function status_total($customer, $system){
$sql_customer = "SELECT * FROM `Customer` WHERE Cust_Name = '$customer' LIMIT 0,1";
$customer_selection = mysqli_query($conn,$sql_customer);
$customer_row = mysqli_fetch_assoc($customer_selection);
$env_lines = $customer_row["Env_Lines"];
$cust_id = $customer_row["Cust_ID"];
$sql_last_records = "SELECT * FROM $system WHERE Cust_ID = $cust_id ORDER BY Time DESC LIMIT $env_lines";
$record_selection = mysqli_query($conn, $sql_last_records);
$result = mysqli_fetch_all($record_selection, MYSQLI_ASSOC);
$states = array_column($result, "Stat");
if($states == array_fill(0, count($states), "Run")) {
echo "Success";
} else
echo "Fail";
}
https://gist.github.com/R2D2-05/78d81566e4bf0eafd1fa
The problem with your code is $conn variable which is treated a local variable inside a function. You should:
function status_total($customer, $system){
global $conn;
$sql_customer = "SELECT * FROM `Customer` WHERE Cust_Name = '$customer' LIMIT 0,1";
$customer_selection = mysqli_query($conn,$sql_customer);
$customer_row = mysqli_fetch_assoc($customer_selection);
$env_lines = $customer_row["Env_Lines"];
$cust_id = $customer_row["Cust_ID"];
$sql_last_records = "SELECT * FROM $system WHERE Cust_ID = $cust_id ORDER BY Time DESC LIMIT $env_lines";
$record_selection = mysqli_query($conn, $sql_last_records);
$result = mysqli_fetch_all($record_selection, MYSQLI_ASSOC);
$states = array_column($result, "Stat");
if($states == array_fill(0, count($states), "Run")) {
echo "Success";
} else
echo "Fail";
}
Or you can also pass the $conn through the function, so change the function's definition to:
function status_total($conn, $customer, $system){...}
I'm having a hard time getting this search results with pagination code to work. It does successfully grab the search keyword entered in the html form on another page and brings it into this search.php page. if I echo $search I see the keyword on the page. But I get no results even though I should for the query. Can anyone see what might be going on?
require "PDO_Pagination.php";
if(isset($_REQUEST["search_text"]) && $_REQUEST["search_text"] != "")
{
$search = htmlspecialchars($_REQUEST["search_text"]);
$pagination->param = "&search=$search";
echo $search;
$pagination->rowCount("SELECT * FROM stories WHERE stories.genre = $search");
$pagination->config(3, 5);
$sql = "SELECT * FROM stories WHERE stories.genre = $search ORDER BY SID ASC LIMIT $pagination->start_row, $pagination->max_rows";
$query = $connection->prepare($sql);
$query->execute();
$model = array();
while($rows = $query->fetch())
{
$model[] = $rows;
}
}
else
{
$pagination->rowCount("SELECT * FROM stories");
$pagination->config(3, 5);
$sql = "SELECT * FROM stories ORDER BY SID ASC LIMIT $pagination->start_row, $pagination->max_rows";
$query = $connection->prepare($sql);
$query->execute();
$model = array();
while($rows = $query->fetch())
{
$model[] = $rows;
}
}
$query = "SELECT * FROM stories";
if(isset($_REQUEST["search_text"]) && $_REQUEST["search_text"] != "")
{
$search = htmlspecialchars($_REQUEST["search_text"]);
$pagination->param = "&search=$search";
$query .= " WHERE genre LIKE '%$search%'";
}
// No need for else statement.
$pagination->rowCount($query);
$pagination->config(3, 5);
$query .= " ORDER BY SID ASC LIMIT {$pagination->start_row}, {$pagination->max_rows}";
$stmt = $connection->prepare($query);
$stmt->execute();
$model = $stmt->fetchAll();
var_dump($model);
In your query do:
WHERE stories.genre LIKE '%string%');
instead of:
WHERE stories.genre = 'string');
Because the equals will want to literally equal the field.
i inherited this code from some-one and this only returns the first row it finds:
function getMessages($userID, $from, $limit)
{
$id = $_SESSION['user_id'];
$sql = "SELECT * FROM rc_message_box_table WHERE profile_id_to = {$userID} AND rc_message_box_table.profile_id_from NOT IN (SELECT profile_id_block FROM rc_blocklist_table WHERE profile_id = {$id}) LIMIT {$from}, {$limit}";
$row = $this->aFetch($sql);
return $row;
}
function aFetch($sSql)
{
//print_r($sSql);
$aResults = array();
if(is_string($sSql))
{
$fnSql = $this->query($sSql);
}
while($row = mysql_fetch_assoc($fnSql))
{
$aResults[] = $row;
}
return $aResults;
}
how can i with this code return all rows where profile_id_to = {$userID} ??
thanks
function aFetch($result = false) {
if($result)
return mysql_fetch_assoc($result);
else
return mysql_fetch_assoc($this->result);
}
you can use this function.
Try removing the LIMIT clause:
SELECT *
FROM rc_message_box_table
WHERE profile_id_to = {$userID}
AND rc_message_box_table.profile_id_from NOT IN
(
SELECT profile_id_block
FROM rc_blocklist_table
WHERE profile_id = {$id}
)
Your result may be limited by $from, $limit parameters.
Look at the code where getMessages is being invoked, and analyse what values it is passing.
I am building a Links voting site, the formula works ok after the link is voted the second time, the problem is that when the link just has 1 vote it shows up backwards, from oldest to newest.
What I want is for links with one vote to show from newest to oldest. This is the line that calls the links in the front page:
$articles = Article::getAll("order by ranking desc limit $offset, $num_items");
This is the getAll function code:
static function getAll($conditions = ' ')
{
/* Retrieve all the records from the
* database according subject to
* conditions
*/
$db = null;
$results = null;
$records = array();
$query = "select id, created, modified, username, url, title, description, points, ranking from articles $conditions";
try
{
$db = parent::getConnection();
$results = parent::execSql($query);
while($row = $results->fetch_assoc())
{
$r_id = $row['id'];
$r_created = $row['created'];
$r_modified = $row['modified'];
$r_title = $row['title'];
$r_description = $row['description'];
if(!get_magic_quotes_gpc())
{
$r_title = stripslashes($r_title);
$r_description = stripslashes($r_description);
}
$r_url = $row['url'];
$r_username = $row['username'];
$r_points = $row['points'];
$r_ranking = $row['ranking'];
$article = new Article($r_title, $r_description , $r_url, $r_username, $r_created, $r_modified);
$article->id = $r_id;
$article->points = $r_points;
$article->ranking = $r_ranking;
$records[] = $article;
}
parent::closeConnection($db);
}
catch(Exception $e)
{
throw $e;
}
return $records;
}
If anyone can help I would appreciate it.
What about adding the created date to the order clause?
$articles = Article::getAll("order by ranking desc, created desc limit $offset, $num_items");
I'll do what David says, just that if you want the links ordered by the newest first then you have to add the "created" column in descending order:
$articles = Article::getAll("order by ranking desc, created DESC limit $offset, $num_items");