pagination not working properly in searching - php

I have created some code of searching and pagination in it. If a user wants to search using some industry then data is shown against it. But whenever he presses any option in the pagination then it shows all the data related to every industry. How can i show pagination related to search result. And if user has selected any industry then it should be selected if user move to page 2 or page 3 or any.
Code of page
<?php
$pd=(isset($script['details']) ? $script['details']:array()) ;
//jobs search and normal page view
$qstr=Querystring();
$obj_pagination = new Pagination();
$obj_pagination->limit = (($FP_LIMIT!="") ? $FP_LIMIT:10);
$pagid=1;
if(isset($qstr["page"])) {
$pagid=$qstr["page"];
}
$obj_pagination->page = $pagid;
$srcqry="";
$industry=0;
if(isset($_POST['industry'])){
if(isset($_POST['industry'])){
if($_POST['industry']!="0") {
$srcqry=$srcqry." AND j.industry ='".(int)$_POST['industry']."'";
$industry=(int)$_POST['industry'];
}
}
$nrs = $obj->query("SELECT * FROM jobs j WHERE j.status='1' ".$srcqry);
} else {
$nrs = $obj->query("SELECT * FROM jobs j WHERE j.status='1' ".$srcqry);
}
$tot_rec= count($nrs->rows);
$obj_pagination->total = $tot_rec;
$obj_pagination->url = "jobs?page={page}";
$num_pages = ceil($obj_pagination->total / $obj_pagination->limit);
$start = ($obj_pagination->page-1)*$obj_pagination->limit;
$end = $obj_pagination->limit;
$limit = "LIMIT $start,$end";
$check;
?>
<h3>Search</h3>
<form name="srchjobs" action="/jobs" method="post">
<div class="col1">
<select class="dropdown1 spacer_side" name="industry">
<option value="0">All Industries</option>
<?php $indrow=getAllIndustries();
if(count($indrow) > 0) {
foreach($indrow as $tr) {?>
<option value="<?php echo $tr['id']; ?>" <?php if($tr['id']==$industry) { ?> selected="selected"<?php } ?>><?php echo $tr['name']; ?></option>
<?php }
$check=$tr['name'];
}
?>
</select>
</div>
<button type='submit'>Search</button>
</form>
<?php
$indrow=getAllIndustries();
$sql="SELECT j.*,u.url FROM jobs as j, aliases as u WHERE j.status='1' ".$srcqry."
AND (j.id=u.slog_id AND u.slog='jobs') ORDER BY j.id DESC ".$limit;
$jresults = $obj->query($sql);
if($jresults->rows) {
foreach($jresults->rows as $j){
$empd=array();
$empd=getIndinfo($j['industry']);
$emplogo="/uploads/no-image.png";
?>
<img src="<?php echo $emplogo;?>" alt="dell1" height="66" width="66" />
<h5><?php echo $j['title']; ?></h5>
APPLY
<?php } ?>
<ul class="pagination">
<?php
$obj_pagination->text_next = ">";
$obj_pagination->text_prev = "<";
$obj_pagination->text_first = "«";
$obj_pagination->text_last = "»";
$obj_pagination->text = "Showing {start} to {end} of {total}";
echo $obj_pagination->render();
?></ul>
<?php } else { ?>
<span class="latest_job">No jobs Found</span>
<?php } ?>
querystring function code
function Querystring(){
$_G=array();
$REQUEST_URI = $_SERVER['REQUEST_URI'];
$REQUEST_URI_ARR = explode("?",$REQUEST_URI);
$REQUEST_URI_ARR_All = explode("&",$REQUEST_URI_ARR[1]);
foreach($REQUEST_URI_ARR_All as $key=>$val){
$gs = explode("=",$val);
if(isset($gs[0]) && isset($gs[1]))
$_G[$gs[0]] = urldecode($gs[1]);
}
return $_G;
}

Output your results in a table and just use a jquery enabled table class. That way pagination will be automatic. Ensure you have a tag as well.

Related

PHP/HTML Button works only in the first element from a loop

