CodeIgniter Forums
default value for a search - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: default value for a search (/showthread.php?tid=21039)



default value for a search - El Forum - 07-29-2009

[eluser]tim1965[/eluser]
Hi
I have a search function that allows a user to search between a start and end date. This works well. However if the user does not poulate the dates and hits submit the form generates a sql error.
I want to apply a default value to $this->input->post('start_date') so that if it isnt populated the query will run and would essentially bring back everything. I am thinking i need to use if(!isset), but cannot figure it out. any help appreciated.

My search function
function data_availability()
{
$start_date=$this->input->post('start_date');
$end_date=$this->input->post('end_date');
$current_date = date("Ymd");


$sql = "SELECT guestcal_entries_static.entry_id, guestcal_entries.object_id, guestcal_entries.class_id, guestcal_entries_static.from, guestcal_entries_static.to FROM guestcal_entries_static inner join guestcal_entries on guestcal_entries_static.entry_id = guestcal_entries.id
where guestcal_entries_static.from >= '$current_date'
and guestcal_entries.class_id in ('2', '3')
and `from` not between $start_date and $end_date
and `to`not between $start_date and $end_date";


$query = $this->db->query($sql);
if($query->num_rows() >0)
{
$row=$query->result_array();
return $row;



default value for a search - El Forum - 07-29-2009

[eluser]jedd[/eluser]
Hi Tim,

Sounds like you just need to do some form validation there. Rather than just isset() you might want to check that you've got a valid date, so maybe strlen check (assuming you're being strict about ISO 8601 / MySQL format dates) for 10 chars (or maybe even just 8), contents are digits and hyphens only, that kind of thing.

Or have I missed the subtle nuances of your question?


default value for a search - El Forum - 07-29-2009

[eluser]tim1965[/eluser]
Hi

Yes the question is how do i apply a default value if the POST value is empty.


default value for a search - El Forum - 07-29-2009

[eluser]rogierb[/eluser]
$_POST['start_date'] = date("Y-m-d");

Would do the trick.

Edit : if(!isset($_POST['start_date'])) $_POST['start_date'] = date("Y-m-d");


default value for a search - El Forum - 07-29-2009

[eluser]tim1965[/eluser]
Thank you

I know this is a dumb question but it has been a long day. Can i just do $start_date = if(!isset($_POST[‘start_date’])) $_POST[‘start_date’] = date(“Y-m-d”); Apologies again i should know the answer but my head is hurting


default value for a search - El Forum - 07-29-2009

[eluser]jedd[/eluser]
[quote author="tim1965" date="1248895859"]Thank you

I know this is a dumb question but it has been a long day. Can i just do $start_date = if(!isset($_POST[‘start_date’])) $_POST[‘start_date’] = date(“Y-m-d”); Apologies again i should know the answer but my head is hurting[/quote]

My point is that you probably want to be a bit more sophisticated than this - and properly test the quality of your start date input.

If you are doing that elsewhere already, and you are sure that if the start date coming through POST data is either 100% valid or 100% not there (and if it's 100% not there you are subsequently happy to have the start date set to now()) then why not do this:

Code:
$start_date = ( isset ($_POST['start_date']) )  ?  $_POST['start_date']  :  date ("Y-m-d");



default value for a search - El Forum - 07-31-2009

[eluser]tim1965[/eluser]
Thanks for the reply. All of these inputs are coming from dropdowns that are saniitised by validation. I just want to always return a query result if the user hits search rather than flagging an error message saying they havent selected any inputs.


default value for a search - El Forum - 07-31-2009

[eluser]thePiet[/eluser]
You might want to use the Input and Security class from CI for security.

Code:
$start_date = ($this->input->post('start_date', TRUE) == FALSE) ? date('Y-m-d') : $this->input->post('start_date');



default value for a search - El Forum - 07-31-2009

[eluser]tim1965[/eluser]
thepiet

Thanks for your replies and time, got this working now.