Hi,

I have set up an internal pm system between my users . Foll. is the code to read the message. But I am being asked to set up an alert like "you have a new message" to pop up immediately after login . I'm not sure how to do this.

Also, in this following script, I need to be able to indicate if the message has gone to multiple recepients - again i dont know how its done because the "To" Field in the table has data like user1,user2 or user5,user3,user7 or user5,user1 etc.

Would appreciate guidance pl. thanks.

<b>Select a message to read.</b><p>

<form method="post" action="read1.php?uid=<? echo $uid ?>">

<td><font size=2 face=Tahoma color="#339966">Select</b></td>
<td><font size=2 face=Tahoma color="#339966">From</b></td>
<td><font size=2 face=Tahoma color="#339966">Date</b></td>
<td><font size=2 face=Tahoma color="#339966">Time</b></td>

</tr>

<?php
$current = getdate();
$current_month = $current["mon"];
$current_year = $current["year"];

mysql_connect("localhost", $dbname, $dbpasswd )
    	or die ("Unable to connect to server.");

mysql_select_db($database)
    	or die ("Unable to select database.");

$result = mysql_query("SELECT * FROM `Messages` WHERE `To` LIKE '%$uid%' && (( (MONTH(`Date`) >= 1) && (YEAR(`Date`) = $current_year)  ORDER BY `Date` desc  ");

if ($myrow = mysql_fetch_array($result)) {


  do {
$temp=$myrow["From"];
$result1 = mysql_query("SELECT * FROM `users` WHERE uid='$temp' ");
$myrow1 = mysql_fetch_array($result1);
$name = $myrow1["name"];

$fulldate = $myrow["Date"];
$sep = explode(" ",$fulldate); // split into day and time
$day = $sep[0];
$time = $sep[1];

$timesep = explode(":",$time); // split time into hr, min, sec
$H = $timesep[0];
$i = $timesep[1];
$s = $timesep[2];

$timea = ("$H.$i.$s") ;


printf("<tr><td><input type=\"radio\" name=\"choice\" value=%d><td>
<font size=2 face=Tahoma color=#339966>%s<td>
<font size=2 face=Tahoma color=#339966>%s<td>
<font size=2 face=Tahoma color=#339966>%s<td>
</tr>",
 $myrow["MID"],
 $name,
 calculatedate($myrow["Date"]),
 $timea );

  } while ($myrow = mysql_fetch_array($result));


} else {

echo "Sorry, no messages for you as yet !";

}
?>


 <input type="hidden" name="uid" value="<? echo $uid ?>" >

</table>      

<p><input type="submit" value="Proceed">
</form>

Script 2

<?php

$a = $_POST["choice"];
$uid= $_POST["uid"];

echo "<p><font color=#CACA00 size=4>Messages received by you this year: ";
?>
<hr>

<?php

$num=0;

mysql_connect("localhost", $dbname, $dbpasswd )
    	or die ("Unable to connect to server.");

mysql_select_db($database)
    	or die ("Unable to select database.");

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

$resulta = mysql_query($sqla);


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

do {

$uid=$myrowa["To"];
// list(u1,u2,u3 ...but how do i know how many users its been sent to ?) = explode ("," , $uid) ;



$temp = $myrowa["From"];

$sqlb= "SELECT * FROM `users` WHERE `uid` = '$temp' ";
//echo $sqlb;
$resultb = mysql_query($sqlb);
$myrowb = mysql_fetch_array($resultb);
$name = $myrowb["name"];

$comment = $myrowa["Message"];
$com1=stripslashes($comment); //strip /
$com2 = nl2br ($com1); //convert CR to BR

$fulldate = $myrowa["Date"];
$sep = explode(" ",$fulldate); // split into day and time
$day = $sep[0];
$time = $sep[1];

$timesep = explode(":",$time); // split time into hr, min, sec
$H = $timesep[0];
$i = $timesep[1];
$s = $timesep[2];

$timea = ("$H.$i.$s") ;

$num=$num+1;



printf("<tr>
<br><font size=2 face=Tahoma color=#CACA00><b>S.No.:</font></b>&nbsp;<font size=2 face=Tahoma color=#808000><b>%d</b>
&nbsp;&nbsp;<font size=2 face=Tahoma color=#CACA00><b>From:</font></b>&nbsp;<font size=2 face=Tahoma color=#808000 ><b>%s</b>
&nbsp;&nbsp;<font size=2 face=Tahoma color=#CACA00><b>Date:</font></b>&nbsp;<font size=2 face=Tahoma color=#808000 ><b>%s</b>
&nbsp;&nbsp;<font size=2 face=Tahoma color=#CACA00><b>Time:</font></b>&nbsp;<font size=2 face=Tahoma color=#808000 ><b>%s</b>
<br><font size=2 face=Tahoma color=#CACA00><b>Message:</font></b>&nbsp;<font size=2 face=Tahoma color=#808000>%s
<br><br><hr>
</tr>",
 $num,
  $name,
  calculatedate($myrowa["Date"]),
  $timea,
  $com2 );

  } while ($myrowa = mysql_fetch_array($resulta));


} else {

echo "Sorry, no messages !";
}


?>
</html>

    You can find the number of items in an array via sizeof, or count - they both do the same thing.

    $string = "a,b,c";

    $array = explode(",", $string);

    echo "$string has " . sizeof($array) . " items";

    Will print:
    a,b,c has 3 items

    Additionally, the following code looks very dodgy:
    " $result = mysql_query("SELECT * FROM Messages WHERE To LIKE '%$uid%' && (("

    LIKE %$uid%

    I assume thats because in your 'To' you have:
    user1,user2,user3

    if $uid was 'user2', this would match one entry.
    What if my userid was the letter 'u'? It would match all of them.

    You should probably re-structure the database to add individual rows for each recipient (this also means they can do stuff like delete their own copy of the message), or setup a relational table between a message ID and its user id (that way, you can link multiple user id's to the same message id).

      Thanks but some queries :

      a. you are right when u say using LIKE is not the best way to do it but how would i change the table ? what would be the table structure and how would i define the "To"field ?

      b. With re. to the following , I modified the print part of the code to just list the user ids ( so it reads as To : CAB12,DE34 but what i ideally want is :To: Jack Smith, Jane Glen ). The foll. sql is not functional as of now.

      
      $toall=$myrow["To"];
      
      // so how would exploding the array convert the number of recepients to the identities of the recepients ?
      
      $sqld= "SELECT * FROM `users` WHERE `uid` LIKE '%$toall%'";
      //echo $sqld;
      $resultd = mysql_query($sqld);
       if ($myrowd = mysql_fetch_array($resultd))
      {
      $first_name = $myrowd["firstname"];
      $last_name = $myrowd["lastname"];
      $to = $first_name." ".$last_name;
      
      
      printf("<tr><td><input type=\"radio\" name=\"choice\" value=%d><td> 
          <font size=2 face=Tahoma color=#339966>%s<td> 
          <font size=2 face=Tahoma color=#339966>%s<td> 
          <font size=2 face=Tahoma color=#339966>%s<td>
        <font size=2 face=Tahoma color=#339966>%s<td>
          </tr>", 
      $myrow["MID"], 
      $name, 
      $myrow["To"],
      calculatedate($myrow["Date"]), 
      $timea ); 

      Any suggestions pl ? Thanks.

        I was informed that phpBB updates a binary switch in the db for each user to one when a PM for them is created. Every page checks to see if that is set to '1' and then displays the pop-up if it is. Once it's displayed once, it changes back to 0.

        How can this be done in php pl ?

        Thanks

          Write a Reply...