Jump to content

Recommended Posts

guys,

 

i need help. i have an advance search form that its working if you select at least 2 or 3 fields on that

form and the result will display correctly.

 

my problem is if i dont select any fields or items in my advance search button

i am getting errors.

 

this is the codes in my advance search form

<form id="form1" name="form1" method="post" action="view3.php">
          <table width="599" border="0" cellspacing="4" cellpadding="0">
            <tr>
              <td colspan="2" valign="top" class="text7">: ADVANCED SEARCH : </td>
              <td width="91" align="right" valign="top"> </td>
              <td width="201" align="left" valign="top"> </td>
            </tr>
            <tr>
              <td colspan="4" align="right" valign="top" bgcolor="#B30110"><img src="../images/main/spacer.gif" width="1" height="1"></td>
              </tr>
            <tr>
              <td width="111" align="right" valign="top"> </td>
              <td width="176" align="left" valign="top"> </td>
              <td width="91" align="right" valign="top"> </td>
              <td width="201" align="left" valign="top"> </td>
            </tr>
            <tr>
              <td align="right" valign="top"><span class="text6">Keywords :</span></td>
              <td width="176" align="left" valign="top"><input type="text" name="keywords"></td>
              <td width="91" align="right" valign="top"><span class="text6">Location
                :</span></td>
              <td width="201" align="left" valign="top"><select name="country">
                <option value="">Select Location</option>
                <option value="Singapore">Singapore</option>
                <option value="Malaysia">Malaysia</option>
                <option value="Philippines">Philippines</option>
                <option value="India">India</option>
                <option value="Hong Kong">Hong Kong</option>
                <option value="">---------------------</option>
                <option value="North America">North America</option>
                <option value="Middle East">Middle East</option>
                <option value="Europe">Europe</option>
              </select></td>
            </tr>
            <tr>
              <td rowspan="3" align="right" valign="top"><span class="text6">Specialization</span> :</td>
              <td rowspan="3" align="left" valign="top">
		  <select name="specialization" size="10" class="textfield1" multiple>
                <option value=""></option>
                <?php
	$uSql = "SELECT specialization FROM specialization ORDER by sid ASC";
	$uResult = mysql_query($uSql, $connection);

	while($uRow = mysql_fetch_row($uResult)){
		echo "<option value=\"". $uRow[0] ."\">". $uRow[0] ."\n";
	}    
?>
              </select></td>
              <td align="right" valign="top"><span class="text6">Position Level :</span></td>
              <td valign="top"><select name="level" multiple>
                <option value=""></option>
                <?php
	$uSql = "SELECT level FROM level";
	$uResult = mysql_query($uSql, $connection);

	while($uRow = mysql_fetch_array($uResult)){
		echo "<option value=\"". $uRow['level'] ."\">". $uRow['level'] ."\n";
	}    
?>
              </select></td>
            </tr>
            <tr>
              <td align="right" valign="top"><span class="text6">Job Type 
                : </span></td>
              <td valign="top"> </td>
            </tr>
            <tr>
              <td valign="top"> </td>
              <td valign="top"><span class="text3">
                <input type="checkbox" name="jtype1" value="Permanent" >
Permanent<br>
<input type="checkbox" name="jtype2" value="Contract" />
Contract<br>
<input type="checkbox" name="jtype3" value="Part-Time" />
Part-Time </span></td>
            </tr>
            <tr>
              <td align="right" valign="top"> </td>
              <td align="left" valign="top"> </td>
              <td colspan="2" valign="top"> </td>
            </tr>
            
            <tr>
              <td colspan="4" valign="top" bgcolor="#B30110"><img src="../images/main/spacer.gif" width="1" height="1"></td>
            </tr>
            <tr>
              <td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
            </tr>
            <tr>
              <td colspan="4"> </td>
            </tr>
          </table>
        </form>

 

 

and this is the code in my view3.php page

<?php
include '../admean/db_connect.php';
include("../admean/pagina/my_pagina_class.php");

//establish query array
$query_array = array();


//append each input field into a new index
//in the query_array if the user selected
//it or entered information in the field
if(!empty($_POST['keywords'])){
$query_array[] = "j_position LIKE '%". $_POST['keywords'] ."%'";
}
if(!empty($_POST['specialization'])){
$query_array[] = "specialization = '". $_POST['specialization'] ."'";
}

if(!empty($_POST['level'])){
$query_array[] = "level = '". $_POST['level'] ."'";
}

if(!empty($_POST['jtype1'])){
$query_array[] = "j_type1 = '". $_POST['jtype1'] ."'";
}

if(!empty($_POST['jtype2'])){
$query_array[] = "j_type2 = '". $_POST['jtype2'] ."'";
}

