Jump to content

query trigger


proctk

Recommended Posts

Below is code for page that displays a query based on search results.  There is also a feature to sort the data based on a value selected by the user from a select option.

 

The initial query returns the results but when I select a sort value all found values are lost.  i echoed the query to see what is happening all appears good as the variable value for $sortBy changes but the query does no keep the results and the screen is returned to blank

 

<?php 
session_start();
include ("Connections/dblocal.php");

$search = mysql_real_escape_string($_POST['search']);

$start = 0;
if(isset($_GET['start'])) {
  $start = intval($_GET['start']);
}
$end = $start + 5;

$sortBy = 'ad_date';
if (isset($_POST['sortBy'])) {
  $sortBy = $_POST['sortBy'];
  $search = $_POST['sortSearch'];
  
}

  $query_sql_get_ads = ("SELECT * FROM ads WHERE title LIKE '%$search%' OR category LIKE '%$search%' OR sub_category LIKE '%$search%' order by $sortBy desc LIMIT $start, $end");
    $sql_get_ads = mysql_query($query_sql_get_ads)or die("SQL Error: $query_sql_get_ads<br>" . mysql_error());

echo  $query_sql_get_ads;

//echo $sortBy.'<br>';
//echo $search;

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Quick Search results</title>

<link href="design/default.css" rel="stylesheet" type="text/css" />


</head>

<body>
<div id="outer">
<?php include('design/banner.php'); ?>

	  <div id="twoColumnRight">
	 		    <table class="forms" style="margin-top:25px;">	
		<form name="sortSearch" method="post" action="<?Php echo $PHP_SELF; ?>">
		<tr> <td colspan="7"><div align="right">Sort By:
		  <select name="sortBy" onchange="this.form.submit()">
		    <option value="">--Select--</option>
		    <option value="ad_type">Type</option>
		    <option value="title">Title</option>
		    <option value="price">Price</option>
		    <option value="views">Views</option>
		    <option value="Status">Status</option>
		    <option value="ad_date">End date</option>
	      </select>
		  <input name="sortSearch" type="hidden" value="<?php echo $_SESSION['temp_search'] ?>" />
		  </div>
		  </form>
		  </td></tr>
<tr>
  <td colspan="7" class="header">
  Search Results </td>
  </tr>
<tr>
      <td style="width:20%;"><div align="center"></div></td>
  <td style="width:10%;"><div align="center">Type</div></td>
      <td style="width:20%;"><div align="center">Item Title </div></td>
      <td style="width:10%;"><div align="center">Price</div></td>
      <td style="width:20%;"><div align="center">End Date </div></td>
  <td style="width:10%;"><div align="center">views</div></td>
  <td style="width:10%;"><div align="center">Status</div></td>
    </tr>
<?php

$color1 = "#CCFFCC";
$color2 = "#BFD8BC";
$row_count = 0;
if(isset($_POST['searchSubmit'])){
while($r = mysql_fetch_array($sql_get_ads)){

    $row_color = ($row_count++ % 2) ? $color1 : $color2;
    ?> 
    <tr>
    <td bgcolor="<?php echo $row_color ?>" style="padding:.2em;"><div align="center"><img src="adImages/<?php echo $r['image']; ?>" height="40" width="40"/></div></td>
 <td bgcolor="<?php echo $row_color ?>"><div align="left"><?php echo $r['ad_type']; ?></div></td>
    <td bgcolor="<?php echo $row_color ?>"><div align="left"><a href="treasureDetails.php?id=<?php echo $r['ad_id'].'&seller='.$r['member_id']; ?>"><?php echo $r['title']; ?></a></div></td>
    <td bgcolor="<?php echo $row_color ?>"><div align="center"><?php echo $r['price']; ?></div></td>
    <td bgcolor="<?php echo $row_color ?>"><div align="center">
    <?php
    $date_array = explode("-",$r['ad_date']);
    $ad_date = mktime(0, 0, 0, $date_array[1], $date_array[2]+30, $date_array[0]);
    $end_date = date("Y-m-d", $ad_date);
    echo $end_date;
    ?>
    </div>    </td>
<td bgcolor="<?php echo $row_color ?>"><div align="center"><?php echo $r['views']; ?></div></td>
<td bgcolor="<?php echo $row_color ?>">

    <?php if($r['status'] == 'Sold'){
    $status_color = '#FF0000; font-weight:bold;';
    }else{
    $status_color = '#000000;';
    }
     ?> 

<div align="center" style="color:<?php echo $status_color; ?>"><?php echo $r['status']; ?></div></td>
    </tr>
    <?php
}


$back = $_SERVER['PHP_SELF']."?start=$end"; ?>
    <tr>
      <td colspan="7"><div align="right"><?php echo "<input type='button' 
  value='Next' onClick='window.location=\"$back\"'>"; ?> <?php echo '<input type=button  value="Prev" onClick="history.go(-1)">';
?></div></td>
      </tr>
<?php } ?>
</table>
	  
	  </div>

	<div id="twoColumnLeft">

		<?php  if(isset($_SESSION['user_id'])) {?>
		<?php include('design/leftlinks.php'); ?>

		<?php }else{
		include('design/externalLinks.php'); 
		}
		 ?>

	</div>

        
	<div id="footer">
	<?php include('design/footer.php'); ?>
	</div>
</div>

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/39111-query-trigger/
Share on other sites

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.