Creating a random quote with multidim array
Posted: Mon Nov 15, 2010 8:03 pm
Hello to all,
I'm a complete newbie here and with PHP so this must be a really silly question.
I'm trying to make a random quote in my site, actually it is a random haiku. I thought using a multidimensional array could get the job done, but my code doesn't work.
Would you please help me fix it? Also, I guess this is a rather bad approach... If you have a better solution please tell me so.
Thanks to all in advance! :DDD
The function:
Now this is the code inside my custom page:
I'm a complete newbie here and with PHP so this must be a really silly question.
I'm trying to make a random quote in my site, actually it is a random haiku. I thought using a multidimensional array could get the job done, but my code doesn't work.
Would you please help me fix it? Also, I guess this is a rather bad approach... If you have a better solution please tell me so.
Thanks to all in advance! :DDD
The function:
Code: Select all
function framework_haiku($type , $num){
$haiku[0][0] = "梅が香にのつと日の出る山路かな";
$haiku[0][1] = "芭蕉";
$haiku[0][2] = "Fragrance of plums in the air, in the distance on the mountain path. The morning sun. ";
$haiku[0][3] = "Bashō";
$haiku[1][0] = "梅が香やどなたが来ても欠茶碗";
$haiku[1][1] = "一茶";
$haiku[1][2] = "Oh the alluring scent of plum tree. And yet was there to be any visitor, [I have] only a broken tea cup.";
$haiku[1][3] = "Issa";
switch ($type) {
case "haiku" :
return $haiku[$num][0];
break;
case "author" :
return $haiku[$num][1];
break;
case "haiku_trans" :
return $haiku[$num][2];
break;
case "author_trans" :
return $haiku[$num][3];
break;
case "size" :
return count($haiku);
break;
}
}Code: Select all
<?php
$rand = rand(0, framework_haiku('size') - 1);
?>
<div class="haiku">
<div class="haiku-text"><?php framework_haiku('haiku', $rand); ?></div>
<div class="haiku-author"><?php framework_haiku('author', $rand); ?></div>
<div class="haiku-text-trans"><?php framework_haiku('haiku_trans', $rand); ?></div>
<div class="haiku-author-trans"><?php framework_haiku('author_trans', $rand); ?></div>
</div>