How to make messages displaying in order?!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
masterphp
Forum Newbie
Posts: 11
Joined: Sun Apr 06, 2008 8:12 pm

How to make messages displaying in order?!

Post by masterphp »

I have three php files: "add.php", "booklist.php", and "search.php", add.php is for user to add the message, booklist.php is for user to view those messages after they added, search.php is for user to search the messages. I have a problem for booklist.php, I designed for 10 messages showing per page, and the way I want webpage to display the message follow descent oder like 10,9,8,7.....1 for first page, and the second page should looks like 20,19,18.....11, but the message order looks like mess up after first 10 messages(first page) display. I think the problem should be inside "booklist.php", the message display function's logic is not right, but I could not figure it out how to correct this! So can any one help me with that? Thanks!

Code: Select all

<!--add.php--------------->
<?php
    
    $name = $_POST["name"];
    $pass = $_POST["pass"];    
    $message = $_POST["message"];
    $email = $_POST["email"];
    $domain  = $_POST["domain"];
    $team = $_POST["team"];
    $email = $_POST["email"];
      
        
        //connect database
          require_once("sys_conf.inc");
          $link_id=mysql_connect($DBHOST,$DBUSER,$DBPWD);           
          mysql_select_db($DBNAME); 
          if(isset($ok))
         
          if (!isset($message) or $message=="" or !isset($name) or $name=="" or !isset($pass) or $pass=="") 
          { 
             mysql_close($link_id);
             echo "<script>";
             echo "alert('(*) Field must be entered')";
             echo "</script>";
            
          }
        
        else
        {
    
        
        $space = "&nbsp;"; 
        $time = date("YmdHi"); 
        $ip=$REMOTE_ADDR; 
        $message=StripSlashes($message);        
        $message=htmlspecialchars($message);
        $message=nl2br($message);                   
        
        $guestcontent = "<tr><td><font color=#AB00E1>Message Content:</font><br>$message"; 
        $guestcontent=$guestcontent."<br><font color=#6633FF>Username:  </font>$name"; 
        if ($email !="") 
            $guestcontent=$guestcontent."<br><font color=#9900CC>Email:</font><a href=\"mailto:$email\">$email</a>"."$space"; 
        if ($domain !="") 
            $guestcontent=$guestcontent."<br><font color=#9900CC>Domain:</font>$domain"; 
        if ($team !="") 
        $guestcontent=$guestcontent."<br><font color=#0000FF>Team:</font>$team";
        //$guestcontent = "<font color=#AB00E1>Message Content:</font><br>$message"; 
        $guestcontent=ereg_replace(chr(10),"",$guestcontent); 
        $guestcontent=$guestcontent."<hr size=1></td></tr>\n"; 
        
        //insert into database
        //require_once("sys_conf.inc");
        //$link_id=mysql_connect($DBHOST,$DBUSER,$DBPWD);           
        //mysql_select_db($DBNAME);     
        $str="insert into guestbook (time,ip,name,m_pass,domain,team,email,message,content,is_delete)";
        $str=$str."values('$time','$ip','$name','$pass','$domain','$team','$email','$message','$guestcontent','n')";
        //$str="insert into guestbook (time,ip,name,m_pass,domain,team,email,message,is_delete)";
        //$str=$str."values('$time','$ip','$name','$pass','$domain','$team','$email','$guestcontent','n')";
        //echo $str;
        $result=mysql_query($str,$link_id); 
        //echo $result; 
        mysql_close($link_id);  
        }
        
?>
<html> 
    <head> 
        <title>Search
            </title> 
    <style> 
        <!-- 
            A:link {text-decoration: none ; color:0000ff} 
            A:visited {text-decoration: none; color:004080} 
            A:active {text-decoration: none} 
            A:hover {text-decoration: underline; color:ff0000} 
            BODY {FONT-SIZE:10pt} 
            TH {FONT-SIZE:10 pt} 
            TD {FONT-SIZE: 10pt} 
            TEXTAREA 
            { 
            FONT-FAMILY: "Time News Roman"; 
            FONT-SIZE: 10pt; 
            }
        --> 
    </style> 
