Jump to content

Recommended Posts

something like this...

if(isset($_POST['submit'])&&($_POST['submit']=="Search")){
$driver=$_POST['driver'];$driver.="`driver` LIKE '%$driver%'";
$sDate=$_POST['sDate'];$sDate.="`pu_date` >= '$sDate%'";
$eDate=$_POST['eDate'];$eDate.="AND `pu_date` <= '$eDate%' ";

$pu_qry="SELECT *
FROM `pu_sched`
WHERE '$sDate','$eDate','$driver'";
mysql_select_db($database_bio);
$pu_rpt=mysql_query($pu_qry);

Link to comment
https://forums.phpfreaks.com/topic/168277-variable-queries/#findComment-887583
Share on other sites

Might not be the best way, and I didnt test it but probably it gives you some ideas unless it doesn't work straight away..

<?php
$where = '';
$flag = false; // Indicates if there was values in where already.
if(isset($_POST['submit'])&&($_POST['submit']=="Search"))
{
if (!empty($_POST['driver']))
{
	$driver = $_POST['driver'];
	$where = "WHERE `driver` LIKE '%$driver%'"
	$flag = true;
}
if (!empty($_POST['sDate']))
{
	if ($flag)
	{
		$sDate = $_POST['sDate'];
		$where = " AND `pu_date` >= '%$sDate%'"
	}
	else
	{
		$sDate = $_POST['sDate'];
		$where = "WHERE `pu_date` >= '%$sDate%'"
		$flag = true;
	}
}
if (!empty($_POST['eDate']))
{
	if ($flag)
	{
		$eDate = $_POST['eDate'];
		$where = " AND `pu_date` <= '%$eDate%'"
	}
	else
	{
		$eDate = $_POST['eDate'];
		$where = "WHERE `driver` <= '%$eDate%'"
	}
}
}
$pu_qry="SELECT * FROM `pu_sched` $where";
mysql_select_db($database_bio);
$pu_rpt=mysql_query($pu_qry);

Link to comment
https://forums.phpfreaks.com/topic/168277-variable-queries/#findComment-887600
Share on other sites

I usually use something like this:

 

<?php
$wheres = array();  // Assume we have no wheres
$driver = getPostValue( 'driver' );
$sdate = getPostValue( 'sdate' );
$edate = getPostValue( 'edate' );
if( $driver !== null ) {
  $driver = mysql_real_escape_string( $driver );
  $wheres[] = "`driver`='{$driver}'";
}
if( $sdate !== null ) {
  $sdate = strtotime( $sdate ); // Using strtotime gives the user more flexibility in their date formats
  if( $sdate !== false ) {
    $sdate = mysql_real_escape_string( date( 'Y-m-d H:i:s', $sdate ) );
    $wheres[] = "`pu_date`>='{$sdate}'";
  }
}
if( $edate !== null ) {
  $edate = strtotime( $edate );
  if( $edate !== false ) {
    $edate = mysql_real_escape_string( date( 'Y-m-d H:i:s', $edate ) );
    $wheres[] = "`pu_date`<='{$edate}'";
  }
}
$wheres = count( $wheres ) > 0 ? (' where ' . implode( ' and ', $wheres ) . ' ') : ' ';
$sql = "select * from `your_table` {$wheres}";
$q = mysql_query( $sql );
// you get the idea

function getPostValue( $name, $default = null ) {
  return isset( $_POST[$name] ) ? $_POST[$name] : $default;
}
?>

 

And combining functions isn't any type of theory.  8P

Link to comment
https://forums.phpfreaks.com/topic/168277-variable-queries/#findComment-887684
Share on other sites

Could always change:

function getPostValue( $name, $default = null ) {
  return isset( $_POST[$name] ) ? $_POST[$name] : $default;
}

// TO
function getPostValue( $name, $default = null ) {
  return isset( $_POST[$name] ) && strlen( $_POST[$name] ) ? $_POST[$name] : $default;
}

 

Depends on your purpose.

Link to comment
https://forums.phpfreaks.com/topic/168277-variable-queries/#findComment-887731
Share on other sites

Yes but, empty() already checks if the variable is set also. So that would be useless extra code.

 

Edit:

Just noticed this actually was about search. So it might be better off checking together with strlen&isset instead empty because if you want to search with value of '0' empty will consider it also as empty.

Link to comment
https://forums.phpfreaks.com/topic/168277-variable-queries/#findComment-887739
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.