Jump to content

Dumb Question


frank_solo

Recommended Posts

I have a script and for some reason I can't get to echo "No results" when nothing matches. I know it is something simple but I can't see it. Can anyone help?

<?php
mysql_connect ("localhost", "","")  or die (mysql_error());
mysql_select_db ("");

$search = mysql_real_escape_string(preg_replace('/[^\w\'\"\@\-\.\,\(\) ]/i', '', $_POST['search']));

if(strlen($search) < 3){

echo "<br /><input type='image' value='Back' src='images/min.png' alt='Back' onClick='history.go(-1);return true;'></FORM>";
}
else{
$sql = mysql_query("SELECT * FROM apartments WHERE contact LIKE '%$search%' OR phone LIKE '%$search%' OR office LIKE '%$search%' OR town LIKE '%$search%' OR cross_streets LIKE '%$search%' OR description LIKE '%$search%' OR email LIKE '%$search%' OR rent LIKE '%$search%' order by `date_created`");
	if ( mysql_num_rows($sql) > 0 )
{
echo "<strong style='color: #6EBEFB'>Click Headers to Sort</strong>";
echo "<br/><strong style='color: #6EBEFB'>Your Results for: </strong>"; 
echo htmlentities($_POST['search']);
echo "<table border='0' align='center' bgcolor='#6EBEFB' cellpadding='2' bordercolor='#000000' table class='sortable' table id='results'> 
<tr>
<th style='color: #FFF'> Title </th> 
<th style='color: #FFF'> Rent </th>
<th style='color: #FFF'> Bed </th>
<th style='color: #FFF'> Bath </th>
<th style='color: #FFF'> Contact </th> 
<th style='color: #FFF'> Office </th> 
<th style='color: #FFF'> Phone </th> 
<th style='color: #FFF'> Image </th>
</tr>";

while ($row = mysql_fetch_array($sql))
{
$image = (!empty($row['imageurl1'])) ? "<img src=\"../user/{$row['imageurl1']}\" width='50'>" : 'No Images';

echo "<tr>
	<td bgcolor='#FFFFFF' style='color: #000' align='center'>
	   <a href='searchapts/index.php?id=".$row['id']."'>" . $row['title'] . "</a></td>
	<td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['rent'] . "</td>
	<td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['rooms'] . "</td>
	<td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['bath'] . "</td>
	<td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['contact'] . "</td> 
	<td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['office'] . "</td> 
	<td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['phone'] . "</td>
	<td bgcolor='#FFFFFF' style='color: #000' align='center'>{$image}</td> 
</tr>"; 



}
} echo "</table>";}
{
  echo "No Results";}



?>

Link to comment
Share on other sites

Sorry is this better?

 

<?php

mysql_connect ("localhost", "","")  or die (mysql_error());

mysql_select_db ("");

$search = mysql_real_escape_string(preg_replace('/[^\w\'\"\@\-\.\,\(\) ]/i', '', $_POST['search']));

