PHP搜索数据库功能 [英] PHP search database function

查看:85
本文介绍了PHP搜索数据库功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前在用户按日期在数据库中搜索数据的项目。



我的表单有2个选项,用户选择通过选择框搜索或输入从日期到另一个日期的字段。



写完我的代码后,我发现我的功能越来越长。

看看在这里,任何我可以缩短功能的想法?

此外,这个功能仅适用于第一个选项,如果我做第二个选项,它将会更长。对不起,我还是编程新手。



Currently on a project where the user search data by date in database.

My form has 2 options, either the user choose to search by select box or input fields from a date to another date.

After written my code I figure out that my function is getting to long.
Have a look here, any idea that I can shorten the function?
Also, this function is only for the first options, if I do the second one it will be much longer. Sorry I am still new to programming.

// Search Form
		if(isset($_POST['go']))
		{
			$myRadio = $_POST['myRadio'];
			$selectedOption = $_POST['mySelect'];
			if($myRadio == 'choose') {				
				$this->view->getOption = $selectedOption . ': ';
				if($selectedOption == 'Today') {
					$getDateData = $db->fetchOne('select count(id) as total_users from users where created_at > DATE_SUB(NOW(), INTERVAL 1 DAY)');
					$this->view->getTodayData = $getDateData;
				} elseif($selectedOption == 'Yesterday') {
					$getDateData = $db->fetchOne('select count(id) as total_users from users where created_at > DATE_SUB(NOW(), INTERVAL 2 DAY)');
					$this->view->getDateData = $getDateData;
				} elseif($selectedOption == 'Last 7 days') {
					$getDateData = $db->fetchOne('select count(id) as total_users from users where created_at > DATE_SUB(NOW(), INTERVAL 7 DAY)');
					$this->view->getDateData = $getDateData;
				} elseif($selectedOption == 'This week') {
					$getDateData = $db->fetchOne('select count(id) as total_users from users where WEEKOFYEAR(created_at) = WEEKOFYEAR(NOW())');
					$this->view->getDateData = $getDateData;
				} elseif($selectedOption == 'Last week') {
					$getDateData = $db->fetchOne('select count(id) as total_users from users where WEEKOFYEAR(created_at) = WEEKOFYEAR(NOW())-1');
					$this->view->getDateData = $getDateData;
				} elseif($selectedOption == 'Last 30 days') {
					$getDateData = $db->fetchOne('select count(id) as total_users from users where created_at > DATE_SUB(NOW(), INTERVAL 30 DAY)');
					$this->view->getDateData = $getDateData;
				} elseif($selectedOption == 'This month') {
					$getDateData = $db->fetchOne('select count(id) as total_users from users where MONTH(created_at) = MONTH(NOW())');
					$this->view->getDateData = $getDateData;
				} elseif($selectedOption == 'Last month') {
					$getDateData = $db->fetchOne('select count(id) as total_users from users where MONTH(created_at) = MONTH(NOW())-1');
					$this->view->getDateData = $getDateData;
				}
				
				
			} elseif($myRadio == 'choose2') {
				//second choice
			} else {
				$this->view->emptySelect = 'Select one of the following options...';
			}
		}
	}

推荐答案

_POST [' go']))
{
_POST['go'])) {


myRadio =
myRadio =


_POST [' myRadio ];
_POST['myRadio'];


这篇关于PHP搜索数据库功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