Need help with foreach function...

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
flight326
Forum Newbie
Posts: 1
Joined: Wed Aug 27, 2008 11:30 am

Need help with foreach function...

Post by flight326 »

I have a dynamically loaded form... I am using a readdir function to populate a simple form. For example, in a folder called "content" I have 3 files: 1.html, 2.html, and 3.html. I have manipulated the readdir function to loop return just the file names (i.e. 1,2,3) using trim. For each page name returned, I have 3 elements in a form: $file (1, 2, or 3), $keywords, and $description.

i.e. (my readdir loop will return 3 form elements):

Code: Select all

 
<form action="somefile.php">
 
<input type="hidden" name="file[]" value="<?=$file?>">
 
<!--user input -->
<textarea name="keywords[]" cols="75" rows="5"></textarea>
<textarea name="description[]" cols="75" rows="5"></textarea>
<input type="submit" name="Submit" value="Go">
<!--user input -->
 
<input type="hidden" name="file[]" value="<?=$file?>">
 
<!--user input -->
<textarea name="keywords[]" cols="75" rows="5"></textarea>
<textarea name="description[]" cols="75" rows="5"></textarea>
<input type="submit" name="Submit" value="Go">
<!--user input -->
 
<input type="hidden" name="file[]" value="<?=$file?>">
 
<!--user input -->
<textarea name="keywords[]" cols="75" rows="5"></textarea>
<textarea name="description[]" cols="75" rows="5"></textarea>
<input type="submit" name="Submit" value="Go">
<!--user input -->
 
</form>
 
---------------------------------------------------------------

My "somefile.php" file will create another page, let's call "include.php," using fopen with a final goal of writing a switch function to "include.php" so that "include.php" looks something like this:

Code: Select all

 
<?
 
switch($_GET['do']){
 
case '1':
$keywords = '1's keywords';
$description = '1's description';
break;
 
case '2':
$keywords = '2's keywords';
$description = '2's description';
break;
 
case '3':
$keywords = '3's keywords';
$description = '3's description';
break;
 
}
 
-----------------------------------------------------------
Here's where I am hung up...

In "somefile.php" I am working with a foreach function to get the $_POST['file'], $_POST['keywords'], and $_POST['description'] for each form element so as to format it to look like this:

Code: Select all

 
<?
$fp = fopen('', w);
fwrite($fp, '<?');
fwrite($fp, "\n");
fwrite($fp, 'switch(\$_GET['do']');
fwrite($fp, "\n");
fwrite($fp, "case '1':");
fwrite($fp, "\n");
fwrite($fp, "\$keywords = '1's keywords';");
...
...
etc...
 
fclose($fp);
?>
 
Sorry for such a long explanation but any help would be much appreciated!

Thanks! Dan
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: Need help with foreach function...

Post by ghurtado »

flight326 wrote: Here's where I am hung up...
Where exactly are you hung up? What is it that happens but shouldn't? What doesn't happen that should? You forgot the most important part of asking for help: the ASKING :)
Post Reply