</head>
<body bgcolor=#FFFFFD> 
<center>
    <?php include  "head.html"?>
    <h2>Post Message</h2>
    <table width="68%" border="1" cellpadding="3" cellspacing="0" bordercolor="#E3E3E3"> 
        <form method="POST" action="add.php"> 
    
 
        
        
        <tr>    
            <td>(*)Field must be entered</td>
            
        </tr> 
        <tr>    
            <td width="32%" bgcolor="#F0F0F0"><font color="#000000">*Username<font color="#FF0033"></font></font></td> 
            <td colspan="2" width="78%" bgcolor="#F0F0F0"><font color="#00FF00">    
                <input type="text" name="name" size="40"> 
                </font></td> 
        </tr> 
        
        <tr>    
            <td width="22%" height="29">*Password:</td> 
            <td colspan="2" height="29" width="78%">    
                <input type="password" name="pass" size="40"> 
            </td> 
        </tr>
        
        <tr>    
            <td width="22%" height="29">Domain:</td> 
            <td colspan="2" height="29" width="78%">    
                <input type="text" name="domain" size="40"> 
            </td> 
        </tr> 
        <tr>    
            <td width="22%" height="27" bgcolor="#F0F0F0">Team:</td> 
            <td colspan="2" height="27" width="78%" bgcolor="#F0F0F0">  
                <input type="text" name="team" size=40">
            </td> 
        </tr> 
        <tr>    
            <td width="22%" height="20">Email:</td> 
            <td colspan="2" height="20" width="78%"><font color="#00FF00">  
                <input type="text" name="email" size="40"> 
                </font></td> 
        </tr> 
        <tr>    
            <td colspan="3" valign="middle" align="left">   
                <div align="center"><font color="#000000">*Message Content</font><font color="#FF0033"></font><font color="#00FF00"><br> 
                <textarea rows="6" name="message" cols="55" wrap="VIRTUAL"></textarea> 
                </font></div> 
            </td> 
        </tr> 
        <tr bgcolor="#F0F0F0">  
            <td colspan="3" height="24">    
                <div align="center"><font color="#00FF00">  
                <input type="submit" value="Submit" name="ok"> 
                &nbsp;&nbsp;&nbsp;  
                <input type="reset" value="Clear" name="cencel"> 
                </font>
            </td> 
        </tr> 
        </form> 
    </table>    
    
    </center> 
    </body>
    
    <center><a href=booklist.php?task=booklist&recordid=".$Arrid[$j-1].">Click here to view the message</a></center>
</html>

Code: Select all

<!--booklist.php:Message List---------------------------->
<script> 
function rusure(par)
{  
    if(par == 'd')
question = confirm("Delete?");
else if(par == 'e')
    question = confirm("Edit?");  
 
    return question;
} 
</script>
 
<?php 
$user = $_POST['user'];
$keyword = $_POST['keyword'];
if($keyword=='')
{
$keyword = $_GET['keyword'];
 
if($keyword)
$keyword=urldecode($keyword);
}
 
?>
 
