wrathican Posted July 21, 2008 Share Posted July 21, 2008 hey guys im trying to do a mass update in a db. the new values will be coming from a form. ive thought about how to do this going backwards. so starting with the actual update. for this i am going to use a foreach loop. so what i need to do is pass an array full of values from my php fom to my processing script. my form is displayed like so: <?php $query = "select * from table"; $result = mysql_query($query); ?> <form> <?php while($row = mysql_fetch_array($result)){ $img = $row['table_image']; $title = $row['table_title']; $imgId = $row['table_id']; ?> <img src="<?php echo $image; ?>" alt="<?php echo $title; ?>" /><br /> <input type="hidden" name="imageid" value="<?php echo $imgId; ?>" /> <input type="text" name="title" /> <?php } ?> <input type="submit" name="send" value="Send" /> </form> the only thing is i dont know how to start an array and store the values i need to in it. my array would(in a perfect world) look like this: $images['3']['title']; so '3' is the id of the image in the table, and 'title' is the title that the user would enter in the form input field. im just confused as to how i would display that in my form help? thanks in advanced Quote Link to comment https://forums.phpfreaks.com/topic/115800-php-arrays/ Share on other sites More sharing options...
numan82 Posted July 21, 2008 Share Posted July 21, 2008 well you can get the form array values (That you want to store in DB) through $_REQUEST. make a new array $MyArray = array(); $MyArray =$_REQUEST; now $MyArray contains the submitted data, it is easy to store in to the DB. Quote Link to comment https://forums.phpfreaks.com/topic/115800-php-arrays/#findComment-595296 Share on other sites More sharing options...
matmunn14 Posted July 21, 2008 Share Posted July 21, 2008 you could do <?php while($row = mysql_fetch_array($result)){ $img = $row['table_image']; $title = $row['table_title']; $imgId = $row['table_id']; $images[] = $imgId; ?> then somewhere along the line you could do: $images[$_POST['imageid']]['title'] = $_POST['title']; Quote Link to comment https://forums.phpfreaks.com/topic/115800-php-arrays/#findComment-595308 Share on other sites More sharing options...
wrathican Posted July 21, 2008 Author Share Posted July 21, 2008 hmm, im really stuck on this. i have tried both examples and none have wond what i need. anyone have more suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/115800-php-arrays/#findComment-595538 Share on other sites More sharing options...
mga_ka_php Posted July 21, 2008 Share Posted July 21, 2008 while($a = mysql_fetch_object($result)) { $images[] = array("img " => $a->table_image, "title" => $a->table_title, "imgId" => $a->table_id); Quote Link to comment https://forums.phpfreaks.com/topic/115800-php-arrays/#findComment-595558 Share on other sites More sharing options...
DarkWater Posted July 21, 2008 Share Posted July 21, 2008 well you can get the form array values (That you want to store in DB) through $_REQUEST. make a new array $MyArray = array(); $MyArray =$_REQUEST; now $MyArray contains the submitted data, it is easy to store in to the DB. Don't use $_REQUEST. Quote Link to comment https://forums.phpfreaks.com/topic/115800-php-arrays/#findComment-595562 Share on other sites More sharing options...
wrathican Posted July 21, 2008 Author Share Posted July 21, 2008 while($a = mysql_fetch_object($result)) { $images[] = array("img " => $a->table_image, "title" => $a->table_title, "imgId" => $a->table_id); this puts the stuff from the db into an array? if im right then its not what i need. the while loop on my display page is fine, except for one thing. the main problem is my processing script. all my display script does is repeat some form fields. so if i had 3 items in my db it would look like this: <form> <img src="someimage" alt="sometitle"/><br /> <input type="hidden" name="imageid" value="2"/> <input type="text" name="title" /> <img src="someimage" alt="sometitle"/><br /> <input type="hidden" name="imageid" value="4"/> <input type="text" name="title" /> <img src="someimage" alt="sometitle"/><br /> <input type="hidden" name="imageid" value="7"/> <input type="text" name="title" /> <input type="submit" /> </form> so, i have multiple form fields called the same thing. i would have 3 fields called imageid, 3 called title. so what i need on my processing script is a way of getting the values of 'title' and 'imageid' to correspond with eachother. so the first imageid matches the title for it. i really hope im making sense. Quote Link to comment https://forums.phpfreaks.com/topic/115800-php-arrays/#findComment-595787 Share on other sites More sharing options...
wildteen88 Posted July 21, 2008 Share Posted July 21, 2008 You'll need to submit the form fields as an array, otherwise only the last form field value will be received by your script. <form> <img src="someimage" alt="sometitle"/><br /> <input type="hidden" name="imageid[]" value="2"/> <input type="text" name="title[]" /> <img src="someimage" alt="sometitle"/><br /> <input type="hidden" name="imageid[]" value="4"/> <input type="text" name="title[]" /> <img src="someimage" alt="sometitle"/><br /> <input type="hidden" name="imageid[]" value="7"/> <input type="text" name="title[]" /> <input type="submit" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/115800-php-arrays/#findComment-595811 Share on other sites More sharing options...
wrathican Posted July 21, 2008 Author Share Posted July 21, 2008 ahh. that helps. thanks. would i be able to make the imageid and title as part of the same array? like image[] is the id number and image[][] is the title? so using the id as the key and title as the value? Quote Link to comment https://forums.phpfreaks.com/topic/115800-php-arrays/#findComment-595817 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.