Jump to content

Search TO and FROM query.


Wayniac

Recommended Posts

Greetings everyone,

 

I'm attempting to set up a search for a FROM date and a TO date so it will filter out specific records. The search works fine, my only deviations in the script below is I added two "find2" textboxes where normally there is only one. I found a link to a presumable working code that allows me to do such. But am uncertain on how to implement this change. I will provide the link below.

 

http://www.daniweb.com/forums/thread53025.html

 

Thank you all in advance :)

 

// CLIENT SIDE (Allows the user to search for a record by entering a FROM date and a TO date.)

<form action="<?=$PHP_SELF?>" method="post" name="search2" id="search2">
          <input type="hidden" name="field" value="date"/>
Select a date: <br />
<input name="find2" type="text" id="find2" size="15"/>
<br />
<input name="find2" type="text" id="find2" size="15"/>
  <input type="hidden" name="searching2" value="yes" />
  <input type="submit" name="search" value="Search" />
        </form>

 

 

// SERVER SIDE (Enables the option to search for a date of a specific entry and present the full record.)

<?
//This is only displayed if they have submitted the form
if ($searching2 =="yes")
{
echo "<h2>Results</h2><p>";

//If they did not enter a search term we give them an error
if ($find2 == "")
{
echo "<p>You forgot to enter a search term";
exit;
}

// We preform a bit of filtering
$find2 = strtoupper($find2);
$find2 = strip_tags($find2);
$find2 = trim ($find2);

//Now we search for our search term, in the field the user specified
$data = mysql_query("SELECT * FROM spreadsheet WHERE upper($field) LIKE'$find2'");

//And we display the results
while($myrow = mysql_fetch_array( $data ))
{

echo "<b>First Name: </b>";
echo $myrow['fname'];
echo "<b>Last Name: </b>";
echo $myrow['lname'];
echo "<b>Date: </b>";
echo $myrow['date'];
echo "<b>Address: </b>";
echo $myrow['ship_add'];
echo "<b>City: </b>";
echo $myrow['ship_city'];
echo "<b>State: </b>";
echo $myrow['ship_state'];
echo "<b>Zip: </b>";
echo $myrow['ship_zip'];
echo "<a href=\"wpi_more.php?spreadsheetid=" . "$spreadsheetid" . $row["spreadsheetid"] . "\">" . "Read" . "</span>" . "</a>";
// Now print the options to (Read,Edit & Delete the entry)
echo "<hr align=left width=620 color=\"#4e592f\">";
}

//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}

//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find2;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/205532-search-to-and-from-query/
Share on other sites

Hi Wayniac,

 

Sorry but I am having a hard time understanding what you want to achieve and the code you presented.

 

Are you trying to achieve a code where you can search for an entry within the date range right? If it is, I don't get the point why you were using 2 hidden fields and another two input fields with the same name, as what would happen is that only one input will be read by the server.

 

bluejay,

On client:

<form action="<?=$PHP_SELF?>" method="post" name="search2" id="search2">
Select a date: <br />
from: <input name="datefrom" type="text" id="datefrom" size="15"/>
<br />
to: <input name="dateto" type="text" id="dateto" size="15"/>
  <input type="submit" name="search" value="Search" />
</form>

 

 

On Server:

<?php
$datefrom = trim ($_POST["datefrom"]); // and do whatever filtering you want
$dateto = trim ($_POST["dateto"]); // and do whatever filtering you want

// do the same query thing with the link you found with the variables above

// for anything additional entry for search like an entity to be used for LIKE, add the one you have in WHERE concatenated with AND
?>

 

generally, that's it, but I am not sure if I am getting you right.

 

bluejay,

Thank you so much, I took what you said and applied from what I learned of the tutorial from that link and it works perfectly now. I posted the working code below. Once again for future readers, this will allow the user to filter out between dates.

 

Thank you so much :D

 

// CLIENT SIDE

<form action="<?=$PHP_SELF?>" method="post" name="search2" id="search2">
          <input type="hidden" name="field" value="date"/>
Select a date: <br />
<input name="datefrom" type="text" id="datefrom" size="15"/>
<br />
<input name="dateto" type="text" id="dateto" size="15"/>
  <input type="hidden" name="searching2" value="yes" />
  <input type="submit" name="search" value="Search" />
        </form>

 

// SERVER SIDE

<?
//This is only displayed if they have submitted the form
if ($searching2 =="yes")
{
echo "<h2>Results</h2><p>";

//If they did not enter a search term we give them an error
if ($datefrom == "")
{
echo "<p>You forgot to enter a search term";
exit;
}

if ($dateto == "")
{
echo "<p>You forgot to enter a search term";
exit;
}

$datefrom = trim ($_POST["datefrom"]); // and do whatever filtering you want
$dateto = trim ($_POST["dateto"]); // and do whatever filtering you want

// do the same query thing with the link you found with the variables above

// for anything additional entry for search like an entity to be used for LIKE, add the one you have in WHERE concatenated with AND


//Now we search for our search term, in the field the user specified
#
$data = mysql_query("SELECT * FROM spreadsheet WHERE upper($field) BETWEEN'$datefrom' AND '$dateto'");
//$data = mysql_query("SELECT * FROM spreadsheet WHERE upper($field) LIKE'$dateto'");

//And we display the results
while($myrow = mysql_fetch_array( $data ))
{

echo "<b>First Name: </b>";
echo $myrow['fname'];
echo "<b>Last Name: </b>";
echo $myrow['lname'];
echo "<b>Date: </b>";
echo $myrow['date'];
echo "<b>Address: </b>";
echo $myrow['ship_add'];
echo "<b>City: </b>";
echo $myrow['ship_city'];
echo "<b>State: </b>";
echo $myrow['ship_state'];
echo "<b>Zip: </b>";
echo $myrow['ship_zip'];
echo "<a href=\"wpi_more.php?spreadsheetid=" . "$spreadsheetid" . $row["spreadsheetid"] . "\">" . "Read" . "</span>" . "</a>";
// Now print the options to (Read,Edit & Delete the entry)
echo "<hr align=left width=620 color=\"#4e592f\">";
}

//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}

//And we remind them what they searched for
echo "<b>Searched For:</b> " .$datefrom;
echo "<b>Searched For:</b> " .$dateto;
}
?>

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.