<html>
    <head>
        <title>Search</title>
    </head>
    <body>
        <?php 
        include  "head.html";
        //if(isset($_POST["search"])) 
        include "search.php";
        $temp_str = urlencode($keyword);
        ?>
        <table width="68%" border="0"> 
            <tr> 
            <td>            
            <?php 
            $int_page_count=$count; //Total Messages;
            $one_page_line=10;//10 messages/page
            
            $pages=$int_page_num=ceil($int_page_count/$one_page_line);//Total Page; 
            echo "<font color=#CC33FF>Page:"; 
                
            for ($i=1;$i<=$int_page_num;$i++) 
            { 
                    echo "<a href=booklist.php?recordid=".$_GET["recordid"]."&page=$i&keyword=$temp_str>".$i."</a>&nbsp;"; 
            } 
            echo "</font>"; 
            
            ?> 
            </td>
            <td><p align=right>Total<font color=red><?php echo "$count"?></font>Messages</p></td> 
            </tr>
        </table> 
        <table width="68%" border="0" align="center"> 
        <?php 
 
        $page=$_GET['page'];
        if (!isset($page) or $page=="") 
        $page=1; 
        $text="";
        $begin_line=$int_page_count-($page-1)*$one_page_line; 
        if ($begin_line<$one_page_line) //if < one page
            $one_page_line=$begin_line; // just equal one page
      
       for ($j=$begin_line;$j>($begin_line-$one_page_line);$j--) 
        { 
            echo "<tr><td align=right colspan=2><a href=replylist.php?&recordid=".$Arrid[$j+1]."&keyword=$temp_str&pager=$page>View Reply</a>&nbsp;";
            echo "<a href=reply.php?task=reply&recordid=".$Arrid[$j+1]."&keyword=$temp_str&page=$page>Reply</a>&nbsp;";
    
            ?>
            <a <?php echo"href=warning.php?op_type=edit&pass=$pass&recordid=".$Arrid[$j+1]."&keyword=$temp_str&page=$page  >Edit</a>&nbsp;";
            ?>      
            <a <?php echo"href=warning.php?op_type=del&pass=$pass&recordid=".$Arrid[$j+1]."&keyword=$temp_str&page=$page  >Delete</a>&nbsp;";
            echo "Number<font color=red>$j</font>Message</td></tr>"; 
 
            print_r ($ArrSearch[$j-1]);
        }
         
 
    echo "<tr>";
    if($page==1)                            //First page
    {   
        echo "<td align=right  >";
        echo "First&nbsp;";
    
        echo "Prev&nbsp;";
        
        $p=$page+1;
        echo "<a href='booklist.php?pass=$pass&page=$p&keyword=$temp_str'>Next";
        //echo "<a href='booklist.php?'>Next";
        echo "</a>&nbsp;";
 
        $p=$pages;
        echo "<a href='booklist.php?pass=$pass&page=$p&keyword=$temp_str'>Last";
        //echo "<a href='booklist.php?'>Last";
        echo "</a>&nbsp;";      
    }
    else if($page < $pages)     //Middle page
    {       
        $p=1; 
        echo "<td align=right  >";
        echo "<a href='booklist.php?pass=$pass&page=$p&keyword=$temp_str'>First";
        //echo "<a href='booklist.php?'>First";
        echo "</a>&nbsp;";
        
        $p=$page-1; 
        echo "<a href='booklist.php?pass=$pass&page=$p&keyword=$temp_str'>Prev";
        //echo "<a href='booklist.php?'>Prev";
        echo "</a>&nbsp;";
        
        $p=$page+1;
        echo "<a href='booklist.php?pass=$pass&page=$p&keyword=$temp_str'>Next";
        //echo "<a href='booklist.php?'>Next";
        echo "</a>&nbsp;";
 
        $p=$pages;
        echo "<a href='booklist.php?pass=$pass&page=$p&keyword=$temp_str'>Last";
        //echo "<a href='booklist.php?'>Last";
 
        echo "</a>&nbsp;";          
                
    }
    else                                            //Last page
    {
            $p=1; 
        echo "<td align=right  >";
        echo "<a href='booklist.php?pass=$pass&page=$p&keyword=$temp_str'>First";
        //echo "<a href='booklist.php?'>First";
        echo "</a>&nbsp;";
        
        $p=$page-1; 
 
        echo "<a href='booklist.php?pass=$pass&page=$p&keyword=$temp_str'>Prev";
        //echo "<a href='booklist.php?'>Prev";
        echo "</a>&nbsp;";
        
      echo "Next&nbsp;";
    
      echo "Last&nbsp;";
    }
    echo "</td></tr>";
        
        ?> 
    
        </table> 
        <p align=center><a href="#" onclick=history.back()>Return</a></p>
        </body>
</html>

Code: Select all

