How to detect duplicate values in session array php?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • israel Parajes
    New Member
    • Oct 2011
    • 1

    How to detect duplicate values in session array php?

    im creating a student subject reservation that will add subject from a table row.I would like to detect the presence of duplicate values,then echo if the values are detected.

    For example this is the click event:
    Code:
    $i=0;
    $b =1;
    while($row=mysql_fetch_assoc($sql) ){
    
    
    echo "<tr><td>".$row['Subject']."<input type='hidden' name='subj[$i]' value=".$row['Subject']."></td>
    <td>".$row['Lec']."<br/>".$row['Lab']."<input type='hidden' name='leclab[$i]' value=".$row['Lec']."><input type='hidden' name='lab[$i]' value=".$row['Lab']."></td>
    
    <td>".$row['Descriptive']."<input type='hidden' name='desc[$i]' value=".$row['Descriptive']."></td>
    <td>".$row['Day']."<br/>".$row['Labday']."<input type='hidden' name='daylabday[$i]' value=".$row['Day']."><input type='hidden' name='labday[$i]' value=".$row['Labday']."></td>
    
    <td>".$row['LecTime']."<br/>".$row['LabTime']."<input type='hidden' name='lectlabt[$i]' value=".$row['LecTime']."><input type='hidden' name='labtime[$i]' value=".$row['LabTime']."></td>
    
    <td>".$row['Room']."<br/>".$row['Labroom']."<input type='hidden' name='roomlabroom[$i]' value=".$row['Room']."><input type='hidden' name='labroom[$i]' value=".$row['Labroom']."></td>
    
    <td><input id='send' name='reserv[$i]' type='submit' value='Add' onclick='addRow('myTable')' /> </td></tr>";
    
    $i++;
    
    
    }
    }
    and this is the code for the displaying in the table row

    Code:
    $c=0;
    
            while($c<=$counter){
                if($_SESSION["S['$c']"]==""){
                echo "-";
                }
            $array1=array($_SESSION["lect['$c']"],$_SESSION["labt['$c']"],$_SESSION["Day['$c']"] ,$_SESSION["lday['$c']"]);
            if(count(array_unique($array1))<count($array1)){
                echo "Duplicate entry found in array";
            }
            else{
                        echo "<tr><td>".$_SESSION["S['$c']"] . " </td>
                    <td>".$_SESSION["lec['$c']"] . "<br/>".$_SESSION["lab['$c']"] . "</td>
                    <td>".$_SESSION["lect['$c']"] . "<br/>".$_SESSION["labt['$c']"] . "</td>
                    <td>".$_SESSION["Day['$c']"] . "<br/>".$_SESSION["lday['$c']"] . "</td>
                    <td>".$_SESSION["room['$c']"] . "<br/>".$_SESSION["lroom['$c']"] . "</td></tr><br>";
                 $unit=$_SESSION["lec['$c']"] + $_SESSION["lab['$c']"];
                   $units= $units+$unit;
            }
    the problem is how to detect if i add duplicate value
    sorry for my bad English.
    thanks in advance
    Attached Files
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    it depends on the definition of "duplicate value" and if you need them. to prevent having two (or more) identical SQL results, simply use SELECT DISTINCT … FROM ….

    arrays can be treated similarly by applying array_unique().

    Comment

    Working...