Jump to content

Archived

This topic is now archived and is closed to further replies.

corbin

Array Help

Recommended Posts

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...

Share this post


Link to post
Share on other sites
[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]

Share this post


Link to post
Share on other sites
That doesnt work... Since its looking for a value that was just pulled from the array of course it returns true...

Share this post


Link to post
Share on other sites
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])

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
Can you post the source for your form?

Ken

Share this post


Link to post
Share on other sites
[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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.