Get ready for BLACK FRIDAY shopping starting in

Forums

PHP

This topic is locked

Need recordset query to ignore empty fields

Posted 05 Jun 2006 18:33:24
1
has voted
05 Jun 2006 18:33:24 Ben Desmond posted:
First up, I'm a bit of noob when it comes to PHP and MySQL. I've been working on a project which has a MySQL database that the user can search by choosing to enter some or all of three fields in a form on a PHP page, which then, obviously, relate to specific variables and fields in the database.

Previously I had the default values of the variables as '-1' which meant that the results page simply ignored any of the variables that hadn't been filled in. However, now that 8.0.2 has forced the Type of each variable, it no longer ignores those values and so returns no results. In order to get a correct result I have to fill in all three, I can't just fill in one of the fields.

What do I have to do to regain the functionality of the results page simply ignoring any field that isn't filled in?

Replies

Replied 06 Jun 2006 12:02:11
06 Jun 2006 12:02:11 Roddy Dairion replied:
In php there is the isset() functions, combining this with and if else statements e.g.
<pre id=code><font face=courier size=2 id=code>
if (isset($_POST['textbox1']))
{
echo $_POST['textbox1'];

}
else
{
echo "Textbox1 is empty fill in something";
}
</font id=code></pre id=code>
So by fiddling around with it you can come up with this results page where, you make your sql statement get only the fields that has a value.
Replied 06 Jun 2006 12:46:09
06 Jun 2006 12:46:09 Ben Desmond replied:
Thanks for the reply Roddy (I've yet to get a response on any of the other forums I've posted this on).

Forgive me ignorance, but this seems (to a newbie like me) excessively complex for something that previously worked with a simple '-1' value to a variable.

I'm amazed there isn't a simple 'any' value for a variable.

But thanks for the reply, I'll look into how I can make that work for me.

Replied 23 Nov 2007 19:48:44
23 Nov 2007 19:48:44 Glenn Blalock replied:
Has this issue been solved in some way. I am having the same problem: if a form field is empty, I want the recordset to ignore it; but it doesn't.

I'm using DW 8.02 as well, about to upgrade to CS3.
Replied 26 Nov 2007 11:20:52
26 Nov 2007 11:20:52 Roddy Dairion replied:
<pre id=code><font face=courier size=2 id=code>
if (!empty($_POST['textbox1']))
{
echo $_POST['textbox1'];

}
else
{
echo "Textbox1 is empty fill in something";
}
</font id=code></pre id=code>
Replied 26 Nov 2007 16:49:35
26 Nov 2007 16:49:35 Glenn Blalock replied:
Thanks for the reminder of what you posted last year. However, being unskilled with coding, I don't know how to convert your suggestion into something workable for me. I don't need a reminder that the textbox is empty. I have 7 different search fields. Users can fill in one or more; they don't need to fill all. So I want the empty fields to be ignored. I also don't want to use an "OR" search, because it returns to many tangential hits (db = 93000 records).

I am hoping someone will have an idea about how to make DW 8 do this.

<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
<pre id=code><font face=courier size=2 id=code>
if (!empty($_POST['textbox1']))
{
echo $_POST['textbox1'];

}
else
{
echo "Textbox1 is empty fill in something";
}
</font id=code></pre id=code>
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>
Replied 26 Nov 2007 18:31:41
26 Nov 2007 18:31:41 Roddy Dairion replied:
The only simple way i know is the long version of what i posted today
You check every form field to see if its empty if it is then you ignore it load of if else. OR
i use this is a pretty simple function.
<pre id=code><font face=courier size=2 id=code>
function search($arrayval)
{
//Array of values that we don't want to retrieve
//'btn' is the only value that i don't want to retrieve as its the submit
$invformfield=array('btn');

while(list($field,$value) = each($arrayval))
{
if(!empty($value))
{
if(!in_array(substr($field,0,3),$invformfield))
{
$field=substr($field,3);
//change 'and' to 'or' if your query needs it but 'and' and 'or' can't be used in the same query
$record.=" $field=trim('$value') and";
}
}
}
return rtrim($record, 'and');
}

if($_POST['btnsubmit'])
{
//calling the function
//only $_POST is used as $_POST will retrieve every textbox name and it value as an array
$search=search($_POST);

$select="select * from tablename where ".$search;

echo $select;
}
</font id=code></pre id=code>
Here's how my form looks
<pre id=code><font face=courier size=2 id=code>
&lt;form name="searchform" method="POST"&gt;
&lt;input type="textbox" name="txtsearchfield1" value="" /&gt;&lt;br /&gt;

&lt;input type="textbox" name="txtsearchfield2" value="" /&gt;&lt;br /&gt;

&lt;input type="textbox" name="txtsearchfield3" value="" /&gt;&lt;br /&gt;

&lt;input type="textbox" name="txtsearchfield4" value="" /&gt;&lt;br /&gt;

&lt;input type="textbox" name="txtsearchfield5" value="" /&gt;&lt;br /&gt;

&lt;input type="textbox" name="txtsearchfield6" value="" /&gt;&lt;br /&gt;

&lt;input type="textbox" name="txtsearchfield7" value="" /&gt;&lt;br /&gt;

&lt;input type="submit" name="btnsubmit" value="Search" /&gt;
&lt;/form&gt;

</font id=code></pre id=code>
I explain how it will work.
The 7 textbox in your form has to be named as the fields in your database with a txt in front e.g.
If in your table you have the following fieldname
<b>
name
age
tel
sex
address1
address2
postcode </b>
then the textboxes in the form will have the following name
<b>
txtname
txtage
txtsex
txtaddress1
txtaddress2
txtpostcode</b>

Thats all there is to it. Now this is what i've used to do my search becoz i found it easier that way. I code with what i know, i never use DW to generate my codes.

Edited by - roders22 on 26 Nov 2007 18:33:29

Edited by - roders22 on 26 Nov 2007 18:34:19

Reply to this topic