Page 1 of 1

Creating a random quote with multidim array

Posted: Mon Nov 15, 2010 8:03 pm
by aficiomaquinas
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:

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;
	}
}
Now this is the code inside my custom page:

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>

Re: Creating a random quote with multidim array

Posted: Mon Nov 15, 2010 9:33 pm
by mikecampbell
How is it not working? Do you get an error?

Re: Creating a random quote with multidim array

Posted: Mon Nov 15, 2010 9:40 pm
by aficiomaquinas
mikecampbell wrote:How is it not working? Do you get an error?
Thanks, I just found my mistake. Adding an echo before calling the function fixed it.

Code: Select all

<?php echo framework_haiku('haiku', $rand); ?>