if(!empty($_POST['jtype3'])){
$query_array[] = "j_type3 = '". $_POST['jtype3'] ."'";
}
if(!empty($_POST['country'])){
$query_array[] = "country = '". $_POST['country'] ."'";
}
//turn array into string
$query_string = implode(" AND ", $query_array);


//i don't know what's going on right here with the dropdowns...
//-for the drop down menu
if(isset($_GET['sort1']) && $_GET['sort1'] !=''){
     $sort1 = $_GET['sort1'];
}else{
     $sort1 = "j_position";
}


if(isset($_GET['sort2']) && $_GET['sort2']!=''){
     $sort2 = $_GET['sort2'];
}else{
     $sort2 = "ASC";
}


$test = new JHpage;

//this is where we use the query string...
//also, i'd put the conditional order by statement here, not up above.
//on top of that, you need a comma (,) between your two sort variables.

//i didn't change anything else... let me know how this works out for ya.
$test->sql = "SELECT * FROM job_ads WHERE j_status ='open' AND ". $query_string ." ORDER BY ".$sort1 ." ".$sort2."";


// the (basic) sql statement (use the SQL whatever you like)
$result = $test->get_page_result(); // result set
$num_rows = $test->get_page_num_rows(); // number of records in result set 
$nav_links = $test->navigation(" | ", "currentStyle"); // the navigation links (define a CSS class selector for the current link)
$nav_info = $test->page_info(); // information about the number of records on page ("to" is the text between the number)
$simple_nav_links = $test->back_forward_link(true); // the navigation with only the back and forward links, use true to use images
$total_recs = $test->get_total_rows(); // the total number of records
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>:: Job HiRings - Jobs ::</title>

<style type="text/css">
<!--
body {
background-image: url(../images/main/bg.gif);
}
-->
</style>
<link href="../css.css" rel="stylesheet" type="text/css">
<script type="text/JavaScript">
<!--
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
//-->
</script>
</head>

<body>
<table width="776" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td><table width="776" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td colspan="3" valign="top"><img src="../images/main/top.gif" width="776" height="6"></td>
      </tr>
      <tr>
        <td width="6" align="left" valign="top" background="../images/main/left.gif"><img src="../images/main/left.gif" width="6" height="14"></td>
        <td width="764" bgcolor="#FFFFFF"><table width="764" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="239" colspan="2" valign="top"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="764" height="346">
              <param name="movie" value="../flash/main.swf">
              <param name="quality" value="high">
              <embed src="../flash/main.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="764" height="346"></embed>
            </object></td>
          </tr>
          
          <tr>
            <td height="62" colspan="2" align="left" valign="top" bgcolor="#FFFFFF"><table width="764" border="0" cellspacing="0" cellpadding="0">
              <form method="post" action="view.php" name="searchform"><tr>
                <td width="181" valign="top" background="../images/main/bgbar.gif"> </td>
                <td width="10" valign="top" background="../images/main/bgbar.gif"><img src="../images/main/spacer.gif" width="10" height="50"></td>
                <td width="573" align="right" valign="middle" background="../images/main/bgbar.gif"> <span class="text7">Keywords</span>
                    <input name="keywords" type="text" class="field2">
                    <select name="specialization" class="field3">
                      <option value="">All</option>
                      <?
					$uSql = "SELECT specialization FROM specialization";
					$uResult = mysql_query($uSql, $connection);
					if(!$uResult){
					echo 'no data found';
					}else{
					while($uRow = mysql_fetch_row($uResult)){
						?>
                      <option value="<?= $uRow[0]?>">
                        <?= $uRow[0]?>
                        </option>
                      <?
						}    
						}
						?>
                    </select>
                    <input name="Submit2" type="submit" class="button1" value="Search">
   </td>
              </tr></form>
              <tr>
                <td colspan="3" valign="top"> </td>
              </tr>
              <tr>
                <td colspan="3" align="center" valign="top">