if(strlen($search) < 3){

echo "<br /><input type='image' value='Back' src='images/min.png' alt='Back' onClick='history.go(-1);return true;'></FORM>";

		}

  		else {

$sql = mysql_query("SELECT * FROM apartments WHERE contact LIKE '%$search%' OR phone LIKE '%$search%' OR office LIKE '%$search%' OR town LIKE '%$search%' OR cross_streets LIKE '%$search%' OR description LIKE '%$search%' OR email LIKE '%$search%' OR rent LIKE '%$search%' order by `date_created`");

if ( mysql_num_rows($sql) > 0 )

{

echo "<strong style='color: #6EBEFB'>Click Headers to Sort</strong>";
echo "<br/><strong style='color: #6EBEFB'>Your Results for: </strong>"; 
echo htmlentities($_POST['search']);
echo "<table border='0' align='center' bgcolor='#6EBEFB' cellpadding='2' bordercolor='#000000' table class='sortable' table id='results'> 
<tr>
<th style='color: #FFF'> Title </th> 
<th style='color: #FFF'> Rent </th>
<th style='color: #FFF'> Bed </th>
<th style='color: #FFF'> Bath </th>
<th style='color: #FFF'> Contact </th> 
<th style='color: #FFF'> Office </th> 
<th style='color: #FFF'> Phone </th> 
<th style='color: #FFF'> Image </th>
</tr>";

while ($row = mysql_fetch_array($sql))

{

	$image = (!empty($row['imageurl1'])) ? "<img src=\"../user/{$row['imageurl1']}\" width='50'>" : 'No Images';

	echo "<tr>

	<td bgcolor='#FFFFFF' style='color: #000' align='center'>
	   <a href='searchapts/index.php?id=".$row['id']."'>" . $row['title'] . "</a></td>
	<td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['rent'] . "</td>
	<td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['rooms'] . "</td>
	<td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['bath'] . "</td>
	<td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['contact'] . "</td> 
	<td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['office'] . "</td> 
	<td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['phone'] . "</td>
	<td bgcolor='#FFFFFF' style='color: #000' align='center'>{$image}</td> 
	</tr>"; 

}

	} echo "</table>";}

		{
  			
			echo "No Results";}



?>

Link to comment
Share on other sites

Not much, consistent indenting will lead to cleaner code, and code that is easy to spot errors in.  It doesn't take long to do, and even less if you do it while you are coding.

<?php

mysql_connect ("localhost", "","")  or die (mysql_error());

mysql_select_db ("");

$search = mysql_real_escape_string(preg_replace('/[^\w\'\"\@\-\.\,\(\) ]/i', '', $_POST['search']));

if(strlen($search) < 3) {
echo "<br /><input type='image' value='Back' src='images/min.png' alt='Back' onClick='history.go(-1);return true;'></FORM>";
} //close if
else {
 $sql = mysql_query("SELECT * FROM apartments WHERE contact LIKE '%$search%' OR phone LIKE '%$search%' OR office LIKE '%$search%' OR town LIKE '%$search%' OR cross_streets LIKE '%$search%' OR description LIKE '%$search%' OR email LIKE '%$search%' OR rent LIKE '%$search%' order by `date_created`");
if ( mysql_num_rows($sql) > 0 ) {
	echo "<strong style='color: #6EBEFB'>Click Headers to Sort</strong>";
	echo "<br/><strong style='color: #6EBEFB'>Your Results for: </strong>"; 
	echo htmlentities($_POST['search']);
	echo "<table border='0' align='center' bgcolor='#6EBEFB' cellpadding='2' bordercolor='#000000' table class='sortable' table id='results'> 
		<tr>
		<th style='color: #FFF'> Title </th> 
		<th style='color: #FFF'> Rent </th>
		<th style='color: #FFF'> Bed </th>
		<th style='color: #FFF'> Bath </th>
		<th style='color: #FFF'> Contact </th> 
		<th style='color: #FFF'> Office </th> 
		<th style='color: #FFF'> Phone </th> 
		<th style='color: #FFF'> Image </th>
		</tr>";

	while ($row = mysql_fetch_array($sql)) {
		$image = (!empty($row['imageurl1'])) ? "<img src=\"../user/{$row['imageurl1']}\" width='50'>" : 'No Images';
		echo "<tr>
			<td bgcolor='#FFFFFF' style='color: #000' align='center'>
			<a href='searchapts/index.php?id=".$row['id']."'>" . $row['title'] . "</a></td>
			<td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['rent'] . "</td>
			<td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['rooms'] . "</td>
			<td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['bath'] . "</td>
			<td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['contact'] . "</td> 
			<td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['office'] . "</td> 
			<td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['phone'] . "</td>
			<td bgcolor='#FFFFFF' style='color: #000' align='center'>{$image}</td> 
			</tr>"; 
	} //close while
} //close if
echo "</table>"; //this needs to be moved above the last bracket, otherwise you could close a table that you haven't opened.
} //close else

{ //there is no clause for this bracket statement. Perhaps an else after the closing bracket for the if(mysql_num_rows()) clause.
  		echo "No Results";
  	}

?>

Link to comment
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.