Rating the articles,news,quotes etc
Posted: Tue Aug 28, 2007 11:48 am
feyd | Please use
The code i posted above so when i write http://localhost/pages.php?page=3 no change is there in the db? no changes even if i write http://localhost/pages.php?page=2 or whatever just displays the article content followed by Notice: Undefined variable: articleId in c:\program files\easyphp1-8\www\pages.php on line 57
Warning: Division by zero in c:\program files\easyphp1-8\www\pages.php on line 67[/syntax]
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
i created a table Rating and added the following data for testing. What changes do i make to pages.php to make it work. Currently no changes are made to db (to the data i have entered manually). What do i do to rate the articles.
[syntax="sql"]
CREATE TABLE `ratings` (
`RateID` int(11) NOT NULL auto_increment,
`ElementID` varchar(20) NOT NULL default '',
`Rating` int(11) NOT NULL default '0',
`IP` varchar(80) NOT NULL default '',
PRIMARY KEY (`RateID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
--
-- Dumping data for table `ratings`
--
INSERT INTO `ratings` VALUES (1, 'wow3', 3, '');Warning: Division by zero in c:\program files\easyphp1-8\www\pages.php on line 67[/syntax]
Code: Select all
<?php
//$lastPage = ceil($numRows/$perPage);
// Database Connection
include("$_SERVER[DOCUMENT_ROOT]/includes/connect.inc.php");
include("$_SERVER[DOCUMENT_ROOT]/includes/stripgpcslash.inc.php");
// If current page number, use it
// if not, set one!
if(!isset($_GET['page']))
{
$page = 1;
}
else
{
if(ctype_digit($_GET['page']))
{
$page=trim(mysql_real_escape_string($_GET['page']));
}
else
{
echo "invalid query";
exit;
}
}
// Define the number of results per page
$max_results = 1;
// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results);
// Perform MySQL query on only the current page number's results
$countviews = mysql_query("UPDATE wow SET views=views+1 WHERE id=$page");
$sql = mysql_query("SELECT * FROM wow WHERE `trusted` = 0 ORDER BY `id` ASC LIMIT $from, $max_results");
while($row = mysql_fetch_array($sql)){
echo "<!--start-->";
// Build your formatted results here.
echo "<font size=3><b>";
echo $row['title']."</b><br /></font>";
echo "<font size=2>";
echo "Contributed By ";
echo $row['contributed_by'];
echo " on ";
echo date("d-m-Y H:i:s", $row['date'])."<p>";
echo $row['content']."</p><br />";
echo "<!--end-->";
echo '<a href="editwiki.php?id='.$row['id'].'"><strong>Edit This Article</strong></a> | <a href="../fpdf/tutorial/pdfmodi.php?id='.$row['id'].'"><strong>PDF</strong></a> | <a href="../print/printpage.php"><strong>Print This Page</strong></a>';
}
function getRatings(&$averageRating, &$totalRatings, &$rateArray, $type, $articleID){
$ratingquery = mysql_query("SELECT `Rating` FROM `Ratings` WHERE `ElementID` = '".$type.$articleid."'");
while($row = mysql_num_rows($ratingquery)){
$rateArray[] = $row['Rating'];
}
$totalRatings = count($rateArray);
if(!$totalRatings){
$averageRating = array_sum($rateArray)/$totalRatings;
}else{
$averageRating = 0;
}
return;
}
$totalRatings = 0;
$averageRating = 0;
$elementType = "wow";
$articleId = $page;
$rateArray = array();
getRatings($averageRating, $totalRatings, $rateArray, $elementType, $articleId);
// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM wow"),0);
// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);
// Build Page Number Hyperlinks
echo "<center>Select a Page<br />";
// Build Previous Link
if($page > 1){
$prev = ($page - 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\">< Prev </a> ";
}
echo $page;
echo " of ";
echo $total_pages;
// Build Next Link
if($page < $total_pages){
$next = ($page + 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\"> Next ></a>";
}
echo "</center>";
mysql_close($con);
?>feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]