damianjames Posted November 22, 2008 Share Posted November 22, 2008 Hi all, I'm stuck! Ok... let's see if I can explain this one... I have a series of three pages to build an HTML email. I'm about to work on the third one, and I need to assign the names of the text inputs to session variables on this page. The text input names are derived based in part on user input, and in part on counting the number of images in a directory with the number they assigned in the first page as part of the file name. I'll include what I have so far from all 3 pages for completeness. Page 1: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Newsletter Builder Screen #1</title> </head> <body> <form action="newsletter1.php" method="post"> <input type="text" name="to" size="50" /> TO: address<br /> <input type="text" name="volume" size="5" /> Volume number<br /> <p>Are the last 3 images together on 1 line?<br /> <label> <input type="radio" name="multimage" value="1" /> Yes</label><br /> <label> <input type="radio" name="multimage" value="0" /> No</label> </p> <input type="submit" value="Submit" /> </form> </body> </html> Page 2: <?php session_start(); if($_POST['to'] != "") { $_SESSION['to'] = $_POST['to']; } else { die("<h3>Please press the back button and fill in the TO: address field</h3>"); } if($_POST['volume'] != "") { $_SESSION['volume'] = $_POST['volume']; } else { die("<h3>Please press the back button and fill in the Volume number field</h3>"); } if(isset($_POST['multimage'])) { $_SESSION['multimage'] = $_POST['multimage']; } else { die("<h3>Please press the back button and select if the last 3 images in the list display 3 across</h3>"); } $to = $_SESSION['to']; $directory = "images/"; $volume = $_SESSION['volume']; $multimage = $_SESSION['multimage']; $imagecount = count(glob($directory . $volume . "*")); if ($imagecount == 0) { die("<h3>No images were found for Volume number $volume</h3>"); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Newsletter Builder Screen #2</title> </head> <body> <form method="post" action="newsletter2.php"> <table> <?php echo "<p>The TO: address has been set to: " . "<span style=\"font-weight:bold; color:red;\">" . $to . "</span><br />"; echo "The Volume number for this newsletter has been set to: " . "<span style=\"font-weight:bold; color:red;\">" . $volume . "</span><br />"; echo "The following images have been selected to include in this newsletter:<br />"; $images = glob($directory . $volume . "*"); foreach($images as $image) { echo "<span style=\"font-weight:bold; color:red;\">" . $image . "</span><br />"; } echo "</p><p>"; for ($counter = 1; $counter <= $imagecount; $counter++) { echo ("<tr><td valign=\"middle\"><img src=\"images/$volume-$counter.jpg\"></td><td><span style=\"font-weight:bold; color:red;\">$volume-$counter.jpg</span><br /><input type=\"text\" name=\"$volume-$counter\" size=\"50\" /><br />Enter link for image if required</td></tr>"); } echo "</p>"; $_SESSION['imagecount'] = $imagecount; $_SESSION['directory'] = $directory; ?> </table> <input type="submit" value="submit" /> </form> </body> </html> Page 3 is the issue... Notice on page 2 I'm setting the name of the input text based on the volume code entered, and counting up to the total number of images that fit the description then cycling through that total count. <input type=\"text\" name=\"$volume-$counter\" size=\"50\" /> What I need to do is dynamically setup session variables based on the total amount of text inputs that have been posted from the previous page. In the test environment I have, there are 5 images in the directory (1-1.jpg through 1-5.jpg). Here is the code for page 3: <?php session_start(); $to = $_SESSION['to']; $volume = $_SESSION['volume']; $multimage = $_SESSION['multimage']; $directory = $_SESSION['directory']; $imagecount = $_SESSION['imagecount']; for($counter = 1; $counter <= $imagecount; $counter++) { $_SESSION['$volume-$counter'] = $_POST['$volume-$counter']; } var_dump($_SESSION); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Newsletter Builder Screen #3</title> </head> <body> </body> </html> For those wondering, no, what I did there did not work! heheh I'm thinking I should be able to iterate through an array somehow but I just can't get around how to do it. I appreciate any help! Quote Link to comment Share on other sites More sharing options...
damianjames Posted November 22, 2008 Author Share Posted November 22, 2008 Well I got a little farther in that now I have an array to work with, but I still don't know how to assign each item in the array to a different session variable. From page 2 now: $url = array(); for ($counter = 1; $counter <= $imagecount; $counter++) { $url[$counter] = $volume . "-" . $counter; echo ("<tr><td valign=\"middle\"><img src=\"images/$volume-$counter.jpg\"></td><td><span style=\"font-weight:bold; color:red;\">$volume-$counter.jpg</span><br /><input type=\"text\" name=\"$url[$counter]\" size=\"50\" /><br />Enter link for image if required</td></tr>"); } echo "</p>"; $_SESSION['imagecount'] = $imagecount; $_SESSION['directory'] = $directory; var_dump($url); The var dump says: array(5) { [1]=> string(3) "1-1" [2]=> string(3) "1-2" [3]=> string(3) "1-3" [4]=> string(3) "1-4" [5]=> string(3) "1-5" } The input text name is now assigning out of the array as well. That's good, now if I can get each item in the array into a session variable we're cookin' with gas. Quote Link to comment Share on other sites More sharing options...
damianjames Posted November 22, 2008 Author Share Posted November 22, 2008 Well I'm farther along... I got the session and post vars into an array, but now I need to figure out the nested loops to display the data correctly. I have the text input names ($inputname) in an array, and I have the text input value ($link) in an array. What I need to do is wrap each input name with each text input value, so a nested loop should (somehow) do it. The input name serves as the variable for the image to display, and the text input value serves as the src in the anchor tag around each image. I've tried things like: foreach($inputname as $value) { foreach($link as $value1) { echo $value $value1; } } and foreach($inputname as $value) { for($counter = 1; $counter = <= $imagecount; $counter++) { echo $value $volume . "-" . $counter } } and nothing has worked. I also tried the extract() command, but since I don't know beforehand how many images are going to be pulled, I don't know how many variables will be produced, so I'm not sure that's going to help. Any pointers? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.