Home arrow PHP arrow Page 3 - Building the Index Page for a PHP Email Application

Retrieving Messages - PHP

In this third part of a four-part article series on building a PHP email application, we will look at the index page. This page is the heart of the application. We will also look at how to handle attachments in a message and how to integrate them into this application.

  1. Building the Index Page for a PHP Email Application
  2. Navigation and Headers
  3. Retrieving Messages
  4. Attachments
By: Leidago
Rating: starstarstarstarstar / 8
November 08, 2006

print this article



Next, the messages are retrieved from the database to be displayed in the table:

//get them out and display
//ALL newly downloaded messages will have checked set to 0
            $query_msgs = "select * from messages WHERE checked =
            $re = mysql_query($query_msgs);
            $n = mysql_num_rows($re);
            if($n > 0){
            while($r = mysql_fetch_array($re)){  
?> <td></td>
  <td valign="top" class="list"><? if($r['checked']==1){?>
            <a href="view.php?mid=<?=$r['msg_id'];?>"
class="email2"><?=$r['from']; ?></a>
                                    <? }else{ ?> 
            <a href="view.php?mid=<?=$r['msg_id'];?>&em=<?
$email; ?>" class="email"><?=$r['from'];?></a>
                          <? } ?> </a></td>
          <td valign="top" class="list"><?=$r['subject']; ?></td>
          <td valign="top" class="list"><?=$r['date']; ?></td>
          <td valign="top" class="list"><a href="#"
                        <? }
}//end for
}//end check

If there are no new messages, in other words, the $numEmails variable is zero, then the script retrieves the old messages stored in the database:

else{//there are no new messages, so get the old messages from database
            include "connect.php";
$query_email = "SELECT user_id FROM user WHERE user_id = '".$_SESSION['userid']."'";
$result_email= mysql_query($query_email);
$num_email= mysql_num_rows($result_email);
if($num_email > 0 ){
while($res = mysql_fetch_array($result_email)){
$user = $res['user_id'];          
echo mysql_error();
//get all msgs
            $query_msgs = "select * from messages";
            $re = mysql_query($query_msgs);
            $n = mysql_num_rows($re);
            if($n > 0){
            while($r = mysql_fetch_array($re))
<tr class="td">
<td><? if($r['checked']==1){?> <img src="images/env.gif" />
<? }else{ ?>
<? echo $r['checked'];}?>
          <td valign="top" class="list"><? if($r['checked']==1){?>
                                    <a href="view.php?mid=<?=$r
['msg_id'];?>" class="email2"><?=$r['from']; ?></a>
                                    <? }else{ ?> 
                        <a href="view.php?mid=<?=$r['msg_id'];?>"
                          <? } ?> </a></td>
          <td valign="top" class="list"><?=$r['subject']; ?></td>
          <td valign="top" class="list"><?=$r['msg_date']; ?
          <td valign="top" class="list"><a href="delete.php?
mid=<?=$['msg_id']?>" class="menu">Delete</a></td>
}//end 2nd while

Last but not least, I've made this code available to the script as optional, because you might want to delete all the messages from the mail server after inserting them into the database. This will save you from downloading the same messages when you return to the index page:

//delete emails from server
if($numEmails > 0){
$num_msg = imap_num_msg($stream);
for ( $i=1; $i<=$num_msg; $i++ ) {
   imap_delete($stream, $i);

>>> More PHP Articles          >>> More By Leidago

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: