PHP $_GET from url on same page - php

I'm doing some pagination in PHP but i run into some problems. I did pagination, and everything is working well but what i wanted is to add a form on the page in which user could type in how much items he wants on each page. The problem is, when i enter number of pages (eg. 3), it works fine for the first time and lists 3 items, but when i click on "next page" it again lists default number of elements (2). I've searched everything and anything but can't really find what seems to be a problem. I'm kinda new to php so i guess that i don't get some things yet, but I hope it's not too much of a trouble for you to take look at this and tell me what you think. Thanks in advance!
I did it like this:
form:
<?php
print("
<form style='padding-left:5px;' method='get'>Broj komponenti po stranici:<input type='text' name='cpp' /><input class='button3' type='submit' name='cppb' value='Promijeni' /></form>
");
check if there is some thing stored in cpp:
$rec_limit = ($_GET['cpp']);
if(!(isset($_GET['cpp']))) { $rec_limit = 2; } //if there is no cpp set, let it be 2 (default)
right here is database and pagination part (i don't think that this is the part that makes troubles):
$sql = "SELECT count(id) FROM komponenta ";
$retval = mysql_query($sql, $conn);
if(!$retval) {
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($retval, MYSQL_NUM );
$rec_count = $row[0];
$max_pages = ($rec_count / $rec_limit) -1;
if(isset($_GET{'page'}) ) {
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
} else {
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$sql = "SELECT * ".
"FROM komponenta ".
"LIMIT $offset, $rec_limit";
$retval = mysql_query($sql, $conn);
if(!$retval) {
die('Could not get data: ' . mysql_error());
}
print("<TABLE class='tablica' border=‘1’>");
print("<TR>");
print("<TD></TD>");
print("<TD>Vrsta</TD>");
print("<TD>Proizvođač</TD>");
print("<TD>Frekv</TD>");
print("<TD>Izlazna</TD>");
print("<TD>Dobavljač</TD>");
print("<TD>Status</TD>");
print("<TD>Datum kupnje</TD>");
print("<TD>Datum zaprimanja</TD>");
print("<TD>Opis</TD>");
print("<TD>Napomena</TD>");
print("<TD>Komada</TD>");
print("<TD>Metara</TD>");
print("</TR>");
$br=0;
while($row = mysql_fetch_array($retval, MYSQL_NUM)) {
print("<TR>");
print("<TD>". $br . "</TD>");
print("<TD>". $row["1"]. "</TD>");
print("<TD>" . $row["2"]. "</TD>");
print("<TD>" . $row["3"]. "</TD>");
print("<TD>" . $row["4"]. "</TD>");
print("<TD>" . $row["5"]. "</TD>");
print("<TD>" . $row["6"]. "</TD>");
print("<TD>" . $row["7"]. "</TD>");
print("<TD>" . $row["8"]. "</TD>");
print("<TD>" . $row["9"]. "</TD>");
print("<TD>" . $row["10"]. "</TD>");
print("<TD>" . $row["11"]. "</TD>");
print("<TD>" . $row["12"]. "</TD>" );
print("</TR>");
$br++;
}
print("</TABLE>");
and here is the part that bothers me:
if( $page >= $max_pages ) {
$last = $page - 2;
echo "<b class='paragraf1'>Page: ". $page ." </b><a class='button3' href=\"protected_page.php?page=$last?cpp=$rec_limit\"><text class='buttontxt'>Last " . $rec_limit . " Records</text></a>";
/*with ?cpp=$rec_limit i set the url
(when i press "next") to have for eg ..."?cpp=3"... and it does appear so,
but the $_GET['cpp'] from the beggining of code obviously doesn't read this..
Well, at least it doesn't store it into $rec_limit*/
} else if($page > 0) {
$last = $page - 2;
echo "<b class='paragraf1'>Page: ". $page ." </b><a class='button3' href=\"protected_page.php?page=$last?cpp=$rec_limit\"><text class='buttontxt'>Last " . $rec_limit . " Records</text></a> ";
echo "<a class='button3' href=\"protected_page.php?page=$page?cpp=$rec_limit\"><text class='buttontxt'>Next " . $rec_limit . " Records</text></a>";
} else if($page == 0) {
echo "<b class='paragraf1'>Page: ". $page ." </b><a class='button3' href=\"protected_page.php?page=$page?cpp=$rec_limit\"><text class='buttontxt'>Next " . $rec_limit . " Records</text></a>";
}

protected_page.php?page=$page?cpp=$rec_limit
replace the 2nd ? by &
protected_page.php?page=$page&cpp=$rec_limit
When you're passing variables in the url after the script name there is a ? then between each variable and the other it is and & that separates.
and at your place I would replace that too:
if(!(isset($_GET['cpp']))){$rec_limit=2;}
by:
if(!isset($_GET['cpp']) || (int)$rec_limit<=0){ $rec_limit=2; }

Related

I have successfully queried my database in wordpress/php however it is just showing blank when I try to display the results in a table

For my website I am making a parametric search to filter through 100's of power supplies, filtering by voltage etc. When I did this locally it worked perfectly however I am struggling when it comes to implementing it to wordpress, here is the code below of my output/the query, I know its a lot of if statements to build up the query, any improvements are greatly appreciated too.
<html>
<?php
$voltage=$_POST["MaxVoltage"];
$sql = "SELECT * FROM powersupplies WHERE MaxVoltage >= '".$voltage."'";
if (!empty($_POST["MaxCurrent"])){
$current=$_POST["MaxCurrent"];
$currentquery = " AND MaxCurrent >= '".$current."'";
$sql = $sql.$currentquery;
}
if (!empty($_POST["Make"]) && ($_POST["Make"] != "Any")){
$make=$_POST["Make"];
$makequery = " AND Make = '".$make."'";
$sql = $sql.$makequery;
}
if (!empty($_POST["MaxPower"])){
$power =$_POST["MaxPower"];
$powerquery = " AND MaxPower >= '".$power."'";
$sql = $sql.$powerquery;
}
if (isset($_POST["Programmable"]) && ($_POST["Programmable"] != "Any")){
$programmable =$_POST["Programmable"];
$programmablequery = " AND Programmable = '".$programmable."'";
$sql = $sql.$programmablequery;
}
if (isset($_POST["Negative"])&& ($_POST["Negative"] != "Any")){
$negative =$_POST["Negative"];
$negativequery = " AND Negative = '".$negative."'";
$sql = $sql.$negativequery;
}
if (isset($_POST["Positive"])&& ($_POST["Positive"] != "Any")){
$positive =$_POST["Positive"];
$positivequery = " AND Positive = '".$positive."'";
$sql = $sql.$positivequery;
}
if (isset($_POST["Bipolar"])&& ($_POST["Bipolar"] != "Any")){
$bipolar =$_POST["Bipolar"];
$bipolarquery = " AND Bipolar = '".$bipolar."'";
$sql = $sql.$bipolarquery;
}
if (isset($_POST["Floating"])&& ($_POST["Floating"] != "Any")){
$floating =$_POST["Floating"];
$floatingquery = " AND Floating = '".$floating."'";
$sql = $sql.$floatingquery;
}
if (isset($_POST["Topology"])&& ($_POST["Topology"] != "Any")){
$topology =$_POST["Topology"];
$topologyquery = " AND Topology = '".$topology."'";
$sql = $sql.$topologyquery;
}
$sql = $sql.";";
$result = $wpdb->get_results($sql, ARRAY_A);
if ($wpdb->last_error) {
echo 'You done bad! ' . $wpdb->last_error;
}
echo count($result)." results have been found";
echo "<table>"
foreach ($result as $row){
echo "<tr><td>" . $row["PartNumber"].
"</td><td>" . $row["MaxVoltage"] .
"</td><td>" . $row["MaxPower"].
"</td><td>". $row["MaxCurrent"] .
"</td><td>". $row["Programmable"] .
"</td><td>". $row["Negative"] .
"</td><td>". $row["Positive"] .
"</td><td>". $row["Bipolar"] .
"</td><td>". $row["Floating"] .
"</td><td>". $row["Topology"] .
"</td><td>". $row["Make"] .
"</td></tr>";
}
echo "</table>"
?>
</html>

PHP Simple Pagination

the below code is getting some values from DB by "select option form" , i recently added Pagination snip to limit the results, when i run the code it fetch 5 recorders as defined,but didn't show the remaining number of pages.
what im doing wrong here ?
<?php
$per_page = 5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page = 1;
}
$start_from = ($page - 1) * $per_page;
if (!empty($_POST['form_val']) && isset($_POST['form_val'])) {
$_POST['form_val'] = 0;
$sql = "SELECT u.log_id , u.user_name, s.site, u.date ,u.comment , l.location, e.picture FROM `pool` u, `location_all` l , `site_all` s JOIN db2.user e
where l.location_id = u.location and s.site_id = u.site and e.user_id = u.user_id";
if (!empty($_POST['Location']) && isset($_POST['Location'])) {
$sql = $sql . " AND location =" . $_POST['Location'];
}
$strtdate = $_POST['Sday'];
$enddate = $_POST['Eday'];
if (!empty($_POST['Sday']) && isset($_POST['Sday']) && !empty($_POST['Eday']) && isset($_POST['Eday'])) {
$sql = $sql . " AND date between '" . $strtdate . "' and '" . $enddate . "'";
} elseif (!empty($_POST['Sday']) && isset($_POST['Sday'])) {
$sql = $sql . " AND date>='" . $strtdate . "'";
} elseif (!empty($_POST['Eday']) && isset($_POST['Eday']))
$sql = $sql . " AND date<='" . $enddate . "'";
}
if (!empty($_POST['Site']) && isset($_POST['Site'])) {
$sql = $sql . " AND u.site=" . $_POST['Site'];
}
$sql = $sql . " LIMIT $start_from, $per_page";
if (mysqli_query($conn, $sql)) {
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) >= 1) {
$rowcount = mysqli_num_rows($result);
echo '<legend> ' . $rowcount . ' Records Found !!!</legend>';
echo '<br><br>';
echo "<table class='srchtable'>
<tr>
<th>Picture</th>
<th>Date</th>
<th>User Name</th>
<th>country</th>
<th>Location</th>
<th>Site</th>
<th>Comment</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td> <img src='" . $row['picture'] . "' alt='' style='width:70%; height:auto; border-radius: 50%;'> </td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['user_name'] . "</td>";
echo "<td>" . $row['country'] . "</td>";
echo "<td>" . $row['location'] . "</td>";
echo "<td>" . $row['site'] . "</td>";
echo "<td>" . $row['comment'] . "</td>";
echo "</tr>";
}
echo "</table>";
$total_pages = ceil($rowcount / $per_page);
echo "<center><a href='?page=1'>" . 'First Page' . "</a> ";
for ($i = 1; $i <= $total_pages; $i++) {
echo "<a href='?page=" . $i . "'>" . $i . "</a> ";
}
echo "<a href='?page=$total_pages'>" . 'Last Page' . "</a></center> ";
} else {
echo '<p>No Results Found !!!</p>';
}
}
}
?>
As I said in my comments, for displaying pagination links:
You're counting total number of rows but incorporating LIMIT and OFFSET clauses in your SELECT query, this won't give the correct number of row count. Your SELECT query should not contain this part, ... LIMIT $start_from, $per_page.
Since you're filtering the results based on several $_POST data, you should incorporate those conditions in your pagination links as well, otherwise when you visit a different page(through pagination link), you won't get the desired result, and that's because $_POST data will not be retained when you hop from page to page. Better that you change the method of your <form> from POST to GET, because in this way it'd be easier for you to catch and manipulate things when you hop from one page to another using pagination links.
So based on the above points, your code should be like this:
$per_page = 5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page = 1;
}
$start_from = ($page - 1) * $per_page;
if (!empty($_GET['form_val']) && isset($_GET['form_val'])) {
$_GET['form_val'] = 0;
$sql = "SELECT u.log_id , u.user_name, s.site, u.date ,u.comment , l.location, e.picture FROM `pool` u, `location_all` l , `site_all` s JOIN db2.user e
where l.location_id = u.location and s.site_id = u.site and e.user_id = u.user_id";
if (!empty($_GET['Location']) && isset($_GET['Location'])) {
$sql = $sql . " AND location =" . $_GET['Location'];
}
$strtdate = $_GET['Sday'];
$enddate = $_GET['Eday'];
if (!empty($_GET['Sday']) && isset($_GET['Sday']) && !empty($_GET['Eday']) && isset($_GET['Eday'])) {
$sql = $sql . " AND date between '" . $strtdate . "' and '" . $enddate . "'";
} elseif (!empty($_GET['Sday']) && isset($_GET['Sday'])) {
$sql = $sql . " AND date>='" . $strtdate . "'";
} elseif (!empty($_GET['Eday']) && isset($_GET['Eday'])) {
$sql = $sql . " AND date<='" . $enddate . "'";
}
if (!empty($_GET['Site']) && isset($_GET['Site'])) {
$sql = $sql . " AND u.site=" . $_GET['Site'];
}
$data_query = $sql . " LIMIT $start_from, $per_page";
$result = mysqli_query($conn, $data_query);
if (mysqli_num_rows($result) >= 1) {
$rowcount = mysqli_num_rows($result);
echo '<legend> ' . $rowcount . ' Records Found !!!</legend>';
echo '<br><br>';
echo "<table class='srchtable'>
<tr>
<th>Picture</th>
<th>Date</th>
<th>User Name</th>
<th>country</th>
<th>Location</th>
<th>Site</th>
<th>Comment</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td> <img src='" . $row['picture'] . "' alt='' style='width:70%; height:auto; border-radius: 50%;'> </td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['user_name'] . "</td>";
echo "<td>" . $row['country'] . "</td>";
echo "<td>" . $row['location'] . "</td>";
echo "<td>" . $row['site'] . "</td>";
echo "<td>" . $row['comment'] . "</td>";
echo "</tr>";
}
echo "</table>";
$query_result = mysqli_query($conn, $sql);
$total_rows = mysqli_num_rows($query_result);
$total_pages = ceil($total_rows / $per_page);
parse_str($_SERVER["QUERY_STRING"], $url_array);
unset($url_array['page']);
$url = http_build_query($url_array);
?>
<center>First Page
<?php
for ($i = 1; $i <= $total_pages; $i++) {
?>
<?php echo $i; ?>
<?php
}
?>
Last Page</center>
<?php
} else {
echo '<p>No Results Found !!!</p>';
}
}