<table width="755" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="6"><img src="../admean/images/spacer.gif" width="6" height="10"></td>
    <td width="743" class="text9"><table width="216" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td colspan="3" valign="top" bgcolor="#666666"><img src="../admean/job/images/spacer.gif" width="1" height="1"></td>
      </tr>
      <tr>
        <td align="left" bgcolor="#666666" width="1"><img src="../admean/job/images/spacer.gif" width="1" height="1"></td>
        <td width="214" valign="top"><table width="741" border="0" cellspacing="2" cellpadding="0">
          <tr>
            <td colspan="5"><form method="get" action="view.php">
              <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td width="450"><span class="text6">
                    <?php
				 echo "   total record found: " .$total_recs; ?>
                  </span></td>
                  <td width="287" align="right" class="text6"><span class="text7">sort by :</span>
                    <select name="sort1">
                        <option value="j_position">Position</option>
                        <option value="location">Location</option>
                        <option value="date_posted">Date Posted</option>
                      </select>
                     
                      <select name="sort2">
                        <option value="ASC">ASC</option>
                        <option value="DESC">DESC</option>
                      </select>
                      <input type="submit" name="Submit" value="Go" />
                       </td>
                </tr>
              </table>
            </form>            </td>
          </tr>
          <tr>
            <td colspan="5"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td width="60%" align="left" class="text8"> </td>
                  <td width="39%" align="right"><!-- nav top -->
                      <?
				echo "<span class='link1'>".$nav_links."</span>";
				?>                  </td>
                  <td width="1%" align="right"><img src="../image/spacer.gif" width="1" height="1" /></td>
                </tr>
            </table></td>
          </tr>
          <tr>
            <td width="78" bgcolor="950101" class="text9">  Date</td>
            <td width="300" valign="top" bgcolor="950101" class="text9">   Position </td>
            <td width="155" bgcolor="950101" class="text9">  Company </td>
            <td width="198" colspan="2" bgcolor="950101" class="text5">  <span class="text9">Location</span></td>
          </tr>
          <?php 
for ($i = 0; $i < $num_rows; $i++) {
$date_posted = mysql_result($result, $i, "date_posted");
$jobid = mysql_result($result, $i, "jobid");
$j_position = mysql_result($result, $i, "j_position");
$c_name = mysql_result($result, $i, "c_name");
$location = mysql_result($result, $i, "location");

?>
          <tr>
            <td bgcolor="#e5e5e5" class="text4">  <? echo "$date_posted"; ?></td>
            <td bgcolor="#e5e5e5">  <a href="javascript:;" class="link1" onClick="MM_openBrWindow('jobpost.php?jobid=<? echo "$jobid"; ?>','','scrollbars=yes,width=720,height=700')"><? echo "$j_position";?></a></td>
            <td bgcolor="#e5e5e5" class="text4">  JobHirings </td>
            <td bgcolor="#e5e5e5" class="text4">  <? echo "$location";?>  </td>
          </tr>
          <?        
}
?>
          <tr>
            <td colspan="5" bgcolor="#950101"><img src="../admean/images/spacer.gif" width="10" height="2" /></td>
          </tr>
          <tr>
            <td colspan="5"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td width="60%" align="left" class="text8"> </td>
                  <td width="39%" align="right"><!-- nav top -->
                      <?
				echo "<span class='link1'>".$nav_links."</span>";
				?>                  </td>
                  <td width="1%" align="right"><img src="../image/spacer.gif" width="1" height="1" /></td>
                </tr>
            </table></td>
          </tr>
  </form>
  
  <tr>
    <td colspan="5"> </td>
  </tr>
        </table></td>
        <td align="right" bgcolor="#666666" width="1"><img src="../admean/job/images/spacer.gif" width="1" height="1"></td>
      </tr>
      <tr>
        <td colspan="3" valign="top" bgcolor="#666666"><img src="../admean/job/images/spacer.gif" width="1" height="1"></td>
      </tr>
    </table></td>
    <td width="10"><img src="../admean/images/spacer.gif" width="6" height="10"></td>
  </tr>
</table>				</td>
              </tr>
              <tr>
                <td colspan="3" valign="top"> </td>
              </tr>
            </table></td>
          </tr>
          <tr>
            <td colspan="2" valign="top" style="background:url(../images/main/footer.gif); background-repeat:no-repeat">
		<? include '../footer.php'; ?>			</td>
          </tr>
        </table></td>
        <td width="6" background="../images/main/right.gif" align="right"><img src="../images/main/right.gif" width="6" height="3"></td>
      </tr>
      <tr>
        <td colspan="3" valign="bottom"><img src="../images/main/bottom.gif" width="776" height="8"></td>
      </tr>
    </table></td>
  </tr>
</table>
</body>
</html>

 

 

hope you could help me with this.

 

thanks so much!

Link to comment
https://forums.phpfreaks.com/topic/43467-problem-on-my-advance-search/
Share on other sites

the reason you're getting errors when you DONT select a search criteria is because your variable $query_string is NOT being populated therefore your query has errors in it.

 

if you dont select any criteria your query looks like this:

 

<?php
$test->sql = "SELECT * FROM job_ads WHERE j_status ='open' AND ORDER BY ".$sort1 ." ".$sort2."";
/*$query_string is not populated if no search criteria is selected.  
this will cause the query to be invalid and cause errors.
put in some type of validation that says if $query_string == '' 
then $test->sql = "Select * from job_ads where j_status='open' order by ".$sort1." ".$sort2."";
something like that will fix your problem*/

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.