Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> Blocks
Author Message
rofreason
New Member
New Member


Joined: Aug 25, 2005
Posts: 5

PostPosted: Fri Sep 16, 2005 12:42 pm Reply with quote

Hi, I have a music site and do hard rock/heavy metal album reviews. I couldn't find a block that would display just a random review from my database so I cobbled one together. Since I wanted to give something back here (as I have taken lots of great information) I thought someone might find this useful? Hopefully this is the right forum to post this in.

Need I say this is my first block and it most likely a mess. If there are ways to optimize it/constructive criticisms I would be most appreciative!

It pulls a review title and score from the database and if there is an image associated with it it gets that. If there is none it displays a default image defaultcover.jpg that lives in the images/reviews folder.

Here is the code:

Code:
<?php


/************************************************************************/
/* PHP-NUKE: Web Portal System                                          */
/* ===========================                                          */
/*                                                                      */
/* Copyright (c) 2005 by Francisco Burzi                                */
/* http://phpnuke.org                                                   */
/*                                                                      */
/* Random Review Block block-RandomReview.php                           */
/*                                                                      */
/* Written by Evan Howell http://www.remnantsofreason.com 09/13/05      */
/*                                                                      */
/* This block displays a random review with associated image            */
/* if there is no associated image it displays defaultcover.jpg         */
/* which is in your images/reviews folder.                              */
/* if you have questions you can email me at Only registered users can see links on this board! Get registered or login! */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/

if ( !defined('BLOCK_FILE') ) {
    Header("Location: ../index.php");
    die();
}

global $prefix, $db;

$sql1 = "SELECT * FROM ".$prefix."_reviews";
$query = $db->sql_query($sql1);
$numrows = $db->sql_numrows($query);
$rannum = rand(1,$numrows);

$sql = "SELECT id, title, cover, score FROM ".$prefix."_reviews WHERE id='$rannum'";
$result = $db->sql_query($sql);
list($id, $title,$cover, $score) = $db->sql_fetchrow($result);
$id = intval($id);
$title = stripslashes($title);
$score = stripslashes($score);
$cover = stripslashes(check_html($cover, "nohtml"));
if (!empty($cover)) {
   $content .= "<img src=\"images/reviews/$cover\" align=\"center\" border=\"1\" width=\"150\" height=\"150\" vspace=\"2\" alt=\"\">";
   $content .= "<br><br>";
} else {
   $content .= "<img src=\"images/reviews/defaultcover.jpg\" align=\"center\" border=\"1\" width=\"150\" height=\"150\" vspace=\"2\" alt=\"\">";
   $content .= "<br><br>";
}
$content .= "<strong><big>&middot;</big></strong>&nbsp;<a href=\"reviews.html?amp;rop=showcontent&amp;id=$id\">$title</a><br><br>";


$image = "<img src=\"images/blue.gif\" alt=\"\">";
$halfimage = "<img src=\"images/bluehalf.gif\" alt=\"\">";
$full = "<img src=\"images/star.gif\" alt=\"\">";

if ($score == 10) {
   for ($i=0; $i < 5; $i++)
   $content .= "$full";
} else if ($score % 2) {
   $score -= 1;
   $score /= 2;
   for ($i=0; $i < $score; $i++)
   $content .= "$image";
   $content .= "$halfimage";
} else {
   $score /= 2;
   for ($i=0; $i < $score; $i++)
   $content .= "$image";
}

?>


I know that there are things I might not need like making two queries, one to get the number of rows and the other to get the info. So if anyone has pointers Razz

Thanks in advance, if you want to see it in action it is at Only registered users can see links on this board! Get registered or login! on the right side.

Evan

_________________
Only registered users can see links on this board! Get registered or login! 
View user's profile Send private message
Raven
Site Admin/Owner


Joined: Aug 27, 2002
Posts: 17086

PostPosted: Fri Sep 16, 2005 3:31 pm Reply with quote

Code:
$rannum = rand(1,$numrows);


$sql = "SELECT id, title, cover, score FROM ".$prefix."_reviews WHERE id='$rannum'";

This will fail you if you ever delete a review. Your logic assumes that the number of rows is always in sync with your id column. But, if you add a review and it gets an ID of 10, then delete it, the next one added will be 11, but there will only be 10 rows in the database. What you really want is a row number id like what Oracle has but MySQL does not. So, you would need to create a temporary table, etc. If you never plan to delete then you will be 99.9% safe. This is just an FYI Wink

modify
Code:
WHERE id='$rannum'
to
Code:
WHERE id=$rannum
to increase efficiency


Code:
if ($score == 10) {

   for ($i=0; $i < 5; $i++)
and
} else if ($score % 2) {
   $score -= 1;
   $score /= 2;
   for ($i=0; $i < $score; $i++)
and
} else {
   $score /= 2;
   for ($i=0; $i < $score; $i++)
is not serving any purpose that I can see. It seems that you should modify that block of code to
Code:
if ($score == 10) {

   $content .= "$full";
} else if ($score % 2) {
   $content .= "$image";
   $content .= "$halfimage";
} else {
   $content .= "$image";
}
 
View user's profile Send private message
rofreason
PostPosted: Fri Sep 16, 2005 4:31 pm Reply with quote

Thank you!

That's exactly what I was looking for. I will make those modifications as you stated.

As for the random pull, so you are saying to make a lookup table like:

row# ID#
1 2
2 3
3 5
4 7
5 8

and then the random number is 2 which would match id[2] or id#3?

Again, thanks for the input, it is appreciated!
 
Raven
PostPosted: Fri Sep 16, 2005 4:37 pm Reply with quote

2 => 3

Use a HEAP/Memory table for speed for your temp table Smile
 
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> Blocks

View next topic
View previous topic
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 vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©