Need help with a guestbook!

Discussion in 'PHP' started by anton-shirikov, Nov 10, 2007.

  1. #1
    Hey all! I am preaty new with PHP and MySQL so need a bit of help with GuestBook. I was thinking how can i print our a new message to the top not add it down..

    This is my script:
    <?php
        require($_SERVER["DOCUMENT_ROOT"]."/21434/config/db_config.php");
        $connection = @mysql_connect($db_host, $db_user, $db_password) or die("Error Connecting");
        mysql_select_db($db_name, $connection);
        
        $name = $_POST["name"];
        $len = strlen($name);
        //only write to DataBase if there's name
        if ($len > 0)
        {
    		$email = $_POST["email"];
    		$comment = $_POST["comment"];
    		$date = time();
    		
    		$query = "INSERT INTO guestbook (autoID, name, email, comment, date_auto) VALUES (NULL, '$name', '$email', '$comment', '$date')";
    		mysql_query($query, $connection) or die(mysql_error());
    	}
    ?>
    
    <html>
    <head>
    <title>Гостевая книга</title>
    <style type="text/css">
    <!--
    body,td,th {
    	font-family: Comic Sans MS;
            font-size: 11pt;
    	color: #FFFFFF;
    }
    body {
    	background-color: #3F1314;
            scrollbar-face-color: #3F1314; 
            scrollbar-highlight-color: white; 
            scrollbar-shadow-color: black; 
            scrollbar-arrow-color: white; 
            scrollbar-track-color: black; 
            scrollbar-darkshadow-color: white
    }
    a:link {
    	color: #FFFFFF;
    	text-decoration: none;
    }
    a:visited {
    	text-decoration: none;
    	color: #907D77;
            font-style:italic;
    }
    a:hover {
    	text-decoration: none;
    }
    a:active {
    	text-decoration: none;
    }
    -->
    </style></head>
    
    <body>
    <table border="0" cellspacing="0" cellpadding="0" align="center">
      <tr>
        <td colspan="3"><img src="images/images/images_01.jpg" width="880" height="236" border="0" usemap="#Map"></td>
      </tr>
      <tr>
        <td><img src="images/images/images_02.jpg" width="41" height="726"></td>
    	<td>
    	
    	<!--Main Part-->
    	
    	<table background="images/images/images_03.jpg" width="793" height="726" border="0" cellspacing="0" cellpadding="0" align="justify">
    		<tr valign="top" height="40">
    			<td>
    				<h3 align="center">Гостевая Книга</h3>
    			</td>
    		</tr>
    		<tr valign="top">
    			<td align="center">
    				<form method="post" action="<?php echo $_SERVER[PHP_SELF]; ?>">
    						Имя: <input type="text" name="name">&nbsp;&nbsp;&nbsp;&nbsp;
    						E-Mail: <input type="text" name="email"><br><br>
    						Сообщение:<br>
    						<textarea style="width: 75%" rows="10" name="comment"></textarea><br>
    						<input type="submit" value="Добавить Сообщение">
    				</form>
    			</td>
    		</tr>
    		<tr>
    			<td align="center"> 
    		<!--Print out Comments-->
    			<div style=" width:793px; height:430px; overflow:auto;">
    				<table bgcolor="#ffffff" width="75%" border="0" cellspacing="1" cellpadding="2">
    					<?php
    						$query = "SELECT * FROM guestbook ORDER BY date_auto";
    						$result = mysql_query($query, $connection);
    						
    						for($i=0; $i < mysql_num_rows($result); $i++)
    						{
    							$name = mysql_result($result, $i, "name");
    							$email = mysql_result($result, $i, "email");
    							$email_len = strlen($email);
    							$comment = mysql_result($result, $i, "comment");
    							$comment = nl2br($comment);
    							$date_auto = mysql_result($result, $i, "date_auto");
    							$show_date = date("H:i:s m/d/Y", $date_auto);
    							
    							if($i % 2)
    							{
    								$bg_color = "#541617";
    							}
    							else
    							{
    								$bg_color = "#852526";
    							}
    							echo '
    		  <tr>
    			<td width="100%" bgcolor="'.$bg_color.'">
    			 <font face="arial" size="2">';
    			   if ($email_len > 0)
    			   {
    					echo '<b>Имя:</b> <a href="mailto:'.$email.'">'.$name.'</a>';
    			   }
    			   else
    			   {
    			   		echo '<b>Имя:</b> '.$name;				
    			   }
    			   echo '
    			   <br>
    			   <br> '.$comment.'
    			</font>
    		</td>
    		<td width="1%" valign="top" nowrap bgcolor="'.$bg_color.'">
    			<font face="arial" size="2">
    			   '.$show_date.'
    			</font>
    		  </td>
    		</tr>
    	  ';
    	  }		  				
    				?>
    				</table>
    			</div>
    		<!--Print out ends-->
    			</td>
    		</tr>	
    	</table>
    	<!--Ends--> 
    	
    	<td><img src="images/images/images_04.jpg" width="46" height="726"></td>
      </tr>
      <tr>
        <td><img src="images/images/images_05.jpg" width="41" height="38"></td>
    	<td><img src="images/images/images_06.jpg" width="793" height="38"></td>
    	<td><img src="images/images/images_07.jpg" width="46" height="38"></td>
      </tr>
    </table>
    <map name="Map">
      
      <area shape="poly" coords="5,176,4,202,149,199,149,170,161,169" href="http://group31434.narod.ru/index.html" alt="Новости">
      <area shape="poly" coords="162,169,163,200,312,193,304,164" href="http://group31434.narod.ru/dokl.html" alt="Доклады">
      <area shape="poly" coords="322,161,325,193,471,181,465,152" href="http://group31434.narod.ru/materials.html" alt="Материалы">
      <area shape="poly" coords="486,151,488,180,566,177,562,147" href="http://group31434.narod.ru/domzad.html" alt="Домашнее задание">
      <area shape="poly" coords="580,147,573,172,715,199,721,172" href="http://www.narod.ru/guestbook/index.xhtml?owner=68275428&sent=1" alt="Гостевая">
      <area shape="poly" coords="732,172,730,198,875,208,871,178" href="http://group31434.narod.ru/foto.html" alt="Фотоальбом">
    </map>
    </html>
    PHP:
    Hope some1 here can help me :) Thank you!
     
    anton-shirikov, Nov 10, 2007 IP
  2. anton-shirikov

    anton-shirikov Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    so can anyone help me with this ? :/
     
    anton-shirikov, Nov 11, 2007 IP
  3. jakomo

    jakomo Well-Known Member

    Messages:
    4,262
    Likes Received:
    82
    Best Answers:
    0
    Trophy Points:
    138
    #3
    Hello,
    Sorry what message ?
     
    jakomo, Nov 11, 2007 IP
  4. anton-shirikov

    anton-shirikov Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I mean comments that users post. In my script this comments are adding to the bottom of page. And i wish the new comments to appear at top. :/
     
    anton-shirikov, Nov 11, 2007 IP
  5. Vbot

    Vbot Peon

    Messages:
    107
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Just change this line:
    $query = "SELECT * FROM guestbook ORDER BY date_auto";
    PHP:
    To:
    $query = "SELECT * FROM guestbook ORDER BY date_auto DESC";
    PHP:
     
    Vbot, Nov 11, 2007 IP
  6. anton-shirikov

    anton-shirikov Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Vbot
    Thanks a lot :)
     
    anton-shirikov, Nov 12, 2007 IP