I am learning PHP and am stuck right now. In a while loop, all the to-do items are displayed. Each of these elements has a delete button and a check button. The delete button works in every element, but the check button only works in the first one. The check button is to be used to check off an element. When the check button is pressed, an error message is displayed that this element could not be deleted. Only in the first element of course not. I can't find the error.
I suspect there is something wrong with the in the index
<?php
$todos = $conn->query("SELECT * FROM todos ORDER BY id DESC");
?>
<div class="show-todo-section">
<?php if($todos->rowCount() <= 0){ ?>
<div class="todo-item">
<div class="empty">
<img src="img/f.png" width="100%" />
<img src="img/Ellipsis.gif" width="80px">
</div>
</div>
<?php } ?>
<?php while($todo = $todos->fetch(PDO::FETCH_ASSOC)) { ?>
<div class="todo-item">
<form action="app/checked.php" method="POST">
<button class="check-box" value="<?php echo $todo['id']; ?>" name='checked' type="submit">Check</button>
</form>
<form action="app/delete.php" method="POST">
<button class="remove-to-do" value="<?php echo $todo['id']; ?>" name='delete' type="submit">x</button>
</from>
<?php if($todo['checked']){ ?>
<h2 class="checked"><?php echo $todo['title'] ?></h2>
<?php }else { ?>
<h2><?php echo $todo['title'] ?></h2>
<?php } ?>
<small>created: <?php echo $todo['date_time'] ?></small>
<?php for ($i = 1; $i <= $todo['coins']; $i++) {
echo "<img class='coins' src='img/coin.gif' width='40px' />";
} ?>
</div>
<?php } ?>
</div>
the checked.php
<?php
if (isset($_POST['checked'])) {
require '../db_conn.php';
$id = $_POST['checked'];
if(empty($id)){
header("Location: ../index.php?mess=errorChecked". $id);
}else{
$todos = $conn->prepare("SELECT id, checked FROM todos WHERE id=?");
$todos->execute([$id]);
$todo = $todos->fetch();
$uId = $todo['id'];
$checked = $todo['checked'];
$uChecked = $checked ? 0 : 1;
$res = $conn->query("UPDATE todos SET checked=$uChecked WHERE id=$uId");
header("Location: ../index.php?mess=successChecked");
$conn = null;
exit();
}
} else{
header("Location: ../index.php?mess=errorChecked");
}

How to avoid blank first result when searching from databse

I have search webpage, but when the search is run, there is a blank first result.
<?php include "headnav.php";
$count = 0;
$sql = "SELECT * FROM lost_property";
if (!empty($_POST)) {
$name = mysqli_real_escape_string($dbconnect, htmlspecialchars($_POST['name']));
$item = mysqli_real_escape_string($dbconnect, htmlspecialchars($_POST['item']));
$area = mysqli_real_escape_string($dbconnect, htmlspecialchars($_POST['area']));
$sql = "
SELECT *
FROM lost_property
JOIN item
ON lost_property.itemID = item.itemID
JOIN area
ON lost_property.areaID = area.areaID
WHERE name LIKE '%$name%'
AND item LIKE '%$item%'
AND area LIKE '%$area%'
ORDER
BY lost_property.name ASC
";
$search_query = mysqli_query($dbconnect, $sql);
$count = mysqli_num_rows($search_query);
}
$result = $dbconnect->query($sql);
?>
<body>
<div class="form">
<h1>Search for lost property here:</h1>
<form action="" method="POST">
Name:
<input type="text" placeholder="Name" name="name">
Item type:
<select name="item" class="dropdown">
<option value="" disabled selected>Item</option>
<?php
$item_sql = "SELECT DISTINCT item FROM `lost_property`
JOIN item ON (lost_property.itemID = item.itemID)
ORDER BY item ASC
";
$item_query = mysqli_query($dbconnect, $item_sql);
$item_rs = mysqli_fetch_assoc($item_query);
do {
?>
<option value="<?php echo $item_rs['item']; ?>"><?php echo $item_rs['item']; ?></option>
<?php
} while ($item_rs = mysqli_fetch_assoc($item_query));
?>
</select>
Area (where it was found):
<select name="area" class="dropdown">
<option value="" disabled selected>Area</option>
<?php
$area_sql = "SELECT DISTINCT area FROM `lost_property`
JOIN area ON (lost_property.areaID = area.areaID)
ORDER BY area ASC
";
$area_query = mysqli_query($dbconnect, $area_sql);
$area_rs = mysqli_fetch_assoc($area_query);
do {
?>
<option value="<?php echo $area_rs['area']; ?>"><?php echo $area_rs['area']; ?></option>
<?php
} while ($area_rs = mysqli_fetch_assoc($area_query));
?>
</select>
<input type="submit" value="Search" name="btn">
</form>
</div>
<div class="gallery">
<h2>Search results:</h2>
<?php
//check for results. If there are none display error
if ($count < 1) {
?>
<div class="error">
<h1>No results were found.</h1>
</div>
<?php
} //end if
else {
do {
?>
<!-- display image and information from database and show in gallery -->
<div class="results">
<h3><?php echo $search_rs['name']; ?></h3>
<h3><?php echo $search_rs['item']; ?></h3>
<p><?php echo $search_rs['area']; ?></p>
</div>
<?php
} // end of do
while ($search_rs = mysqli_fetch_assoc($search_query));
} //end else
//if there are any display
?>
</div>
</table>
</body>
</html>
It's hard to see without any CSS, but no matter what is searched, there is always one result, but the h3 and p fields don't have any content. If there wasn't the no results error message it would pop up there too. What is causing this first result?
Use while (){}, if you use do instead it will run once first (credit to Magnus Eriksson).
It ends up like
else {
while ($search_rs = mysqli_fetch_assoc($search_query)) {
?>
<!-- display image and information from database and show in gallery -->
<div class="results">
<h3><?php echo $search_rs['name']; ?></h3>
<h3><?php echo $search_rs['item']; ?></h3>
<p><?php echo $search_rs['area']; ?></p>
</div>
<?php
} // end of do
} //end else
//if there are any display
?>
instead of
else {
do {
?>
<!-- display image and information from database and show in gallery -->
<div class="results">
<h3><?php echo $search_rs['name']; ?></h3>
<h3><?php echo $search_rs['item']; ?></h3>
<p><?php echo $search_rs['area']; ?></p>
</div>
<?php
} // end of do
while ($search_rs = mysqli_fetch_assoc($search_query));
} //end else
//if there are any display
?>