PHP Pagination for MySQL database - display the first page by default

Code:
$totalItemsRequired = 8;
$query = "SELECT * FROM fruits ORDER BY origin ASC ";
$result = mysqli_query($connection, $query);
if ($result == false)
{
echo "<p>Selecting all fruits failed.</p>";
}
else
{
$totalRecords = mysqli_num_rows($result);
$totalPages = ceil($totalRecords / $totalItemsRequired);
echo "<p>Page: ";
for ($i = 1; $i <= $totalPages; $i++)
{
echo "<a href='?page=" . $i . "'>" . $i . "</a> ";
}
echo "</p>";
if (isset($_GET['page']))
{
$currentPageNum = $_GET['page'];
$offset = ($currentPageNum - 1) * $totalItemsRequired;
$query = "SELECT * FROM fruits " . "LIMIT " . $offset . ", " . $totalItemsRequired;
$result = mysqli_query($connection, $query);
if ($result == false)
{
echo "<p>Selecting subset (page) of fruits failed.</p>";
}
else
{
echo "<ul>";
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo "<li>" . $row["id"] . "," . $row["name"] . ", " . $row["origin"] . ", " . $row["stock"] . "</li>";
}
echo "</ul>";
}
}
else
{
echo
// By default, load first page of records here
}
}
?>
Hi, I'm trying to load the first page of records by default where I have put the comment. There are no problems with the code, and the initial output is good. I'm just not sure how to show by default. Can anyone help please?
EDIT: Can anyone help?
Why not use LIMIT clause in your MySQL statement?
SELECT * FROM fruits ORDER BY origin ASC LIMIT 8 //First 8 rows of the table
SELECT * FROM fruits ORDER BY origin ASC LIMIT 8,8 //Second 8 rows
SELECT * FROM fruits ORDER BY origin ASC LIMIT 16,8 //Third 8 rows
In General:
LIMIT offset,row_count will return row_count rows starting from offset+1 row.
Instead of if statement, you should just be able to change
$currentPageNum = $_GET['page'];
$offset = ($currentPageNum - 1) * $totalItemsRequired;
to
$currentPageNum = isset($_GET['page']) ? $_GET['page'] : 0;
$offset = $currentPageNum > 0 ? (($currentPageNum - 1) * $totalItemsRequired) : 0;
Full code:
<?php
$totalItemsRequired = 8;
$query = "SELECT * FROM fruits ORDER BY origin ASC ";
$result = mysqli_query($connection, $query);
if ($result == false)
{
echo "<p>Selecting all fruits failed.</p>";
}
else
{
$totalRecords = mysqli_num_rows($result);
$totalPages = ceil($totalRecords / $totalItemsRequired);
echo "<p>Page: ";
for ($i = 1; $i <= $totalPages; $i++)
{
echo "<a href='?page=" . $i . "'>" . $i . "</a> ";
}
echo "</p>";
$currentPageNum = isset($_GET['page']) ? $_GET['page'] : 0;
$offset = $currentPageNum > 0 ? (($currentPageNum - 1) * $totalItemsRequired) : 0;
$query = "SELECT * FROM fruits " . "LIMIT " . $offset . ", " . $totalItemsRequired;
$result = mysqli_query($connection, $query);
if ($result == false)
{
echo "<p>Selecting subset (page) of fruits failed.</p>";
}
else
{
echo "<ul>";
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo "<li>" . $row["id"] . "," . $row["name"] . ", " . $row["origin"] . ", " . $row["stock"] . "</li>";
}
echo "</ul>";
}
}
}
?>

