Help with a PHP issue in DW

  • Travis Coats
  • Graduate
  • Graduate
  • Travis Coats
  • Posts: 107

Post 3+ Months Ago

Hey guys, I use DW CS3 for most all of my design and coding needs so this is definitely going to present as an amateur question. :)

I have this PHP/MYSQL query I made to average the ratings of individual tracks and then order them descending from highest (5.0) to lowest(1). I have managed to get the math functioning but I have been unable to do like my designer intended - make the numeral output of the averages represented by Unicode stars ★. The goal is to have the numbers present as the unicode html on the fly before the page is served. However, DW's code is rather intense for my novice mind and I have been unable to implement the following snippet into my code successfully:

Code: [ Select ]
$rater_stars="";


// Assign star image
if ($query_ratings_average <= 0 ){$rater_stars = "0";}
if ($query_ratings_average >= 0.5){$rater_stars = "&#189;}
if ($query_ratings_average >= 1 ){$rater_stars = "&#9733;";}
if ($query_ratings_average >= 1.5){$rater_stars = "&#9733;&#189;";}
if ($query_ratings_average >= 2 ){$rater_stars = "&#9733;&#9733;";}
if ($query_ratings_average >= 2.5){$rater_stars = "&#9733;&#9733;&#189;";}
if ($query_ratings_average >= 3 ){$rater_stars = "&#9733;&#9733;&#9733;";}
if ($query_ratings_average >= 3.5){$rater_stars = "&#9733;&#9733;&#9733;&#189;";}
if ($query_ratings_average >= 4 ){$rater_stars = "&#9733;&#9733;&#9733;&#9733;";}
if ($query_ratings_average >= 4.5){$rater_stars = "&#9733;&#9733;&#9733;&#9733;&#189;";}
if ($query_ratings_average >= 5 ){$rater_stars = "&#9733;&#9733;&#9733;&#9733;&#9733;";}
  1. $rater_stars="";
  2. // Assign star image
  3. if ($query_ratings_average <= 0 ){$rater_stars = "0";}
  4. if ($query_ratings_average >= 0.5){$rater_stars = "&#189;}
  5. if ($query_ratings_average >= 1 ){$rater_stars = "&#9733;";}
  6. if ($query_ratings_average >= 1.5){$rater_stars = "&#9733;&#189;";}
  7. if ($query_ratings_average >= 2 ){$rater_stars = "&#9733;&#9733;";}
  8. if ($query_ratings_average >= 2.5){$rater_stars = "&#9733;&#9733;&#189;";}
  9. if ($query_ratings_average >= 3 ){$rater_stars = "&#9733;&#9733;&#9733;";}
  10. if ($query_ratings_average >= 3.5){$rater_stars = "&#9733;&#9733;&#9733;&#189;";}
  11. if ($query_ratings_average >= 4 ){$rater_stars = "&#9733;&#9733;&#9733;&#9733;";}
  12. if ($query_ratings_average >= 4.5){$rater_stars = "&#9733;&#9733;&#9733;&#9733;&#189;";}
  13. if ($query_ratings_average >= 5 ){$rater_stars = "&#9733;&#9733;&#9733;&#9733;&#9733;";}


Of course the variables I used in this are just temporary until I can figure out how to make it work. Here's the area concerned, in code view, that DW made for me - and I am currently analyzing:

Code: [ Select ]
mysql_select_db($database_main_guestbook, $main_guestbook);
$query_comments_all = "SELECT * FROM listenerfeedback ORDER BY id DESC";
$comments_all = mysql_query($query_comments_all, $main_guestbook) or die(mysql_error());
$row_comments_all = mysql_fetch_assoc($comments_all);
$totalRows_comments_all = mysql_num_rows($comments_all);

$maxRows_ratings_average = 10;
$pageNum_ratings_average = 0;
if (isset($_GET['pageNum_ratings_average'])) {
 $pageNum_ratings_average = $_GET['pageNum_ratings_average'];
}
$startRow_ratings_average = $pageNum_ratings_average * $maxRows_ratings_average;

mysql_select_db($database_main_catalog, $main_catalog);
$query_ratings_average = "SELECT track, ROUND( avg( rating ) , 1 ) as avg_rating, COUNT(rating) as total_ratings FROM `trackRatings` GROUP BY track ORDER BY avg_rating DESC, total_ratings DESC";
$query_limit_ratings_average = sprintf("%s LIMIT %d, %d", $query_ratings_average, $startRow_ratings_average, $maxRows_ratings_average);
$ratings_average = mysql_query($query_limit_ratings_average, $main_catalog) or die(mysql_error());
$row_ratings_average = mysql_fetch_assoc($ratings_average);

if (isset($_GET['totalRows_ratings_average'])) {
 $totalRows_ratings_average = $_GET['totalRows_ratings_average'];
 
} else {
 $all_ratings_average = mysql_query($query_ratings_average);
 $totalRows_ratings_average = mysql_num_rows($all_ratings_average);
}
$totalPages_ratings_average = ceil($totalRows_ratings_average/$maxRows_ratings_average)-1;
  1. mysql_select_db($database_main_guestbook, $main_guestbook);
  2. $query_comments_all = "SELECT * FROM listenerfeedback ORDER BY id DESC";
  3. $comments_all = mysql_query($query_comments_all, $main_guestbook) or die(mysql_error());
  4. $row_comments_all = mysql_fetch_assoc($comments_all);
  5. $totalRows_comments_all = mysql_num_rows($comments_all);
  6. $maxRows_ratings_average = 10;
  7. $pageNum_ratings_average = 0;
  8. if (isset($_GET['pageNum_ratings_average'])) {
  9.  $pageNum_ratings_average = $_GET['pageNum_ratings_average'];
  10. }
  11. $startRow_ratings_average = $pageNum_ratings_average * $maxRows_ratings_average;
  12. mysql_select_db($database_main_catalog, $main_catalog);
  13. $query_ratings_average = "SELECT track, ROUND( avg( rating ) , 1 ) as avg_rating, COUNT(rating) as total_ratings FROM `trackRatings` GROUP BY track ORDER BY avg_rating DESC, total_ratings DESC";
  14. $query_limit_ratings_average = sprintf("%s LIMIT %d, %d", $query_ratings_average, $startRow_ratings_average, $maxRows_ratings_average);
  15. $ratings_average = mysql_query($query_limit_ratings_average, $main_catalog) or die(mysql_error());
  16. $row_ratings_average = mysql_fetch_assoc($ratings_average);
  17. if (isset($_GET['totalRows_ratings_average'])) {
  18.  $totalRows_ratings_average = $_GET['totalRows_ratings_average'];
  19.  
  20. } else {
  21.  $all_ratings_average = mysql_query($query_ratings_average);
  22.  $totalRows_ratings_average = mysql_num_rows($all_ratings_average);
  23. }
  24. $totalPages_ratings_average = ceil($totalRows_ratings_average/$maxRows_ratings_average)-1;


With these examples, can someone push me in the right direction as to how I can incorporate this code into DW's plan so as to not cause to much head scratching on my part? The code that actually displays the listener ratings is as follows (so you can see the variable data):

Code: [ Select ]
<li><h4>Listener rating</h4></li>
                                <?php do { ?>
                                 <li><strong><?php echo $row_ratings_average['track']; ?></strong><?php echo $row_ratings_average['avg_rating']; ?> &#9733;'s<br/> Rated <?php echo $row_ratings_average['total_ratings']; ?> time(s)</li>
                         <?php } while ($row_ratings_average = mysql_fetch_assoc($ratings_average)); ?>
  1. <li><h4>Listener rating</h4></li>
  2.                                 <?php do { ?>
  3.                                  <li><strong><?php echo $row_ratings_average['track']; ?></strong><?php echo $row_ratings_average['avg_rating']; ?> &#9733;'s<br/> Rated <?php echo $row_ratings_average['total_ratings']; ?> time(s)</li>
  4.                          <?php } while ($row_ratings_average = mysql_fetch_assoc($ratings_average)); ?>


This is the actual output of the repeating region where the query is put out with a max of the top ten averages. Again, thanks for any help. Sorry for the long post.
  • Travis Coats
  • Graduate
  • Graduate
  • Travis Coats
  • Posts: 107

Post 3+ Months Ago

No thoughts or suggestions? I have tried a few more times to integrate the code for the stars into the functions that DW creates, but I keep getting a white page - which denotes an error. I am an absolute beginner with PHP and I would appreciate anything you have to offer. :)
  • devilwood
  • Silver Member
  • Silver Member
  • User avatar
  • Posts: 436

Post 3+ Months Ago

I don't see where $query_ratings_average variable in your "if" statements is actually getting set a value. That variable is just your SQL text and doesn't contain the values (0,0.5,1,etc) you need to compare. I'm also not seeing where you use $rater_stars when you display your Listener Rating so I'm a little lost as to what the whole first section of your code is for. Otherwise, it actually sounds like you got the hard part done and you just need to swap an "img" (this case a unicode image) with the values pulled from the db, but I think those values are not stored in $query_ratings_average.

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 63 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.