how to create a simple pagination in zend framework?

I have a project on which i need to implement pagination. I tried searching various time but couldn't find any satisfactory result. That's why I'm posting my question here --
This is Controller file IndexController.php
This is my indexAction, in which I'm trying to implement pagination -
public function indexAction() {
$this->_helper->layout->setLayout('layout');
//pagination
$page = $this->_request->getParam('page');
if (empty($page)) {
$page = 1;
}
$paginator = $this->_objCoupon->getOnePageOfOrderEntries($page);
$this->view->paginator = $paginator;
//pagination ends
$coupons = $this->_objCoupon->getAllcoupon();
$storage = new Zend_Auth_Storage_Session();
$dataUser = $storage->read();
$id = $dataUser->id;
//$id = $storage->id;
$arrCoupon = array();
if(count($coupons) > 0) {
$i= 0;
foreach($coupons as $couponArray) {
$arrCoupon[$i]["pid"] = $couponArray['id'];
$arrCoupon[$i]["title"] = $couponArray['partner_name'];
$arrCoupon[$i]["img"] = $couponArray['companylogo'];
$arrCoupon[$i]["value"] = $couponArray['coupon_value'];
$i++;
}
}
$this->view->arrCoupon = $arrCoupon;
$this->view->selectedcheckbox = $this->selectCheckboxes;
$this->view->id = $id;
$this->view->asd = 'all';
}
This is model file CouponModel.php
And this is my model function
public function getOnePageOfOrderEntries($page=1) {
//$paginator = $this->_coupon->getAllcoupon();
$query = "SELECT * FROM rechargecoupon WHERE online_offer = 'N' ORDER BY id ASC";
$paginator = new Zend_Paginator(
new Zend_Paginator_Adapter_DbTableSelect($query)
);
$paginator->setItemCountPerPage(20);
$paginator->setCurrentPageNumber($page);
return $paginator;
}
And in view I'm just trying to print paginator variable value for now.
This is View file index.phtml
<?php if (count($this->paginator)): ?>
<?php print_r($this->paginator); die; ?>
When I'm executing this file, I'm getting "Class Zend_Paginator_Adapter_DbTableSelect not found error".
You should have these variables in your action:
$currentPage = value obtained from URL, has to be >= 1
$itemsPerPage = hard coded value, ideally taken from configuration
$itemsCount = SELECT COUNT(*) FROM ... WHERE ...
$pagesCount = ceil($itemsCount / $itemsPerPage)
$items = SELECT * FROM ... WHERE ... LIMIT ($currentPage - 1) * $itemsPerPage, $itemsPerPage, these are items to be displayed, for example articles
When you set these variables in your action, you should pass them to your view and generate pagination from them, that means:
If pages count is larger than 1:
display pagination
generate pagination from 1 to $pagesCount = 1, 2, 3 ... 10
each generated pagination number should have associated URL to your action
If pages count is 1:
do not display pagination
First of all.
Correct to use
$query = $this->select()
->from('rechargecoupon')
->where('online_offer = ?', 'N')
->order('id ASC')
$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbTableSelect($query));
$paginator->setItemCountPerPage('50');
$paginator->setCurrentPageNumber($page);
This is in the model.
In view
<?php if ($this->paginator->getTotalItemCount() !== 0): ?>
// echo elements you need
<?php endif; ?>
<?php echo $this->paginationControl( $this->paginator, 'Sliding', 'pagination.phtml' ); ?>
pagination.phtml displays pagination.
in pagination.phtml
<?php if ($this->pageCount > 1): ?>
<div>
<!-- Previous page -->
<?php if (isset($this->previous)): ?>
<a href="<?php echo $this->url(array('page' => $this->previous)); ?>">
<span>< <?php echo 'previous'; ?></span>
</a>
<?php else: ?>
<span class="disabled">< <?php echo 'previous'; ?></span>
<?php endif; ?>
<!-- Page links -->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<a href="<?php echo $this->url(array('page' => $page)); ?>">
<span><?php echo $page; ?></span>
</a>
<?php else: ?>
<?php endif; ?>
<?php endforeach; ?>
<?php if(!in_array($this->pageCount, $this->pagesInRange)): ?>
<a href="<?php echo $this->url(array('page' => $this->pageCount)); ?>">
<span><?php echo $this->pageCount; ?></span>
</a>
<?php endif;?>
<?php if (isset($this->next)): ?>
<a href="<?php echo $this->url(array('page' => $this->next)); ?>">
<span><?php echo 'next'; ?> ></span>
</a>
<?php else: ?>
<span class="disabled"><?php echo 'next'; ?> ></span>
<?php endif; ?>
</div>
<?php endif; ?>

