Jump to content


Search feature, query problems


9 replies to this topic

#1 The Creator

    Young Padawan

  • Members
  • Pip
  • 115 posts
  • Gender:Male
  • Location:England
  • Interests:Computers, Music, Technology, Sport

Posted 24 November 2006 - 05:04 PM

ok, im trying to do a search feature for my site, and this is what i got:

$search = addslashes(htmlspecialchars($_POST['search']));
$section = $_POST['section'];


>>> I DO THIS NEXT BIT CAUS THE CATEGORIES IN THE DB ARE IN NUMBERS NOT WORDS <<<

if($section == "PHP"){
$section = 1;
}

if($section == "HTML"){
$section = 2;
}

if($section == "Tips and Tricks"){
$section = 3;
}

if($section == "Photoshop"){
$section = 4;
}

if($section == "Other"){
$section = 5;
}

if($section == "MySql"){
$section = 6;
}

if($section == "Javascript"){
$section = 7;
}

if($section == "CSS"){
$section = 8;
}

if($section == "Flash"){
$section = 10;
}

if($section == "3Ds Max"){
$section = 14;
}

if($section == "Maya"){
$section = 15;
}

if($section == "Dreamweaver"){
$section = 17;
}

if($section == "ASP"){
$section = 18;
}

if($section == "Visual Basic"){
$section = 19;
}

if($section == "Cinema 4D"){
$section = 20;
}

if($section == "Paintshop Pro"){
$section = 21;
}

if($section == "Fireworks"){
$section = 22;
}

if($section == "Imageready"){
$section = 23;
}

if($section == "C++"){
$section = 24;
}

if($section == "After Effects"){
$section = 25;
}

if($section == "Premiere"){
$section = 26;
}


$query2 = mysql_query("SELECT * FROM tutorial WHERE approved='approved' AND category='%$section%' AND title LIKE '%$search%' OR description LIKE '%$search%'"); 

$check1 = mysql_num_rows($query2);

if($check1 == 0){ 

echo "Please Check back Soon! <br> There are no tutorials yet!"; 

} else {  

echo "<table width='100%' cellpadding='0' cellspacing='0' border='0' style='border-collapse: collapse'>

<tr>


<td width='2%' class='formcontent' style='border-bottom: 1px #BBBBBB solid;'><b>&nbsp;</b></td>

<td width='30%' class='formcontent' style='border-bottom: 1px #BBBBBB solid;'><b>Tutorial & Description</b></td> 

<td width='1%'  class='formcontent' style='border-bottom: 1px #BBBBBB solid;'></td>

<td width='30%' class='formcontent' style='border-bottom: 1px #BBBBBB solid;'><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Rating & Difficulty</b></td> 

<td width='1%'  class='formcontent' style='border-bottom: 1px #BBBBBB solid;'></td>

<td width='1%'  class='formcontent' style='border-bottom: 1px #BBBBBB solid;'></td>

<td width='15%' class='formcontent' style='border-bottom: 1px #BBBBBB solid;'><b>Author</b></td>

<td width='10%' class='formcontent' style='border-bottom: 1px #BBBBBB solid;'><b>Views </b></td>

<td width='5%'  class='formcontent' style='border-bottom: 1px #BBBBBB solid;'></td>


<tr><td>&nbsp;</td></tr>

</tr>";


while($b = mysql_fetch_array($query2)) { 

(code for displaying tutorials - i knw there are no mistakes here)


}


echo "</table>";

}

and basically, it works, but it shows up tutorials from the wrong categories...

i was hoping if u could help me,

thx,

Joe

#2 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 24 November 2006 - 06:12 PM

$query2 = mysql_query("SELECT * FROM `tutorial` WHERE `approved` = 'approved' AND `category` ='$section' AND `title` LIKE '%$search%' OR description LIKE '%$search%'");

Should fix ur problem.

Matt

#3 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 25 November 2006 - 01:03 AM

Theres two things i want to mention.

1. You could database the names and category ids on their own table so you can simply select them (it would help ease future updates).

