Page 4 of 4

Re: php search query

Posted: Wed Sep 25, 2013 11:39 am
by chris98
Would I have to include multiple if statements and multiple select statements to make sure I am covered with all options, or would there be an easier way?

I.E.

Code: Select all

if (empty($file))
{
SELECT COUNT(*) AS id FROM downloads WHERE name LIKE :file AND difficulty = :difficulty AND estates = :estates AND missions = :missions AND balanced = :balance AND category = :category
}
if (empty($author))
{
SELECT COUNT(*) AS id FROM downloads WHERE username LIKE :user AND difficulty = :difficulty AND estates = :estates AND missions = :missions AND balanced = :balance AND category = :category
}.etc.etc

Re: php search query

Posted: Wed Sep 25, 2013 11:51 am
by Celauran
Why not loop through your POST array and build your query string that way?

Re: php search query

Posted: Wed Sep 25, 2013 11:54 am
by chris98
How could I do that?

Re: php search query

Posted: Wed Sep 25, 2013 1:33 pm
by Christopher

Code: Select all

foreach ($_POST as $key => $value) {
     // do something here
}

Re: php search query

Posted: Fri Sep 27, 2013 9:28 am
by chris98
So would something like this do the trick?

Code: Select all

foreach ($_GET as $ps => $row) {
SELECT COUNT(*) AS id FROM downloads WHERE name LIKE :file AND difficulty = :difficulty AND estates = :estates AND missions = :missions AND balanced = :balance AND category = :category
}

Re: php search query

Posted: Fri Sep 27, 2013 9:48 am
by Celauran
No. If you're running a query inside a loop, you're definitely doing it wrong. Use the loop to build your query string. Something along these lines:

Code: Select all

$columns = array('file', 'username', 'difficulty', 'estates', 'missions', 'balanced', 'category');
$query = "SELECT COUNT(id) AS count FROM downloads WHERE ";
foreach ($_POST as $key => $value) {
	if (in_array($key, $columns)) {
		$query .= "{$key} = :{$key} AND ";
	}
}
$query = rtrim($query, 'AND ');