Adding expiry date to database from drop-down list

Posted By: Ian on Feb 25, 2014 in PHP, Snippets
Last modified on January 29th, 2015 at 9:31 pm,

In Banner Bar you set the expiry date from a drop-down list and the issue is that the user has to see one thing and the database another. Before i did it a very long winded way but like most things i try to do on here now “Write Less Do More” so here is the better way to do it. Hope it helps.
<?php
/**
* Split (explode) the drop-down posted value (string) by ":" (delimiter)
* return it as array so we can access it by it's elements starting with 0
*/
$choice = explode(':', $_POST['blah']);
$left = $choice[0];
$right = $choice[1];
/**
* Value from database if set
*/
//$dbv = $conn->query("SELECT expiry FROM $t_settings")->fetchColumn();
/**
* Value from post for example
*/
$dbv = $_POST['blah'];
?>

<?php
function doDropdown($name, $hours, $days, $weeks, $months)
{
global $dbv;
$dd = '<select name="' . $name . '" id="' . $name . '">';
/**
*Hours
*/
for ($i = "1"; $i <= $hours; $i++) {
$dd .= '<option value="' . $i . ':hour"' . ($dbv == $i . ':hour' ? ' selected="selected"' : '') . '>' . $i . ($i == '1' ? ' hour' : ' hours') . '</option>';
}
/**
* Days
*/
for ($i = "1"; $i <= $days; $i++) {
$dd .= '<option value="' . $i . ':day"' . ($dbv == $i . ':day' ? ' selected="selected"' : '') . '>' . $i . ($i == '1' ? ' day' : ' days') . '</option>';
}
/**
* Weeks
*/
for ($i = "1"; $i <= $weeks; $i++) {
$dd .= '<option value="' . ($i * '7') . ':day"' . ($dbv == ($i * '7') . ':day' ? ' selected="selected"' : '') . '>' . $i . ($i == '1' ? ' week' : ' weeks') . '</option>';
}
/**
* Months
*/
for ($i = "1"; $i <= $months; $i++) {
$dd .= '<option value="' . $i . ':month"' . ($dbv == $i . ':month' ? ' selected="selected"' : '') . '>' . $i . ($i == '1' ? ' month' : ' months') . '</option>';
}
/**
* 1 Year and 10 years
*/
$dd .= '<option value="1:year"' . ($dbv == '1:year' ? ' selected="selected"' : '') . '>1 year</option>';
$dd .= '<option value="10:year"' . ($dbv == '10:year' ? ' selected="selected"' : '') . '>Unlimited</option>';
$dd .= '</select>';
return $dd;
}
?>

Syntax
echo doDropdown("NAME & ID of drop-down","HOURS","DAYS","WEEKS","MONTHS");

Example usage for inserting/updating database
$conn->query("UPDATE $table SET expires=DATE_ADD(NOW(), INTERVAL $left $right) WHERE id='$id'");
//ADD $left value and $right frequency to today's date and update expires in database

Read upon

Try MeDownload

AnonymousIan.J.Gough

Did this help you? Please Let me know by commenting below even if it’s just to say “Thanks”

leave a comment

About This Site

Dreams are built from lines of code well it's true!
If you can dream it you can usually code it.

I have learned so much from other people on the Internet and this site is for me to give back some of what i have learned and hope people can now learn from me.
Have fun and surf safely,
Ian.J.Gough

protected by copyscape duplicate content check

Categories


Powered by Banner Bar 125