RaythMistwalker Posted January 5, 2010 Share Posted January 5, 2010 <?php //Get Shoutbox Messages if Person is Allowed to see if ($canViewShoutbox == 1) { $getShoutBox="SELECT * FROM shoutbox ORDER BY msg_id"; $shoutbox=mysql_query($getShoutBox); $lines=mysql_numrows($shoutbox); $last=mysql_numrows($shoutbox); if ($last < { $last = 0; } else { $last -= 7; } $counter=mysql_numrows($shoutbox); while ($counter > $last) { --$counter; $message=mysql_result($shoutbox,$counter,"message"); $sender=mysql_result($shoutbox,$counter,"sender_name"); ?> <tr><td valign=top><b><?php echo $sender;?></b>:</td><td> <? echo $message; ?></td></tr> <?php } echo "</table>"; ?> The above code is my current shoutbox code which i finished earlier today. Is there a way i can just load the LAST 8 lines of the shoutbox instead of loading them all then making it stop the counter at 7 lines under the last? It also has to stay the same way and have latest message at the top as shown. Link to comment https://forums.phpfreaks.com/topic/187327-shoutbox-done-but-i-want-this-to-be-a-little-easier/ Share on other sites More sharing options...
RaythMistwalker Posted January 5, 2010 Author Share Posted January 5, 2010 UPDATE. This is my full shoutbox.php code which i want to load just 8 values from database instead of everything (even tho this still displays just <?php session_start(); require_once('auth.php'); include("config.php"); ini_set('display_errors', 'on'); error_reporting(E_ALL); //Get IP $ip = $_SERVER['REMOTE_ADDR']; $id=$_SESSION['SESS_MEMBER_ID']; mysql_connect(DB_HOST,DB_USER,DB_PASSWORD); @mysql_select_db(DB_DATABASE) or die( "Unable to select database"); $query="SELECT * FROM members WHERE member_id='$id'"; $result=mysql_query($query) or trigger_error('Query error! Query: <pre>'.$query.'</pre>Reason: ' . mysql_error()); $num=mysql_numrows($result); $i=0; while ($i < $num) { $fname=mysql_result($result,$i,"firstname"); $lname=mysql_result($result,$i,"lastname"); $rank=mysql_result($result,$i,"rank"); if((trim($rank) > '98')) { $canViewShoutbox=1; } else { $canViewShoutbox=0; } ++$i; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Member Index</title> <link href="loginmodule.css" rel="stylesheet" type="text/css" /> </head> <body text="#0000ff" link="#00a5e9" vlink="#00a5e9" alink="#00a5e9" leftmargin="2" topmargin="2" marginwidth="2" marginheight="0"> <table width=100% class='tableborder'> <?php //Get Shoutbox Messages if Person is Allowed to see if ($canViewShoutbox == 1) { $getShoutBox="SELECT * FROM shoutbox ORDER BY msg_id"; $shoutbox=mysql_query($getShoutBox); $lines=mysql_numrows($shoutbox); $last=mysql_numrows($shoutbox); if ($last < { $last = 0; } else { $last -= 7; } $counter=mysql_numrows($shoutbox); while ($counter > $last) { --$counter; $message=mysql_result($shoutbox,$counter,"message"); $sender=mysql_result($shoutbox,$counter,"sender_name"); ?> <tr><td valign=top><b><?php echo $sender;?></b>:</td><td> <? echo $message; ?></td></tr> <?php } echo "</table>"; ?> <br> <table class='tableborder' width=100%> <form action="sendshout.php"> <input type="hidden" value="<?php echo $id; ?>" name="sender_id"> <input type="hidden" value="<?php echo $fname." ".$lname; ?>" name="sender_name"> <tr><td>Name:</td><td><b> <?php echo $fname." ".$lname; ?></td></tr> <tr><td>Message:</td><td> <input type="text" name="message" maxlength=2500 size=100%></td></tr> <input type="hidden" value="<?php echo $ip; ?>" name="ip"> </table> <input type="submit" value="Post"> </form> <? } if ($canViewShoutbox == 0) { echo "Sorry, you do not have the correct permissions to view this shoutbox."; } ?> The following is sendshout.php <?php ini_set('display_errors', 'on'); error_reporting(E_ALL); include("config.php"); mysql_connect(DB_HOST,DB_USER,DB_PASSWORD); //Function to sanitize values received from the form. Prevents SQL injection function clean($str) { $str = @trim($str); if(get_magic_quotes_gpc()) { $str = stripslashes($str); } return mysql_real_escape_string($str); } //Sanitize the POST values $sender_name = clean($_GET['sender_name']); $sender_id = clean($_GET['sender_id']) $message = clean($_GET['message']); $ip = clean($_GET['ip']); @mysql_select_db(DB_DATABASE) or die( "Unable to select database"); $qry = "INSERT INTO shoutbox(sender_name, sender_id, message, ip) VALUES('$sender_name','$sender_id','$message', '$ip')"; $result=mysql_query($qry) or trigger_error('Query error! Query: <pre>'.$query.'</pre>Reason: ' . mysql_error()); mysql_close(); header("location: shoutbox.php"); exit(); ?> The problem with this is: 1. It's not uploading to the database and so 2. shoutbox is not updated. It isn't returning any errors either so i prob just missed something small. Link to comment https://forums.phpfreaks.com/topic/187327-shoutbox-done-but-i-want-this-to-be-a-little-easier/#findComment-989197 Share on other sites More sharing options...
RaythMistwalker Posted January 5, 2010 Author Share Posted January 5, 2010 Ok code fixed and it now posts. Now i'm just interested if theres a way to only return so many values from the database? Link to comment https://forums.phpfreaks.com/topic/187327-shoutbox-done-but-i-want-this-to-be-a-little-easier/#findComment-989211 Share on other sites More sharing options...
monkeypaw201 Posted January 5, 2010 Share Posted January 5, 2010 Yes, you can easily do this with MySQL's LIMIT. For example, replace $getShoutBox="SELECT * FROM shoutbox ORDER BY msg_id"; with (you can change 8 to any number) $getShoutBox="SELECT * FROM shoutbox ORDER BY msg_id LIMIT 8"; Link to comment https://forums.phpfreaks.com/topic/187327-shoutbox-done-but-i-want-this-to-be-a-little-easier/#findComment-989229 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.