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
https://forums.phpfreaks.com/topic/95505-displaying-records-from-a-search/
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,

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.