Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with multiple form selections 1

Status
Not open for further replies.

meeble

Programmer
Joined
Sep 24, 2002
Messages
137
Location
GB
I have a web form:

<form method=post action=&quot;timesheet_view2.php&quot;>
<table width=&quot;760&quot; border=&quot;0&quot; cellspacing=&quot;3&quot; cellpadding=&quot;3&quot; align=&quot;left&quot;>
<tr>
<td width=&quot;200&quot; class=&quot;heading3&quot;>Staff Name</td>
<td width=&quot;560&quot; class=&quot;heading4&quot;><SELECT multiple NAME=&quot;name&quot;>
<option>jamesf4218</option>
<option>Pete</option>
<option>Alex Warbler</option>
</SELECT></td></tr>
<tr>
<td width=&quot;200&quot; class=&quot;heading3&quot;>Job Number</td>
<td width=&quot;560&quot; class=&quot;heading4&quot;><select multiple name=&quot;jobnumber&quot;>
<option>acd000</option>
<option>acd001</option>
<option>CAU032/401</option>
<option>MOT023/102</option>
</select>
</td>
</tr>
<tr>
<td width=&quot;200&quot; class=&quot;heading3&quot;>Client</td>
<td width=&quot;560&quot; class=&quot;heading4&quot;><select multiple name=&quot;client&quot;>
<option>foo</option>
<option>blah</option>
<option>meep</option>
</td>
</tr>
<tr>
<td width=&quot;200&quot; class=&quot;heading3&quot;>Description of Job/Task</td>
<td width=&quot;560&quot; class=&quot;heading4&quot;>
<textarea cols=&quot;19&quot; rows=&quot;5&quot; name=&quot;description&quot;>
</textarea>
</td>
</tr>
<tr>
<td width=&quot;200&quot; class=&quot;heading3&quot;>Stage</td>
<td width=&quot;560&quot; class=&quot;heading4&quot;><select multiple name=&quot;stage&quot;>
<option>N/A</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select></td>
</tr>
<tr>
<td width=&quot;200&quot;class=&quot;heading3&quot;>Start Date</td>
<td width=&quot;560&quot; class=&quot;heading4&quot;>
<input type=&quot;text&quot; value=&quot;15-05-03&quot; size=12 name=&quot;firstdate&quot;></td>
</tr>
<tr><td width=&quot;200&quot;class=&quot;heading3&quot;>Finish Date</td><td width=&quot;560&quot; class=&quot;heading4&quot;>
<input type=&quot;text&quot; size=12 name=&quot;lastdate&quot; value=&quot;15-05-03&quot;></td>
</tr>
<tr>
<td width=&quot;200&quot; class=&quot;heading3&quot;>Time (in Hrs i.e 4 or 4.5)</td><td width=&quot;560&quot; class=&quot;heading4&quot;>
<input type=text name=timetaken size=12></td>
</tr>
<tr>
<td>
<input class=&quot;button&quot; type=&quot;submit&quot; value=&quot;Submit...&quot;></td>
</tr></table>
<br clear=all>
</form>
</table>

When the user chooses their criteria and click submit, the following script searches the database and brings up the records based on the criteria.

function englishToAmericanDate($str)
{
$elements = explode(&quot;-&quot;, $str);
$americandate = $elements[2].&quot;-&quot;.$elements[1].&quot;-&quot;.$elements[0];
return $americandate;
}
$americandate = englishToAmericanDate($firstdate);
$americandate2 = englishToAmericanDate($lastdate);

$db = mysql_connect(&quot;localhost&quot;, &quot;root&quot;);

mysql_select_db(&quot;acwh&quot;,$db);

if ($name == &quot;&quot;)
{$name = '%';}

if ($jobnumber == &quot;&quot;)
{$jobnumber = '%';}

if ($client == &quot;&quot;)
{$client = '%';}

if ($description == &quot;&quot;)
{$description = '%';}

if ($stage == &quot;&quot;)
{$stage = '%';}

if ($timetaken == &quot;&quot;)
{$timetaken = '%';}


$result = mysql_query (&quot;SELECT * FROM timesheets WHERE name LIKE '%$name%' AND jobnumber LIKE '%$jobnumber%' AND client LIKE '%$client%' AND description LIKE '%$description%' AND stage LIKE '%$stage%' AND timetaken LIKE '%$timetaken%' AND entrydate BETWEEN '&quot;.$americandate.&quot;' AND '&quot;.$americandate2.&quot;' order by entrydate desc&quot;,$db);

