Jump to content

Recommended Posts

ok.. I have just set up an online marketplace website and right know browsing through items is fairly simple.. just paginates throught the results and gives likes to listing.php that then displays all the info/picture..... and recently i added a search but using 'LIKE' its simple easy and fast and it searches through title and description..... but know i want to make it work abit better.. here are the things i want to happen....

1. add catagagorys... view all in a catagory and search through catagorys individualy..( database set with catagorys.. id and catagory are the only rows....) **COMPLETE**
2. when i user clicks on an item and takes them to listing.php i want it be be able to go back to browse.php with the same search**COMPLETE**
3. configue the paginating to work for search results...i have an exit; there right now because what it does is the search results show on the first page then the second , third ,foruth ...etc show all the listings... how they would be if there wasnt a search...**COMPLTETE PAGINATION WORKS ON SEARCH RESULTS TOO WITH MINNAMULE CODE ADDED!!!****

heres the code for browse.php...its a mess but what the heck... if you need code for listing.php i can get to but its basicly just calling all the information from the database and displaying it....

Browse.php
[code]<?PHP
session_start();
$user=$_SESSION['user'];
if(empty($user)){
$logged="You Are Not Signed In!";
$l="0";
}
else
{
$l=1;
$logged="Welcome $user";
}
echo "<div align=center><img border=0 src=images/Logo1.JPG width=305 height=194><br><p align=center>$logged</font><br><hr><br>";
echo "<a href=browse.php>Return to List of items</a><br><br>";
echo "<a href=index.php>Return Home</a>";
$username="**********";
$password="********";
$database="********";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
echo "<form method=POST>";

$display = 10;

$page = "SELECT COUNT(*) FROM listings ORDER BY id ASC";
$resultp = mysql_query($page);
$row = mysql_fetch_array($resultp, MYSQL_NUM);
$num_records = $row[0];

echo "<p align=center>There are $num_records Listings</p>";
if(isset($_GET['np'])){
$num_pages = $_GET['np'];
}else{
$page = "SELECT COUNT(*) FROM listings ORDER BY id ASC";
$resultp = mysql_query($page);
$row = mysql_fetch_array($resultp, MYSQL_NUM);
$num_records = $row[0];
if($num_records > $display){
$num_pages = ceil($num_records/$display);
}else{
$num_pages = 1;
}
}
if(isset($_GET['s'])){
$start=$_GET['s'];
}else{
$start=0;
}

if(isset($_POST['submit'])){
$search=$_POST['search'];
$query="SELECT * FROM listings WHERE title LIKE '%$search%' OR  description LIKE '%$search%'";
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
$re= mysql_num_rows($result);
if($re > 0)
{
echo "<link rel=stylesheet type=text/css
href=store.css>
</head>
<body bgcolor=#0065FF>
<form method=POST>
<p>Search:<input type=text name=search size=45>
<input type=submit value=Search name=submit></p>";
echo "<p>$re Search results";
echo "<table border=1>";
while($row = mysql_fetch_row($result)){
$member1="SELECT * FROM members WHERE username='$row[5]'";
$resultm = mysql_query($member1) or die ("Error in query: $member. ".mysql_error());
while($mem = mysql_fetch_row($resultm)){
echo "<tr><td width=300><p align=center><a href=listing.php?id=$row[0]>$row[1]</a><br><font size=2>Location: $mem[10]</td><td width=100>Price: $$row[2]</p></td></tr>";
}
}
exit;
}else{
echo "<table border=1>";
echo "<link rel=stylesheet type=text/css
href=store.css>
<title>No Search Results Found</title>
</head>
<body bgcolor=#0065FF>
<form method=POST>
<p>Search:<input type=text name=search size=45>
<input type=submit value=Search name=submit></p>";
echo "<tr><td width=500><p align=center>NO Matches were Found</p></td></tr>";
exit;
}
}else{
echo "<table border=1>";
$query2="SELECT * FROM listings ORDER BY id ASC LIMIT $start, $display";
$result2 = mysql_query($query2) or die ("Error in query: $query2. ".mysql_error());
$num = mysql_num_rows ($result2);
echo "<table border=1>";
while($row = mysql_fetch_row($result2)){
$member1="SELECT * FROM members WHERE username='$row[5]'";
$resultm = mysql_query($member1) or die ("Error in query: $member. ".mysql_error());
while($mem = mysql_fetch_row($resultm)){
echo "<tr><td width=300><p align=center><a href=listing.php?id=$row[0]>$row[1]</a><br><font size=2>Location: $mem[10]</td><td width=100>Price: $$row[2]</p></td></tr>";
}
}
}
?>
<html>
<head>
<title>Browsing Ze Localz</title>
<link rel="stylesheet" type="text/css"
href="store.css">
</head>
<body bgcolor=#0065FF>
<form method="POST" action="--WEBBOT-SELF--">
<p>Search:<input type="text" name="search" size="45">
<input type="submit" value="Search" name="submit"></p>
<?PHP
echo "</form>";
if ($num_pages > 1){
echo '<br><p>';
$current_page = ($start/$display) + 1;
if($current_page != 1){
echo '<a href="browse.php?s'.($start - $display).'$np='.$num_pages.'">Previous</a> ';
}
for($i =1; $i <= $num_pages; $i++){
if ($i != $current_page){
echo '<a href="browse.php?s=' . (($display*($i - 1))) . '&np=' . $num_pages . '">' . $i . '</a> ';
}else{
echo $i.'';
}
}
if($current_page != $num_pages){
echo '<a href="browse.php?s='.($start + $display).'&np='.$num_pages . '">Next</a> ';
}
}
?>
</html>[/code]
Link to comment
https://forums.phpfreaks.com/topic/15936-done-advanced-searching-through-database/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.