how to use str_replace with $row link

how to use str_replace with $row link
now my link is showing like this
example.com/view.php?category=laptop&model=dell i5
and i want like this
example.com/view.php?category=laptop&model=dell_i5
this is a link code
<a href=\"detail.php?category=" . $row['category'] . "&model=" . $row['model'] ."\" >
so how can i use str_replace with the link " . $row['model'] ."
please help me to solve this issue
Complete code
<?php
//connect to database
mysql_connect('localhost','user','password');
mysql_select_db('newalldata');
$page = (empty($_GET['page'])) ? 1 : $_GET['page'];
$max_results = 6;
$from = (($page * $max_results) - $max_results);
if(empty($_POST)) {
$query = "SELECT * FROM alldata LIMIT $from, $max_results";
}
$result = mysql_query("SET NAMES utf8"); //the main trick
$result = mysql_query($query) or die(mysql_error());
$rows = mysql_num_rows($result);
$count=0;
while($row = mysql_fetch_array($result))
{
if($count%6==0)
{
echo "<tr/>";
echo "<tr>";
}
echo "<td><hr><div class='style99' align='center'><img src='/media/image.php?width=200&height=210&image=/media/" . $row['photo'] . "' title=". $row['price'] ." alt=". $row['model'] ." style='FILTER: alpha(opacity=100);-moz-opacity: 1.0; opacity: 1.0;' onmouseover=BeginOpacity(this,100,40) onmouseout=EndOpacity(this,100)><p><font color='#3366FF'>" . $row['category'] . "</font></p><p><font color='#3366FF'>" . $row['model'] . "</font></p><p><font color='#336600'>" . $row['price'] . "</font></p><div></td>";
$count++;
}
?>
$var = str_replace(" ","_",$row['model']);
<a href=\"detail.php?category=" . $row['category'] . "&model=" . $var ."\" >;
should do it
You may check http://php.net/manual/de/function.str-replace.php
The first parameter is what you search ( in this case the " ") , the second is what u want to heave instead ("_") and then which string is checked.