if ($myrow = mysql_fetch_row($result)) {


do {

echo &quot;<table width=\&quot;760\&quot; border=\&quot;0\&quot; cellspacing=\&quot;3\&quot; cellpadding=\&quot;3\&quot; align=\&quot;left\&quot;>
<tr><td nowrap width=\&quot;120\&quot; class=\&quot;nound2\&quot;>&quot;,$myrow[1], &quot;</td><td nowrap width=\&quot;100\&quot; class=\&quot;nound2\&quot;>&quot;,$myrow[2], &quot;</td>
<td nowrap width=\&quot;120\&quot; class=\&quot;nound2\&quot;>&quot;,$myrow[3], &quot;</td><td nowrap width=\&quot;220\&quot; class=\&quot;nound2\&quot;>&quot;,$myrow[4], &quot;</td><td nowrap width=\&quot;50\&quot; class=\&quot;nound2\&quot;>&quot;,$myrow[5], &quot;</td><td nowrap width=\&quot;85\&quot; class=\&quot;nound2\&quot;>&quot;,$myrow[6], &quot;</td><td nowrap width=\&quot;65\&quot; class=\&quot;nound2\&quot;>&quot;,$myrow[7], &quot;</td></tr></table><br clear=all>&quot;;

$sum += $myrow[7];



} while($myrow = mysql_fetch_row($result));

} else {print &quot;<font color=\&quot;red\&quot;>Sorry, no records were found!</font><br>&quot;;}

echo &quot;<table width=\&quot;760\&quot; border=\&quot;0\&quot; cellspacing=\&quot;3\&quot; cellpadding=\&quot;3\&quot; align=\&quot;left\&quot;><tr><td colspan=\&quot;2\&quot;><hr color=\&quot;#003366\&quot;></td></tr><tr><td nowrap valign=\&quot;top\&quot; align=\&quot;right\&quot; width=\&quot;740\&quot;>The total number of hours is</td><td nowrap valign=\&quot;top\&quot; class=\&quot;nound3\&quot; align=\&quot;right\&quot; width=\&quot;20\&quot;>  &quot;, $sum, &quot;</td></tr></table>&quot;;

This all works fine. However, if the user tries to choose multiple selections in the same field on the form, it doesn't work. For example, if the users chooses jamesf4218 only, the computer returns all records for jamesf4218. But if the user chooses jamesf4218 and Pete then it just returns the records for pete.

How do I change this please?

Many thanks

James
 
u can workaround this only by using javascript. this is peculiar only to multiple selection box (works fine with check boxes).

loop throught the contents of the drpdown save the values that are seleced to a hidden field and retrieve the hidden field valu in the second page.

Known is handfull, Unknown is worldfull
 
>>>>>>>>>>>>u can workaround this only by using javascript. this is peculiar only to multiple selection box (works fine with check boxes).

This isn't the case. I believe that you can put the values into an array using the [] notation and display the records using FOREACH. I just need some help on how to do this. I find the manual a bit complicated......


 
If you need multiple selections in a select box, change the name of the element to &quot;foo[]&quot; instead of just &quot;foo&quot;. PHP will then be able to provide any number of selected options and will provide them to you in an array -- even if only one option was selected.

If the use of &quot;[]&quot; element-naming notation is in the manual, I've never been able to find it.

Want the best answers? Ask the best questions: TANSTAAFL!
 
I know to make the form elements, ie name=&quot;jobname[]&quot; but I don't know how, on the script page, to pull records from the database based on the values in the arrays
 
Ok, here's the deal:
Code:
<select multiple name=&quot;stage[]&quot;>
<option>N/A</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
The square brackets in the name pass the posted value(s) as an array.

What you need to do in composing your SQL statement is make sure that the multiple selections are combined with an OR statement.

Alternatively you can use the FIND_IN_SET(str,strlist) SQL function:
Code:
$myList = implode(',',$_POST[stage]);
$SQL = &quot;SELECT * FROM myTable WHERE FIND_IN_SET(mycolumn,'$myList')&quot;;
 
Hello,

I thought I'd try with the 'stage' field so I changed the code to this:

$myList = implode(',',$_POST[stage]);

$result = mysql_query (&quot;SELECT * FROM timesheets WHERE name LIKE '%$name%' AND jobnumber LIKE '%$jobnumber%' AND client LIKE '%$client%' AND description LIKE '%$description%' AND FIND_IN_SET(stage,'$myList') AND timetaken LIKE '%$timetaken%' AND entrydate BETWEEN '&quot;.$americandate.&quot;' AND '&quot;.$americandate2.&quot;' order by entrydate desc&quot;,$db);


