Général 2 min de lecture · 263 mots

Random info to display from table

After a small delay, the promised hacks are out !

I wrote something very generic in order to be able to fill nearly all requirements !
This hack can handle what you want ! for the usual « Quote of the day », the current « Book », the « Movie », the « link » as I said what ever ou want !

The function to call is :

the_genRandom(
$cTag, // Tag Type
$count=1, // Number of rows to display
$cCollectBefore= », // Thing to display before anything else
$cCollectAfter= », // Thing to display after everything
$cItemBefore= », // Thing to display before each row
$cItemAfter= », // Thing to display after each row
$cValBefore= », // Thing to display before main value
$cValAfter= », // Thing to display after main value
$cAssBefore= », // Thing to display before the associated value
$cAssAfter= ») // Thing to display after the associated value

The table used must have at least the following structure :

id int(11) NOT NULL auto_increment,
KeyField varchar(15) NOT NULL default  »,
KeyValue tinytext NOT NULL,
KeyAssociated tinytext,
KeyCreated timestamp(14) NOT NULL,
PRIMARY KEY (id),
KEY KeyField (KeyField)
) ENGINE=MyISAM;

The PHP code is very very difficult… ( less than 10 lines !!!!!!) 😉

function the_genRandom( $cTag, $count=1, $cCollectBefore='',$cCollectAfter='', $cItemBefore='', $cItemAfter='',$cValBefore='',$cValAfter='',$cAssBefore='',$cAssAfter='')
{
	global $wpdb;

	srand((double)microtime()*1000000);

	$Key     = $wpdb->get_results("select KeyValue, KeyAssociated from psn_generic where KeyField ='$cTag' ORDER BY rand();");
	echo "\n".$cCollectBefore."\n";

	$count = min($count, sizeof($Key)); // never know.... ;o)
	for($nItem=0; $nItem < $count ; $nItem++ ) {
		echo "\n\t".$cItemBefore."\n\t\t".$cValBefore.$Key[$nItem]->KeyValue.$cValAfter."\n\t\t".$cAssBefore.$Key[$nItem]->KeyAssociated.$cAssAfter."\n\t".$cItemAfter;
	}
	echo "\n".$cCollectAfter."\n";
}
>

Une remarque, un retour ?

Cet article est vivant — corrections, contre-arguments et retours de production sont les bienvenus. Trois canaux, choisissez celui qui vous convient.

  1. Makes no sense, and there is no admin area to easily update these entries. Try again