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 derfloh 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 select into array

Status
Not open for further replies.

tyrannus

MIS
Apr 6, 2006
115
US
Ok I have this form that has a few multiple selects on it. Those array's are to pass to another page that displays information. What I am trying to do is use the information from the array's to help build an sql script. Basically I want to take the elements of the array and put it into a single variable. (code to follow) It works as far as the "IDEA" goes, but i keep getting [variable]'array' instead of [variable]'elements of array'.

Here is the form code.
<form action="rpt600a.php" method="post">
<? echo("<input type=hidden name=prevmenu value='$prevmenu'>"); ?>
<table align='center' width="800">
<tr>
<td width=50% align="right">Start Date:</td>
<td width=50%><input type="text" name="startdate" size="10" maxlength="10" onFocus="javascript:vDateType='1'" onKeyUp="DateFormat(this,this.value,event,false,'1')" onBlur="DateFormat(this,this.value,event,true,'1')"></td>
</tr>
<tr>
<td width=50% align="right">Stop Date:</td>
<td width=50%><input type="text" name="stopdate" size="10" maxlength="10" onFocus="javascript:vDateType='1'" onKeyUp="DateFormat(this,this.value,event,false,'1')" onBlur="DateFormat(this,this.value,event,true,'1')"></td>
</tr>
<tr>
<td width=50% align="right">Total Gift Amounts Greater Than (Inclusive):</td>
<td width=50%><input type="text" name="greaterthan" size="10" maxlength="10"></td>
</tr>
<tr>
<td width=50% align="right">Total Gift Amounts Less Than (Inclusive):</td>
<td width=50%><input type="text" name="lessthan" size="10" maxlength="10"></td>
</tr>
<!--
<tr>
<td width=50% align="right">Sort By:</td>
<td width=50%><input type="radio" name="orderby" value="0" checked> Name <input type="radio" name="orderby" value="1"> Date <input type="radio" name="orderby" value="2"> Amount </td>
</tr>
-->
<tr>
<td width=50% align="right">Constituency:</td>
<td><select name="constituency[]" size="4" multiple="yes">
<!--<option value="0"></option>-->
<?


$qs = "select * from sup_con_code";
$q = mysql_query($qs);
while ($row = mysql_fetch_array($q))
{
echo "<option value='$row[0]'";
echo ">$row[1]:$row[2]</option>\n";
}
?>
</select>
</td>

</tr>
<tr>
<td width=50% align="right">Campaign:</td>
<td><select name="campaign[]" size="4" multiple="yes">
<!--<option value="0">N/A</option>-->
<?
$qs = "select * from sup_campaign where active='y'";
$q = mysql_query($qs);
while ($row = mysql_fetch_array($q))
{
echo "<option value='$row[0]'";
echo ">$row[1]:$row[2]</option>\n";
}
?>
</select>
</td>

</tr>
<tr>
<td width=50% align="right">Appeal:</td>
<td><select name="appeal[]" size="4" multiple="yes">
<!--<option value="0">N/A</option>-->
<?
$qs = "select * from sup_appeal";
$q = mysql_query($qs);
while ($row = mysql_fetch_array($q))
{
echo "<option value='$row[0]'";
echo ">$row[1]:$row[2]</option>\n";
}
?>
</select>
</td>

</tr>
<tr>
<td width=50% align="right">Account:</td>
<td><select name="acct_code[]" size="4" multiple="yes">
<!--<option value="0">N/A</option>-->
<?
$qs = "select * from sup_acct_code";
$q = mysql_query($qs);
while ($row = mysql_fetch_array($q))
{
echo "<option value='$row[0]'";
echo ">$row[1]:$row[2]</option>\n";
}
?>
<!-- 8-28-2006 Added in drop down list to select Type of Gift as requested from Dianne -->
<tr>
<td width=50% align="right">Gift Type:</td>
<td>
<select name="gift_type[]" size="4" multiple="yes">
<!--<option value="0">All - Except IN-Kind</option>-->

<?php
$qs = "select * from sup_typegift";
$q = mysql_query($qs);
while ($row = mysql_fetch_array($q)){
echo "<option value = '$row[0]'";
echo ">$row[1]</option>\n";
}
?>
<!-- End Added Script -->

</select>
</td>

</tr>
<!--
<tr>
<td width=50% align="right">Constituent:</td>
<td width=50%>
<input type=button onClick='if2=document.forms[0].zname;ifield=document.forms[0].zcon; chooser=window.open("relate_chooser.php", "chooser", "toolbar=no,menubar=no,scrollbar=no,width=400,height=300");chooser.ifield = ifield;chooser.if2=if2' value="...">
<input type="text" name="zname">
<input type="hidden" name="zcon" value="0">
</td>
</tr>
-->
<tr>
<td><input type="hidden" name="action" value="submitted"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Preview"></td>
</tr>
</table>

</form>
<div align="center">
<?
if ($prevmenu<>"")
{
echo ("<div align='center'><a href='menu.php?menu=$prevmenu'>Return to previous Menu</a></div>");
}
?>


