Jump to content

Archived

This topic is now archived and is closed to further replies.

legohead6

Help with searching through database

Recommended Posts

ok i have an online market place and i have kinda been leaving the search thing to the end and now that i acculy have afew listings i think its time to add it! currently it seems to leave out some matches and the paginating goes screwy! it shows the number of pages that all the listings take up and if you click on the next page it goes to what would normally show up on the next oage(nothing to do with search) then if you go back to the other page its back to the normal few listings! I have tried reseting values of the paginating thing to 0 so they dont work but it never works...i dont care if paginating doesnt work at all on the search but as long as it doesnt let users passed the page with the search results here is the code but warning its 102 lines!

[code]<?PHP
session_start();
$user=$_SESSION['user'];
echo "<div align=center><img border=0 src=images/Logo1.JPG width=305 height=194><br><p align=center>Welcome $user! <font size=2>Not you? <a href=index.php>Sign in!</a></font><br><hr><br>";
echo "<a href=browse.php>Return to List of items</a><br><br>";
echo "<a href=home.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 MATCH(title, description) AGAINST ('$search') LIMIT $start, $display";
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
$re= mysql_num_rows($result);
if($re > 0)
{
echo "<p>$re Search results";
echo "<table border=1>";
while($row = mysql_fetch_row($result)){
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></tr><td width=100>Price: $$row[2]</p></td></tr>";
}
}else{
echo "<table border=1>";
echo "<tr><td width=500><p align=center>NO Matches were Found</p></td></tr>";
}
}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">
    <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]

heres the exact page
[a href=\"http://shop.mattswebpage.com/browse.php\" target=\"_blank\"]http://shop.mattswebpage.com/browse.php[/a]

Share this post


Link to post
Share on other sites

×

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.