374 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 1+ members
  • Has sold $1,000+ on Envato Market
  • Has collected 10+ items on Envato Market
+3 more
_rohan says

Right, here’s my problem. I’ve got a databsae, and I wanted to tun a quick filtering script through it. I have a form, with two dropdowns, one for each DB field.

eg: Field – Name Options in dropdown – *, John, Harry

Field – Age Options in dropdown – *, 20, 30

Then, in the PHP , I use $_POST to get the values of the dropdowns. I use WHERE in the MySql query to get the desired results:

WHERE name=$name AND age=$age

($name and $age store the inputs)

If the inputs are not ’ * ’, it works fine. But I can find no way to display the results when the selected option is ’ * ’. Just FYI , ’ * ’ means ‘anything’.

So, can anyone help?

Thanks.

352 posts
  • Has been part of the Envato Community for over 6 years
  • Has referred 50+ members
  • Has sold $10,000+ on Envato Market
  • Had an item featured in an Envato Bundle
+6 more
danharper says

Just use an if statement to check whether a field is set as ‘’.

Try this

I wrote it quickly, so it may be slightly pseudo-code (and there may be a better way to do it).

So, as an example, if $name 'Dan' and $age 18, the SQL should be:

SELECT * FROM `tablename` WHERE `name` = 'Dan' AND `age` = 18;

If $name 'Dan' but $age ‘’ then it should be:

SELECT * FROM `tablename` WHERE `name` = 'Dan';

Or $name '*' but $age ‘18’:

SELECT * FROM `tablename` WHERE `age` = 18;

:)

374 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 1+ members
  • Has sold $1,000+ on Envato Market
  • Has collected 10+ items on Envato Market
+3 more
_rohan says

Thanks. That’s s good solution :-)

374 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 1+ members
  • Has sold $1,000+ on Envato Market
  • Has collected 10+ items on Envato Market
+3 more
_rohan says

But if you have 3,4 or more inputs, it becomes less useful :S

352 posts
  • Has been part of the Envato Community for over 6 years
  • Has referred 50+ members
  • Has sold $10,000+ on Envato Market
  • Had an item featured in an Envato Bundle
+6 more
danharper says

Right now I can’t think of a much better solution, since you can’t execute a SQL query like:

SELECT * FROM `tablename` WHERE `name` = '*';

Because it would be looking for a ‘name’ record containing an asterisk. And

SELECT * FROM `tablename` WHERE `name` = *;

Would return an error.

374 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 1+ members
  • Has sold $1,000+ on Envato Market
  • Has collected 10+ items on Envato Market
+3 more
_rohan says
Right now I can’t think of a much better solution, since you can’t execute a SQL query like: SELECT * FROM `tablename` WHERE `name` = ’*’;

Because it would be looking for a ‘name’ record containing an asterisk. And

SELECT * FROM `tablename` WHERE `name` = *;
Would return an error.

Yeah, that was my original problem. Anyway, I’ve done lots of repetition and coded it for five inputs. Thanks =)

27 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 100+ items on Envato Market
  • Sells items exclusively on Envato Market
+2 more
incrediblebytes says

Another solution may be to use LIKE operator function in a where clause

http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_like

753 posts
  • Has referred 10+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Had an item featured on Envato Market
+4 more
AdamGold says

Use an if statement to check if the field equals to ’*’. If it does, just don’t write the where operator in the query.

426 posts
  • Has been part of the Envato Community for over 6 years
  • Has referred 10+ members
  • Has sold $5,000+ on Envato Market
  • Has been a beta tester for an Envato feature
+3 more
vasilios says

This is a dynamic solution, but hasn’t been tested.

<?php // Name for the submit button (<input type="submit" [...] name="BLAH_BLAH_BLAH" [...] ?>)
$submitName = 'submit';

// Query WITHOUT the WHERE section.
$query = 'SELECT * FROM `tablename`';

// Build the WHERE section.
$where = array();

// Assumes the $_POST array contains the fields in this format: $_POST['field_name']
foreach($_POST as $key => $value) {
    if ($key == $submitName || $value == '*') continue;
    $where[] = '`'.$key.'` = "'.$value.'"';
}

// Append WHERE section to $query
if (count($where) > 0) $query .= ' WHERE '.implode(' AND ', $where);

// Anything else you want to append to $query should go here.
// $query .= ' ORDER BY `field_name` DESC';
// etc..
?>

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes

by
by
by
by
by
by