ttmt Posted January 20, 2009 Share Posted January 20, 2009 Hi all Still finding my feet so go easy I'm trying to construct a photo gallery where the images are grouped by keywords. I have the keywords already on the database, when I upload the images I want to be able to select a keyword from whats avaiable on the database to link to an image. I want to able to load a number of images from one window so I'm using a for loop to construct the structrue of the page. Inside this loop I'm using a while loop to populate a dropdown menu with the keywords that are on the database. The problem is only the first dropdown menu is being populated with the keywords. <?php require_once("includes/connection.php"); ?> <?php require_once("includes/functions.php"); ?> <?php include("includes/header.php"); ?> <?php $query = "SELECT key_id, words FROM keywords"; $result = mysql_query($query); confirm_query($result); $number_of_fields = 4; ?> <body> <div id="con"> <form action="upload.php" method="post" name="upload_form" enctype="multipart/form-data"> <?php for($counter=1; $counter<=$number_of_fields;$counter++){ $output = '<p id="top">'; $output .= "Image :"." ".$counter.":"; $output .= '<input name="image_filename[]" type="file">'; $output .= '</p>'; $output .= '<p>'; $output .= 'Title :'; $output .= '<textarea name="title[]" cols="30" rows="1"></textarea>'; $output .= '</p>'; $output .= '<p id="bottom">'; $output .= 'Keyword :'; $output .= '<select name="category">'; while($row = mysql_fetch_array($result)){ $output .="<option value=".$row[0].">{$row[1]}</option>\n"; } $output .= '</select>'; $output .= '</p>'; echo $output; } ?> <p> <input type="submit" name="submit" value="Add Photo" /> </p> </form> </div> </body> </html> Link to comment https://forums.phpfreaks.com/topic/141613-solved-while-loop-works-once/ Share on other sites More sharing options...
cooldude832 Posted January 20, 2009 Share Posted January 20, 2009 Link to it? It looks good assuming for($counter=1; $counter<=$number_of_fields;$counter++){ that that doesn't run only 1 time The while loop will generate the same select field every time so it is probably advantageous to generate the select input out side the for loop 1 time in a variable and then connotate it back into the $output. Also since you are new I need to ask what is confirm_query() Link to comment https://forums.phpfreaks.com/topic/141613-solved-while-loop-works-once/#findComment-741231 Share on other sites More sharing options...
ttmt Posted January 20, 2009 Author Share Posted January 20, 2009 confirm_query() is just a function to confirm the query worked. function confirm_query($subject_set){ if(!$subject_set){ die("query failed ". mysql_error()); } } I'm using MAMP to test locally so I can't upload a link that will work - I'll try and sort something out Link to comment https://forums.phpfreaks.com/topic/141613-solved-while-loop-works-once/#findComment-741303 Share on other sites More sharing options...
rhodesa Posted January 20, 2009 Share Posted January 20, 2009 you can't have this: while($row = mysql_fetch_array($result)){ inside a loop. mysql_fetch_array() moves a pointer, so once it's at the end, it won't reset itself. instead, put the loop at the top, and build a php array. then loop that array: <?php require_once("includes/connection.php"); require_once("includes/functions.php"); include("includes/header.php"); //Get Keywords $query = "SELECT key_id, words FROM keywords"; $result = mysql_query($query); confirm_query($result); $keywords = array(); while($row = mysql_fetch_array($result)){ $keywords[] = $row; } $number_of_fields = 4; ?> <body> <div id="con"> <form action="upload.php" method="post" name="upload_form" enctype="multipart/form-data"> <?php for($counter=1; $counter<=$number_of_fields;$counter++){ ?> <p id="top"> Image : <?php echo $counter; ?>: <input name="image_filename[]" type="file"> </p> <p> Title : <textarea name="title[]" cols="30" rows="1"></textarea> </p> <p id="bottom"> Keyword : <select name="category"> <?php foreach($keywords as $key){ print "<option value=\"{$key[0]}\">{$key[1]}</option>\n"; } ?> </select> </p> <?php } ?> <p> <input type="submit" name="submit" value="Add Photo" /> </p> </form> </div> </body> </html> Link to comment https://forums.phpfreaks.com/topic/141613-solved-while-loop-works-once/#findComment-741311 Share on other sites More sharing options...
rhodesa Posted January 20, 2009 Share Posted January 20, 2009 better yet: <?php require_once("includes/connection.php"); require_once("includes/functions.php"); include("includes/header.php"); //Get Keywords $query = "SELECT key_id, words FROM keywords"; $result = mysql_query($query); confirm_query($result); $options = ""; while($row = mysql_fetch_array($result)){ $options .= "<option value=\"{$key[0]}\">{$key[1]}</option>\n"; } $number_of_fields = 4; ?> <body> <div id="con"> <form action="upload.php" method="post" name="upload_form" enctype="multipart/form-data"> <?php for($counter=1; $counter<=$number_of_fields;$counter++){ ?> <p id="top"> Image : <?php echo $counter; ?>: <input name="image_filename[]" type="file"> </p> <p> Title : <textarea name="title[]" cols="30" rows="1"></textarea> </p> <p id="bottom"> Keyword : <select name="category"> <?php echo $options; ?> </select> </p> <?php } ?> <p> <input type="submit" name="submit" value="Add Photo" /> </p> </form> </div> </body> </html> also, you can't have HTML elements inside the loop that have an ID. in an HTML document, and ID must be unique. if you are using <div id="con">,<div id="top">,<div id="botton"> for styling, use a class instead: <div class="con"> Link to comment https://forums.phpfreaks.com/topic/141613-solved-while-loop-works-once/#findComment-741315 Share on other sites More sharing options...
ttmt Posted January 20, 2009 Author Share Posted January 20, 2009 Thats a great way of doing it Thanks Link to comment https://forums.phpfreaks.com/topic/141613-solved-while-loop-works-once/#findComment-741328 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.