or 2. Rather than checking a condition 50 times, and take time doing it, use a switch.
switch($section){
	case "PHP":
		$section = 1;
		break;
	case "HTML":
		$section = 2;
		break;
	case "Tips and Tricks":
		$section = 3;
		break;
	case "Photoshop":
		$section = 4;
/* ... */
	case "After Effects":
		$section = 25;
		break;
	case "Premiere":
		$section = 26;
		break;
	default:
		$section = false;
}

Edited by rc69, 25 November 2006 - 01:04 AM.


#4 The Creator

    Young Padawan

  • Members
  • Pip
  • 115 posts
  • Gender:Male
  • Location:England
  • Interests:Computers, Music, Technology, Sport

Posted 25 November 2006 - 09:27 AM

thanks for the replies guys,

but the code u gave Mr. Matt works, its just that it shows all tutorials, not ones in the specific sections...

also if u notice on my site (the one in my sig) the search bar goes over two lines for some reason, i have put no line breaks or anything, why does it do that?

Edited by The Creator, 25 November 2006 - 10:06 AM.


#5 The Creator

    Young Padawan

  • Members
  • Pip
  • 115 posts
  • Gender:Male
  • Location:England
  • Interests:Computers, Music, Technology, Sport

Posted 26 November 2006 - 04:10 PM

still having some problems, noone can help?

#6 Av-

    I Feel Left Out

  • Members
  • PipPipPipPip
  • 1,971 posts
  • Gender:Male
  • Location:10 ft. below sea level

Posted 26 November 2006 - 05:01 PM

Can't see any problems, what exactly do you mean by going over two lines?

#7 The Creator

    Young Padawan

  • Members
  • Pip
  • 115 posts
  • Gender:Male
  • Location:England
  • Interests:Computers, Music, Technology, Sport

Posted 27 November 2006 - 01:47 PM

dont worry i fixed that the other day, its the search problem i really need help with... =(

the new code:

$search = addslashes(htmlspecialchars($_POST['search']));

$impcat = $_POST['section'];

if($impcat == "all"){

$section = "AND";

} else {
$section = "AND `category` = '%$impcat%' AND";
}

$query2 = mysql_query("SELECT * FROM `tutorial` WHERE `approved` = 'approved' $section `title` LIKE '%$search%' OR description LIKE '%$search%'");

$check1 = mysql_num_rows($query2);


if (empty($_POST['search'])){
echo "You didnt enter anything into the search box";

} else {

if($check1 == 0){ 

echo "No tutorials were Found"; 

} else {


after the else it displays the tutorial...

thanks again,

Joe

#8 The Creator

    Young Padawan

  • Members
  • Pip
  • 115 posts
  • Gender:Male
  • Location:England
  • Interests:Computers, Music, Technology, Sport

Posted 30 November 2006 - 02:23 PM

someone please help, this problem has not been resolved...

#9 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 30 November 2006 - 05:12 PM

Not quite sure what exactly you are asking really.
But try this.

$search = addslashes(htmlspecialchars($_POST['search']));
$section = ($_POST['section'] == 'all') ? 'AND' : "AND `category` = '%$impcat%' AND";
$sql = "SELECT * FROM `tutorial` WHERE `approved` = 'approved' $section `title` LIKE '%$search%' OR description LIKE '%$search%'";

echo $sql; // Make sure it is showing what it should in the syntax
$query2 = mysql_query($sql) or die(mysql_error());

$check1 = mysql_num_rows($query2);

if (empty($_POST['search'])) echo 'You didnt enter anything into the search box';
else{
if($check1 == 0) echo 'No tutorials were Found';
else{
// Tutorial Here
}

I myself use the RLIKE command for searches, as I had less problems getting it to work with me correctly. It's basically a synonym for the command REGEX, and would go like this in your case.
$sql = "SELECT * FROM `tutorial` WHERE `approved` = 'approved' $section `title` RLIKE '$search' OR description RLIKE '$search'";

Feel free to try that if the above doesn't yield any debugging problems.

#10 The Creator

    Young Padawan

  • Members
  • Pip
  • 115 posts
  • Gender:Male
  • Location:England
  • Interests:Computers, Music, Technology, Sport

Posted 01 December 2006 - 04:46 PM

sorry, didnt help, ill keep trying this weekend :ph34r:





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users