RE: [PHP] update query based on array
-Original Message- From: Jason Dulberg [mailto:[EMAIL PROTECTED]] Sent: 09 December 2002 23:52 So you mean do something like: input type=text name=category[15] value=Prep School input type=text name=rank[15] value=30 size=4 input type=checkbox name=rankid[15] value=166 Doesn't that create 2 additional arrays though? Yes -- but how else are you going to get multiple values for each name from your form to your PHP script? Without the array subscripts on both category and rank, you will only ever get one category and one rank passed to your script. Using the above, just do: foreach ($_POST['rankid'] as $row, $rankid): // in here, $rankid is the value of the current rankid[] // $_POST['category'][$row] is associated category[] value // $_POST['rank'][$row] is associated rank[] value endforeach; Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] update query based on array
Thanks Mike, that fixed the problem that I had! Jason -Original Message- From: Ford, Mike [LSS] [mailto:[EMAIL PROTECTED]] Sent: December 10, 2002 6:21 AM To: 'Jason Dulberg' Cc: [EMAIL PROTECTED] Subject: RE: [PHP] update query based on array -Original Message- From: Jason Dulberg [mailto:[EMAIL PROTECTED]] Sent: 09 December 2002 23:52 So you mean do something like: input type=text name=category[15] value=Prep School input type=text name=rank[15] value=30 size=4 input type=checkbox name=rankid[15] value=166 Doesn't that create 2 additional arrays though? Yes -- but how else are you going to get multiple values for each name from your form to your PHP script? Without the array subscripts on both category and rank, you will only ever get one category and one rank passed to your script. Using the above, just do: foreach ($_POST['rankid'] as $row, $rankid): // in here, $rankid is the value of the current rankid[] // $_POST['category'][$row] is associated category[] value // $_POST['rank'][$row] is associated rank[] value endforeach; Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] update query based on array
not real sure of the setup of the form but if you have mutiple groups of items to be updated then you should make the rank an array -- rank[pid#] you should make all the items -- that are related part of the same group -- by using [] On Mon, 2002-12-09 at 15:06, Jason Dulberg wrote: I am displaying a list of data (from an sql query) and some of the fields in that list are editable through a form. If the user chooses to edit one or more of the rows of data, they must click on a checkbox to add that row to an update array. The problem is that when I read that array to pass to an UPDATE sql statement, it only passes the last entry of the array. Here's an example of the data being passed: 13 - 4 - UPDATE ranking SET category='Prep School', rank='30' WHERE pid=4 14 - 169 - UPDATE ranking SET category='Prep School', rank='30' WHERE pid=169 15 - 166 - UPDATE ranking SET category='Prep School', rank='30' WHERE pid=166 The above is created based on the following html code: //the last row input type=text name=category value=Prep School input type=text name=rank value=30 size=4 input type=checkbox name=rankid[15] value=166 So as you can see, its only reading the last row and assigning its values to the data above as well. Here's what I'm using for the sql query: while(list($key,$val)=each($rankid)) { $upd=mysql_query(UPDATE ranking SET category='$category', rank='$rank' WHERE pid=$val); } If anyone has any suggestions for fixing this problem, please let me know :) THanks __ Jason Dulberg Extreme MTB http://extreme.nas.net -- Jimmy Brake [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] update query based on array
So you mean do something like: input type=text name=category[15] value=Prep School input type=text name=rank[15] value=30 size=4 input type=checkbox name=rankid[15] value=166 Doesn't that create 2 additional arrays though? Basically my form is just row after row of the html code above... each row has a different ID of course. The update query should only update fields with checked checkboxes. Thanks for your input! Jason -Original Message- From: Jimmy Brake [mailto:[EMAIL PROTECTED]] Sent: December 9, 2002 6:29 PM To: Jason Dulberg Cc: [EMAIL PROTECTED] Subject: Re: [PHP] update query based on array not real sure of the setup of the form but if you have mutiple groups of items to be updated then you should make the rank an array -- rank[pid#] you should make all the items -- that are related part of the same group -- by using [] On Mon, 2002-12-09 at 15:06, Jason Dulberg wrote: I am displaying a list of data (from an sql query) and some of the fields in that list are editable through a form. If the user chooses to edit one or more of the rows of data, they must click on a checkbox to add that row to an update array. The problem is that when I read that array to pass to an UPDATE sql statement, it only passes the last entry of the array. Here's an example of the data being passed: 13 - 4 - UPDATE ranking SET category='Prep School', rank='30' WHERE pid=4 14 - 169 - UPDATE ranking SET category='Prep School', rank='30' WHERE pid=169 15 - 166 - UPDATE ranking SET category='Prep School', rank='30' WHERE pid=166 The above is created based on the following html code: //the last row input type=text name=category value=Prep School input type=text name=rank value=30 size=4 input type=checkbox name=rankid[15] value=166 So as you can see, its only reading the last row and assigning its values to the data above as well. Here's what I'm using for the sql query: while(list($key,$val)=each($rankid)) { $upd=mysql_query(UPDATE ranking SET category='$category', rank='$rank' WHERE pid=$val); } If anyone has any suggestions for fixing this problem, please let me know :) THanks __ Jason Dulberg Extreme MTB http://extreme.nas.net -- Jimmy Brake [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] update query based on array
On Tuesday 10 December 2002 07:06, Jason Dulberg wrote: I am displaying a list of data (from an sql query) and some of the fields in that list are editable through a form. If the user chooses to edit one or more of the rows of data, they must click on a checkbox to add that row to an update array. The problem is that when I read that array to pass to an UPDATE sql statement, it only passes the last entry of the array. Here's an example of the data being passed: 13 - 4 - UPDATE ranking SET category='Prep School', rank='30' WHERE pid=4 14 - 169 - UPDATE ranking SET category='Prep School', rank='30' WHERE pid=169 15 - 166 - UPDATE ranking SET category='Prep School', rank='30' WHERE pid=166 The above is created based on the following html code: //the last row input type=text name=category value=Prep School input type=text name=rank value=30 size=4 The above two are named the same for ALL your rows so it will take the value of the last row, hence your problem. In general, each of your form elements must have a unique name. input type=checkbox name=rankid[15] value=166 So as you can see, its only reading the last row and assigning its values to the data above as well. Name your rows something along the lines of: input type=text name=rows[15][category] value=Prep School input type=text name=rows[15][rank] value=30 size=4 input type=checkbox name=rows[15][rankid] value=166 input type=text name=rows[16][category] value=Prep School input type=text name=rows[16][rank] value=30 size=4 input type=checkbox name=rows[16][rankid] value=166 Then to process: foreach ($rows as $row) { echo $row['category'], $row['rank'], $row['rankid']; } -- Jason Wong - Gremlins Associates - www.gremlins.biz Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * /* Fudd's First Law of Opposition: Push something hard enough and it will fall over. */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php