Jump to content

Displaying records from a search


PRodgers4284

Recommended Posts

I have a basic search script working that using two dropdown lists, the script works fine but when i run the code it displays all the records in the database, i want the records to only display once the user selects the options from the dropbox lists and clicks submit. Can anyone please help me with this?

 

My code is:

 

<!--Search Job form  -->                    
<form method="POST" action="">
<fieldset>    
<span class="navyboldtxt">    
<label for="jobcatergory">Job Category:  </label></span>
<select name="jobcatergory">
<option value="Please Select">Please Select</option> 
<?php 
  $jobcatergory_opts = array( 
    "Accountancy and Finance", 
    "Banking and Insurance", 
    "Construction", 
    "Customer Service", 
    "Engineering", 
    "Management",
    "Hotel and Catering", 
    "Information Technology",
    "Legal",
    "Marketing", 
    "Medical",
    "Retail",
    "Sales",
    "Secretarial",
    "Transport and Distribution",
    "Working from home",        
  ); 
  foreach($jobcatergory_opts as $opt){
    $selected = $_POST['jobcatergory'] == $opt ? " selected=true":"";
    print "<option value=\"{$opt}\"{$selected}>{$opt}</option>";
  }
?>
</select><p></p>   
<p><label for="joblocation"><span class="navyboldtxt">Job Location:</label></span>    
<select name="joblocation">
<option value="Please Select">Please Select</option>
<?php
  $joblocation_opts = array(
    "Co.Antrim",
    "Co.Armagh",
    "Co.Down",
    "Co.Fermanagh",
    "Co.Londonderry",
    "Co.Tyrone",
  );
  foreach($joblocation_opts as $opt){
    $selected = $_POST['joblocation'] == $opt ? " selected=true":"";
    print "<option value=\"{$opt}\"{$selected}>{$opt}</option>";
  }
?>
</select><span class="redboldtxt">       </span>
</p>
<p><span class="redboldtxt"> <?php echo "$joblocation_message";?><?php echo $error['joblocation']; ?></span>
<input type="submit" value="Find a Job" /></p>
<p> </p>
</fieldset>  
</form>

<?php 

if ("submit"){

$sql = "SELECT * FROM job"; 
$jobcat = mysql_real_escape_string(trim($_POST['jobcatergory'])); 
$jobloc = mysql_real_escape_string(trim($_POST['joblocation']));

if ($jobcat != '' && $jobloc != '') { 
$sql .= " WHERE jobcatergory LIKE '$jobcat%' AND joblocation LIKE '%$jobloc%'";
} 
else if ($jobcat != '' && $jobloc == '') { 
  $sql .= " WHERE jobcatergory LIKE '%$jobcat%'"; 
} 
else if ($jobloc != '' && $jobcat == '') { 
  $sql .= " WHERE joblocation LIKE '%$jobloc%'"; 
} 

$query = mysql_query($sql) or die(mysql_error()); 

if(mysql_num_rows($query) > 0) 
{ 
while ($job = mysql_fetch_array($query)) 
{ 
    $username=$job["username"]; 
    $id=$job["id"]; 
    $jobtitle=$job["jobtitle"];  
    $jobcatergory=$job["jobcatergory"];  
   ?> 
   
   
   <table class="sofT" cellspacing="0">

<tr>
<td class="Header">Company Name</td>
<td class="Header">Job Title</td>
<td class="Header">Job Location</td>
<td class="Header">View Job</td>
<td class="Header">Contact Employer</td>
</tr>
<tr>
<td class="Body"><?php echo $job["username"]; ?></td>
<td class="Body"><?php echo $job["jobtitle"]; ?></td>  
<td class="Body"><?php echo $job["jobcatergory"]; ?></td> 
<td class="Body"><?php echo "<a href='searchjobview.php?username=$username&id=$id'>View Job</a>"?></td>
<td class="Body"><?php echo "<a href='searchemployerdetails.php?username=$username'>Contact Employer</a>"?></td> 
</tr> 
<br>
</table>   
<?php 
  }  
  } 
  else 
  { 
  echo '<p>There are no search results with the search criteria you entered.</p>'; 
  }
  }
?>

 

 

Link to comment
Share on other sites

if ("submit")

 

I guess that should be $_POST['<name of submit button']..  :)

 

he means change this:

 

<?php 

if ("submit"){

$sql = "SELECT * FROM job"; 
$jobcat = mysql_real_escape_string(trim($_POST['jobcatergory'])); 
$jobloc = mysql_real_escape_string(trim($_POST['joblocation']));

if ($jobcat != '' && $jobloc != '') { 
$sql .= " WHERE jobcatergory LIKE '$jobcat%' AND joblocation LIKE '%$jobloc%'";
} 
else if ($jobcat != '' && $jobloc == '') { 
  $sql .= " WHERE jobcatergory LIKE '%$jobcat%'"; 
} 
else if ($jobloc != '' && $jobcat == '') { 
  $sql .= " WHERE joblocation LIKE '%$jobloc%'"; 
} 

$query = mysql_query($sql) or die(mysql_error()); 

if(mysql_num_rows($query) > 0) 
{ 
while ($job = mysql_fetch_array($query)) 
{ 
    $username=$job["username"]; 
    $id=$job["id"]; 
    $jobtitle=$job["jobtitle"];  
    $jobcatergory=$job["jobcatergory"];  
   ?> 

 

to this:

 

<?php 

if ($_POST['submit']){

$sql = "SELECT * FROM job"; 
$jobcat = mysql_real_escape_string(trim($_POST['jobcatergory'])); 
$jobloc = mysql_real_escape_string(trim($_POST['joblocation']));

if ($jobcat != '' && $jobloc != '') { 
$sql .= " WHERE jobcatergory LIKE '$jobcat%' AND joblocation LIKE '%$jobloc%'";
} 
else if ($jobcat != '' && $jobloc == '') { 
  $sql .= " WHERE jobcatergory LIKE '%$jobcat%'"; 
} 
else if ($jobloc != '' && $jobcat == '') { 
  $sql .= " WHERE joblocation LIKE '%$jobloc%'"; 
} 

$query = mysql_query($sql) or die(mysql_error()); 

if(mysql_num_rows($query) > 0) 
{ 
while ($job = mysql_fetch_array($query)) 
{ 
    $username=$job["username"]; 
    $id=$job["id"]; 
    $jobtitle=$job["jobtitle"];  
    $jobcatergory=$job["jobcatergory"];  
   ?> 

----

also you will need to change this:

<input type="submit" value="Find a Job" /></p>

 

to this:

<input type="submit" value="Find a Job" name="submit" /></p>

 

hope this helps,

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.