Please help me, i tried to create search engine with pagination, everything seems fine except when i click back to page1's link, it pull out data from mysql orderly not from my inputted query, but this happen only on my page one.when i click i need really help on this!!!
<?php
session_start();
error_reporting(E_ERROR);
$link = mysql_connect("localhost","root","1234");
mysql_query("Use igstore");
$pagesquery = mysql_query("SELECT COUNT('creatorid') FROM `creator`");
$pages = ceil(mysql_result($pagesquery, 0)/$perpage);
$page = (isset($_GET['page']))?(int)$_GET['page']:1;
$start = ($page-1)*$perpage;
//to filter from inputted data from textbox and combobox
if($value == null&&$genre == null&&$star == null){
$sql = "SELECT * FROM `creator` WHERE `title` LIKE '%$find%' OR `creatorid` LIKE '$item' LIMIT $start, $perpage";}
else if($value != null){
$sql = "SELECT * FROM `creator` WHERE `aprice` = '$value' OR `creatorid` LIKE '$item' LIMIT $start, $perpage";}
else if($genre != null){
$sql = "SELECT * FROM `creator` WHERE `type` = '$genre' OR `creatorid` LIKE '$item' LIMIT $start, $perpage";}
else if($star != null){
$sql = "SELECT * FROM `creator` WHERE `popular` = '$star' OR `creatorid` LIKE '$item' LIMIT $start, $perpage";}
$result = mysql_query($sql) or die(mysql_error());
while($_GET = mysql_fetch_array($result)){
$head = $_GET['title'];
$def = $_GET['description'];
$pric = $_GET['aprice'];
$blob = $_GET['img1'];
echo"<html>";
echo'<table height="180" width="1300">';
echo"<tr>";
echo'<th align="center">';
echo'<div class="ab">';
echo'<img src="data:image/jpeg;base64,'.base64_encode($blob).'"/ width="150px" height="150px">';
echo'</div>';
echo'</th>';
echo'<th align="left">';
echo'<div class="container">';
echo"<a href='#'><h3>$head</h3></a>";
echo"<textarea style='resize: none;border: none' rows='5' cols='60' readonly>$def</textarea><br>";
echo"<label class='control-label col-sm-2'><h5>ราคา: $pric</h5></label><br><br>";
echo"</div>";
echo'</th>';
echo"</tr>";
echo'</table>';
echo'<hr width="100%">';
echo"</html>";
}
//this is for showing page number
$prev = $page - 1;
$next = $page + 1;
echo "<a href='result.php?page=$prev' style='display: inline-block';>Prev</a>";
for($index=1;$index<=$pages;$index++){
echo ($page == $index)?'<b><a href="?page='.$index.'" style="display: inline-block"; >'.$index.'</a> </b>':'<a href="?page='.$index.'" style="display: inline-block"; >'.$index.'</a>';
}
echo "<a href='result.php?page=$next' style='display: inline-block';>Next</a>";
?>
Related
i am still a novice in PHP. please i need help. i am creating a blog page with pagination and i want the ranking to descend start from the last "id" from my database table not from the first "id" so that if i add a new post to my database, it will display at the top of my blog page. here is my coding:
<?php
$per_page = 5;
if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}
if($page<=1)
$start = 0;
else
$start = $page * $per_page - $per_page;
include 'connect.php';
$sql = "SELECT * FROM pagination";
$num_rows = mysql_num_rows(mysql_query($sql));
$num_pages = ceil($num_rows / $per_page);
$sql .= " LIMIT $start, $per_page";
$result = mysql_query($sql);
While($row = mysql_fetch_array($result)){
echo $row['name']. "<br>";
}
$prev = $page - 1;
$next = $page + 1;
echo "<hr>";
if($prev > 0)
echo "<a href='?page=$prev'>Previous</a> ";
if($page < ceil($num_rows/$per_page))
echo " <a href='?page=$next'>Next</a>";
?>
and same with my comment page, i want the last comment to display first. here is my coding:
<?php
include 'connect.php';
function getuser($id, $field) {
$query = mysql_query("SELECT $field FROM suggest WHERE id='$id'");
$run = mysql_fetch_array($query);
return $run[$field];
}
$readq = mysql_query("SELECT id FROM suggest");
while($run_p = mysql_fetch_array($readq)){
$id = $run_p['id'];
$name = getuser($id, 'name');
$title = getuser($id, 'title');
$post = getuser($id, 'post');
?>
<table width="60%">
<tr>
<td><b><font color="blue"><?php echo $title; ?></font><br><br><?php echo $post; ?><br>Suggestion From: <font color="blue"><?php echo $name; ?></font></b><hr width="50%"></td>
</tr>
</table>
<?php
}
?>
SELECT column_name, column_name
FROM table_name
ORDER BY column_name ASC|DESC
you can use the ORDER BY column_name ASC or DESC in you query to display it the ways you want.
For latest records you need to use ORDER BY DESC in your query.
You can be use this query:
SELECT id FROM suggest
Like that:
SELECT id FROM suggest ORDER BY id DESC
Side note: i suggest to use mysqli_* or PDO because mysql_* is deprecated and closed in PHP 7.
Hi what I'm trying to do is make a pagination for the whole and gives also a chance the user to search for a name and returns a paginated result.
Here is my code
<form method = "POST">
<td>
Search:<input name="search_name" type="text" id="t_searchkey" style="width:35%;" placeholder = "Name">
<input type="submit" name="b_find" id="b_find" title="Find" value = "Find">
</td>
PHP code:
<?php
if(!isset($_POST['b_find']))
{
$query = "SELECT * FROM reginformation
WHERE deleted = 0";
$search_name = "";
}
if(isset($_POST['b_find']))
{
$search_name = trim($_POST['search_name']);
$query = "SELECT * FROM reginformation WHERE name = '$search_name' AND deleted = 0 ";
}
?>
<?php
$result = mysql_query($query) or die(mysql_error());
?>
<?php
$num_rec_per_page=5;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
if (isset($_GET["search"]))
{
$search_name = $_GET['search'];
}
$start_from = ($page-1) * $num_rec_per_page;
$query2 =$query . " LIMIT $start_from, $num_rec_per_page";
echo "query2 $query2";
$rs_result = mysql_query ($query2); //run the query
while ($row = mysql_fetch_assoc($rs_result)) {
echo "<tr onClick =window.location='infodetailsresp.php?id=$row[regID]'><td>$row[name]</td><td>$row[emailadd]</td><td>$row[contactno]</td><td>$row[event]</td><td>$row[date_register]</td></tr>";
};
//$sql = "SELECT * FROM reginformation";
$rs_result = mysql_query($query); //run the query
$total_records = mysql_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
?>
<?php
echo "<a href='reglistresp.php?page=1&search=$search_name'>".'|< '."</a> "; // Goto 1st page
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='reglistresp.php?page=".$i."&search=$search_name'>". $i ."</a> ";
};
echo "<a href='reglistresp.php?page=$total_pages&search=$search_name'>".' >| '."</a> "; // Goto last page
?>
What happens when I click the next page it gives me the query in the
if(!isset($_POST['b_find']))
So what should be changed so I can get my desired query to the next page?
my default load page is ok with next and prev results.. but for search results i can get only first page then blank for the next page. let say total search results row is 9, it display 3 pages with next link. but when i click the next link, it queries no result on the page 2. here is my code:
echo '<table><tr><td>';
echo '<form method="post" name="frmSearch" action="mypage.php>
Search by name
<input type="text" name="txtSearch">
<input type="submit" name="submit" value="Search">
</form>';
$per_page = 5;
$page = 1;
if (isset($_GET['page']))
{
$page = intval($_GET['page']);
if($page < 1) $page = 1;
}
$start_from = ($page - 1) * $per_page;
$Prev_Page = $page - 1;
$Next_Page = $page + 1;
if (!$_POST){
//$sql = "SELECT * FROM mytable LIMIT $start_from, $per_page";
//$totalr = mysql_query("SELECT COUNT(*) FROM mytable");
//$totalr = mysql_fetch_row($totalr);
//$totalr = $totalr[0];
//$total_pages = $totalr / $per_page;
//$total_pages = ceil($total_pages);
//$listresult = mysql_query($sql);
//$total = mysql_num_rows($listresult);
}
else {
if ($_POST['txtSearch']!="") {$cond1 = " name LIKE
'%".$_POST['txtSearch']."%' ";} else {$cond1 = 1; }
$sql = "SELECT * FROM mytable WHERE ".$cond1." LIMIT $start_from, $per_page";
$totalr = mysql_query("SELECT COUNT(*) FROM mytable WHERE name LIKE
'%".$_POST['txtSearch']."%' ");
$totalr = mysql_fetch_row($totalr);
$totalr = $totalr[0];
$total_pages = $totalr / $per_page;
$total_pages = ceil($total_pages);
$listresult = mysql_query($sql);
$total = mysql_num_rows($listresult);
}
if($total == 0){
echo "<center>No records found.</center>";
}
echo "<span style='float:right;margin-top:8px;'>[ <a href='new.php'>Add New</a>
]</span></td></tr>";
echo "<table>
<tr>
<th>ID</th>
<th>Name</th>
</tr>
";
while ($_POST = mysql_fetch_assoc($listresult)){
echo "<tr>";
echo "<td>" . $_POST['id'] . "</td>";
echo "<td>" . $_POST['name'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "</table>";
echo $totalr." result";
echo "<br>";
if ($Prev_Page) {
echo " <a href ='{$_SERVER['PHP_SELF']}?page=$Prev_Page'><< Prev</a> ";
}
if ($totalr > $per_page) {
for($i = 1; $i <= $total_pages; ++$i)
{
echo "<a href='{$_SERVER['PHP_SELF']}?page=$i'>$i</a> | ";
}
}
if ($page!=$total_pages) {
echo " <a href='{$_SERVER['PHP_SELF']}?page=$Next_Page'>Next >></a> ";
}
maybe you need to use SELECT COUNT(*) FROM mytable WHERE name LIKE '%$search%' ... remember to escape the $_POST['txtSearch']
On my home page I have one image(big) on left side and small box in my right panel where I show random thumb of images on every refresh page. Here is how I show this image:
$rand = mysqli_query($con, "SELECT * from images order by RAND() LIMIT 0,1");
while ($res = mysqli_fetch_assoc($rand))
{
echo "<img style=\"width:100%;margin:0 auto;\" src=\"upload/".$res['name']."\" /><br />";
echo "<div id=\"caption\" style=\"text-align:center;\">".$res['caption']."</div><br />";
echo "Open in new page »";
}
How to make when someone click on thumb to open that image in new page? This echo "Open in new window »"; doesn't open the proper image. In fact just refreshing the page but the image is the same.
UPDATE:
pics.php
$sql = "SELECT COUNT(*) FROM images";
$result = mysqli_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysqli_fetch_row($result);
$numrows = $r[0];
$rowsperpage = 1;
$totalpages = ceil($numrows / $rowsperpage);
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
$currentpage = (int) $_GET['currentpage'];
} else {
$currentpage = 1;
}
if ($currentpage > $totalpages) {
$currentpage = $totalpages;
} // end if
if ($currentpage < 1) {
$currentpage = 1;
} // end if
$offset = ($currentpage - 1) * $rowsperpage;
$sql = "SELECT name, caption FROM images LIMIT $offset, $rowsperpage";
$result = mysqli_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
while ($list = mysqli_fetch_assoc($result)) {
echo "<div id=\"picture\">";
echo "<img style=\"width:100%;margin:0 auto;\" src=\"upload/".$list['name']."\" /></a><br />";
echo "<div id=\"caption\">".$list['caption']."</div><br />";
} // end while
First there is no need to put LIMIT 0,1 Just have
$rand = mysqli_query($con, "SELECT * from images order by RAND() LIMIT 1");
$res = mysqli_fetch_assoc($rand);
echo "<img style=\"width:100%;margin:0 auto;\" src=\"upload/".$res['name']."\" /><br />";
echo "<div id=\"caption\" style=\"text-align:center;\">".$res['caption']."</div><br />";
echo "Open in new page »";
And in you pics.php you must specify the id of the image in your sql query
$id = abs((int)$_GET['id']);
$sql = "SELECT name, caption FROM images WHERE id='{$id}' LIMIT $offset, $rowsperpage";
$result = mysqli_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
while ($list = mysqli_fetch_assoc($result)) {
echo "<div id=\"picture\">";
echo "<img style=\"width:100%;margin:0 auto;\" src=\"upload/".$list['name']."\" /></a><br />";
echo "<div id=\"caption\">".$list['caption']."</div><br />";
} // end while
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("data_wis1") or die(mysql_error());
if(isset($_GET["id"])) {
$id = $_GET["id"];
$sql = "DELETE FROM info WHERE ID = '".$id."'";
mysql_query($sql) or die(mysql_error());
}
if(isset($_GET["page"])) {
$page = $_GET["page"];
}
else {
$page = 1;
}
$start_from = ($page-1) * 1;
$sql = "SELECT * FROM info LIMIT 0, 1";
$query = mysql_query($sql) or die(mysql_error());
?>
<html>
<table>
<?php
for($i = 0; $i <mysql_num_rows($query); $i++) {
$id = mysql_result($query, $i, "ID");
$caseStatus = mysql_result($query, $i, "Case_Status");
echo "<tr>";
echo "<td>".$id."</td>";
echo "<td>".$caseStatus."</td>";
echo "<td><a href='del.php?id=".$id."'><input type='button' value='Delete'></a></td>";
echo "</tr>";
}
?>
</table>
<?php
$sql = "SELECT COUNT(ID) FROM info";
$query = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_row($query);
$total_records = $row[0];
$total_pages = ceil($total_records / 1);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='del.php?page=".$i."'>".$i."</a> ";
}
?>
</html>
My problem is that whenever i click page 2,3,4... the display doesn't change. Page 2,3,4 gets the display of page 1. It should be like this. For example, page 1 should display ID = 1 and case status = open. Page 2 should display ID = 2 and case status = close and so on.
You never used $start_from in anywhere. I think $sql = "SELECT * FROM info LIMIT '".$start_from."', 1"; is the answer