Jump to content
Larry Ullman's Book Forums
rocky66

Knowledge Is Power - Chapter 5 Bonus, Rate Content

Recommended Posts

Firstly.. This book is by far the best PHP e-commerce book I found on Amazon, period ! Thank you Larry for this great opportunity to learn from you. Your ideas are truly unique and so helpful for my project.

 

I'm trying to rate my content according to your bonus material from chapter 5 "Rating Content".

I would like to rate my content displayed in page.php using drop-down box like u suggested but unfortunately I'm lost after many attempts. I tried to add query from bonus material to insert values from drop-down box but, I couldn't succeeded.Can anyone please help me solve this problem. Thanks

 

this is query from  Chapter 5 Bonus Pages, page 4:

INSERT INTO page_ratings (user_id, page_id, rating) VALUES ($_SESSION['user_id'], $_POST['id'] , $_POST['rating'])

 

code for page.php :

 

 

?php
 
// This page displays a specific page of HTML content.
// This script is created in Chapter 5.
 
// Require the configuration before any PHP code as the configuration controls error reporting:
require ('./includes/config.inc.php');
// The config file also starts the session.
 
// Require the database connection:
require(MYSQL);
 
// Validate the category ID:
if (isset($_GET['id']) && filter_var($_GET['id'], FILTER_VALIDATE_INT, array('min_range' => 1))) {
 
// Get the page info:
$q = 'SELECT title, description, content FROM pages WHERE id=' . $_GET['id'];
$r = mysqli_query($dbc, $q);
if (mysqli_num_rows($r) != 1) { // Problem!
$page_title = 'Error!';
include ('./includes/header.html');
echo '<p class="error">This page has been accessed in error.</p>';
include ('./includes/footer.html');
exit();
}
 
// Fetch the page info:
$row = mysqli_fetch_array($r, MYSQLI_ASSOC);
$page_title = $row['title'];
include ('includes/header.html');
echo "<h3>$page_title</h3>";
 
// Display the content if the user's account is current:
if (isset($_SESSION['user_not_expired'])) {
 
// Bonus material! Referenced in Chapter 5.
// Create add to favorites and remove from favorites links:
// See if this is favorite:
$q = 'SELECT user_id FROM favorite_pages WHERE user_id=' . $_SESSION['user_id'] . ' AND page_id=' . $_GET['id'];
$r = mysqli_query($dbc, $q);
if (mysqli_num_rows($r) == 1) {
echo '<p> This is a favorite! To remove from list press thumb down<a href="remove_from_favorites.php?id=' . $_GET['id'] . '"><img src="images/thumbdown.jpg" border="0" width="48" height="48" align="middle" /></a></p>';
} else {
echo '<p>Make this a favorite! <a href="add_to_favorites.php?id=' . $_GET['id'] . '"><img src="images/thumbup.jpg" border="0" width="48" height="48" align="middle" /></a>';
}
 
 
// Show the page content:
echo "<div>{$row['content']}</div>";
 
// Bonus material referenced in chapter 5
// Rate Content
 
 
 
 
// Bonus material! Referenced in Chapter 5.
// Record this visit to the history table:
$q = "INSERT INTO history (user_id, type, page_id) VALUES ({$_SESSION['user_id']}, 'page', {$_GET['id']}) AND ";
$r = mysqli_query($dbc, $q);
 
} elseif (isset($_SESSION['user_id'])) { // Logged in but not current.
echo '<p class="error">Thank you for your interest in this content, but your account is no longer current. Please <a href="renew.php">renew your account</a> in order to view this page in its entirety</p>';
echo "<div>{$row['description']}</div>";
} else { // Not logged in.
echo '<p class="error">Thank you for your interest in this content. You must be a logged in as a registered user to view this page in its entirety.</p>';
echo "<div>{$row['description']}</div>";
}
 
} else { // No valid ID.
$page_title = 'Error!';
include ('includes/header.html');
echo '<p class="error">This page has been accessed in error.</p>';
} // End of primary IF.
?>
<label>Rating</label>
    <select id="rating" name="rating">
      <option value="rating" selected="selected">-- Rate This tutorial--</option>
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
      <option value="6">6</option>
      <option value="7">7</option>
      <option value="8">8</option>
      <option value="9">9</option>
      <option value="10">10</option>
  
    </select>
  
<?php
include ('./includes/footer.html');
?>

Share this post


Link to post
Share on other sites

Thank you very much for the nice words on the book. Very kind of you to say. 

 

As for your problem, could you please expand a bit on what's going on? What is happening that shouldn't? What isn't happening that should? What debugging steps have you taken and what were the results? 

Share this post


Link to post
Share on other sites

Hi Larry. I'm following knowledge is power example and I almost finished. I would like to add a possibility to rate content displayed in page.php .You describbed rating process in " EffortlessECommerce Chapter 05  Bonus pages, page 5" available to download from your website. I've created drop-down box in page.php to rate content dispalyed in page.php  .I want to insert from session user_id, page_id  and rating value from drop-down box.

 

