Jump to content

Recommended Posts

Hi everyone,

 

I have a basic search set up here:

http://72.167.22.157/search.html

 

But for some reason I cannot get a simple section to work. Try the above link and search the term 'hil'

 

When you get the results you'll see an address and Contact. My problem is I don't want the word contacts to show up if there are no contacts. I cannot get this to work. When I checked mysql_num_row it says 1 on every result, even if there is no contact...

 

<?php
if (isset($_POST['submit'])) { // If the Submit button was pressed.
if (isset($_POST['search'])) { //If there is data iun the search form.
$search = trim($_POST['search']); //Trim whitespace.
$result = mysql_real_escape_string($search); // Secure the data.
$sql = mysql_query("SELECT DISTINCT FirmName, Address, City, State, PostalCode FROM membership WHERE FirmName LIKE '%".$result."%' ORDER BY FirmName") or die(mysql_error());
$total = mysql_num_rows($sql);
	while ($row = mysql_fetch_assoc($sql)) {
	echo "<fieldset>";
	echo "<legend>" . $row['FirmName'] . "</legend>";
	echo "<b>Address:</b><br />";
	echo $row['Address'];
	echo "<br />";
	echo $row['City'] . ", " . $row['State'] . " " . $row['PostalCode'] . "";
		if ($total > 0) {
		$results = mysql_query("SELECT * FROM membership WHERE FirmName = '".$row['FirmName']."' 
		AND Address = '".$row['Address']. "'
		AND PostalCode = '".$row['PostalCode']. "' ORDER BY principal DESC") or die(mysql_error());
		//$tot = mysql_num_rows($results);
		echo "<br />";
		echo "<br />";
		echo "<b>Contacts:</b><br />"; //Only want this to show if there is at least 1 result
		while ($name = mysql_fetch_array($results)) {
		$principal = ($name['principal'] == 1) ? " - Principal" : "";
		echo $name['FirstName'] . " " . $name['LastName'] . "$principal";
		echo "<br />";				
			}
		 }
	echo "</fieldset>";
	echo "<br />";
 	 }
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/68432-wtf/
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.