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 to the forums!
*/
/* */
/* 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();
}
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
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 to the forums!
Joined: Aug 27, 2002 Posts: 16987 Location: Kansas
Posted:
Fri Sep 16, 2005 3:31 pm
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
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
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