Page 1 of 1

What's wrong with the code?

Posted: Sat Jan 22, 2005 7:18 am
by Stelios
Am trying to create an advance search engine for my database where users can can fill in a form and submit it to search for a song. The problem is that I am using if...elseif and for some reason the code is working just for the last case. Here is my code:

Code: Select all

<?php
$page_title = 'Advanced Search Results';
include_once('header.html');
include_once('mysql_connect.php');
include_once('config.inc');

$var=$_GET&#1111;'title'];
$trimmed=trim($var);
$var1=$_GET&#1111;'artist'];
$trimmed1=trim($var1);
$var2=$_GET&#1111;'type'];
$var3=$_GET&#1111;'rating'];


if (!isset($_SESSION&#1111;'first_name'])) &#123;
	
	header ("Location:  http://" . $_SERVER&#1111;'HTTP_HOST'] . dirname($_SERVER&#1111;'PHP_SELF']) . "/login.php");
	ob_end_clean(); // Delete the buffer.
	exit(); // Quit the script.
&#125;


if ($var && $var1 && $var2 && var3) &#123;
$query = "SELECT * FROM uploads,images,artists WHERE song_title  like "%$trimmed%" AND artist_name like "%$trimmed1%" 
AND type='$var2' AND rating='$var3' AND uploads.song_id = images.image_id AND uploads.artist_id=artists.artist_id ORDER BY upload_date DESC";

&#125; elseif ($var2) &#123;
$query = "SELECT * FROM uploads,images,artists WHERE type='$var2' AND uploads.song_id = images.image_id AND uploads.artist_id=artists.artist_id ORDER BY upload_date DESC";

&#125; elseif ($var3) &#123;
$query = "SELECT * FROM uploads,images,artists WHERE rating='$var3' AND uploads.song_id = images.image_id AND uploads.artist_id=artists.artist_id ORDER BY upload_date DESC";

&#125; elseif ($var1) &#123;
$query = "SELECT * FROM uploads,images,artists WHERE artist_name like "%$trimmed1%" AND uploads.song_id = images.image_id AND uploads.artist_id=artists.artist_id ORDER BY upload_date DESC";

&#125; else &#123;
$query = "SELECT * FROM uploads,images,artists WHERE song_title like "%$trimmed%" AND uploads.song_id = images.image_id AND uploads.artist_id=artists.artist_id ORDER BY upload_date DESC";

$result = mysql_query ($query);
$num_results = mysql_num_rows($result);

echo "<fieldset><h4><legend><font face="Palatino Linotype, Verdana, Arial"><strong>Advanced Search Results</strong></font></font></legend></h4>";
echo '<table border="0" width="100%" cellspacing="2" cellpadding="2" align="center">
<tr bgcolor="#0066FF" bordercolor="#000000">
      <td align="center" width="20%"><font color="#FFFFFF" face="Palatino Linotype, Verdana, Arial"><b>Song Title</b></font></td>
	<td align="center" width="20%"><font color="#FFFFFF" face="Palatino Linotype, Verdana, Arial"><b>Artist/Group</b></font></td>
	<td align="center" width="20%"><font color="#FFFFFF" face="Palatino Linotype, Verdana, Arial"><b>Genre</b></font></td>
</tr>';

while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) &#123;

	// Display each record.
	echo "	<tr>
		
		<td align="center" bgcolor="#FFFFCC">" . stripslashes($row&#1111;'song_title']) . "</td>
		<td align="center" bgcolor="#FFFFCC">" . stripslashes($row&#1111;'artist_name']) . "</td>
		<td align="center" bgcolor="#FFFFCC">" . stripslashes($row&#1111;'type']) . "</td>

		
			</tr>\n";

&#125;
&#125;


echo '</table></fieldset>'; // Close the table.

include_once('footer.html');
?>

Posted: Sat Jan 22, 2005 8:19 am
by neophyte
else {
$query = "SELECT * FROM uploads,images,artists WHERE song_title like \"%$trimmed%\" AND uploads.song_id = images.image_id AND uploads.artist_id=artists.artist_id ORDER BY upload_date DESC";

$result = mysql_query ($query);
$num_results = mysql_num_rows($result);

echo "<fieldset><h4><legend><font face=\"Palatino Linotype, Verdana, Arial\"><strong>Advanced Search Results</strong></font></font></legend></h4>";
echo '<table border="0" width="100%" cellspacing="2" cellpadding="2" align="center">
<tr bgcolor="#0066FF" bordercolor="#000000">
<td align="center" width="20%"><font color="#FFFFFF" face="Palatino Linotype, Verdana, Arial"><b>Song Title</b></font></td>
<td align="center" width="20%"><font color="#FFFFFF" face="Palatino Linotype, Verdana, Arial"><b>Artist/Group</b></font></td>
<td align="center" width="20%"><font color="#FFFFFF" face="Palatino Linotype, Verdana, Arial"><b>Genre</b></font></td>
</tr>';

while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {

// Display each record.
echo " <tr>

<td align=\"center\" bgcolor=\"#FFFFCC\">" . stripslashes($row['song_title']) . "</td>
<td align=\"center\" bgcolor=\"#FFFFCC\">" . stripslashes($row['artist_name']) . "</td>
<td align=\"center\" bgcolor=\"#FFFFCC\">" . stripslashes($row['type']) . "</td>


</tr>\n";

}
}
////////////////////////////////////////END ELSE STATEMENT/////////////////////////////////////////////////



If your trying to get the html to the screen. The only scenario that works is if $var is true and only $var is true. If $var1 $var2 $var3 are true the else statment won't print the html and won't query. I trimmed your code down to bare essentials for testing. Try playing with the 'var' variables at the top and turning various ones on and off (true or false) and see the results.

Code: Select all

<?php

$var=true;
$var1=false;
$var2=false;
$var3=true;

if ($var && $var1 && $var2 && var3) &#123;
echo "whatever1";
&#125; elseif ($var2) &#123;
echo "whatever2";
&#125; elseif ($var3) &#123;
echo "whatever3";
&#125; elseif ($var1) &#123;
echo "whatever4";
&#125; else &#123;
echo "whatever5";
echo "<fieldset><h4><legend><font face="Palatino Linotype, Verdana, Arial"><strong>Advanced Search Results</strong></font></font></legend></h4>";
echo '<table border="0" width="100%" cellspacing="2" cellpadding="2" align="center">
<tr bgcolor="#0066FF" bordercolor="#000000">
      <td align="center" width="20%"><font color="#FFFFFF" face="Palatino Linotype, Verdana, Arial"><b>Song Title</b></font></td>
   <td align="center" width="20%"><font color="#FFFFFF" face="Palatino Linotype, Verdana, Arial"><b>Artist/Group</b></font></td>
   <td align="center" width="20%"><font color="#FFFFFF" face="Palatino Linotype, Verdana, Arial"><b>Genre</b></font></td>
</tr>';
   // Display each record.
   echo 'html to the screen';
&#125;

echo '</table></fieldset>'; // Close the table.

?>