$q = INSERT INTO page_ratings (user_id, page_id, rating) VALUES ($_SESSION['user_id'], $_POST['id'] , $_POST['rating']);

$r = mysqli_query($dbc, $q);

 

I'm using dreamweaver cs6 and its only showing me that there is error in query, when I try to debug it, it doesn't display error in div content like before... I've started to learn php from your books and I still have a lot to learn. If you could look at the page 5 in " EffortlessECommerce Chapter 05  Bonus pages " you will know what I'm trying to accomplish. Up to this point everything went smoothly and I didn't have major problems with "Knowledge is Power' example. I'm a novice so its difficult for me to solve this alone....  thank you :)

Share this post


Link to post
Share on other sites

Run the query in something like phpmyadmin substituting values in the values clause of your query to see if you get an error. Its difficult to help without knowing what error you're receiving.

  • Upvote 1

Share this post


Link to post
Share on other sites

Firstly.. This book is by far the best PHP e-commerce book I found on Amazon, period !

 

This is good news, I just ordered the book a couple days ago. :)  Can't wait to get started.

 

Share this post


Link to post
Share on other sites

here's my fix...

 

on the page.php... here's the code you need...

 

// Bonus material! Referenced in Chapter 5.
// Rate This Page:
$q = 'SELECT user_id, rating FROM page_ratings WHERE user_id=' . $_SESSION['user_id'] . ' AND page_id=' . $_GET['id'];
$r = mysqli_query($dbc, $q);
$id = $_GET['id'];
  // Fetch the page info:
$row = mysqli_fetch_array($r, MYSQLI_ASSOC);
$page_title = $row['rating'];
if (mysqli_num_rows($r) == 1) {
echo '<br> <br><div>
Page Already Rated,  Your Page Rating Is:'.$page_title.'  </div>';
 
 
else {
echo ' <br> <br>
<div>
Please Rate This Page!
<form action="rate_this_page.php">
<select id="rating" name="rating">
      <option value="rating" selected="selected">-- Rate This tutorial--</option>
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
      <option value="6">6</option>
      <option value="7">7</option>
      <option value="8">8</option>
      <option value="9">9</option>
      <option value="10">10</option>
  
    </select>
<input type="hidden" name="id" value="'.$id.'">
 
<input type="submit"/>
</form>
</div>';
}
 
 
 
then create the processing page... rate_this_page.php or so...
 
code you need...
 
<?php
 
// This page adds a page of content to the list of the user's favorites.
// This is bonus material based upon recommendations suggested in Chapter 5.
 
// Require the configuration before any PHP code as the configuration controls error reporting:
require ('./includes/config.inc.php');
// The config file also starts the session.
 
// If the user isn't active, redirect them:
redirect_invalid_user('user_not_expired');
 
// Require the database connection:
require(MYSQL);
 
// Validate the category ID:
if (filter_var($_GET['id'], FILTER_VALIDATE_INT, array('min_range' => 1))) {
 
 
 
// Get the page info:
$q = 'SELECT title, description, content FROM pages WHERE id=' . $_GET['id'];
$r = mysqli_query($dbc, $q);
if (mysqli_num_rows($r) != 1) { // Problem!
$page_title = 'Error!';
include ('./includes/header.html');
echo '<p class="error">This page has been accessed in error.</p>';
include ('./includes/footer.html');
exit();
}
 
// Fetch the page info:
$row = mysqli_fetch_array($r, MYSQLI_ASSOC);
$page_title = $row['title'];
include ('includes/header.html');
echo "<h3>$page_title</h3>";
 
 
 
 
 
 
 
 
// Add this rating to the database:
$q = 'REPLACE INTO page_ratings (user_id, page_id, rating) VALUES (' . $_SESSION['user_id'] . ', ' . $_GET['id'] . ','.$_GET['rating'].')';
$r = mysqli_query($dbc, $q);
if (mysqli_affected_rows($dbc) == 1) {
echo '<p>Pge has been rated</p>';
} else {
trigger_error('A system error occurred. We apologize for any inconvenience.');
}
 
 
 
 
 
 
 
 
 
 
// Show the page content:
echo "<div>{$row['content']}</div>";
 
} else { // No valid ID.
$page_title = 'Error!';
include ('includes/header.html');
echo '<p class="error">This page has been accessed in error.</p>';
} // End of primary IF.
 
// Include the HTML footer:
include ('./includes/footer.html');
?>
 
 
 
on the database  itself you might want to insert some test records, optional...
 
works  on my site...
 
good luck 
 

Share this post


Link to post
Share on other sites

Hello rocky66,

 

I use the exemple of this site to develope  a recipes sharing site

 

So, i use this "plus" on my site

 

recettesoriginales.fr 

 

…if you are a user evidently

 

you can read also this article from Larry on peachpit'site,

 

Creating an Ajax-Enabled Rating System for Your Website

 

http://www.peachpit.com/articles/article.aspx?p=1681764

 

So thanks Larry for your books.

cordialy

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...