# Array Help

10 replies to this topic

### #1 corbin

corbin
• Staff Alumni
• 8,129 posts

Posted 05 August 2006 - 03:44 AM

Ive been trying to figure this out for a while... Anyways I'm looking for a function that does:
```if(\$_POST['sub'] == "yes") {
\$p_array = array(\$_POST['p0'], \$_POST['p1'], \$_POST['p2'], \$_POST['p3'], \$_POST['p4'], \$_POST['p5'], \$_POST['p6'], \$_POST['p7']);
}
\$c = count(\$p_array);
\$i = 0;
if(\$_POST['sub'] == "yes") {
while (\$i < \$c) {
if((\$p_array[\$i] == \$p_array[1]) || (\$p_array[\$i] == \$p_array[2]) || (\$p_array[\$i] == \$p_array[2]) || (\$p_array[\$i] == \$p_array[3]) || (\$p_array[\$i] == \$p_array[4]) || (\$p_array[\$i] == \$p_array[5]) || (\$p_array[\$i] == \$p_array[6]) || (\$p_array[\$i] == \$p_array[7])) {
\$error = "yes";
\$i++;
}
}
}
```
more efficiently...
Why doesn't anyone ever say hi, hey, or whad up world?

### #2 CTM

CTM
• Members
• Member
• 15 posts

Posted 05 August 2006 - 04:07 AM

```if ( \$_POST['sub'] == "yes" )
{
\$field_num = 8 ;
\$field_prefix = 'p' ;

\$p_array = array ( ) ;
for ( \$i = 0 ; \$i < \$field_num ; \$i ++ )
{
\$p_array[] = \$_POST[ \$field_prefix . \$i ] ;
}
}

if ( \$_POST['sub'] == "yes" )
{
\$values = array_values ( \$p_array ) ;
foreach ( \$p_array as \$value )
{
if ( in_array ( \$value, \$values ) )
{
\$error = "yes" ;
}
}
}```

### #3 corbin

corbin
• Staff Alumni
• 8,129 posts

Posted 05 August 2006 - 04:15 AM

Oh... Thanks
Why doesn't anyone ever say hi, hey, or whad up world?

### #4 corbin

corbin
• Staff Alumni
• 8,129 posts

Posted 05 August 2006 - 04:31 AM

That doesnt work... Since its looking for a value that was just pulled from the array of course it returns true...
Why doesn't anyone ever say hi, hey, or whad up world?

### #5 extrovertive

extrovertive
• Members
• 235 posts

Posted 05 August 2006 - 04:41 AM

Some weird array.

What are you trying to do here?

if((\$p_array[\$i] == \$p_array[1]) || (\$p_array[\$i] == \$p_array[2]) || (\$p_array[\$i] == \$p_array[2]) || (\$p_array[\$i] == \$p_array[3]) || (\$p_array[\$i] == \$p_array[4]) || (\$p_array[\$i] == \$p_array[5]) || (\$p_array[\$i] == \$p_array[6]) || (\$p_array[\$i] == \$p_array[7])

### #6 corbin

corbin
• Staff Alumni
• 8,129 posts

Posted 05 August 2006 - 04:43 AM

I have 7 drop down boxes in a form, all with the same  possible values, and I'm trying to make sure none of them are the same value when theyre submitted...
Why doesn't anyone ever say hi, hey, or whad up world?

### #7 kenrbnsn

kenrbnsn
• Staff Alumni
• 8,235 posts
• LocationHillsborough, NJ, USA

Posted 05 August 2006 - 05:26 AM

Can you post the source for your form?

Ken

### #8 corbin

corbin
• Staff Alumni
• 8,129 posts

Posted 05 August 2006 - 05:35 AM

```<form Action="" method=POST>
<? if(\$error == "yes") { echo "<font color=\"red\">Fields cannot be the same.</font>"; } ?>
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="22%">
<tr>
<td width="50%" class="td_right">Name</td>
<td width="50%" class="td_left"><input type="text" name="name"></td>
</tr>
<tr>
<td width="50%" class="td_right">First Period</td>
<td width="50%" class="td_left"><? \$i = 1; drop_down(\$i, \$p_array[\$i]); \$i++; ?></td>
</tr>
<tr>
<td width="50%" class="td_right">Second Period</td>
<td width="50%" class="td_left"><? drop_down(\$i, \$p_array[\$i]); \$i++; ?></td>
</tr>
<tr>
<td width="50%" class="td_right">Third Period</td>
<td width="50%" class="td_left"><? drop_down(\$i, \$p_array[\$i]); \$i++; ?></td>
</tr>
<tr>
<td width="50%" class="td_right">Fourth Period</td>
<td width="50%" class="td_left"><? drop_down(\$i, \$p_array[\$i]); \$i++; ?></td>
</tr>
<tr>
<td width="50%" class="td_right">Fifth Period</td>
<td width="50%" class="td_left"><? drop_down(\$i, \$p_array[\$i]); \$i++; ?></td>
</tr>
<tr>
<td width="50%" class="td_right">Sixth Period</td>
<td width="50%" class="td_left"><? drop_down(\$i, \$p_array[\$i]); \$i++; ?></td>
</tr>
<tr>
<td width="50%" class="td_right">Seventh Period</td>
<td width="50%" class="td_left"><? drop_down(\$i, \$p_array[\$i]); ?></td>
</tr>
</table>
<input type="hidden" name="sub" value="yes">
<INPUT type=submit value="Submit">
</form>
```
and drop_down is
```function drop_down(\$period, \$sel) {
echo "<select name=\"p" . \$period . "\" value=\"3\">";
echo "<option></option>";
\$result = mysql_query("SELECT * from `class_teachers`") or die (mysql_error());
while(\$row = mysql_fetch_assoc(\$result))  { echo "<option value=\"" . \$row['id'] . "\"";
if(\$row['id'] == \$sel) { echo "SELECTED"; }
echo ">" . \$row['class'] . " - " . \$row['teacher'] . "</option>"; }
echo "</select>";
}
```

Why doesn't anyone ever say hi, hey, or whad up world?

### #9 ryanlwh

ryanlwh
• Staff Alumni
• 511 posts

Posted 05 August 2006 - 06:18 AM

if you name your selection boxes like this:
`echo "<select name=\"p[" . \$period . "]\" value=\"3\">";`

Then you can reduce the first line of the processing to:
`\$p_array = \$_POST['p'];`

```or [php] * 1000...

### #10 corbin

corbin
• Staff Alumni
• 8,129 posts

Posted 05 August 2006 - 06:24 AM

Oh... wow that makes sense... hehe Ill do that now...
Why doesn't anyone ever say hi, hey, or whad up world?

### #11 ryanlwh

ryanlwh
• Staff Alumni
• 511 posts

Posted 05 August 2006 - 06:27 AM

oh, why do you have value in the <select> tag?
```or [php] * 1000...