Jump to content

variable queries


nickelus

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

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.