But this draws out no results and I get the error message:

PHP Warning: Bad arguments to implode() in c:\inetpub\ on line 45
 
Troubleshooting:

1. Did you add the [] in the HTML tag?
2. Are you using method=&quot;post&quot;?
3. print out all posted variables to the screen to inspect what's posted:
print_r($_POST);

Let's go from there.
 
Hello,

I have changed the code for all 4 fileds so now the code is:

$myname = implode(',',$_POST[name]);
$myjobnumber = implode(',',$_POST[jobnumber]);
$myclient = implode(',',$_POST[client]);
$mystage = implode(',',$_POST[stage]);

$result = mysql_query (&quot;SELECT * FROM timesheets WHERE FIND_IN_SET(name,'$myname') AND FIND_IN_SET(jobnumber,'$myjobnumber') AND FIND_IN_SET(client,'$myclient') AND description LIKE '%$description%' AND FIND_IN_SET(stage,'$mystage') AND timetaken LIKE '%$timetaken%' AND entrydate BETWEEN '&quot;.$americandate.&quot;' AND '&quot;.$americandate2.&quot;' order by entrydate desc&quot;,$db);

This just gives the error messages:

PHP Warning: Bad arguments to implode() in c:\inetpub\ on line 45 PHP Warning: Bad arguments to implode() in c:\inetpub\ on line 46 PHP Warning: Bad arguments to implode() in c:\inetpub\ on line 47 PHP Warning: Bad arguments to implode() in c:\inetpub\ on line 48

And print_r($_POST); just gives print_r(Array);

What am I doing wrong?

Cheers

James
 
Hmm, I think the problem might be if I don't select any options in the fields. Before, if I didn't select anything it looked through EVERYTHING. Now, if I don't select something it selects NOTHING and comes up with the error.

If I select options in all 4 fields it seems to work. What do I do if I don't want to select any options in a particular drop down list?

Cheers

James
 
Instead of just writing out the SQL statement assuming that there will be all creiteria present it is advisable to compile the statement according to the state of the passed arrays.
The procedure for each array is similar:
1. if nothing was selected there is no $_POST[] for fieldname.
2. Only tag on the 'FIND_IN_SET()' when there is something in the array.
3. If the array is empty omit the condition.
 
Well, I tried that and the code I have now is:

$db = mysql_connect(&quot;localhost&quot;, &quot;root&quot;);

mysql_select_db(&quot;acwh&quot;,$db);

if ($description == &quot;&quot;)
{$description = '%';}

if ($timetaken == &quot;&quot;)
{$timetaken = '%';}

if(is_array($_POST[name]) )
{
$myname = implode(',',$_POST[name]);
}
else $myname = &quot;&quot;;

if(is_array($_POST[jobnumber]) )
{
$myjobnumber = implode(',',$_POST[jobnumber]);
}
else $myjobnumber = &quot;&quot;;

if(is_array($_POST[stage]) )
{
$mystage = implode(',',$_POST[stage]);
}
else $mystage = &quot;&quot;;

if(is_array($_POST[client]) )
{
$myclient = implode(',',$_POST[client]);
}
else $myclient = &quot;&quot;;

$result = mysql_query (&quot;SELECT * FROM timesheets WHERE FIND_IN_SET(name,'$myname') AND FIND_IN_SET(jobnumber,'$myjobnumber') AND FIND_IN_SET(client,'$myclient') AND description LIKE '%$description%' AND FIND_IN_SET(stage,'$mystage') AND timetaken LIKE '%$timetaken%' AND entrydate BETWEEN '&quot;.$americandate.&quot;' AND '&quot;.$americandate2.&quot;' order by entrydate desc&quot;,$db);

This gets rid of the error messages but brings up no records whatever I choose.

What is wrong?

Cheers

James
 
You are still making the assumption that you have to use the FIND_IN_SET.
Do it like this for each field:
Code:
if(is_array($_POST[client])){
   $myclient = implode(',',$_POST[client]);
   $condition .= &quot;AND FIND_IN_SET(client,'$myclient') &quot;;
}
[code]
The later assemble the SQL statement with the part that never changes first:
[code]
$SQL = &quot;SELECT * FROM timesheets WHERE timetaken LIKE '%$timetaken%' AND entrydate BETWEEN '&quot;.$americandate.&quot;' AND '&quot;.$americandate2.&quot;' $condition order by entrydate desc&quot;;
$result = mysql_query($SQL,$db) OR die(mysql_error());
This way it will only apply the conditions if there is a selection made.
Just be aware that all these are AND, that means the record must match all the criteria.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top