Jump to content

Trouble with Comparing Arrays


swatisonee

Recommended Posts

Hi,

I've set up a simple pm system and need to set up a deletion of messages when the message is read by the recepient. Since recepients can be multiple, my system must allow deletion only when all recepients have read a message.

Problem is the order in which the message is read can be different.

Here's what I have :

[code]$sqla=  "SELECT * FROM `Messages` WHERE `MID`= $a";

$resulta = mysql_query($sqla) or die (mysql_error($sqla);

if ($myrowa = mysql_fetch_array($resulta)) {

do {

$toall=$myrowa["To"];
$seenby = $myrowa["Seen_By"];

if  ( $toall == $seenby)

{
$sqly = " DELETE  FROM  `Messages` WHERE  `MID`= $a ";

$resulty =  mysql_query($sqly);

if ( !@mysql_query($resulty) ) {
      echo "<p>Message with <b>ID $a</b> has been deleted successfully.";
  }
  else {
      die ( "Unable to delete this message as it has not been viewed by all recepients" );
  }
}[/code]

However,

if $toall = uid1, uid3, uid4 (in that order) and $seen_by = uid3, uid1,uid4, uid3 (in that order as one recepient can view a message multiple times)

then $toall will not be equal to $seenby.


So , i thought of extracting the individual userids through the array by referring to the info. given at

[a href=\"http://builder.com.com/5100-6371-5792851.html\" target=\"_blank\"]http://builder.com.com/5100-6371-5792851.html[/a]

and thats where I'm stuck because the codes dont work and when I echo $ta1 and $sb1 ,all i get is Ärray1 and the messages get deleted regardless of whether they've been read or not. Here's what I have and would appreciate if someone to correct me .Thanks.

[code]

<?

$sqla=  "SELECT * FROM `Messages` WHERE `MID`= $a";

$resulta = mysql_query($sqla) or die (mysql_error($sqla);

if ($myrowa = mysql_fetch_array($resulta)) {

do {

$toall=$myrowa["To"];
$seenby = $myrowa["Seen_By"];

$ta1= array(",", $toall );
$ta2=sort($ta1);

$sb =  (array_unique($seenby));
$sb1 = array(",", $sb );
$sb2= sort($sb1);

if ($ta2==$sb2)
{
$sqly = " DELETE  FROM  `Messages` WHERE  `MID`= $a ";
$resulty =  mysql_query($sqly);

if ( !@mysql_query($resulty) ) {
      echo "<p>Message with <b>ID $a</b> has been deleted successfully.";
  }
  else {
      die ( "Unable to delete this message as it has not been viewed by all recepients" );
}
}
} while ($myrowa = mysql_fetch_array($resulta));
}

?>[/code]










Link to comment
https://forums.phpfreaks.com/topic/5334-trouble-with-comparing-arrays/
Share on other sites

no idea if this will work lol..totally untested and a stab in the dark.. what about doing this?

so say $seenby = uid1, uid2, uid3, uid2

sort($seenby); // so now $seenby should = uid1, uid2, uid2, uid3
$seenbyclean = array_unique($seenby); // gets rid of duplicates

echo $seenbyclean ; // should output uid1, uid2, uid3

like i said though..no idea if this wud work..


The rods one creates for one's back when data isn't normailized!

Try

[code]$toall=$myrowa["To"];
$seenby = $myrowa["Seen_By"];

$toArray = array_unique(explode(', ', $toall));  // assumes comma-space as separator
$sbArray = array_unique(explode(', ', $seenby));  // assumes comma-space as separator
sort($toArray);
sort($sbArray);

if ($toArray == $sbArray) {
       # ok to delete
}
else {
       # not seen by all
}
[/code]
More like buying a tie and then finding a shirt to go with it !

As always, thank you !

I dont know if you saw my other post on the next rod I need to create but i think its the most serious of all the issues i've run into thus far.

Would you have a moment to glance thru it and see what I could try ?


[a href=\"http://www.phpfreaks.com/forums/index.php?showtopic=88832\" target=\"_blank\"]http://www.phpfreaks.com/forums/index.php?showtopic=88832[/a]

Thanks again !
Swati

Archived

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

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