Show Ads In Middle Of Results

I'm currently using sphider on one of my websites, my questions is how can I break the results page into 2 parts to add a 200px break to place a ad slot.
Code:
<?php
extract($search_results);
?>
<?php if ($search_results['did_you_mean']){?>
<div id="did_you_mean">
<?php echo $sph_messages['DidYouMean'];?>: <?php print $search_results['did_you_mean_b']; ?>?
</div>
<?php }?>
<?php if ($search_results['ignore_words']){?>
<div id="common_report">
<?php while ($thisword=each($ignore_words)) {
$ignored .= " ".$thisword[1];
}
$msg = str_replace ('%ignored_words', $ignored, $sph_messages["ignoredWords"]);
echo $msg; ?>
</div>
<?php }?>
<?php if ($search_results['total_results']==0){?>
<div id ="result_report">
<?php
$msg = str_replace ('%query', $ent_query, $sph_messages["noMatch"]);
echo $msg;
?>
</div>
<?php }?>
<?php if ($total_results != 0 && $from <= $to){?>
<div id ="result_report">
<?php
$result = $sph_messages['Results'];
$result = str_replace ('%from', $from, $result);
$result = str_replace ('%to', $to, $result);
$result = str_replace ('%all', $total_results, $result);
$matchword = $sph_messages["matches"];
if ($total_results== 1) {
$matchword= $sph_messages["match"];
} else {
$matchword= $sph_messages["matches"];
}
$result = str_replace ('%matchword', $matchword, $result);
$result = str_replace ('%secs', $time, $result);
echo $result;
?>
</div>
<?php }?>
<?php if (isset($qry_results)) {
?>
<div id="results">
<!-- results listing -->
<?php foreach ($qry_results as $_key => $_row){
$last_domain = $domain_name;
extract($_row);
if ($show_query_scores == 0) {
$weight = '';
} else {
$weight = "[$weight%]";
}
?>
<?php if ($domain_name==$last_domain && $merge_site_results == 1 && $domain == "") {?>
<div class="idented">
<?php }?>
<b><?php print $num?>.</b> <?php print $weight?>
<?php print ($title?$title:$sph_messages['Untitled'])?><br/>
<div class="description"><?php print $fulltxt?></div>
<div class="url"><?php print $url2?> - <?php print $page_size?></div>
<?php if ($domain_name==$last_domain && $merge_site_results == 1 && $domain == "") {?>
[ More results from <?php print $domain_name?> ]
</div class="idented">
<?php }?>
<br/>
<?php }?>
</div>
<?php }?>
<!-- links to other result pages-->
<?php if (isset($other_pages)) {
if ($adv==1) {
$adv_qry = "&adv=1";
}
if ($type != "") {
$type_qry = "&type=$type";
}
?>
<div id="other_pages">
<?php print $sph_messages["Result page"]?>:
<?php if ($start >1){?>
<?php print $sph_messages['Previous']?>
<?php }?>
<?php foreach ($other_pages as $page_num) {
if ($page_num !=$start){?>
<?php print $page_num?>
<?php } else {?>
<b><?php print $page_num?></b>
<?php }?>
<?php }?>
<?php if ($next <= $pages){?>
<?php print $sph_messages['Next']?>
<?php }?>
</div>
<?php }?>
<div class="divline">
</div>
I'm also not aware of a live PHP code editor, if you know of one please comment and share so I can add a link!
Presuming $from and $to are the result numbers, so you're displaying "Showing results 10 to 30 of 100" for example:
<div id="results">
<!-- results listing -->
<?php $adbreak = ($to - $from) / 2;
<?php foreach ($qry_results as $_key => $_row){
<?php if ($adbreak == 0) { ?>
<div id="results-adbreak">
<img src="buy-a-car.jpg" alt="one careful owner!" />
</div>
<?php }
$adbreak--;
?>
// rest of your code
This will put a div approximately (give or take one) half way down your page of results. You can obviously replace the ad with a call to whatever you want.
adding something like:
<?php $adbreak = ($to - $from) / 2;
<?php if ($adbreak < 5) $adbreak = -1; ?>
will ensure that it doesn't display at all if the results list is too short.
If you don't know $to and $from in advance, you can still do it, but you'll have to calculate the equivalent from the query result first.

How do I output two fields in PHP?

I have a database that has two fields: current, and previous.
Here is the code:
<?
$username="*****";
$password="*****";
$database="******";
mysql_connect(localhost,$username,$password) or die("Unable to connect to server");
mysql_select_db($database) or die( "Unable to select database");
$query='SELECT * FROM tasks WHERE current=1 OR previous=1';
$result=mysql_query($query);
$num=mysql_num_rows($result);
mysql_close();
?>
It then output it and manipulate it like this:
<div class="tasklist">
<div class="currentProject">
Current Project:
</div>
<?
$i=0;
while ($i < $num) {
$title=mysql_result($result,$i,"Title");
$link=mysql_result($result,$i,"Weblink");
$description=mysql_result($result,$i,"Description");
$id=mysql_result($result,$i,"ID");
$howto=mysql_result($result,$i,"howto");
$blogpost=mysql_result($result,$i,"blogpost");
$done=mysql_result($result,$i,"done");
$current=mysql_result($result,$i,"current");
$previous=mysql_result($result,$i,"previous");
if ( $current == 1 ) {
$current = "current";
} else {
$current = "";
}
if ( $previous == 1 ) {
$previous = "previous";
} else {
$previous = "";
}
?>
<div class="<? echo $done ?> task <? echo $id ?>" id="<? echo $current.$previous ?>">
<div class="titleWrapper">
<div class="title">
<a class="article" href="<? echo $link ?>"><? echo $title ?></a>
</div>
</div>
</div><BR>
<?
$i++;
}
echo "</div>";
?>
The problem is that since previous comes before current in the database, it outputs previous and then current.
Question:
How do I make sure to output current first, and then previous?
Instead of outputting both directly, store them until you actually have both, and then output both in the correct order.
Just append this to your query order by previous.
It must work as long as current task registry has previous=0 and previous task registry has previous=1.

Categories