xcoderx Posted October 23, 2008 Share Posted October 23, 2008 hi friends i got this news script i like but there is something missing in a page we can add limit on how many messages to show, say if u add 6 and u make 10 news entries it will not show them all it will only show the latest 6 news. kindly help me add a next and previous page option so that all news can be viewed here is the coding <?php /** * NewsScript.php * * A Simple News script class * * @author www.surneo.com * @version 1.0 */ class NewsScript { var $maxLength, $lim, /* Num items to show */ $template, /* Template to use */ $db_name, /* DB Settings */ $db_host, $db_user, $db_password, $connection; /** * Constructor, executed when we make a new object. * * @return NewsScript Object */ function NewsScript() { // Import Database settings etc. require('config.php'); $this->db_host = $db_host; $this->db_name = $db_name; $this->db_user = $db_user; $this->db_password = $db_password; $this->lim = $lim; $this->template = $template; } /** * Display ALL news articles * */ function displayAll() { $this->connect(); // Use the limit in our settings to decide how many to show $result = mysql_query("SELECT * FROM newstbl ORDER BY date DESC LIMIT 0, $this->lim"); /* While we have more rows display them */ while($row = mysql_fetch_assoc($result)) { // Display the row $id = $row['id']; $author = $row['author']; $content = $row['content']; $date = $this->convertDate($row['date']); /* Variables are used in the template file to display news info */ require($this->template); } $this->close(); } /** * Display a single news item given it's ID. * * @param $id - ID of item to display * @return true/false */ function displayItem($id) { if(is_numeric($id)) { $this->connect(); $result = mysql_query("SELECT * FROM newstbl WHERE id = '$id'"); $row = mysql_fetch_assoc($result); $author = $row['author']; $content = $row['content']; $date = $this->convertDate($row['date']); require($this->template); // Show item with our template. $this->close(); return true; } else return false; // Not valid number } /** * Add a News Item to the DB * * @return true/false (true if we are successful) */ function addNews($author, $content) { $this->connect(); // Use MySQL CURDATE function to get the current Date. $sql = sprintf("INSERT INTO newstbl (author, content ,date) VALUES ('%s','%s',CURDATE())", $this->clean($author), $this->clean($content)); $result = mysql_query($sql, $this->connection); $this->close(); // are we successful? if($result) { return true; } else return false; } /** * Deletes the given News Item * * @return true or false (false if error) */ function deleteNews($id) { // Only allow numbers for ID if(is_numeric($id)) { $this->connect(); $result = mysql_query("DELETE FROM newstbl WHERE id='$id'"); $this->close(); // are we successful? if($result) { return true;//yes } else return false;//no } else return false; // None numeric value } /** * update a news atricle * * @param int $id * @param str $author * @param str $content * * @return true/false (successful?) */ function updateNews($id, $author, $content) { if(is_numeric($id)) { $this->connect(); $sql = sprintf("UPDATE newstbl SET author='%s', content='%s' WHERE id='%d'", $this->clean($author), $this->clean($content), $id); $result = mysql_query($sql); $this->close(); // are we successful? if($result) { return true;//yes } else return false;//no } return false; } /** * Connect to DB * * @return true/false */ function connect() { $this->connection = mysql_connect($this->db_host, $this->db_user, $this->db_password) or die("Unable to connect to MySQL"); mysql_select_db($this->db_name, $this->connection) or die("Unable to select DB!"); // Valid connection object? if(!$this->connection) { return false; } else return true; } /** * close DB Connection * */ function close() { mysql_close($this->connection); } /** * Convert from MySQL Date format: yyyy-mm-dd * into dd/mm/yyyy * * @param MySQL $date * @return string dd/mm/yyyy */ function convertDate($date) { $date_array = explode("-",$date); // split the array $y = $date_array[0]; $m = $date_array[1]; $d = $date_array[2]; return $d . "/" . $m . "/" . $y; } /** * Cleans a string for input into a MySQL Database. * Gets rid of unwanted characters/SQL injection etc. * * @return string */ function clean($str) { // Only remove slashes if it's already been slashed by PHP if(get_magic_quotes_gpc()) { $str = stripslashes($str); } // Let MySQL remove nasty characters. $str = mysql_real_escape_string($str); return $str; } /** * Replace smiley text in the given text with * link to the actual smiley image. */ function addSmiley($msg) { // example replacing '' with the smiley image file. $new_msg = str_replace("",'<img src="smileys/smiley1.gif">', $msg); return $new_msg; } } ?> Link to comment https://forums.phpfreaks.com/topic/129713-help-me-some-genious-people-add-a-next-page-here/ Share on other sites More sharing options...
xcoderx Posted October 23, 2008 Author Share Posted October 23, 2008 it will really be very greatful of u if you can help me out. thank you in advance. Link to comment https://forums.phpfreaks.com/topic/129713-help-me-some-genious-people-add-a-next-page-here/#findComment-672565 Share on other sites More sharing options...
JasonLewis Posted October 23, 2008 Share Posted October 23, 2008 Google "PHP Pagination Tutorial". At least try yourself before you get other people to do it. If you can't do it, post in the Freelance section, or perhaps someone will be feeling kind and do it. But I'm tired at the moment, so that's my advice. Link to comment https://forums.phpfreaks.com/topic/129713-help-me-some-genious-people-add-a-next-page-here/#findComment-672571 Share on other sites More sharing options...
trq Posted October 23, 2008 Share Posted October 23, 2008 There is in fact a tutorial on our main page. Here. Link to comment https://forums.phpfreaks.com/topic/129713-help-me-some-genious-people-add-a-next-page-here/#findComment-672574 Share on other sites More sharing options...
xcoderx Posted October 23, 2008 Author Share Posted October 23, 2008 i did try because i was unable that is why i help friends but thanks guess nobody will help me ??? Link to comment https://forums.phpfreaks.com/topic/129713-help-me-some-genious-people-add-a-next-page-here/#findComment-672575 Share on other sites More sharing options...
trq Posted October 23, 2008 Share Posted October 23, 2008 Were not going to do it for you if thats what your asking. Post the code your actually having problems with. Link to comment https://forums.phpfreaks.com/topic/129713-help-me-some-genious-people-add-a-next-page-here/#findComment-672604 Share on other sites More sharing options...
xcoderx Posted October 23, 2008 Author Share Posted October 23, 2008 here i try see class NewsScript { var $maxLength, $lim, /* Num items to show */ $template, /* Template to use */ $db_name, /* DB Settings */ $db_host, $db_user, $db_password, $connection; /** * Constructor, executed when we make a new object. * * @return NewsScript Object */ function NewsScript() { // Import Database settings etc. require('config.php'); $this->db_host = $db_host; $this->db_name = $db_name; $this->db_user = $db_user; $this->db_password = $db_password; $this->lim = $lim; $this->template = $template; } /** * Display ALL news articles * */ function displayAll() { $this->connect(); if ($_GET['page']==NULL){ $start = 0; } else { $start = $_GET['page']; } // the if above, states that if the variable in the URL named page has a value, $start will be set to it, otherwise by default it is set to 0, meaning that it will start at the beginning of the table by default. if($_GET['page'] < 0){ header("Location: http://site.com/news.php?page=0&lim=".$lim); } // Use the limit in our settings to decide how many to show $result = mysql_query("SELECT * FROM news ORDER BY date DESC LIMIT 0, $this->lim"); /* While we have more rows display them */ while($row = mysql_fetch_assoc($result)) { // Display the row $id = $row['id']; $author = $row['author']; $content = $row['content']; $date = $this->convertDate($row['date']); /* Variables are used in the template file to display news info */ require($this->template); } $previous = $start + $lim; $next = $start - $lim; echo "<div align='center'> <a href='http://site.com/news.php?start=".$previous."'>Previous Page</a> - <a href='http://site.com/news.php?start=".$next."'>Next Page</a> </div> "; $this->close(); } but could not Link to comment https://forums.phpfreaks.com/topic/129713-help-me-some-genious-people-add-a-next-page-here/#findComment-672637 Share on other sites More sharing options...
xcoderx Posted October 23, 2008 Author Share Posted October 23, 2008 in my config lim as is limit is set to 3 limit is done from the config file i tried everything but i cnt seem to get it work Link to comment https://forums.phpfreaks.com/topic/129713-help-me-some-genious-people-add-a-next-page-here/#findComment-672640 Share on other sites More sharing options...
xcoderx Posted October 24, 2008 Author Share Posted October 24, 2008 help fix this please Link to comment https://forums.phpfreaks.com/topic/129713-help-me-some-genious-people-add-a-next-page-here/#findComment-673490 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.