Arrays and foreach loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Novice40
Forum Newbie
Posts: 3
Joined: Wed Jan 28, 2009 6:51 am

Arrays and foreach loop

Post by Novice40 »

pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hi there,

I am a beginner with PHP and still learning. I have to create a game like MadLibs (word game). I was provided with the function below:

Code: Select all

<?php
function getStoryArray($storyid) {
    switch($storyid) {
        case 1:
            return array ("adjective1", "plural_noun1", "adejctive2", "proper_noun2", "name1", "number1");
        case 2:
            return array ("proper_noun1", "adjective1", "adverb1", "adjective2", "adjective3", "adjective4");
        case 3:
            return array ("city1", "proper_noun2", "fruit1", "adjective1");
        default: return array();
    }
}
?>
In another file, I have used require () to add the above function in this file.

Using $storyNumber = rand(1, 3); to generate a random number

I am not sure how to retrieve the corresponding array from the above function and then generate a dynamic form using the information in the respective array. I was asked to use foreach loop but not sure how to apply it in this situation.

Can any one help ??


pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Re: Arrays and foreach loop

Post by mickeyunderscore »

I'm not quite sure what you mean, but I'll have a go at answering. You mean you need to generate a form for a user to input words into, based on the results of the array? Perhaps try:

Code: Select all

 
$arr = getStoryArray(rand(1, 3));
echo '<form>';
foreach($arr as $key => $val){
     echo "<p>$val <input type=\"text\" name=\"input_$key\" \></p>";
}
echo '</form>';
 
This would provide a dynamic form with inputs named input_0 to input_n
Novice40
Forum Newbie
Posts: 3
Joined: Wed Jan 28, 2009 6:51 am

Re: Arrays and foreach loop

Post by Novice40 »

Thanks, that's what I wanted.

However if I wanted to get the random number value that it is generating from the function, how can I get it . $arr appears as an "Array"

I want to set that random number value to a hidden field to retrieve a story text file in my third php file.
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Re: Arrays and foreach loop

Post by mickeyunderscore »

$arr shows up as an array when you echo it because it is an array so it cannot be output as an 'ordinary' one-dimensional variable, you must use print_r() to print the full contents of an array. Or specify the index you want to show e.g. echo $arr[0];

If you need to find out what random numer was generated, then the random number must be put into a variable and echoed into a hidden field:

Code: Select all

 
$n = rand(1, 3);
$arr = getStoryArray($n);
echo '<form>';
echo "<input type=\"hidden\" name=\"random_number\" value=\"$n\" />";
foreach($arr as $key => $val){
     echo "<p>$val <input type=\"text\" name=\"input_$key\" /></p>";
}
echo '</form>';
 
Post Reply