<!--search.php:--------------------->
<?php
    require_once("sys_conf.inc");
    
    
    function search($keyword,$content) 
    { 
        $count=count($content); 
        $j=0; 
        $ArrSearch=array(); 
        for ($i=0;$i<$count;$i++) 
        {
            if (ereg($keyword,$content[$i]))    
            { 
                
                $ArrSearch[$j]=str_replace($keyword,"<font color=red><b>$keyword</b></font>",$content[$i]);
                $j++; 
            } 
        } 
        return $ArrSearch; 
    }
    
    
    function searchid($keyword,$rid,$content) 
    { 
        $count=count($content); 
        $j=0; 
        $Arrid=array(); 
        for ($i=0;$i<$count;$i++) 
        { 
            if (ereg($keyword,$content[$i]))    
            { 
                $Arrid[$j]=$rid[$i];
                $j++; 
            } 
        } 
        return $Arrid; 
    }
 
    $one_page_line=10; 
    $content=array();       
    $id=array();                
    $ArrSearch=array(); 
    $Arrid=array();         
    
    
    $link_id=mysql_connect($DBHOST,$DBUSER,$DBPWD);         
 
    mysql_select_db($DBNAME);
    $str="select rid,content from guestbook";
    
    $result=mysql_query($str,$link_id);
    
    $i=0;
    while(list($rid, $con)=mysql_fetch_row($result))
    {
        $content[$i]=$con;
        $id[$i]=$rid;
        $i++;
    }
    
    mysql_close($link_id);   
 
    if ($keyword) 
    //if (isset($_POST["search"]) and isset($_POST["keyword"]) and $_POST["keyword"]!="") 
    { 
        $ArrSearch=search($keyword,$content); 
        $Arrid=searchid($keyword,$id,$content); 
    }
    else
    {
        $ArrSearch=$content; 
        $Arrid=$id; 
    }
    //print_r($ArrSearch);
    //print_r($Arrid);
    $count =count($ArrSearch); 
?>
User avatar
vargadanis
Forum Contributor
Posts: 158
Joined: Sun Jun 01, 2008 3:48 am
Contact:

Re: How to make messages displaying in order?!

Post by vargadanis »

Best way to sort messages is letting MySQL do the job for you:

Code: Select all

SELECT * FROM tablename ORDER BY col ASC LIMIT 20, 10
It would get rows from 20-31

and please specify php after the [ code ] tag: [ code = php ]
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: How to make messages displaying in order?!

Post by califdon »

Use a search engine and search for php pagination mysql. You will find hundreds of tutorials on the subject, such as: http://www.tonymarston.net/php-mysql/pagination.html
User avatar
vargadanis
Forum Contributor
Posts: 158
Joined: Sun Jun 01, 2008 3:48 am
Contact:

Re: How to make messages displaying in order?!

Post by vargadanis »

Just don't forget the fun of coding. Go for tutorials but do not forget to learn and customize what you were told. ( I like figuring things our for myself with no tutors :) )
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: How to make messages displaying in order?!

Post by califdon »

vargadanis wrote:Just don't forget the fun of coding. Go for tutorials but do not forget to learn and customize what you were told. ( I like figuring things our for myself with no tutors :) )
I wouldn't disagree with that, but "tutorial" is defined as "Something that provides individual instruction through step by step explanations of content and concepts relating to a titled subject." On the web, a tutorial is usually a structured explanation of a subject, and good ones teach you the basic details first, so that your learning progresses without the confusion that occurs if you start "in the middle" and don't understand the necessary earlier steps. I don't think that we're in disagreement, but perhaps just in terminology. I certainly agree that an important element in learning is actually applying what you've learned.
User avatar
vargadanis
Forum Contributor
Posts: 158
Joined: Sun Jun 01, 2008 3:48 am
Contact:

Re: How to make messages displaying in order?!

Post by vargadanis »

That is definitely true. Good tutorials are helpful if the reader takes the time and effort to read everything through. However most poeple just copy-paste the code and ask why doesn't it work. Of course I am not saying that "masterphp" is that kind of person. Just warn him not to do this. I might have composed my post misunderstandable. I hope it is clear now.
For somebody who is just getting to know PHP it is vital to understand the basic concepts. Even if he is learning slower, it will pay off 2x.
Post Reply