By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,652 Members | 1,842 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,652 IT Pros & Developers. It's quick & easy.

How to detect duplicate values in session array php?

P: 1
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:
Expand|Select|Wrap|Line Numbers
  1. $i=0;
  2. $b =1;
  3. while($row=mysql_fetch_assoc($sql) ){
  4.  
  5.  
  6. echo "<tr><td>".$row['Subject']."<input type='hidden' name='subj[$i]' value=".$row['Subject']."></td>
  7. <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>
  8.  
  9. <td>".$row['Descriptive']."<input type='hidden' name='desc[$i]' value=".$row['Descriptive']."></td>
  10. <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>
  11.  
  12. <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>
  13.  
  14. <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>
  15.  
  16. <td><input id='send' name='reserv[$i]' type='submit' value='Add' onclick='addRow('myTable')' /> </td></tr>";
  17.  
  18. $i++;
  19.  
  20.  
  21. }
  22. }
  23.  
and this is the code for the displaying in the table row

Expand|Select|Wrap|Line Numbers
  1. $c=0;
  2.  
  3.         while($c<=$counter){
  4.             if($_SESSION["S['$c']"]==""){
  5.             echo "-";
  6.             }
  7.         $array1=array($_SESSION["lect['$c']"],$_SESSION["labt['$c']"],$_SESSION["Day['$c']"] ,$_SESSION["lday['$c']"]);
  8.         if(count(array_unique($array1))<count($array1)){
  9.             echo "Duplicate entry found in array";
  10.         }
  11.         else{
  12.                     echo "<tr><td>".$_SESSION["S['$c']"] . " </td>
  13.                 <td>".$_SESSION["lec['$c']"] . "<br/>".$_SESSION["lab['$c']"] . "</td>
  14.                 <td>".$_SESSION["lect['$c']"] . "<br/>".$_SESSION["labt['$c']"] . "</td>
  15.                 <td>".$_SESSION["Day['$c']"] . "<br/>".$_SESSION["lday['$c']"] . "</td>
  16.                 <td>".$_SESSION["room['$c']"] . "<br/>".$_SESSION["lroom['$c']"] . "</td></tr><br>";
  17.              $unit=$_SESSION["lec['$c']"] + $_SESSION["lab['$c']"];
  18.                $units= $units+$unit;
  19.         }
  20.  
the problem is how to detect if i add duplicate value
sorry for my bad English.
thanks in advance
Attached Images
File Type: jpg waaaaa.jpg (26.0 KB, 140 views)
Oct 21 '11 #1
Share this Question
Share on Google+
1 Reply


Dormilich
Expert Mod 5K+
P: 8,639
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().
Oct 24 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.