Jump to content

SOLVED: SQL Problem


Rezert

Recommended Posts

This works fine on the first page (5 Entries).  On the second page though it gets the 5 then everything on the 3rd page.  Even if I hard code 5 - 10.

 

www.mlfmp.com

 

<?php
// Simple News Script

function bb_decode($text) {
$text = htmlspecialchars($text, ENT_QUOTES);
$patterns = array("/\[b\](.*?)\[\/b\]/i", "/\[i\](.*?)\[\/i\]/i", "/\[u\](.*?)\[\/u\]/i", "/\[url\]http:\/\/(.*?)\[\/url\]/i", "/\[url=http:\/\/(.*?)\](.*?)\[\/url\]/i", "/\[img\](.*?)\[\/img\]/i");
$replaces = array("<b>$1</b>", "<i>$1</i>", "<u>$1</u>", "<a href='http://$1'>$1</a>", "<a href='http://$1'>$2</a>", "<img src='$1' />");
$retval   = preg_replace($patterns, $replaces, $text);
$retval   = str_replace(array("", "", "", "", "", ":clap:", ""), array("<img src='images/smileys/smile.gif' alt='smile.gif' />", "<img src='images/smileys/sad.gif' alt='sad.gif' />", "<img src='images/smileys/tongue.gif' alt='tongue.gif' />", "<img src='images/smileys/biggrin.gif' alt='biggrin.gif' />", "<img src='images/smileys/blink.gif' alt='blink.gif' />", "<img src='images/smileys/clap.gif' alt='clap.gif' />", "<img src='images/smileys/lol.gif' alt='lol.gif' />"), $retval);
return $retval;
}

$host     = "localhost";
$username = "zenpets_news";
$password = "####";
$database = "zenpets_news";

$conn = mysql_connect($host, $username, $password);
mysql_select_db($database, $conn);

$rows    = mysql_num_rows(mysql_query("SELECT * FROM `news`", $conn));
$rowspp  = 5;
$range   = 3;
$pages   = ceil($rows / $rowspp);

if($rows == 0) {
$body = "<p><i>There is no news to display!</i></p>";
} else {
if(isset($_GET['pg']) && is_numeric($_GET['pg'])) {
$current = intval($_GET['pg']);
} else {
$current = 1;
}

if($current > $pages) {
$current = $pages;
} elseif($current < 1) {
$current = 1;
}

$prev    = $current - 1;
$next    = $current + 1;

$offset = ($current * $rowspp) - $rowspp;
$onset  = ($current * $rowspp);

$result = mysql_query("SELECT * FROM `news` ORDER BY `id` DESC LIMIT $offset, $onset", $conn);

$body = "<table width='400px'>
";

while($row = mysql_fetch_array($result)) {
$comments = mysql_num_rows(mysql_query("SELECT * FROM `comments` WHERE `nid`='" . $row['id'] . "'", $conn));
$body .= "<tr>
<td colspan='2'><center><strong><h2>" . $row['title'] . "</h2></strong></center></td>
</tr>
<tr>
<td>Written By:</td>
<td>" . $row['author'] . "</td>
</tr>
<tr>
<td>Date and Time:</td>
<td>" . $row['date'] . "</td>
</tr>
<tr>
<td colspan='2'>News Post:</td>
</tr>
<tr>
<td colspan='2'>" . nl2br(bb_decode($row['post'])) . "</td>
</tr>
<tr>
<td></td>
<td><a href='/comments.php?id=" . $row['id'] . "'>(" . $comments . ") View Comments</a></td>
</tr>
<tr>
<td><br /><br /></td>
</tr>
";
}

$body .= "</table>
";

if($pages > 1) {
$body .= "<p name='navbar' align='center'>
";

if($current > 1) {
	if($current - 1 != 1) {
		$body .= "<a href='" . $_SERVER['PHP_SELF'] . "?pg=1'><<</a> ";
	}
	$body .= "<a href='" . $_SERVER['PHP_SELF'] . "?pg=" . $prev . "'><</a> ";
}

for($i=(($current - $range) - 1);$i<(($current + $range) + 1);$i++) {
	if($i > 0 && $i <= $pages) {
		if($i == $current) {
			$body .= "<strong>" . $i . "</strong> ";
		} else {
			$body .= "<a href='" . $_SERVER['PHP_SELF'] . "?pg=" . $i . "'>" . $i . "</a> ";
		}
	}
}

if($current < $pages) {
	$body .= "<a href='" . $_SERVER['PHP_SELF'] . "?pg=" . $next . "'>></a> ";
	if($current + 1 != $pages) {
		$body .= "<a href='" . $_SERVER['PHP_SELF'] . "?pg=" . $pages . "'>>></a> ";
	}
}

$body .= "
</p>";
}

}

?>

Link to comment
https://forums.phpfreaks.com/topic/114535-solved-sql-problem/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.