Jump to content

Recommended Posts

guys,

 

i need help on my quick search menu

assuming that this is my quick search menu item

keywords (textfield)

job specialization (list)

submit button

<form id="form1" name="form1" method="post" action="view.php">
  Keywords :            
  <input type="text" name="textfield" />
   <br><br>
  Job Specialization : 
  <select name="select">
<option value="president">president</option>
<option value="vice-president">vice-president</option>
<option value="director">director</option>
<option value="mananger">manager</option>
<option value="supervisor">supervisor</option>
  </select>
  <input type="submit" name="Submit" value="Search" />
</form>

 

 

what will be the proper code in view.php

 

normally we usually put the query select and placed the values of

the keywords, job specialization

 

but i dont have any idea how to construct it

considering the possibility that if there is no data found

how do i show the word

no item found on the page

 

and assuming the data from my database has 2 words

but the user only input the first word

 

how will the codes in view.php function that

whether they type the first or second word only

 

you can still be able to view the item

 

hope you guys can help me with this

 

thanks a lot!

Link to comment
https://forums.phpfreaks.com/topic/40544-need-help-on-quick-search-menu/
Share on other sites

any update or help on this?

 

thanks!

 

it's pretty simple:

<?php
/*view.php*/
        $db = mysql_connect("xxx", "xxx", "xxx");
        mysql_select_db("your_db", $db);

        $sql = "SELECT * FROM your_table WHERE column1 LIKE '%". $_POST['textfield'] ."%' AND column2 = '". $_POST['select'] ."'";
        $query = mysql_query($sql) OR die(mysql_error());

        $numRows = mysql_num_rows($query);

        if($numRows < 1){
                echo "<font color=\"FF0000\">Sorry, there are no matches. Please try again.</font><br />\n";
        }else{
                while($row = mysql_fetch_array($query)){
                        echo $row['column1'] .", ". $row['column2'] .", ". $row['column3'] ." etc..etc...";
                }
        }
?>

 

does that make sense?

here is the code that will do it for you...

 

you need to specify the hostname, username, password, databasename, table name.

 


<?php

$localhost = '';
$user = '';
$pass = '';
$dbName = '';

$keyword = '';
$jobSpe = '';
$table1 = '';

if(isset($_POST['textfield'])  && $_POST['textfield'] !='')
{
$keyword = $_POST['textfield'];
}

if(isset($_POST['select']) && $_POST['select'] !='')
{
$jobSpe = $_POST['select'];
}

$conn = mysql_connect($localhost, $user, $pass) or die("Could Not connect to database.");

mysql_select_db($dbName);

$where = array();

if($keyword != '')
$where[] = " keyword like %".$keywords."%";
if($jobSpe != '')
$where[] = " jobSpecification Like %".$jobSpe."%";

$QueryCond = " where ";

foreach($where as $cond)
{
$QueryCond .= $cond . " AND ";
}

$QueryCond = rtrim($QueryCond,"AND");

if(!empty($where))
{
$query = "Select * from ".$table1." ".$QueryCond;
$result = mysql_query($query);
$totalRows = mysql_num_rows($result);

if($totalRpws == 0)
{
	echo "No records found";
	die();
}

// do wht ever you want to do here...
}

else
{
echo "Please select atleast one Job Spec or write some keyword";
}
?>

boo_lolly can u tell me where the parse error will be..

 

Thanks...

 

for example your sql query. LIKE %something%

 

needs to be LIKE '%something%'

 

your dynamic sql query by adding AND to the end if isset, isn't that efficient. there is room for improvement. try doing something like, if isset, put it into an array. then, implode the array with AND in between.

hi,

 

what will be the code if let say i never type anything from the keywords field and just

select any item from the drop down.

 

with that i can still view all the list of items that fall on that same category?

 

this is my code for my index.php

<form id="form1" name="form1" method="post" action="jobs/view1.php">
  Keywords :            
  <input name="keywords" type="text" size="35" />
   <br>
  <br>
  Job Specialization : 
<select name="specialization">
		<?
		include 'admean/db_connect.php';
		$uSql = "SELECT specialization FROM specialization ORDER by sid";
		$uResult = mysql_query($uSql, $connection);
		if(!$uResult){
		echo 'no data found';
		}else{
		while($uRow = mysql_fetch_array($uResult)){
			?>
<option value="<?= $uRow[specialization]?>"><?= $uRow[specialization]?></option>
		  <?
			}    
			}
			?>			
</select>	
  <input type="submit" name="Submit" value="Search" />
</form>

 

and this is my code for my view1.php

<?php
include '../admean/db_connect.php';

        $sql = "SELECT * FROM job_ads WHERE j_position LIKE '%". $_POST['keywords'] ."%' AND specialization = '". $_POST['specialization'] ."'";
        $query = mysql_query($sql) OR die(mysql_error());
	//$uResult = mysql_query($uSql, $connection);
        $numRows = mysql_num_rows($query);

        if($numRows < 1){
                echo "<font color=\"FF0000\">Sorry, there are no matches. Please try again.</font><br />\n";
        }else{
                while($row = mysql_fetch_array($query)){
                        echo $row['j_position'] .", ". $row['specialization'] .", ". $row['date_posted'] ." etc..etc...";
                }
        }
?>

 

need help on this.

 

thanks!

another scenario is

 

what if i didnt select any items on the drop down

 

but i just type the word on the keyword area

 

what will be the code for this second scenario.

 

need help please.

 

thanks!

 

this is an example. i've achieved the same task like this:

<?php
       /*
        *checks to see which input fields had data
        *then depending on the input field
        *enter in the correct sql query for that input field
        *into a new index in the array $query_array
        */
                        $query_array = array();
                        if(!empty($lname)){
                                $query_array[] = "brideLname LIKE '%". $lname ."%' OR groomLname LIKE '%". $lname ."%'";
                        }
                        if(!empty($fname)){
                                $query_array[] = "brideFname LIKE '%". $fname ."%' OR groomFname LIKE '%". $fname ."%'";
                        }
                        if(!empty($event_day)){
                                $query_array[] = "event_day LIKE '%". $event_day ."%'";
                        }
                        if(!empty($event_month)){
                                $query_array[] = "event_month LIKE '%". $event_month ."%'";
                        }
                        if(!empty($event_year)){
                                $query_array[] = "event_year LIKE '%". $event_year ."%'";
                        }

       /*
        *$query string becomes all the sql queries
        *with 'AND' in between them
        */
                        $query_string = implode(" AND ", $query_array);

                        /*check sql query string*/
                        #echo $query_string ."<br />\n";

                        $result = mysql_query("SELECT * FROM my_search_table WHERE ". $query_string ."") OR die(mysql_error());
?>

 

understand?

The code I gave you above will handel this as well. just change these lines

if($keyword != '')

$where[] = " keyword like %".$keywords."%";

if($jobSpe != '')

$where[] = " jobSpecification Like %".$jobSpe."%";

 

to

 

if($keyword != '')

$where[] = " keyword like '%".$keywords."%'";

if($jobSpe != '')

$where[] = " jobSpecification Like '%".$jobSpe."%'";

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.