11
I used
hsalazar's hack for a while and I noticed that sometimes quotes appear blank. I checked it and it seems the hack doesn't work if you deleted quotes, and thus resulting in non-existing
id's in the
'citas' table. The
count(*) = id syntax would not reach the end of the
id list.
I tried my hand at solving the problem and would appreciate comments.
One solution came out like this:
Quote:
function random_quote_show() {
global $xoopsDB;
$block = array();
$texto = "";
$result = $xoopsDB -> query("SELECT id FROM ".$xoopsDB -> prefix('citas')." ORDER BY id DESC LIMIT 1");
list($endid) = $xoopsDB -> fetchRow($result);
$show = rand (1, $endid);
while ($texto == "") {
$result2 = $xoopsDB->query("SELECT texto, autor FROM ".$xoopsDB->prefix('citas')." WHERE id=".$show);
list($texto, $autor)= $xoopsDB->fetchRow($result2);
}
$block['texto']=$texto;
$block['autor']=$autor;
return $block;
}
But after testing offline, it had a good chance of 'timing out' MySQL queries if you have a lot of holes in the
id column.
To work around this, I changed it to:
Quote:
function random_quote_show() {
global $xoopsDB;
$block = array();
$result = $xoopsDB -> query("SELECT id FROM ".$xoopsDB -> prefix('citas')." ORDER BY id DESC LIMIT 1");
list($endid) = $xoopsDB -> fetchRow($result);
$show = rand (1, $endid);
$result2 = $xoopsDB->query("SELECT texto, autor FROM ".$xoopsDB->prefix('citas')." WHERE id=".$show);
list($texto, $autor)= $xoopsDB->fetchRow($result2);
if ($texto == "") {
$result2 = $xoopsDB->query("SELECT texto, autor FROM ".$xoopsDB->prefix('citas')." WHERE id=".$endid);
list($texto, $autor)= $xoopsDB->fetchRow($result2);
}
$block['texto']=$texto;
$block['autor']=$autor;
return $block;
}
This second is not exactly a good solution since it just resorts to using the last quote in place of a 'missing' quote, but it works for now.
I hope others would come up with a better solution. This module is great and I hope it gets updated. In the future, I would like the option to activate/deactivate some quotes.
Thanks!