How can i get the names that i search for, and add a pagination to the results that are present

The list just stayed at ten items with no pagination to get the rest of the other list, this sucks, But thanks Guys, maybe i need more practice i really need this one to be working so i can learn from this one, any way the first code worked, the second works but stops at ten items, you said try the ceil i did with some code that i had already, the next and back and number of pages came up but no list, tried it again no next and back but ten items came up, i'm beginning to get a bit frustrated here.
<DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>Residential</title>
</head>
<body>
<section>
<form method="post" action="index.php?go" id="searchform">
<input type="text" id="sf" name="name" class="inputtext" >
<input type="submit" name="submit" id="sb" alt="Btn" value="GO">
</form>
</section>
<div class="results">
<?php
error_reporting(0);
if(isset($_POST['submit'])) {
if(isset($_GET['go'])) {
if(preg_match("/[A-Z | a-z]+/", $_POST['name'])){ //"/[A-Z | a-z]+/",
$name=$_POST['name'];
//connect to the database
$db=mysql_connect ("localhost", "root", "") or die ('I cannot connect to the database because: ' . mysql_error());
//-select the database to use
$mydb=mysql_select_db("residential");
This is the beginning of the changed code
//////////pagination////////////
$sql=mysql_query("SELECT userId, FirstName, LastName FROM residentialbackup WHERE FirstName ='" . $name . "' OR LastName = '" . $name ."'");
//-count results
$numrows=mysql_num_rows($sql);
// Get the value of the last page in the pagination result set
$lastPage = ceil($numrows / $itemsPerPage);
$itemsPerPage = 10;
$page = $_GET['page'];
if (!$page) $page = 1;
$offset = ($page - 1) * $itemsPerPage;
$sql2=mysql_query("SELECT userId, FirstName, LastName FROM residentialbackup WHERE FirstName = '" . $name . "' OR LastName = '" . $name ."' LIMIT ". $offset . ", ". $itemsPerPage);
$paginationDisplay = ""; // Initialize the pagination output variable
// This code runs only if the last page variable is ot equal to 1, if it is only 1 page we require no paginated links to display
if ($lastPage != "1") {
// This shows the user what page they are on, and the total number of pages
$paginationDisplay .= 'Page <strong>' . $page . '</strong> of ' . $lastPage. ' ';
// If we are not on page 1 we can place the Back button
if ($page != 1) {
$previous = $page - 1;
$paginationDisplay .= ' Back ';
}
// Lay in the clickable numbers display here between the Back and Next links
$paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';
// If we are not on the very last page we can place the Next button
if ($page != $lastPage) {
$nextPage = $page + 1;
$paginationDisplay .= ' Next ';
}
}
echo $paginationDisplay;
///////////////pagination end////////////
i'm stuck up to here!
//echo "<p>" .$numrows . " results found for " . stripslashes($name) . "</p>";
//-create while loop and loop through result set
while($row=mysql_fetch_array($sql2)) {
$FirstName =$row['FirstName'];
$LastName=$row['LastName'];
$userId=$row['userId'];
//-display the result of the array
echo "<ul>\n";
echo "<li>" . "" .$FirstName . " " . $LastName . " </li>\n";
echo "</ul>";
}
}
else {
echo "<p>Please enter a search query</p>";
}
}
/*
if(isset($_GET['by'])){
$name=$_GET['by'];
include "connectres.php";
//-query the database table
$sql="SELECT userId, FirstName, LastName FROM residentialbackup WHERE FirstName LIKE '%" . $name . "%' OR LastName LIKE '%" . $name ."%'";
//-run the query against the mysql query function
$result=mysql_query($sql);
//-count results
$numrows=mysql_num_rows($result);
echo "<p>" .$numrows . " results found for " . $name . "</p>";
//-create while loop and loop through result set
while($row=mysql_fetch_array($result)){
$FirstName =$row['FirstName'];
$LastName=$row['LastName'];
$userId=$row['userId'];
//-display the result of the array
echo "<ul>\n";
echo "<li>" . " " . $LastName . " " . $FirstName . " </li>\n";
echo "</ul>";
}
}
*/
if(isset($_GET['id'])) {
$userId=$_GET['id'];
//connect to the database
include "connectres.php";
//-query the database table
$sql="SELECT * FROM residentialbackup WHERE userId=" . $userId;
//-run the query against the mysql query function
$result=mysql_query($sql);
//-create while loop and loop through result set
while($row=mysql_fetch_array($result)) {
$FirstName =$row['FirstName'];
$LastName=$row['LastName'];
$PhoneNumber=$row['PhoneNumber'];
$Address=$row['Address'];
//-display the result of the array
echo "<ul>\n";
echo "<li>" . "<img src=\"../images/person.png\"/>" . $FirstName . " " . $LastName . " </li>\n";
echo "<li>" . "<img src=\"../images/tell.png\"/>" . " " . "" . $PhoneNumber . "</li>\n";
echo "<li>" . "<img src=\"../images/house.png\"/>" . " " . $Address . "</li>\n";
echo "</ul>";
}
}
?>
</div>
</div>
</body>
</html>
Your query has wildcards in it, remove them to only fetch exact matches
$sql="SELECT userId, FirstName, LastName FROM residentialbackup WHERE FirstName ='" . $name . "' OR LastName = '" . $name ."'";
You can make a pagination like this:
$itemsPerPage = 10;
$page = $_GET['page'];
if (!$page) $page = 1;
$offset = ($page - 1) * $itemsPerPage;
$sql="SELECT userId, FirstName, LastName FROM residentialbackup WHERE FirstName = '" . $name . "' OR LastName = '" . $name ."' LIMIT ". $offset . ", ". $itemsPerPage;

Categories