Advanced Search

Results 1 to 2 of 2

Thread: comparing array values?

  1. #1
    Join Date
    Aug 2008
    Posts
    23
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default comparing array values?

    I need to display an error message "you have checked same iqid", if the $iqid of $arr2[$iqid][$userid] is equal to other, could you please help me?

    PHP Code:
    <?php 
    session_start
    ();
    include 
    'conn.php';
    $post $_GET['post'];
    $slected = array();
    if(isset(
    $_POST['checkb'])){
        if(
    count($_POST['cid']) > 0){            
            foreach(
    $_POST['cid'] as $awrd) {
            
    $result mysql_query("UPDATE ctable SET award='yes' WHERE id='$awrd'") or die(mysql_error());
            
    $slected[] = $awrd;
                }        
        }            
    } else{

    ?>   
    <form method="post" name="formk" action="<?= $_SERVER['PHP_SELF']. '?' $_SERVER['QUERY_STRING'?>">
    <?php
    $result 
    mysql_query("SELECT cid, iqid, cost, usersid FROM ctable WHERE post=$post");
    while (
    $row mysql_fetch_array($result))
    {
      
    $arr[$row['iqid']][$row['usersid']] = $row['cost'];
      
    $arr2[$row['iqid']][$row['usersid']] = $row['cid'];
      
    $uid[] = $row['usersid'];
    }
    $uid array_unique($uid);
    $qid array_keys($arr);

    echo 
    '<table><tr><td></td>';
    foreach(
    $uid as $userid) echo "<td>$userid</td>";
    echo 
    '</tr>';
    foreach(
    $qid as $iqid)
    {
      echo 
    "<tr><td>$iqid</td>";
      foreach(
    $uid as $userid)
      {
        if (isset(
    $arr[$iqid][$userid]))
        {       
            echo 
    "<td>".$arr[$iqid][$userid]."<input type='checkbox' name='cid[]' value='{$arr2[$iqid][$userid]}'></td>";    
        }
        else echo 
    "<td></td>";    
      }
      echo 
    "</tr>";
    }
    echo 
    '</table>';
    echo 
    '<input type="submit" name="checkb" />';
     echo
    '</form>';    

    }
    ?>

  2. #2
    Join Date
    Jan 2008
    Posts
    4,158
    Thanks
    28
    Thanked 623 Times in 619 Posts
    Blog Entries
    1

    Default

    Does this work for you:
    PHP Code:
    <?php 
    session_start
    ();
    include 
    'conn.php';
    $post $_GET['post'];
    $slected = array();
    if(isset(
    $_POST['checkb'])){
        if(
    count($_POST['cid']) > 0){            
            foreach(
    $_POST['cid'] as $awrd) {
            
    $result mysql_query("UPDATE ctable SET award='yes' WHERE id='$awrd'") or die(mysql_error());
            
    $slected[] = $awrd;
                }        
        }            
    } else{

    ?>   
    <form method="post" name="formk" action="<?= $_SERVER['PHP_SELF']. '?' $_SERVER['QUERY_STRING'?>">
    <?php
    $result 
    mysql_query("SELECT cid, iqid, cost, usersid FROM ctable WHERE post=$post");
    while (
    $row mysql_fetch_array($result))
    {
      
    $arr[$row['iqid']][$row['usersid']] = $row['cost'];
      
    $arr2[$row['iqid']][$row['usersid']] = $row['cid'];
      
    $uid[] = $row['usersid'];
    }
    $uid array_unique($uid);
    $qid array_keys($arr);

    echo 
    '<table><tr><td></td>';
    foreach(
    $uid as $userid) echo "<td>$userid</td>";
    echo 
    '</tr>';
    foreach(
    $qid as $iqid)
    {
      echo 
    "<tr><td>$iqid</td>";
      foreach(
    $uid as $userid)
      {
        if (isset(
    $arr[$iqid][$userid]))
        {       
    if(
    $arr[$iqid][$userid] === $iqid || $arr[$iqid][$userid] == $iqid){ die('You have checked same iqid'); }
            echo 
    "<td>".$arr[$iqid][$userid]."<input type='checkbox' name='cid[]' value='{$arr2[$iqid][$userid]}'></td>";    
        }
        else echo 
    "<td></td>";    
      }
      echo 
    "</tr>";
    }
    echo 
    '</table>';
    echo 
    '<input type="submit" name="checkb" />';
     echo
    '</form>';    

    }
    ?>
    Jeremy | jfein.net

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •