Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 02-03-2010, 05:22 PM PHP search help?
Skilled Talker

Posts: 65
Name: Tony Connelly
Location: Scotland
Trades: 0
Hi I have been on before and have found some great help on a php script to submit reviews to my site. I now have this working using PHP and my mysql database I set up and can fill in a form and submit to the fields of the table I created.

I also created a script to retrieve the table and display on my website all entries.

What I need now is some help with a search function. I found a good tutorial and have completed the search part and it sends back results to my page as hyperlinks that end in .php?id=1 etc... the number going up relating to the ID field of my mysql table. If I click on these hyperlinks I thought it would display the table entry for that ID but it just goes back to my search box.

To see what I mean if you go to http://www.guitarandsong.com/search_reviews.php and type in guitar it returns two results. That you can hover over and see the .php?id=1 end.

Any Ideas? Here is the code I have used so far from tutorials.

PHP Code:
<?php   
  
if(isset($_POST['submit'])){   
  if(isset(
$_GET['go'])){   
  if(
preg_match("/^[  a-zA-Z]+/"$_POST['name'])){   
  
$name=$_POST['name'];   
 
$db=mysql_connect  ("localhost""web117-reviews""weejon95") or die ('I cannot connect to the database  because: ' mysql_error());
$mydb=mysql_select_db("web117-reviews");
   
$sql="SELECT  ID, Title, Equipment FROM reviews WHERE Title LIKE '%" $name .  "%' OR Equipment LIKE '%" $name ."%'";   
       
$result=mysql_query($sql);   
  
    
while(
$row=mysql_fetch_array($result)){  
     
          
$Title  =$row['Title'];   
          
$Equipment=$row['Equipment'];   
          
$ID=$row['ID']; 
   
   
  echo 
"<ul>\n";   
  echo 
"<li>" "<a  href=\"search_reviews.php?id=$ID\">"   .$Title " " $Equipment .  " " $Build .  " " $Functions .  "</a></li>\n";   
  echo 
"</ul>";   
   }   
   }   
  else{   
  echo  
"<p>Please enter a search query</p>";   
   }   
   }   
   }  
  
if(isset(
$_GET['by'])){
$letter=$_GET['by'];
$db=mysql_connect  ("localhost""web117-reviews""weejon95") or die ('I cannot connect to the database  because: ' mysql_error());
$mydb=mysql_select_db("web117-reviews");

$sql="SELECT  ID, Title, Equipment FROM reviews WHERE Title LIKE '%" $letter "%' OR Equipment LIKE '%" $letter ."%'";

$result=mysql_query($sql);

$numrows=mysql_num_rows($result);
echo  
"<p>" .$numrows " results found for " $letter "</p>";

while(
$row=mysql_fetch_array($result)){
$Title  =$row['Title'];
            
$Equipment=$row['Equipment'];
            
$ID=$row['ID'];

echo  
"<ul>\n";
echo  
"<li>" "<a  href=\"search_reviews.php?id=$ID\">"   .$Title " " $Equipment .  "</a></li>\n";
echo  
"</ul>";
}
}
?>
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Last edited by chrishirst; 02-03-2010 at 08:11 PM.. Reason: php code tags added
connelly73 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-03-2010, 09:53 PM Re: PHP search help?
Average Talker

Posts: 16
Name: Filip Filipov
Location: Bulgaria
Trades: 0
Quote:
If I click on these hyperlinks I thought it would display the table entry for that ID but it just goes back to my search box.
I can't see in this script where you check for $_GET['id'].

Try this:

if(isset($_GET['id'])){
$db=mysql_connect ("localhost", "web117-reviews", "weejon95") or die ('I cannot connect to the database because: ' . mysql_error());
$mydb=mysql_select_db("web117-reviews");
$sql="SELECT ID, Title, Equipment FROM reviews WHERE ID =
" . $_GET['id'];
$result=mysql_query($sql);
$numrows=mysql_num_rows($result);
echo
"<p>" .$numrows . " results found for ID: " .
$_GET['id']. "</p>";
while(
$row=mysql_fetch_array($result)){
$Title =$row['Title'];
$Equipment=$row['Equipment'];
$ID=$row['ID'];
echo
"<ul>\n";
echo
"<li>" . "<a href=\"search_reviews.php?id=$ID\">" .$Title . " " . $Equipment . "</a></li>\n";
echo
"</ul>";
}
}
primecode is offline
Reply With Quote
View Public Profile Visit primecode's homepage!
 
Old 02-04-2010, 12:49 PM Re: PHP search help?
Skilled Talker

Posts: 65
Name: Tony Connelly
Location: Scotland
Trades: 0
Added the code with my existing code (not sure if you meant it was to replace it). It still worked but link when clicked on just disp[layed text like
"1 results found for ID: 3" but didn't display contents of mysql tabe?

It does look a bit better any other ideas?
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
connelly73 is offline
Reply With Quote
View Public Profile
 
Old 02-04-2010, 05:48 PM Re: PHP search help?
Skilled Talker

Posts: 65
Name: Tony Connelly
Location: Scotland
Trades: 0
Thanks. Managed to get it working. I also have a search by letter in my code above does anyone know how to get it to just list results on first letter?

If I click A in my list it returns results with A in it. I would like it to be for Title or something begining with A?
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
connelly73 is offline
Reply With Quote
View Public Profile
 
Old 02-05-2010, 01:14 AM Re: PHP search help?
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
To search for only parts of a string (like only first letter) you can use the LIKE operator and wildcards (%). To search for something beginning with the letter A you could use
Code:
SELECT * FROM table WHERE title LIKE 'A%'
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Reply     « Reply to PHP search help?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.12798 seconds with 12 queries