Jump to content

Array Help


corbin

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.