corbin Posted August 5, 2006 Share Posted August 5, 2006 Ive been trying to figure this out for a while... Anyways I'm looking for a function that does:[code]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++;}}}[/code]more efficiently... Quote Link to comment https://forums.phpfreaks.com/topic/16610-array-help/ Share on other sites More sharing options...
CTM Posted August 5, 2006 Share Posted August 5, 2006 [code=php:0]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" ; } }}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16610-array-help/#findComment-69605 Share on other sites More sharing options...
corbin Posted August 5, 2006 Author Share Posted August 5, 2006 Oh... Thanks :D Quote Link to comment https://forums.phpfreaks.com/topic/16610-array-help/#findComment-69607 Share on other sites More sharing options...
corbin Posted August 5, 2006 Author Share Posted August 5, 2006 That doesnt work... Since its looking for a value that was just pulled from the array of course it returns true... Quote Link to comment https://forums.phpfreaks.com/topic/16610-array-help/#findComment-69613 Share on other sites More sharing options...
extrovertive Posted August 5, 2006 Share Posted August 5, 2006 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]) Quote Link to comment https://forums.phpfreaks.com/topic/16610-array-help/#findComment-69614 Share on other sites More sharing options...
corbin Posted August 5, 2006 Author Share Posted August 5, 2006 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... Quote Link to comment https://forums.phpfreaks.com/topic/16610-array-help/#findComment-69615 Share on other sites More sharing options...
kenrbnsn Posted August 5, 2006 Share Posted August 5, 2006 Can you post the source for your form?Ken Quote Link to comment https://forums.phpfreaks.com/topic/16610-array-help/#findComment-69619 Share on other sites More sharing options...
corbin Posted August 5, 2006 Author Share Posted August 5, 2006 [code]<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>[/code]and drop_down is[code]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>";}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16610-array-help/#findComment-69620 Share on other sites More sharing options...
ryanlwh Posted August 5, 2006 Share Posted August 5, 2006 if you name your selection boxes like this:[code]echo "<select name=\"p[" . $period . "]\" value=\"3\">";[/code]Then you can reduce the first line of the processing to:[code]$p_array = $_POST['p'];[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16610-array-help/#findComment-69623 Share on other sites More sharing options...
corbin Posted August 5, 2006 Author Share Posted August 5, 2006 Oh... wow that makes sense... hehe Ill do that now... Quote Link to comment https://forums.phpfreaks.com/topic/16610-array-help/#findComment-69625 Share on other sites More sharing options...
ryanlwh Posted August 5, 2006 Share Posted August 5, 2006 oh, why do you have value in the <select> tag? Quote Link to comment https://forums.phpfreaks.com/topic/16610-array-help/#findComment-69626 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.