Here is the partial code of the page it goes to where the sql script is generated.

if ($campaign>0)
{
$zsearch=$zsearch." and g.campaign='$campaign'";
}

if ($appeal>0)
{
$zsearch=$zsearch." and g.appeal='$appeal'";
}

if ($acct_code>0)
{
$zsearch=$zsearch." and g.acct_code='$acct_code'";
}

if ($gift_type>0){
$zsearch=$zsearch." and g.typegift='$gift_type'";
}else{
$zsearch=$zsearch." and g.typegift<>'4'";
}

if ($greaterthan <> "")
{
$zsearch1=$zsearch1." total >= '$greaterthan'";
}

if ($lessthan <> "")
{
if ($greaterthan == "")
{
$zsearch1=$zsearch1." total <= '$lessthan'";
} else {
$zsearch1=$zsearch1." and total <= '$lessthan'";
}
}
if ($zcon>0)
{
$zsearch=$zsearch." and c.con_id='$zcon'";
}
elseif ($constituency>0)
{
$zsearch=$zsearch." and (constituency0='$constituency' or constituency1='$constituency' or constituency2='$constituency' or constituency3='$constituency' or constituency4='$constituency' or constituency5='$constituency' or constituency6='$constituency' or constituency7='$constituency' or constituency8='$constituency' or constituency9='$constituency')";
}
This is what I get if I echo $zsearch

and g.campaign='Array' and g.appeal='Array' and g.acct_code='Array' and g.typegift='Array' and (constituency0='Array' or constituency1='Array' or constituency2='Array' or constituency3='Array' or constituency4='Array' or constituency5='Array' or constituency6='Array' or constituency7='Array' or constituency8='Array' or constituency9='Array')

I would like for it to look like this.

and g.campaign='1,2,3,4' and g.appeal='1,2,3,4' and g.acct_code='123,234,345,456' (etc...)
 
The first thing I notice, is your assuming register-globals to be on. and hence expect your form elements to autmatically be translated into variables $prevmenu. If register_globals is off which is the case on almost all servers running PHP thwese days, then those variables will not exist and generate errors. You should always access values from forms, using the corresponding super global array. If your form's method is post you use $_POST. if it is GEt you use$_GEt. so $prevmenu should be accessed as $_POST['prevmenu'] or $_GET['prevmenu'].

Second to access an array you cannot simply use the variable, you need to address the elements within the array. so $constituency would need to be something like $_POST['constituency'][1] where the number represents one of the elements in the array.

Third, and this is for this forum, always surround your code with code tags:
[ignore]
Code:
some code here
[/ignore] to make it easier to read, as it will generate a code box around your code. like so:
Code:
Some code here

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks for the code tip. I will remember that later. Register Globals is on, I guess I really need to use for loops for each of the if statements. The number of elements will always be different depending on what report needs to be done. Some like $campaign can have from 1 - 10 options. And others like $acct_code can have upto 50 options. I was hoping there was a better/easier way rather than write for loops for each statement.
 
Actually i think a foreach statement would work best in this situation. I will report back after I have had a chance to test it out.
 
Yes, you'll have to cyckle through the arrays. either "For" loops, or "For each" loops which seem to be the more elegant method of doing it.

In any case i must urge you to not use register-globals set to on. its a security risk, and all PHP versions now have it set to off by default.





----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Ok, I am still having problems with this. I have it cycling through the array and putting out the correct information. However, I am getting results like this " and (g.campaign='45' or g.campaign='67' or g.campaign='21' or)" I have the right idea, but that last or keeps generating errors so I have to figure out how to tell the script to only add the or until the last element of the array has been displayed. Any ideas about what I am talking about and what I need to do?
 
ok have to come back to this...

I am trying to revamp a program that someone else wrote a few years ago and me being new to PHP I am lost with direction sometimes. Part of the script is written like this. Remember I am building an SQL statement from a form here so bear with me.

Code:
$zsearch=$zsearch." and (constituency0='$value' or constituency1='$value' or constituency2='$value' or constituency3='$value' or constituency4='$value' or constituency5='$value' or constituency6='$value' or constituency7='$value' or constituency8='$value' or constituency9='$value')";

When I display the result it comes out like this:

and (constituency0='1' or constituency1='1' or constituency2='1' or constituency3='1' or constituency4='1' or constituency5='1' or constituency6='1' or constituency7='1' or constituency8='1' or constituency9='1') and (constituency0='18' or constituency1='18' or constituency2='18' or constituency3='18' or constituency4='18' or constituency5='18' or constituency6='18' or constituency7='18' or constituency8='18' or constituency9='18')

As you can see thats not going to work.. What I need to do is make it display as:
and (constituency0='1' or constituency0='18' or constituency1='1' or constituency1='18') etc...

Any help is really greatly appreciated.
 
Egads, that's ugly.

And it looks to me like your database isn't particularly normalized. What kind of data are you storing in the constituencyX columns?



Want the best answers? Ask the best questions! TANSTAAFL!
 
Sorry I have been gone so long, I have the issue resolved so I appreciate everyones help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top