RE: [PHP] populate form input option dropdown box from existing data
On 17 June 2009 15:01, PJ advised: It does, indeed. This confirms my inexperienced conclusion that in_array() does not work on associative arrays per se; it works on simple arrays and I just don't have the experience to think of extracting only the id fields. Let's squash this misconception again -- PHP only has associative arrays, it's just that in most contexts it treats numeric keys specially. So, internally, array('fred', 'bill', 'joe') is stored using exactly the same techniques as array('one'='fred', 'two'='bill', 'three'='joe'), but because the first one has numeric keys there are some things you can do with it that you can't with the second. But functions such as in_array, where all that really matters is the array's values, really couldn't care whether the keys are numeric, string, or a mixture. Also, the other problem was the option selected definition required Shawn's clarification select name='component-select' multiple ... which now highlights the selected fields. In all my searches (horrendously wasted time) I did not find any mention of component-select either in php.net or w3c.org I totally don't understand this comment: 'component-select' is just the name that Shawn chose to give his select field -- he could just as well have named it 'arnold-frog' without it making a blind bit of difference (so long as every other reference to it was changed to match!). Cheers! Mike -- Mike Ford, Electronic Information Developer, C507, Leeds Metropolitan University, Civic Quarter Campus, Woodhouse Lane, LEEDS, LS1 3HE, United Kingdom Email: m.f...@leedsmet.ac.uk Tel: +44 113 812 4730 To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] populate form input option dropdown box from existing data
On 17 June 2009 14:30, PJ advised: For the moment, I am trying to resolve the problem of extracting a value from a string returned by a query. I thought that in_array() would do it, but the tests I have run on it are 100% negative. The only thing I have not used in the tests is third parameter bool $strict which only affects case-sensitivity if the $needle is a string. $strict has nothing whatsoever at all in any way to do with case sensitivity -- $strict controls whether the values are compared using equality (==) or identity (===) tests. As is stated quite clearly on the manual page, in_array() is always case-sensitive. This leads me to believe that in_array() is either inappropriately defined in the manual and not effective on associative arrays Complete rubbish -- the in_array() manual page is excellent and totally accurate, complete with 3 working examples. Cheers! Mike -- Mike Ford, Electronic Information Developer, C507, Leeds Metropolitan University, Civic Quarter Campus, Woodhouse Lane, LEEDS, LS1 3HE, United Kingdom Email: m.f...@leedsmet.ac.uk Tel: +44 113 812 4730 To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] populate form input option dropdown box from existing data
PJ wrote: I'm including the relevant code: // select categories for book to be updated $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = '$bid' book_categories.categories_id = categories.id; if ( ( $results = mysql_query($sql, $db) ) ) { while ( $row = mysql_fetch_assoc($results) ) { $selected[] = $row['id']; } } else $selected = Array( 0 = '0'); echo $selected; print_r($selected); $sql = SELECT * FROM categories; echo select name='categoriesIN[]' multiple size='8'; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { if (in_array($row['id'], $selected)) { echo option value=, $row['id'], selected='selected' , $row['category'], /optionbr /; } else echo option value=, $row['id'], , $row['category'], /optionbr /; } } Problem #1)in the first query result. I can't figure out how to deal with it. The code works fine if there are categories assigned to the book. If not, an undefined variable error is spewed out for selected. That's because the test you use for the success of the query: ( ( $results = mysql_query($sql, $db) ) !== false ) is true if and only if the query succeeds, whether or not you get any rows returned. You then start looping over the fetched rows, and if there are none $selected never gets anything assigned to it, and so never gets defined. Since you don't rely on $selected being actually populated (in_array works fine on an empty array...), you might be better off pre-setting $selected before you start the first query: $selected = Array(); $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = '$bid' book_categories.categories_id = categories.id; if ( ( $results = mysql_query($sql, $db) ) ) { while ( $row = mysql_fetch_assoc($results) ) { $selected[] = $row['id']; } } Problem #2) in the second query, the selected is in the source code but it is not highlited. Several times I did get the categories highlighted, but I could never catch what it was that made it work. When I had the $id problem, i was trying this code from Yuri (but I don't understand where he got the $id from ) : The HTML you generate in the selected case is not quite right - you should have quotes around the value attribute's value, and you missed a closing '' off the option tag... while ( $row = mysql_fetch_assoc($results) ) { if (in_array($row['id'], $selected)) { echo option value=', $row['id'], ' selected='selected' , $row['category'], /optionbr /; } else { echo option value=, $row['id'], , $row['category'], /optionbr /; } } More succinctly: while ( $row = mysql_fetch_assoc($results) ) { $sel = in_array($row['id'], $selected) ? selected='selected':; echo option value='{$row['id']}' $sel{$row['category']}/optionbr /; } Unless the code is seriously performance critical, I still think variable interpolation is nicer to read than all those quotes and commas, and it keeps the HTML structure together better... Good luck -- Peter Ford phone: 01580 89 Developer fax: 01580 893399 Justcroft International Ltd., Staplehurst, Kent -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] populate form input option dropdown box from existing data
Stuart wrote: 2009/6/18 PJ af.gour...@videotron.ca: I snipped to make it short... continue at bottom... Step back from the code and consider the steps you need to perform... 1) Get an array of the categories, ideally in the form $cats[catid] = categoryname. 2) Get an array of the category IDs that should be selected, i.e. $selectedcats = array(3, 5, 7, 9). 3) Start the HTML select element 4) foreach ($cats as $id = $catname) 5) Determine whether it should be selected. e.g. $selected = (in_array($id, $selectedcats) ? 'selected=selected' : ''. 6) Output the HTML option element, like option value=$id $selected$catname/option, escaping where appropriate. 7) End of loop, job done. If your code doesn't have that structure then you may want to consider starting again. I'm quite sure the structure is correct. Secondly, check that you're not using the same variable name twice. I did find that in an instance of $id being repeated so I changed it to $bid. In one of your previous emails you used $selected to hold the array of selected categories, and in another you used it for the text to be inserted into the option element. The latter will blat over the former leading to no more than 1 option selected, and even then only if it's the first option displayed. The $selected were not mine... as I was using $ccc ; only started using $selected a couple of hours ago. If you're still stuck please post more of your code in a single chunk including all the elements in my step-by-step above. The snippets you're currently posting are not giving us enough context to spot even the most common mistakes. I'm including the relevant code: // select categories for book to be updated $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = '$bid' book_categories.categories_id = categories.id; if ( ( $results = mysql_query($sql, $db) ) ) { while ( $row = mysql_fetch_assoc($results) ) { $selected[] = $row['id']; } } else $selected = Array( 0 = '0'); echo $selected; print_r($selected); $sql = SELECT * FROM categories; echo select name='categoriesIN[]' multiple size='8'; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { if (in_array($row['id'], $selected)) { echo option value=, $row['id'], selected='selected' , $row['category'], /optionbr /; } else echo option value=, $row['id'], , $row['category'], /optionbr /; } } Problem #1)in the first query result. I can't figure out how to deal with it. The code works fine if there are categories assigned to the book. If not, an undefined variable error is spewed out for selected. It's best practice to initialise all variables before using them. The code you have will not create the $selected variable if there are no results... (code repeated for clarity) if ( ( $results = mysql_query($sql, $db) ) ) { If there are no results this will still work so will drop through to... while ( $row = mysql_fetch_assoc($results) ) { $selected[] = $row['id']; } But since there are no results the first call to mysql_fetch_assoc will return false so the line in the middle will never get executed. } else $selected = Array( 0 = '0'); Drop this else line and instead put $selected = array(); before the mysql_query line. Not sure why you want an element 0 = '0' in there, I'm guessing it's one of your attempts to get rid of the notice. Problem #2) in the second query, the selected is in the source code but it is not highlited. Several times I did get the categories highlighted, but I could never catch what it was that made it work. Can you post the HTML you're getting for this select using the above code. If the selected attributes are in the code correctly then there must be a syntax error in there somewhere and the easiest way to find it will be by looking at the HTML. Stuart, you are one clear-headed dude. This is such a nice and clear explanation - it's what we need for dummies llike me. I had tried to declcare the variable, but didn't understand that it needed to be declared as an array; I had put $selected = ; and obviously it din't work. As for the seconf problem, today it works. without any changes. I think the Quirk Daemon struck my browser. Actuall, I have been sabotaged by the cache when, in not thinking and not taking my ridalin, I would confuse refresh with go to previous page. I guess I can get some sleep now. THANKS TO ALL in clearing my brain toward the path of PHP enlightenment. :-) -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com
Re: [PHP] populate form input option dropdown box from existing data
Ford, Mike wrote: On 17 June 2009 15:01, PJ advised: It does, indeed. This confirms my inexperienced conclusion that in_array() does not work on associative arrays per se; it works on simple arrays and I just don't have the experience to think of extracting only the id fields. Let's squash this misconception again -- PHP only has associative arrays, it's just that in most contexts it treats numeric keys specially. So, internally, array('fred', 'bill', 'joe') is stored using exactly the same techniques as array('one'='fred', 'two'='bill', 'three'='joe'), but because the first one has numeric keys there are some things you can do with it that you can't with the second. But functions such as in_array, where all that really matters is the array's values, really couldn't care whether the keys are numeric, string, or a mixture. Thanks for the explanation. Also, the other problem was the option selected definition required Shawn's clarification select name='component-select' multiple ... which now highlights the selected fields. In all my searches (horrendously wasted time) I did not find any mention of component-select either in php.net or w3c.org I totally don't understand this comment: 'component-select' is just the name that Shawn chose to give his select field -- he could just as well have named it 'arnold-frog' without it making a blind bit of difference (so long as every other reference to it was changed to match!). The way it was put, I understood that that was the definitive attribute for the field. This confused me even more! Cheers! Mike -- Mike Ford, Electronic Information Developer, C507, Leeds Metropolitan University, Civic Quarter Campus, Woodhouse Lane, LEEDS, LS1 3HE, United Kingdom Email: m.f...@leedsmet.ac.uk Tel: +44 113 812 4730 To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] populate form input option dropdown box from existing data
It is a sintax error if (in_array($ex, $selected) --- missing ) echo br /yes; else echo br /no; On Thu, Jun 18, 2009 at 10:13 AM, PJ af.gour...@videotron.ca wrote: Ford, Mike wrote: On 17 June 2009 14:30, PJ advised: For the moment, I am trying to resolve the problem of extracting a value from a string returned by a query. I thought that in_array() would do it, but the tests I have run on it are 100% negative. The only thing I have not used in the tests is third parameter bool $strict which only affects case-sensitivity if the $needle is a string. $strict has nothing whatsoever at all in any way to do with case sensitivity -- $strict controls whether the values are compared using equality (==) or identity (===) tests. As is stated quite clearly on the manual page, in_array() is always case-sensitive. This leads me to believe that in_array() is either inappropriately defined in the manual and not effective on associative arrays Complete rubbish -- the in_array() manual page is excellent and totally accurate, complete with 3 working examples. I would really like to understand why my attempts to reproduce the first example just did not work. Note also that the examples do not show in_array($string, $array) My array was Array ([0]=6[1]=14), so when I tried if (in_array($string, $array) , echo $string did not return 14 as I had expected; neither did if(in_array(14, $array) ... nor if(in_array(14, $array). It still does not... actually, the screen goes blank. So, what am I doing wrong? Here's what is not working... I'm trying to reproduce the example from php.net: $selected = array(); if ( ( $results = mysql_query($sql, $db) ) ) { while ( $row = mysql_fetch_assoc($results) ) { $selected[] = $row['id']; } } print_r($selected); $ex = 14; if (in_array($ex, $selected) echo br /yes; else echo br /no; Regardless if I put 14 into $ex or 14 or '14' or even if I put the 14 instead of the $ex into the if line, I get a blank screen. It seems tp me, from what I see in the manual that this should work... or am I supposed to know something that is not clear in the examples... ? -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Martin Scotta
Re: [PHP] populate form input option dropdown box from existing data
Martin Scotta wrote: It is a sintax error if (in_array($ex, $selected) --- missing ) echo br /yes; else echo br /no; On Thu, Jun 18, 2009 at 10:13 AM, PJ af.gour...@videotron.ca mailto:af.gour...@videotron.ca wrote: Ford, Mike wrote: On 17 June 2009 14:30, PJ advised: For the moment, I am trying to resolve the problem of extracting a value from a string returned by a query. I thought that in_array() would do it, but the tests I have run on it are 100% negative. The only thing I have not used in the tests is third parameter bool $strict which only affects case-sensitivity if the $needle is a string. $strict has nothing whatsoever at all in any way to do with case sensitivity -- $strict controls whether the values are compared using equality (==) or identity (===) tests. As is stated quite clearly on the manual page, in_array() is always case-sensitive. This leads me to believe that in_array() is either inappropriately defined in the manual and not effective on associative arrays Complete rubbish -- the in_array() manual page is excellent and totally accurate, complete with 3 working examples. I would really like to understand why my attempts to reproduce the first example just did not work. Note also that the examples do not show in_array($string, $array) My array was Array ([0]=6[1]=14), so when I tried if (in_array($string, $array) , echo $string did not return 14 as I had expected; neither did if(in_array(14, $array) ... nor if(in_array(14, $array). It still does not... actually, the screen goes blank. So, what am I doing wrong? Here's what is not working... I'm trying to reproduce the example from php.net http://php.net: $selected = array(); if ( ( $results = mysql_query($sql, $db) ) ) { while ( $row = mysql_fetch_assoc($results) ) { $selected[] = $row['id']; } } print_r($selected); $ex = 14; if (in_array($ex, $selected) echo br /yes; else echo br /no; Regardless if I put 14 into $ex or 14 or '14' or even if I put the 14 instead of the $ex into the if line, I get a blank screen. It seems tp me, from what I see in the manual that this should work... or am I supposed to know something that is not clear in the examples... ? -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com mailto:p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Martin Scotta Actually, we have to call it a typo because i fixed that and the results are exactly the same = blank screen. :-( -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] populate form input option dropdown box from existing data
2009/6/18 PJ af.gour...@videotron.ca: Martin Scotta wrote: It is a sintax error if (in_array($ex, $selected) --- missing ) echo br /yes; else echo br /no; On Thu, Jun 18, 2009 at 10:13 AM, PJ af.gour...@videotron.ca mailto:af.gour...@videotron.ca wrote: Ford, Mike wrote: On 17 June 2009 14:30, PJ advised: For the moment, I am trying to resolve the problem of extracting a value from a string returned by a query. I thought that in_array() would do it, but the tests I have run on it are 100% negative. The only thing I have not used in the tests is third parameter bool $strict which only affects case-sensitivity if the $needle is a string. $strict has nothing whatsoever at all in any way to do with case sensitivity -- $strict controls whether the values are compared using equality (==) or identity (===) tests. As is stated quite clearly on the manual page, in_array() is always case-sensitive. This leads me to believe that in_array() is either inappropriately defined in the manual and not effective on associative arrays Complete rubbish -- the in_array() manual page is excellent and totally accurate, complete with 3 working examples. I would really like to understand why my attempts to reproduce the first example just did not work. Note also that the examples do not show in_array($string, $array) My array was Array ([0]=6[1]=14), so when I tried if (in_array($string, $array) , echo $string did not return 14 as I had expected; neither did if(in_array(14, $array) ... nor if(in_array(14, $array). It still does not... actually, the screen goes blank. So, what am I doing wrong? Here's what is not working... I'm trying to reproduce the example from php.net http://php.net: $selected = array(); if ( ( $results = mysql_query($sql, $db) ) ) { while ( $row = mysql_fetch_assoc($results) ) { $selected[] = $row['id']; } } print_r($selected); $ex = 14; if (in_array($ex, $selected) echo br /yes; else echo br /no; Regardless if I put 14 into $ex or 14 or '14' or even if I put the 14 instead of the $ex into the if line, I get a blank screen. It seems tp me, from what I see in the manual that this should work... or am I supposed to know something that is not clear in the examples... ? -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com mailto:p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Martin Scotta Actually, we have to call it a typo because i fixed that and the results are exactly the same = blank screen. :-( If you're getting a blank screen instead of an error message I highly recommend turning display_errors on and setting error_reporting to E_ALL in you dev php.ini - that way you'll actually see error messages rather than having to hunt through the code by hand. -Stuart -- http://stut.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] populate form input option dropdown box from existing data
Stuart wrote: 2009/6/18 PJ af.gour...@videotron.ca: Martin Scotta wrote: It is a sintax error if (in_array($ex, $selected) --- missing ) echo br /yes; else echo br /no; On Thu, Jun 18, 2009 at 10:13 AM, PJ af.gour...@videotron.ca mailto:af.gour...@videotron.ca wrote: Ford, Mike wrote: On 17 June 2009 14:30, PJ advised: For the moment, I am trying to resolve the problem of extracting a value from a string returned by a query. I thought that in_array() would do it, but the tests I have run on it are 100% negative. The only thing I have not used in the tests is third parameter bool $strict which only affects case-sensitivity if the $needle is a string. $strict has nothing whatsoever at all in any way to do with case sensitivity -- $strict controls whether the values are compared using equality (==) or identity (===) tests. As is stated quite clearly on the manual page, in_array() is always case-sensitive. This leads me to believe that in_array() is either inappropriately defined in the manual and not effective on associative arrays Complete rubbish -- the in_array() manual page is excellent and totally accurate, complete with 3 working examples. I would really like to understand why my attempts to reproduce the first example just did not work. Note also that the examples do not show in_array($string, $array) My array was Array ([0]=6[1]=14), so when I tried if (in_array($string, $array) , echo $string did not return 14 as I had expected; neither did if(in_array(14, $array) ... nor if(in_array(14, $array). It still does not... actually, the screen goes blank. So, what am I doing wrong? Here's what is not working... I'm trying to reproduce the example from php.net http://php.net: $selected = array(); if ( ( $results = mysql_query($sql, $db) ) ) { while ( $row = mysql_fetch_assoc($results) ) { $selected[] = $row['id']; } } print_r($selected); $ex = 14; if (in_array($ex, $selected) echo br /yes; else echo br /no; Regardless if I put 14 into $ex or 14 or '14' or even if I put the 14 instead of the $ex into the if line, I get a blank screen. It seems tp me, from what I see in the manual that this should work... or am I supposed to know something that is not clear in the examples... ? -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com mailto:p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Martin Scotta Actually, we have to call it a typo because i fixed that and the results are exactly the same = blank screen. :-( If you're getting a blank screen instead of an error message I highly recommend turning display_errors on and setting error_reporting to E_ALL in you dev php.ini - that way you'll actually see error messages rather than having to hunt through the code by hand. Duh, Stuart, I'm wet behind the ears, but that was one of my first revelations - to use error_reporting(E_ALL); ini_set('display_errors', 1); ;-) But that's what's weird... if I comment those last 4 lines out, the proggie works (or shall I say, limps; I still have other problems) But the in_array just refuses to cooperate. -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] populate form input option dropdown box from existing data
On 16 June 2009 20:48, PJ advised: Now, I was happy to learn that it is simpler to populate the insert new books page dynamically from the db. Much shorter neater. It looks to me like the best solution for the edit page is close to what Yuri suggests. Since the edit page is very similar to the insert new books page, I merely need to populate the Select options box slightly differently. This is the code to populate the insert page: select name=categoriesIN[] multiple size=8 ?php $sql = SELECT * FROM categories; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo option value=, $row['id'], , $row['category'], /optionbr /; } } /select The problem nowis to find a way to add a conditional clause above that will insert the option=selected in the output. The input for this comes from: // do categories $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = $idIN book_categories.categories_id = categories.id;; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo$row['id'], br /; } } This may return any number of category ids so the problem is to figure out a way to pass the ids from the above code to the right ids in the first code above. How what do I search to match the two ids? Well, if I'm understanding your queries correctly, you need to compare the two sets of $row['id'] from the two queries above -- so your first query should be the second one above (SELECT id, category FROM ...), and you need to save the ids it returns for use in the loop which emits the selects. This can be done by replacing the echo $row['id'] with $selected_ids[] = $row['id']. Now you have an array of the selected ids which you can use in your in_array(). So your finished code is going to look something like this: select name=categoriesIN[] multiple size=8 ?php // do categories $selected_ids = array(); $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = $idIN book_categories.categories_id = categories.id; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { $selected_ids[] = $row['id']; } } $sql = SELECT * FROM categories; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo option value=, $row['id'], (in_array($row['id'], $selected_ids)? selected:), , $row['category'], /option\n; } } ? /select Hope this helps. Cheers! Mike -- Mike Ford, Electronic Information Developer, C507, Leeds Metropolitan University, Civic Quarter Campus, Woodhouse Lane, LEEDS, LS1 3HE, United Kingdom Email: m.f...@leedsmet.ac.uk Tel: +44 113 812 4730 To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] populate form input option dropdown box from existing data
sorry, maybe I have been lazy in that comment, I admit, whem wrote that solution I was in a such hurry and without time. I dont really read what I wrote, but now I think this solution is good. select name=categories multiple style='width:120px;height:150px' ? $sql = SELECT id,name FROM categories; if ( ( $results = mysql_query($sql, $conn) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { $selected = ($id == $row['id'] ? 'selected=selected' : ''); echo option value=.$row['id']. .$selected..$row['name']./option; } } ? /select Date: Wed, 17 Jun 2009 10:16:15 +0100 From: m.f...@leedsmet.ac.uk To: php-general@lists.php.net Subject: RE: [PHP] populate form input option dropdown box from existing data On 16 June 2009 20:48, PJ advised: Now, I was happy to learn that it is simpler to populate the insert new books page dynamically from the db. Much shorter neater. It looks to me like the best solution for the edit page is close to what Yuri suggests. Since the edit page is very similar to the insert new books page, I merely need to populate the Select options box slightly differently. This is the code to populate the insert page: select name=categoriesIN[] multiple size=8 ?php $sql = SELECT * FROM categories; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo option value=, $row['id'], , $row['category'], /optionbr /; } } /select The problem nowis to find a way to add a conditional clause above that will insert the option=selected in the output. The input for this comes from: // do categories $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = $idIN book_categories.categories_id = categories.id;; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo$row['id'], br /; } } This may return any number of category ids so the problem is to figure out a way to pass the ids from the above code to the right ids in the first code above. How what do I search to match the two ids? Well, if I'm understanding your queries correctly, you need to compare the two sets of $row['id'] from the two queries above -- so your first query should be the second one above (SELECT id, category FROM ...), and you need to save the ids it returns for use in the loop which emits the selects. This can be done by replacing the echo $row['id'] with $selected_ids[] = $row['id']. Now you have an array of the selected ids which you can use in your in_array(). So your finished code is going to look something like this: select name=categoriesIN[] multiple size=8 ?php // do categories $selected_ids = array(); $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = $idIN book_categories.categories_id = categories.id; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { $selected_ids[] = $row['id']; } } $sql = SELECT * FROM categories; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo option value=, $row['id'], (in_array($row['id'], $selected_ids)? selected:), , $row['category'], /option\n; } } ? /select Hope this helps. Cheers! Mike -- Mike Ford, Electronic Information Developer, C507, Leeds Metropolitan University, Civic Quarter Campus, Woodhouse Lane, LEEDS, LS1 3HE, United Kingdom Email: m.f...@leedsmet.ac.uk Tel: +44 113 812 4730 To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php _ Emoticons e Winks super diferentes para o Messenger. Baixe agora, é grátis! http://specials.br.msn.com/ilovemessenger/pacotes.aspx
RE: [PHP] populate form input option dropdown box from existing data
sorry, maybe I have been lazy in that comment, I admit, whem wrote that solution i was in a such hurry and without time. I dont really read what i wrote, but now i think this solution is good select name=categories multiple style='width:120px;height:150px' ? $sql = SELECT id,name FROM categories; if ( ( $results = mysql_query($sql, $conn) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { $selected = ($id == $row['id'] ? 'selected=selected' : ''); echo option value=.$row['id']. .$selected..$row['name']./option; } } ? /select Date: Tue, 16 Jun 2009 20:46:29 -0400 From: af.gour...@videotron.ca To: a...@ashleysheridan.co.uk CC: gargari...@hotmail.com; m...@dajve.co.uk; php-general@lists.php.net; tedd.sperl...@gmail.com; nos...@mckenzies.net Subject: Re: [PHP] populate form input option dropdown box from existingdata Ashley Sheridan wrote: On Tue, 2009-06-16 at 18:19 -0400, PJ wrote: Ashley Sheridan wrote: On Tue, 2009-06-16 at 15:48 -0400, PJ wrote: jenai tomaka wrote: You can try like this, $row = stored data; and write the options like this option value=id (id == $row ? selected : ) /option Yuri Yarlei. http://brasil.microsoft.com.br/IE8/mergulhe/?utm_source=MSN%3BHotmailutm_medium=Taglineutm_campaign=IE8 Yuri, I'm still wet behind the ears on this so I don't quite understand what you mean by stored data ; and what does the id (id==$row?selected:) mean? I get the idea that this might be translated into something in the code I have dreamed up - further down. echo option value=, $row['id'], , $row['category'], /optionbr /; I suppose that I must add an if clause to insert the selected option for the categories that are relevant... Gentlemen, I have been diligently studying all this and have just returned my attention to the list, so I've read the replies/inputs and now comment: BTW, I had some problems with the multiple statement - it does not take any parameters; it must simply be stated multiple without quotes. Now, I was happy to learn that it is simpler to populate the insert new books page dynamically from the db. Much shorter neater. It looks to me like the best solution for the edit page is close to what Yuri suggests. Since the edit page is very similar to the insert new books page, I merely need to populate the Select options box slightly differently. This is the code to populate the insert page: select name=categoriesIN[] multiple size=8 ?php $sql = SELECT * FROM categories; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo option value=, $row['id'], , $row['category'], /optionbr /; } } ? /select The problem nowis to find a way to add a conditional clause above that will insert the option=selected in the output. The input for this comes from: // do categories $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = $idIN book_categories.categories_id = categories.id;; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo$row['id'], br /; } } This may return any number of category ids so the problem is to figure out a way to pass the ids from the above code to the right ids in the first code above. How what do I search to match the two ids? -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php option value=id (id == $row ? selected : ) /option is pretty bad HTML, as attributes should always have a value. Personally, I do something like this as it tends not to confuse the code too much $selected = ($id == $row)?'selected=selected':''; print option value=\$id\ $selected /option; I was unable to get any of the suggestions to work, except in_array(). However, the selected item refuses to become highlighted. code: select name=categoriesIN[] multiple size=8 ?php $sql = SELECT * FROM categories; //$selected = ($id == $row)?'selected=selected':''; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { if (in_array($row['id'], $ccc)) { echo option value=, $row['id'], selected , $row['category'], /optionbr /; } else echo option value=, $row['id'], , $row['category'], /optionbr /; } } ? /select I can't find anything that explains why the selected item is not highlighted
Re: [PHP] populate form input option dropdown box from existing data
Ashley Sheridan wrote: On Tue, 2009-06-16 at 20:46 -0400, PJ wrote: Ashley Sheridan wrote: On Tue, 2009-06-16 at 18:19 -0400, PJ wrote: Ashley Sheridan wrote: On Tue, 2009-06-16 at 15:48 -0400, PJ wrote: jenai tomaka wrote: You can try like this, $row = stored data; and write the options like this option value=id (id == $row ? selected : ) /option Yuri Yarlei. http://brasil.microsoft.com.br/IE8/mergulhe/?utm_source=MSN%3BHotmailutm_medium=Taglineutm_campaign=IE8 Yuri, I'm still wet behind the ears on this so I don't quite understand what you mean by stored data ; and what does the id (id==$row?selected:) mean? I get the idea that this might be translated into something in the code I have dreamed up - further down. echo option value=, $row['id'], , $row['category'], /optionbr /; I suppose that I must add an if clause to insert the selected option for the categories that are relevant... Gentlemen, I have been diligently studying all this and have just returned my attention to the list, so I've read the replies/inputs and now comment: BTW, I had some problems with the multiple statement - it does not take any parameters; it must simply be stated multiple without quotes. Now, I was happy to learn that it is simpler to populate the insert new books page dynamically from the db. Much shorter neater. It looks to me like the best solution for the edit page is close to what Yuri suggests. Since the edit page is very similar to the insert new books page, I merely need to populate the Select options box slightly differently. This is the code to populate the insert page: select name=categoriesIN[] multiple size=8 ?php $sql = SELECT * FROM categories; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo option value=, $row['id'], , $row['category'], /optionbr /; } } ? /select The problem nowis to find a way to add a conditional clause above that will insert the option=selected in the output. The input for this comes from: // do categories $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = $idIN book_categories.categories_id = categories.id;; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo$row['id'], br /; } } This may return any number of category ids so the problem is to figure out a way to pass the ids from the above code to the right ids in the first code above. How what do I search to match the two ids? -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php option value=id (id == $row ? selected : ) /option is pretty bad HTML, as attributes should always have a value. Personally, I do something like this as it tends not to confuse the code too much $selected = ($id == $row)?'selected=selected':''; print option value=\$id\ $selected /option; I was unable to get any of the suggestions to work, except in_array(). However, the selected item refuses to become highlighted. code: select name=categoriesIN[] multiple size=8 ?php $sql = SELECT * FROM categories; //$selected = ($id == $row)?'selected=selected':''; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { if (in_array($row['id'], $ccc)) { echo option value=, $row['id'], selected , $row['category'], /optionbr /; } else echo option value=, $row['id'], , $row['category'], /optionbr /; } } ? /select I can't find anything that explains why the selected item is not highlighted. -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php Have you actually looked at the html this produces to see if any of the elements are being marked with the selected=selected attribute? Thanks Ash www.ashleysheridan.co.uk I just cannot find a way to pass the selected fields to the options script. Even if I add the selected to all the fields, they show up in the source code but the fields are still not highlighted. There has to be a way to get this right??? :'( -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan
Re: [PHP] populate form input option dropdown box from existing data
Ford, Mike wrote: On 16 June 2009 20:48, PJ advised: Now, I was happy to learn that it is simpler to populate the insert new books page dynamically from the db. Much shorter neater. It looks to me like the best solution for the edit page is close to what Yuri suggests. Since the edit page is very similar to the insert new books page, I merely need to populate the Select options box slightly differently. This is the code to populate the insert page: select name=categoriesIN[] multiple size=8 ?php $sql = SELECT * FROM categories; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo option value=, $row['id'], , $row['category'], /optionbr /; } } /select The problem nowis to find a way to add a conditional clause above that will insert the option=selected in the output. The input for this comes from: // do categories $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = $idIN book_categories.categories_id = categories.id;; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo$row['id'], br /; } } This may return any number of category ids so the problem is to figure out a way to pass the ids from the above code to the right ids in the first code above. How what do I search to match the two ids? Well, if I'm understanding your queries correctly, you need to compare the two sets of $row['id'] from the two queries above -- so your first query should be the second one above (SELECT id, category FROM ...), and you need to save the ids it returns for use in the loop which emits the selects. This can be done by replacing the echo $row['id'] with $selected_ids[] = $row['id']. Now you have an array of the selected ids which you can use in your in_array(). So your finished code is going to look something like this: select name=categoriesIN[] multiple size=8 ?php // do categories $selected_ids = array(); $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = $idIN book_categories.categories_id = categories.id; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { $selected_ids[] = $row['id']; } } $sql = SELECT * FROM categories; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo option value=, $row['id'], (in_array($row['id'], $selected_ids)? selected:), , $row['category'], /option\n; } } ? /select Hope this helps. It does, indeed. This confirms my inexperienced conclusion that in_array() does not work on associative arrays per se; it works on simple arrays and I just don't have the experience to think of extracting only the id fields. I actually am using a slightly more complicated if else statement which works. Also, the other problem was the option selected definition required Shawn's clarification select name='component-select' multiple ... which now highlights the selected fields. In all my searches (horrendously wasted time) I did not find any mention of component-select either in php.net or w3c.org (I don't think my queries on Google brought up anything from php.net) but w3c.org did and I had looked at the page but somehow missed it. I'm going to have to look at the way I search things. When you are looking for something specific, other, even relevant, solutions seem to get screened out. Anyway, I learned quite a bit, here. Thank you very, very much, gentlemen. PJ -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] populate form input option dropdown box from existing data
On Wed, 2009-06-17 at 10:01 -0400, PJ wrote: Ford, Mike wrote: On 16 June 2009 20:48, PJ advised: Now, I was happy to learn that it is simpler to populate the insert new books page dynamically from the db. Much shorter neater. It looks to me like the best solution for the edit page is close to what Yuri suggests. Since the edit page is very similar to the insert new books page, I merely need to populate the Select options box slightly differently. This is the code to populate the insert page: select name=categoriesIN[] multiple size=8 ?php $sql = SELECT * FROM categories; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo option value=, $row['id'], , $row['category'], /optionbr /; } } /select The problem nowis to find a way to add a conditional clause above that will insert the option=selected in the output. The input for this comes from: // do categories $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = $idIN book_categories.categories_id = categories.id;; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo$row['id'], br /; } } This may return any number of category ids so the problem is to figure out a way to pass the ids from the above code to the right ids in the first code above. How what do I search to match the two ids? Well, if I'm understanding your queries correctly, you need to compare the two sets of $row['id'] from the two queries above -- so your first query should be the second one above (SELECT id, category FROM ...), and you need to save the ids it returns for use in the loop which emits the selects. This can be done by replacing the echo $row['id'] with $selected_ids[] = $row['id']. Now you have an array of the selected ids which you can use in your in_array(). So your finished code is going to look something like this: select name=categoriesIN[] multiple size=8 ?php // do categories $selected_ids = array(); $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = $idIN book_categories.categories_id = categories.id; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { $selected_ids[] = $row['id']; } } $sql = SELECT * FROM categories; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo option value=, $row['id'], (in_array($row['id'], $selected_ids)? selected:), , $row['category'], /option\n; } } ? /select Hope this helps. It does, indeed. This confirms my inexperienced conclusion that in_array() does not work on associative arrays per se; it works on simple arrays and I just don't have the experience to think of extracting only the id fields. I actually am using a slightly more complicated if else statement which works. Also, the other problem was the option selected definition required Shawn's clarification select name='component-select' multiple ... which now highlights the selected fields. In all my searches (horrendously wasted time) I did not find any mention of component-select either in php.net or w3c.org (I don't think my queries on Google brought up anything from php.net) but w3c.org did and I had looked at the page but somehow missed it. I'm going to have to look at the way I search things. When you are looking for something specific, other, even relevant, solutions seem to get screened out. Anyway, I learned quite a bit, here. Thank you very, very much, gentlemen. PJ -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php in_array() does work with associative arrays, I used it myself before! Don't forget, it only attempts to match the value in an associative array, not the key. Thanks Ash www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] populate form input option dropdown box from existing data
PJ wrote: It does, indeed. This confirms my inexperienced conclusion that in_array() does not work on associative arrays per se; it works on simple arrays and I just don't have the experience to think of extracting only the id fields. I actually am using a slightly more complicated if else statement which works. Also, the other problem was the option selected definition required Shawn's clarification select name='component-select' multiple ... which now highlights the selected fields. In all my searches (horrendously wasted time) I did not find any mention of component-select either in php.net or w3c.org (I don't think my queries on Google brought up anything from php.net) but w3c.org did and I had looked at the page but somehow missed it. The name is not what makes it work. That's just an example name I copied from w3c. It could be anything. What makes it work is the multiple. This will work just as well (notice you need the array if you want multiple values passed in $_POST): select name=pjs-select-thing[] multiple -Shawn I'm going to have to look at the way I search things. When you are looking for something specific, other, even relevant, solutions seem to get screened out. Anyway, I learned quite a bit, here. Thank you very, very much, gentlemen. PJ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] populate form input option dropdown box from existing data
Ashley Sheridan wrote: On Wed, 2009-06-17 at 10:01 -0400, PJ wrote: Ford, Mike wrote: On 16 June 2009 20:48, PJ advised: Now, I was happy to learn that it is simpler to populate the insert new books page dynamically from the db. Much shorter neater. It looks to me like the best solution for the edit page is close to what Yuri suggests. Since the edit page is very similar to the insert new books page, I merely need to populate the Select options box slightly differently. This is the code to populate the insert page: select name=categoriesIN[] multiple size=8 ?php $sql = SELECT * FROM categories; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo option value=, $row['id'], , $row['category'], /optionbr /; } } /select The problem nowis to find a way to add a conditional clause above that will insert the option=selected in the output. The input for this comes from: // do categories $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = $idIN book_categories.categories_id = categories.id;; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo$row['id'], br /; } } This may return any number of category ids so the problem is to figure out a way to pass the ids from the above code to the right ids in the first code above. How what do I search to match the two ids? Well, if I'm understanding your queries correctly, you need to compare the two sets of $row['id'] from the two queries above -- so your first query should be the second one above (SELECT id, category FROM ...), and you need to save the ids it returns for use in the loop which emits the selects. This can be done by replacing the echo $row['id'] with $selected_ids[] = $row['id']. Now you have an array of the selected ids which you can use in your in_array(). So your finished code is going to look something like this: select name=categoriesIN[] multiple size=8 ?php // do categories $selected_ids = array(); $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = $idIN book_categories.categories_id = categories.id; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { $selected_ids[] = $row['id']; } } $sql = SELECT * FROM categories; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo option value=, $row['id'], (in_array($row['id'], $selected_ids)? selected:), , $row['category'], /option\n; } } ? /select Hope this helps. It does, indeed. This confirms my inexperienced conclusion that in_array() does not work on associative arrays per se; it works on simple arrays and I just don't have the experience to think of extracting only the id fields. I actually am using a slightly more complicated if else statement which works. Also, the other problem was the option selected definition required Shawn's clarification select name='component-select' multiple ... which now highlights the selected fields. In all my searches (horrendously wasted time) I did not find any mention of component-select either in php.net or w3c.org (I don't think my queries on Google brought up anything from php.net) but w3c.org did and I had looked at the page but somehow missed it. I'm going to have to look at the way I search things. When you are looking for something specific, other, even relevant, solutions seem to get screened out. Anyway, I learned quite a bit, here. Thank you very, very much, gentlemen. PJ -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php in_array() does work with associative arrays, I used it myself before! Don't forget, it only attempts to match the value in an associative array, not the key. Could you show me how, because just running in_array($test_string, $assoc_array) never produced a result regardless of what I put into the $test_string or even if I used value, value, 'value', 14, 14, '14', and the corresponding string existed in the array - the results were zip, zero, nothing; like a dead fish. And now there is another little glitch, if the array finds that there is no category listed, then I get an error of undeclared variable... man, talk about contortions... :-( -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List
Re: [PHP] populate form input option dropdown box from existing data
PJ wrote: Ashley Sheridan wrote: On Wed, 2009-06-17 at 10:01 -0400, PJ wrote: Ford, Mike wrote: On 16 June 2009 20:48, PJ advised: Now, I was happy to learn that it is simpler to populate the insert new books page dynamically from the db. Much shorter neater. It looks to me like the best solution for the edit page is close to what Yuri suggests. Since the edit page is very similar to the insert new books page, I merely need to populate the Select options box slightly differently. This is the code to populate the insert page: select name=categoriesIN[] multiple size=8 ?php $sql = SELECT * FROM categories; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo option value=, $row['id'], , $row['category'], /optionbr /; } } /select The problem nowis to find a way to add a conditional clause above that will insert the option=selected in the output. The input for this comes from: // do categories $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = $idIN book_categories.categories_id = categories.id;; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo$row['id'], br /; } } This may return any number of category ids so the problem is to figure out a way to pass the ids from the above code to the right ids in the first code above. How what do I search to match the two ids? Well, if I'm understanding your queries correctly, you need to compare the two sets of $row['id'] from the two queries above -- so your first query should be the second one above (SELECT id, category FROM ...), and you need to save the ids it returns for use in the loop which emits the selects. This can be done by replacing the echo $row['id'] with $selected_ids[] = $row['id']. Now you have an array of the selected ids which you can use in your in_array(). So your finished code is going to look something like this: select name=categoriesIN[] multiple size=8 ?php // do categories $selected_ids = array(); $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = $idIN book_categories.categories_id = categories.id; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { $selected_ids[] = $row['id']; } } $sql = SELECT * FROM categories; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo option value=, $row['id'], (in_array($row['id'], $selected_ids)? selected:), , $row['category'], /option\n; } } ? /select Hope this helps. It does, indeed. This confirms my inexperienced conclusion that in_array() does not work on associative arrays per se; it works on simple arrays and I just don't have the experience to think of extracting only the id fields. I actually am using a slightly more complicated if else statement which works. Also, the other problem was the option selected definition required Shawn's clarification select name='component-select' multiple ... which now highlights the selected fields. In all my searches (horrendously wasted time) I did not find any mention of component-select either in php.net or w3c.org (I don't think my queries on Google brought up anything from php.net) but w3c.org did and I had looked at the page but somehow missed it. I'm going to have to look at the way I search things. When you are looking for something specific, other, even relevant, solutions seem to get screened out. Anyway, I learned quite a bit, here. Thank you very, very much, gentlemen. PJ -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php in_array() does work with associative arrays, I used it myself before! Don't forget, it only attempts to match the value in an associative array, not the key. Could you show me how, because just running in_array($test_string, $assoc_array) never produced a result regardless of what I put into the $test_string or even if I used value, value, 'value', 14, 14, '14', and the corresponding string existed in the array - the results were zip, zero, nothing; like a dead fish. Pretty simple: $needle01 = 1; $needle02 = 'test name'; $haystack = array('id' = 1, name = 'test name'); if (in_array($needle01, $haystack)) { echo FOUND $needle01; } if (in_array($needle02, $haystack)) { echo FOUND $needle02; } And now there is another little glitch, if the array finds that there is no category listed, then I get an error of undeclared variable... man, talk about contortions... :-( I don't know
Re: [PHP] populate form input option dropdown box from existing data
On Wed, 2009-06-17 at 16:29 -0400, PJ wrote: Ashley Sheridan wrote: On Wed, 2009-06-17 at 10:01 -0400, PJ wrote: Ford, Mike wrote: On 16 June 2009 20:48, PJ advised: Now, I was happy to learn that it is simpler to populate the insert new books page dynamically from the db. Much shorter neater. It looks to me like the best solution for the edit page is close to what Yuri suggests. Since the edit page is very similar to the insert new books page, I merely need to populate the Select options box slightly differently. This is the code to populate the insert page: select name=categoriesIN[] multiple size=8 ?php $sql = SELECT * FROM categories; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo option value=, $row['id'], , $row['category'], /optionbr /; } } /select The problem nowis to find a way to add a conditional clause above that will insert the option=selected in the output. The input for this comes from: // do categories $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = $idIN book_categories.categories_id = categories.id;; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo$row['id'], br /; } } This may return any number of category ids so the problem is to figure out a way to pass the ids from the above code to the right ids in the first code above. How what do I search to match the two ids? Well, if I'm understanding your queries correctly, you need to compare the two sets of $row['id'] from the two queries above -- so your first query should be the second one above (SELECT id, category FROM ...), and you need to save the ids it returns for use in the loop which emits the selects. This can be done by replacing the echo $row['id'] with $selected_ids[] = $row['id']. Now you have an array of the selected ids which you can use in your in_array(). So your finished code is going to look something like this: select name=categoriesIN[] multiple size=8 ?php // do categories $selected_ids = array(); $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = $idIN book_categories.categories_id = categories.id; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { $selected_ids[] = $row['id']; } } $sql = SELECT * FROM categories; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo option value=, $row['id'], (in_array($row['id'], $selected_ids)? selected:), , $row['category'], /option\n; } } ? /select Hope this helps. It does, indeed. This confirms my inexperienced conclusion that in_array() does not work on associative arrays per se; it works on simple arrays and I just don't have the experience to think of extracting only the id fields. I actually am using a slightly more complicated if else statement which works. Also, the other problem was the option selected definition required Shawn's clarification select name='component-select' multiple ... which now highlights the selected fields. In all my searches (horrendously wasted time) I did not find any mention of component-select either in php.net or w3c.org (I don't think my queries on Google brought up anything from php.net) but w3c.org did and I had looked at the page but somehow missed it. I'm going to have to look at the way I search things. When you are looking for something specific, other, even relevant, solutions seem to get screened out. Anyway, I learned quite a bit, here. Thank you very, very much, gentlemen. PJ -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php in_array() does work with associative arrays, I used it myself before! Don't forget, it only attempts to match the value in an associative array, not the key. Could you show me how, because just running in_array($test_string, $assoc_array) never produced a result regardless of what I put into the $test_string or even if I used value, value, 'value', 14, 14, '14', and the corresponding string existed in the array - the results were zip, zero, nothing; like a dead fish. And now there is another little glitch, if the array finds that there is no category listed, then I get an error of undeclared variable... man, talk about contortions... :-( I'm using this code on a site, and it works: $styles = Array('main' = 'Pastel', 'modern' = 'Modern');
Re: [PHP] populate form input option dropdown box from existing data
Yuri Yarlei wrote: sorry, maybe I have been lazy in that comment, I admit, whem wrote that solution I was in a such hurry and without time. I dont really read what I wrote, but now I think this solution is good. select name=categories multiple style='width:120px;height:150px' ? $sql = SELECT id,name FROM categories; if ( ( $results = mysql_query($sql, $conn) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { $selected = ($id == $row['id'] ? 'selected=selected' : ''); echo option value=.$row['id']. .$selected..$row['name']./option; } } ? /select doesnt quite work: there was a conflict with a $id that I fixed; but your code did not pass the $selected ids - don't know why. Here's what finally worked and it required the categoriesIN[] for the name: echo select name='categoriesIN[]' multiple size='8'; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { if (in_array($row['id'], $selected)) { echo option value=, $row['id'], selected , $row['category'], /optionbr /; } else echo option value=, $row['id'], , $row['category'], /optionbr /; } } I think it will work now. Thanks much for the input the support. :-) Date: Wed, 17 Jun 2009 10:16:15 +0100 From: m.f...@leedsmet.ac.uk To: php-general@lists.php.net Subject: RE: [PHP] populate form input option dropdown box from existing data On 16 June 2009 20:48, PJ advised: Now, I was happy to learn that it is simpler to populate the insert new books page dynamically from the db. Much shorter neater. It looks to me like the best solution for the edit page is close to what Yuri suggests. Since the edit page is very similar to the insert new books page, I merely need to populate the Select options box slightly differently. This is the code to populate the insert page: select name=categoriesIN[] multiple size=8 ?php $sql = SELECT * FROM categories; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo option value=, $row['id'], , $row['category'], /optionbr /; } } /select The problem nowis to find a way to add a conditional clause above that will insert the option=selected in the output. The input for this comes from: // do categories $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = $idIN book_categories.categories_id = categories.id;; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo$row['id'], br /; } } This may return any number of category ids so the problem is to figure out a way to pass the ids from the above code to the right ids in the first code above. How what do I search to match the two ids? Well, if I'm understanding your queries correctly, you need to compare the two sets of $row['id'] from the two queries above -- so your first query should be the second one above (SELECT id, category FROM ...), and you need to save the ids it returns for use in the loop which emits the selects. This can be done by replacing the echo $row['id'] with $selected_ids[] = $row['id']. Now you have an array of the selected ids which you can use in your in_array(). So your finished code is going to look something like this: select name=categoriesIN[] multiple size=8 ?php // do categories $selected_ids = array(); $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = $idIN book_categories.categories_id = categories.id; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { $selected_ids[] = $row['id']; } } $sql = SELECT * FROM categories; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo option value=, $row['id'], (in_array($row['id'], $selected_ids)? selected:), , $row['category'], /option\n; } } ? /select Hope this helps. Cheers! Mike -- Mike Ford, Electronic Information Developer, C507, Leeds Metropolitan University, Civic Quarter Campus, Woodhouse Lane, LEEDS, LS1 3HE, United Kingdom Email: m.f...@leedsmet.ac.uk Tel: +44 113 812 4730 To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php _ Emoticons e Winks super diferentes para o Messenger. Baixe agora, � gr�tis! http://specials.br.msn.com/ilovemessenger/pacotes.aspx
Re: [PHP] populate form input option dropdown box from existing data
Shawn McKenzie wrote: PJ wrote: Ashley Sheridan wrote: On Wed, 2009-06-17 at 10:01 -0400, PJ wrote: Ford, Mike wrote: On 16 June 2009 20:48, PJ advised: Now, I was happy to learn that it is simpler to populate the insert new books page dynamically from the db. Much shorter neater. It looks to me like the best solution for the edit page is close to what Yuri suggests. Since the edit page is very similar to the insert new books page, I merely need to populate the Select options box slightly differently. This is the code to populate the insert page: select name=categoriesIN[] multiple size=8 ?php $sql = SELECT * FROM categories; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo option value=, $row['id'], , $row['category'], /optionbr /; } } /select The problem nowis to find a way to add a conditional clause above that will insert the option=selected in the output. The input for this comes from: // do categories $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = $idIN book_categories.categories_id = categories.id;; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo$row['id'], br /; } } This may return any number of category ids so the problem is to figure out a way to pass the ids from the above code to the right ids in the first code above. How what do I search to match the two ids? Well, if I'm understanding your queries correctly, you need to compare the two sets of $row['id'] from the two queries above -- so your first query should be the second one above (SELECT id, category FROM ...), and you need to save the ids it returns for use in the loop which emits the selects. This can be done by replacing the echo $row['id'] with $selected_ids[] = $row['id']. Now you have an array of the selected ids which you can use in your in_array(). So your finished code is going to look something like this: select name=categoriesIN[] multiple size=8 ?php // do categories $selected_ids = array(); $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = $idIN book_categories.categories_id = categories.id; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { $selected_ids[] = $row['id']; } } $sql = SELECT * FROM categories; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo option value=, $row['id'], (in_array($row['id'], $selected_ids)? selected:), , $row['category'], /option\n; } } ? /select Hope this helps. It does, indeed. This confirms my inexperienced conclusion that in_array() does not work on associative arrays per se; it works on simple arrays and I just don't have the experience to think of extracting only the id fields. I actually am using a slightly more complicated if else statement which works. Also, the other problem was the option selected definition required Shawn's clarification select name='component-select' multiple ... which now highlights the selected fields. In all my searches (horrendously wasted time) I did not find any mention of component-select either in php.net or w3c.org (I don't think my queries on Google brought up anything from php.net) but w3c.org did and I had looked at the page but somehow missed it. I'm going to have to look at the way I search things. When you are looking for something specific, other, even relevant, solutions seem to get screened out. Anyway, I learned quite a bit, here. Thank you very, very much, gentlemen. PJ -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php in_array() does work with associative arrays, I used it myself before! Don't forget, it only attempts to match the value in an associative array, not the key. Could you show me how, because just running in_array($test_string, $assoc_array) never produced a result regardless of what I put into the $test_string or even if I used value, value, 'value', 14, 14, '14', and the corresponding string existed in the array - the results were zip, zero, nothing; like a dead fish. Pretty simple: $needle01 = 1; $needle02 = 'test name'; $haystack = array('id' = 1, name = 'test name'); if (in_array($needle01, $haystack)) { echo FOUND $needle01; } if (in_array($needle02, $haystack)) { echo FOUND $needle02; } And now there is another little glitch, if the array finds that there is no category listed, then I get an error of undeclared variable... man, talk about contortions... :-( I don't know what you mean
Re: [PHP] populate form input option dropdown box from existing data
Oops, hit reply instead of reply to all. Sorry for the duplicate PJ. 2009/6/17 PJ af.gour...@videotron.ca: Shawn McKenzie wrote: PJ wrote: Ashley Sheridan wrote: On Wed, 2009-06-17 at 10:01 -0400, PJ wrote: Ford, Mike wrote: On 16 June 2009 20:48, PJ advised: Now, I was happy to learn that it is simpler to populate the insert new books page dynamically from the db. Much shorter neater. It looks to me like the best solution for the edit page is close to what Yuri suggests. Since the edit page is very similar to the insert new books page, I merely need to populate the Select options box slightly differently. This is the code to populate the insert page: select name=categoriesIN[] multiple size=8 ?php $sql = SELECT * FROM categories; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo option value=, $row['id'], , $row['category'], /optionbr /; } } /select The problem nowis to find a way to add a conditional clause above that will insert the option=selected in the output. The input for this comes from: // do categories $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = $idIN book_categories.categories_id = categories.id;; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo$row['id'], br /; } } This may return any number of category ids so the problem is to figure out a way to pass the ids from the above code to the right ids in the first code above. How what do I search to match the two ids? Well, if I'm understanding your queries correctly, you need to compare the two sets of $row['id'] from the two queries above -- so your first query should be the second one above (SELECT id, category FROM ...), and you need to save the ids it returns for use in the loop which emits the selects. This can be done by replacing the echo $row['id'] with $selected_ids[] = $row['id']. Now you have an array of the selected ids which you can use in your in_array(). So your finished code is going to look something like this: select name=categoriesIN[] multiple size=8 ?php // do categories $selected_ids = array(); $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = $idIN book_categories.categories_id = categories.id; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { $selected_ids[] = $row['id']; } } $sql = SELECT * FROM categories; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo option value=, $row['id'], (in_array($row['id'], $selected_ids)? selected:), , $row['category'], /option\n; } } ? /select Hope this helps. It does, indeed. This confirms my inexperienced conclusion that in_array() does not work on associative arrays per se; it works on simple arrays and I just don't have the experience to think of extracting only the id fields. I actually am using a slightly more complicated if else statement which works. Also, the other problem was the option selected definition required Shawn's clarification select name='component-select' multiple ... which now highlights the selected fields. In all my searches (horrendously wasted time) I did not find any mention of component-select either in php.net or w3c.org (I don't think my queries on Google brought up anything from php.net) but w3c.org did and I had looked at the page but somehow missed it. I'm going to have to look at the way I search things. When you are looking for something specific, other, even relevant, solutions seem to get screened out. Anyway, I learned quite a bit, here. Thank you very, very much, gentlemen. PJ -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php in_array() does work with associative arrays, I used it myself before! Don't forget, it only attempts to match the value in an associative array, not the key. Could you show me how, because just running in_array($test_string, $assoc_array) never produced a result regardless of what I put into the $test_string or even if I used value, value, 'value', 14, 14, '14', and the corresponding string existed in the array - the results were zip, zero, nothing; like a dead fish. Pretty simple: $needle01 = 1; $needle02 = 'test name'; $haystack = array('id' = 1, name = 'test name'); if (in_array($needle01, $haystack)) { echo FOUND $needle01; } if (in_array($needle02, $haystack)) { echo FOUND $needle02; } And now there is another little glitch, if the array finds that there is no category listed, then I
Re: [PHP] populate form input option dropdown box from existing data
I snipped to make it short... continue at bottom... Step back from the code and consider the steps you need to perform... 1) Get an array of the categories, ideally in the form $cats[catid] = categoryname. 2) Get an array of the category IDs that should be selected, i.e. $selectedcats = array(3, 5, 7, 9). 3) Start the HTML select element 4) foreach ($cats as $id = $catname) 5) Determine whether it should be selected. e.g. $selected = (in_array($id, $selectedcats) ? 'selected=selected' : ''. 6) Output the HTML option element, like option value=$id $selected$catname/option, escaping where appropriate. 7) End of loop, job done. If your code doesn't have that structure then you may want to consider starting again. I'm quite sure the structure is correct. Secondly, check that you're not using the same variable name twice. I did find that in an instance of $id being repeated so I changed it to $bid. In one of your previous emails you used $selected to hold the array of selected categories, and in another you used it for the text to be inserted into the option element. The latter will blat over the former leading to no more than 1 option selected, and even then only if it's the first option displayed. The $selected were not mine... as I was using $ccc ; only started using $selected a couple of hours ago. If you're still stuck please post more of your code in a single chunk including all the elements in my step-by-step above. The snippets you're currently posting are not giving us enough context to spot even the most common mistakes. I'm including the relevant code: // select categories for book to be updated $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = '$bid' book_categories.categories_id = categories.id; if ( ( $results = mysql_query($sql, $db) ) ) { while ( $row = mysql_fetch_assoc($results) ) { $selected[] = $row['id']; } } else $selected = Array( 0 = '0'); echo $selected; print_r($selected); $sql = SELECT * FROM categories; echo select name='categoriesIN[]' multiple size='8'; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { if (in_array($row['id'], $selected)) { echo option value=, $row['id'], selected='selected' , $row['category'], /optionbr /; } else echo option value=, $row['id'], , $row['category'], /optionbr /; } } Problem #1)in the first query result. I can't figure out how to deal with it. The code works fine if there are categories assigned to the book. If not, an undefined variable error is spewed out for selected. Problem #2) in the second query, the selected is in the source code but it is not highlited. Several times I did get the categories highlighted, but I could never catch what it was that made it work. When I had the $id problem, i was trying this code from Yuri (but I don't understand where he got the $id from ) : select name=categoriesIN[] multiple ? $sql = SELECT id, category FROM categories; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { $selected = ($id == $row['id'] ? 'selected=selected' : ''); echo option value=.$row['id']. .$selected..$row['name']./option; } } ? /select I think there is an error in this last code... could it be $id is supposed to be the array holding the category ids? -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] populate form input option dropdown box from existing data
2009/6/18 PJ af.gour...@videotron.ca: I snipped to make it short... continue at bottom... Step back from the code and consider the steps you need to perform... 1) Get an array of the categories, ideally in the form $cats[catid] = categoryname. 2) Get an array of the category IDs that should be selected, i.e. $selectedcats = array(3, 5, 7, 9). 3) Start the HTML select element 4) foreach ($cats as $id = $catname) 5) Determine whether it should be selected. e.g. $selected = (in_array($id, $selectedcats) ? 'selected=selected' : ''. 6) Output the HTML option element, like option value=$id $selected$catname/option, escaping where appropriate. 7) End of loop, job done. If your code doesn't have that structure then you may want to consider starting again. I'm quite sure the structure is correct. Secondly, check that you're not using the same variable name twice. I did find that in an instance of $id being repeated so I changed it to $bid. In one of your previous emails you used $selected to hold the array of selected categories, and in another you used it for the text to be inserted into the option element. The latter will blat over the former leading to no more than 1 option selected, and even then only if it's the first option displayed. The $selected were not mine... as I was using $ccc ; only started using $selected a couple of hours ago. If you're still stuck please post more of your code in a single chunk including all the elements in my step-by-step above. The snippets you're currently posting are not giving us enough context to spot even the most common mistakes. I'm including the relevant code: // select categories for book to be updated $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = '$bid' book_categories.categories_id = categories.id; if ( ( $results = mysql_query($sql, $db) ) ) { while ( $row = mysql_fetch_assoc($results) ) { $selected[] = $row['id']; } } else $selected = Array( 0 = '0'); echo $selected; print_r($selected); $sql = SELECT * FROM categories; echo select name='categoriesIN[]' multiple size='8'; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { if (in_array($row['id'], $selected)) { echo option value=, $row['id'], selected='selected' , $row['category'], /optionbr /; } else echo option value=, $row['id'], , $row['category'], /optionbr /; } } Problem #1) in the first query result. I can't figure out how to deal with it. The code works fine if there are categories assigned to the book. If not, an undefined variable error is spewed out for selected. It's best practice to initialise all variables before using them. The code you have will not create the $selected variable if there are no results... (code repeated for clarity) if ( ( $results = mysql_query($sql, $db) ) ) { If there are no results this will still work so will drop through to... while ( $row = mysql_fetch_assoc($results) ) { $selected[] = $row['id']; } But since there are no results the first call to mysql_fetch_assoc will return false so the line in the middle will never get executed. } else $selected = Array( 0 = '0'); Drop this else line and instead put $selected = array(); before the mysql_query line. Not sure why you want an element 0 = '0' in there, I'm guessing it's one of your attempts to get rid of the notice. Problem #2) in the second query, the selected is in the source code but it is not highlited. Several times I did get the categories highlighted, but I could never catch what it was that made it work. Can you post the HTML you're getting for this select using the above code. If the selected attributes are in the code correctly then there must be a syntax error in there somewhere and the easiest way to find it will be by looking at the HTML. -Stuart -- http://stut.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] populate form input option dropdown box from existing data
-Original Message- From: PJ [mailto:af.gour...@videotron.ca] Sent: 15 June 2009 23:10 To: php-general@lists.php.net Subject: [PHP] populate form input option dropdown box from existing data I am having difficulties figuring out how enter retrieved data into a dropdown box for editing. Here's a snippet: ...snip select name=categoriesIN[] multiple size=8 option value=1Civilization/option option value=2Monuments, Temples amp; Tombs/option option value=3Pharaohs and Queens/option... snip As I understand it, I need to put an array ( $categoriesIN[] ) somewhere in the above code... or do I need to insert a series of value selected fields for the values? The closest thing to what I need, seems to be in this post: http://www.hpfreaks.com/forums/index.php?topic=165215 But it doesn't appear too inviting... not sure if they ever got it to work... Hi, Something like this should work for you ?php // Set up initial values // This could be hard-coded or brought in from DB or other source $categoriesIN_options = array(1 = 'Civilzation', 2 = 'Monuments, Temples amp; Tombs', 3 = 'Pharaohs and Queens', ); // Create a holder for values posted if you want // to repopulate the form on submission $selected_clean = array(); // Check for passed values and validate if (array_key_exists('categoriesIN',$_REQUEST)) { // Prevents errors with the foreach $passed_categoriesIN = (array)$_POST['categoriesIN']; foreach ($passed_categoriesIN as $options_key) { // If the value isn't in our pre-defined array, handle the error if (!array_key_exists($options_key,$categoriesIN_options)) { // Handle error continue; } $selected_clean[] = $options_key; } } ? [snip] select name='categoriesIN' multiple size='8' ?php // Loop over the options set above foreach ($categoriesIN_options as $key = $value) { echo option value='{$key}'; // Repopulate with selected if (in_array($key,$selected_clean)) { echo selected='selected'; } echo {$value}/option\n; } ? /select -- for (thoughts, ramblings doodles) { goto http://dajve.co.uk ; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] populate form input option dropdown box from existing data
the above post points to a 404 page any ways as suggested above in_array() will do the trick for you. i prefer using a template engine like smarty.. http://www.smarty.net/manual/en/language.function.html.options.php One line of code and you are done -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] populate form input option dropdown box from existing data
You can try like this, $row = stored data; and write the options like this option value=id (id == $row ? selected : ) /option Yuri Yarlei. From: m...@dajve.co.uk To: af.gour...@videotron.ca; php-general@lists.php.net Date: Tue, 16 Jun 2009 09:33:49 +0100 Subject: RE: [PHP] populate form input option dropdown box from existing data -Original Message- From: PJ [mailto:af.gour...@videotron.ca] Sent: 15 June 2009 23:10 To: php-general@lists.php.net Subject: [PHP] populate form input option dropdown box from existing data I am having difficulties figuring out how enter retrieved data into a dropdown box for editing. Here's a snippet: ...snip select name=categoriesIN[] multiple size=8 option value=1Civilization/option option value=2Monuments, Temples amp; Tombs/option option value=3Pharaohs and Queens/option... snip As I understand it, I need to put an array ( $categoriesIN[] ) somewhere in the above code... or do I need to insert a series of value selected fields for the values? The closest thing to what I need, seems to be in this post: http://www.hpfreaks.com/forums/index.php?topic=165215 But it doesn't appear too inviting... not sure if they ever got it to work... Hi, Something like this should work for you ?php // Set up initial values // This could be hard-coded or brought in from DB or other source $categoriesIN_options = array(1 = 'Civilzation', 2 = 'Monuments, Temples amp; Tombs', 3 = 'Pharaohs and Queens', ); // Create a holder for values posted if you want // to repopulate the form on submission $selected_clean = array(); // Check for passed values and validate if (array_key_exists('categoriesIN',$_REQUEST)) { // Prevents errors with the foreach $passed_categoriesIN = (array)$_POST['categoriesIN']; foreach ($passed_categoriesIN as $options_key) { // If the value isn't in our pre-defined array, handle the error if (!array_key_exists($options_key,$categoriesIN_options)) { // Handle error continue; } $selected_clean[] = $options_key; } } ? [snip] select name='categoriesIN' multiple size='8' ?php // Loop over the options set above foreach ($categoriesIN_options as $key = $value) { echo option value='{$key}'; // Repopulate with selected if (in_array($key,$selected_clean)) { echo selected='selected'; } echo {$value}/option\n; } ? /select -- for (thoughts, ramblings doodles) { goto http://dajve.co.uk ; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php _ Conheça os novos produtos Windows Live! Clique aqui. http://www.windowslive.com.br
Re: [PHP] populate form input option dropdown box from existing data
At 6:09 PM -0400 6/15/09, PJ wrote: I am having difficulties figuring out how enter retrieved data into a dropdown box for editing. Here's a snippet: ...snip select name=categoriesIN[] multiple size=8 option value=1Civilization/option option value=2Monuments, Temples amp; Tombs/option option value=3Pharaohs and Queens/option... snip As I understand it, I need to put an array ( $categoriesIN[] ) somewhere in the above code... or do I need to insert a series of value selected fields for the values? The closest thing to what I need, seems to be in this post: http://www.hpfreaks.com/forums/index.php?topic=165215 But it doesn't appear too inviting... not sure if they ever got it to work... Think about it. A Select control returns *one* value and not an array. As such you don't need to provide an array to collect a single value. Here's the solution: HTML select name=categories option value=1Civilization/option option value=2Monuments, Temples amp; Tombs/option option value=3Pharaohs and Queens/option /select PHP $category = $_POST['categories']; The variable $category will contain 1, 2, or 3 after a post form submit. Try it. The reference/link you cite above is for a dynamic select control that incorporates javascript to populate the second select control. It's not a simple select. If you are looking to understand what basic html does, try this: http://www.htmlcodetutorial.com/ I often use this site to refresh my failing memory about html stuff. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] populate form input option dropdown box from existing data
Yeah, I used to write that way but found I and others in the team saved a lot more time troubleshooting and extending the code when returning 6 months later if it was more verbose. Horses for courses, I guess. Also, the selected attribute should be selected='selected' to validate correctly as xhtml. By the same token, looking back at my example, I notice the multiple attribute of the select should be multiple='multiple' - apologies for missing that the first time =o) -Original Message- From: jenai tomaka [mailto:gargari...@hotmail.com] Sent: 16 June 2009 15:56 To: m...@dajve.co.uk; af.gour...@videotron.ca; php-general@lists.php.net Subject: RE: [PHP] populate form input option dropdown box from existing data You can try like this, $row = stored data; and write the options like this option value=id (id == $row ? selected : ) /option Yuri Yarlei. From: m...@dajve.co.uk To: af.gour...@videotron.ca; php-general@lists.php.net Date: Tue, 16 Jun 2009 09:33:49 +0100 Subject: RE: [PHP] populate form input option dropdown box from existing data -Original Message- From: PJ [mailto:af.gour...@videotron.ca] Sent: 15 June 2009 23:10 To: php-general@lists.php.net Subject: [PHP] populate form input option dropdown box from existing data I am having difficulties figuring out how enter retrieved data into a dropdown box for editing. Here's a snippet: ...snip select name=categoriesIN[] multiple size=8 option value=1Civilization/option option value=2Monuments, Temples amp; Tombs/option option value=3Pharaohs and Queens/option... snip As I understand it, I need to put an array ( $categoriesIN[] ) somewhere in the above code... or do I need to insert a series of value selected fields for the values? The closest thing to what I need, seems to be in this post: http://www.hpfreaks.com/forums/index.php?topic=165215 But it doesn't appear too inviting... not sure if they ever got it to work... Hi, Something like this should work for you ?php // Set up initial values // This could be hard-coded or brought in from DB or other source $categoriesIN_options = array(1 = 'Civilzation', 2 = 'Monuments, Temples amp; Tombs', 3 = 'Pharaohs and Queens', ); // Create a holder for values posted if you want // to repopulate the form on submission $selected_clean = array(); // Check for passed values and validate if (array_key_exists('categoriesIN',$_REQUEST)) { // Prevents errors with the foreach $passed_categoriesIN = (array)$_POST['categoriesIN']; foreach ($passed_categoriesIN as $options_key) { // If the value isn't in our pre-defined array, handle the error if (!array_key_exists($options_key,$categoriesIN_options)) { // Handle error continue; } $selected_clean[] = $options_key; } } ? [snip] select name='categoriesIN' multiple size='8' ?php // Loop over the options set above foreach ($categoriesIN_options as $key = $value) { echo option value='{$key}'; // Repopulate with selected if (in_array($key,$selected_clean)) { echo selected='selected'; } echo {$value}/option\n; } ? /select -- for (thoughts, ramblings doodles) { goto http://dajve.co.uk ; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php _ Conheça os novos produtos Windows Live! Clique aqui. http://www.windowslive.com.br -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] populate form input option dropdown box from existing data
A *multiple* select control, as in Phil's initial request will return an array, however. -Original Message- From: tedd [mailto:tedd.sperl...@gmail.com] Sent: 16 June 2009 15:58 To: PJ; php-general@lists.php.net Subject: Re: [PHP] populate form input option dropdown box from existing data At 6:09 PM -0400 6/15/09, PJ wrote: I am having difficulties figuring out how enter retrieved data into a dropdown box for editing. Here's a snippet: ...snip select name=categoriesIN[] multiple size=8 option value=1Civilization/option option value=2Monuments, Temples amp; Tombs/option option value=3Pharaohs and Queens/option... snip As I understand it, I need to put an array ( $categoriesIN[] ) somewhere in the above code... or do I need to insert a series of value selected fields for the values? The closest thing to what I need, seems to be in this post: http://www.hpfreaks.com/forums/index.php?topic=165215 But it doesn't appear too inviting... not sure if they ever got it to work... Think about it. A Select control returns *one* value and not an array. As such you don't need to provide an array to collect a single value. Here's the solution: HTML select name=categories option value=1Civilization/option option value=2Monuments, Temples amp; Tombs/option option value=3Pharaohs and Queens/option /select PHP $category = $_POST['categories']; The variable $category will contain 1, 2, or 3 after a post form submit. Try it. The reference/link you cite above is for a dynamic select control that incorporates javascript to populate the second select control. It's not a simple select. If you are looking to understand what basic html does, try this: http://www.htmlcodetutorial.com/ I often use this site to refresh my failing memory about html stuff. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] populate form input option dropdown box from existing data
jenai tomaka wrote: You can try like this, $row = stored data; and write the options like this option value=id (id == $row ? selected : ) /option Yuri Yarlei. http://brasil.microsoft.com.br/IE8/mergulhe/?utm_source=MSN%3BHotmailutm_medium=Taglineutm_campaign=IE8 Yuri, I'm still wet behind the ears on this so I don't quite understand what you mean by stored data ; and what does the id (id==$row?selected:) mean? I get the idea that this might be translated into something in the code I have dreamed up - further down. echo option value=, $row['id'], , $row['category'], /optionbr /; I suppose that I must add an if clause to insert the selected option for the categories that are relevant... Gentlemen, I have been diligently studying all this and have just returned my attention to the list, so I've read the replies/inputs and now comment: BTW, I had some problems with the multiple statement - it does not take any parameters; it must simply be stated multiple without quotes. Now, I was happy to learn that it is simpler to populate the insert new books page dynamically from the db. Much shorter neater. It looks to me like the best solution for the edit page is close to what Yuri suggests. Since the edit page is very similar to the insert new books page, I merely need to populate the Select options box slightly differently. This is the code to populate the insert page: select name=categoriesIN[] multiple size=8 ?php $sql = SELECT * FROM categories; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo option value=, $row['id'], , $row['category'], /optionbr /; } } ? /select The problem nowis to find a way to add a conditional clause above that will insert the option=selected in the output. The input for this comes from: // do categories $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = $idIN book_categories.categories_id = categories.id;; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo$row['id'], br /; } } This may return any number of category ids so the problem is to figure out a way to pass the ids from the above code to the right ids in the first code above. How what do I search to match the two ids? -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] populate form input option dropdown box from existing data
On Tue, 2009-06-16 at 15:48 -0400, PJ wrote: jenai tomaka wrote: You can try like this, $row = stored data; and write the options like this option value=id (id == $row ? selected : ) /option Yuri Yarlei. http://brasil.microsoft.com.br/IE8/mergulhe/?utm_source=MSN%3BHotmailutm_medium=Taglineutm_campaign=IE8 Yuri, I'm still wet behind the ears on this so I don't quite understand what you mean by stored data ; and what does the id (id==$row?selected:) mean? I get the idea that this might be translated into something in the code I have dreamed up - further down. echo option value=, $row['id'], , $row['category'], /optionbr /; I suppose that I must add an if clause to insert the selected option for the categories that are relevant... Gentlemen, I have been diligently studying all this and have just returned my attention to the list, so I've read the replies/inputs and now comment: BTW, I had some problems with the multiple statement - it does not take any parameters; it must simply be stated multiple without quotes. Now, I was happy to learn that it is simpler to populate the insert new books page dynamically from the db. Much shorter neater. It looks to me like the best solution for the edit page is close to what Yuri suggests. Since the edit page is very similar to the insert new books page, I merely need to populate the Select options box slightly differently. This is the code to populate the insert page: select name=categoriesIN[] multiple size=8 ?php $sql = SELECT * FROM categories; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo option value=, $row['id'], , $row['category'], /optionbr /; } } ? /select The problem nowis to find a way to add a conditional clause above that will insert the option=selected in the output. The input for this comes from: // do categories $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = $idIN book_categories.categories_id = categories.id;; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo$row['id'], br /; } } This may return any number of category ids so the problem is to figure out a way to pass the ids from the above code to the right ids in the first code above. How what do I search to match the two ids? -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php option value=id (id == $row ? selected : ) /option is pretty bad HTML, as attributes should always have a value. Personally, I do something like this as it tends not to confuse the code too much $selected = ($id == $row)?'selected=selected':''; print option value=\$id\ $selected /option; Thanks Ash www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] populate form input option dropdown box from existing data
Ashley Sheridan wrote: On Tue, 2009-06-16 at 15:48 -0400, PJ wrote: jenai tomaka wrote: You can try like this, $row = stored data; and write the options like this option value=id (id == $row ? selected : ) /option Yuri Yarlei. http://brasil.microsoft.com.br/IE8/mergulhe/?utm_source=MSN%3BHotmailutm_medium=Taglineutm_campaign=IE8 Yuri, I'm still wet behind the ears on this so I don't quite understand what you mean by stored data ; and what does the id (id==$row?selected:) mean? I get the idea that this might be translated into something in the code I have dreamed up - further down. echo option value=, $row['id'], , $row['category'], /optionbr /; I suppose that I must add an if clause to insert the selected option for the categories that are relevant... Gentlemen, I have been diligently studying all this and have just returned my attention to the list, so I've read the replies/inputs and now comment: BTW, I had some problems with the multiple statement - it does not take any parameters; it must simply be stated multiple without quotes. Now, I was happy to learn that it is simpler to populate the insert new books page dynamically from the db. Much shorter neater. It looks to me like the best solution for the edit page is close to what Yuri suggests. Since the edit page is very similar to the insert new books page, I merely need to populate the Select options box slightly differently. This is the code to populate the insert page: select name=categoriesIN[] multiple size=8 ?php $sql = SELECT * FROM categories; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo option value=, $row['id'], , $row['category'], /optionbr /; } } ? /select The problem nowis to find a way to add a conditional clause above that will insert the option=selected in the output. The input for this comes from: // do categories $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = $idIN book_categories.categories_id = categories.id;; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo$row['id'], br /; } } This may return any number of category ids so the problem is to figure out a way to pass the ids from the above code to the right ids in the first code above. How what do I search to match the two ids? -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php option value=id (id == $row ? selected : ) /option is pretty bad HTML, as attributes should always have a value. Personally, I do something like this as it tends not to confuse the code too much $selected = ($id == $row)?'selected=selected':''; print option value=\$id\ $selected /option; I was unable to get any of the suggestions to work, except in_array(). However, the selected item refuses to become highlighted. code: select name=categoriesIN[] multiple size=8 ?php $sql = SELECT * FROM categories; //$selected = ($id == $row)?'selected=selected':''; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { if (in_array($row['id'], $ccc)) { echo option value=, $row['id'], selected , $row['category'], /optionbr /; } else echo option value=, $row['id'], , $row['category'], /optionbr /; } } ? /select I can't find anything that explains why the selected item is not highlighted. -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] populate form input option dropdown box from existing data
On Tue, 2009-06-16 at 18:19 -0400, PJ wrote: Ashley Sheridan wrote: On Tue, 2009-06-16 at 15:48 -0400, PJ wrote: jenai tomaka wrote: You can try like this, $row = stored data; and write the options like this option value=id (id == $row ? selected : ) /option Yuri Yarlei. http://brasil.microsoft.com.br/IE8/mergulhe/?utm_source=MSN%3BHotmailutm_medium=Taglineutm_campaign=IE8 Yuri, I'm still wet behind the ears on this so I don't quite understand what you mean by stored data ; and what does the id (id==$row?selected:) mean? I get the idea that this might be translated into something in the code I have dreamed up - further down. echo option value=, $row['id'], , $row['category'], /optionbr /; I suppose that I must add an if clause to insert the selected option for the categories that are relevant... Gentlemen, I have been diligently studying all this and have just returned my attention to the list, so I've read the replies/inputs and now comment: BTW, I had some problems with the multiple statement - it does not take any parameters; it must simply be stated multiple without quotes. Now, I was happy to learn that it is simpler to populate the insert new books page dynamically from the db. Much shorter neater. It looks to me like the best solution for the edit page is close to what Yuri suggests. Since the edit page is very similar to the insert new books page, I merely need to populate the Select options box slightly differently. This is the code to populate the insert page: select name=categoriesIN[] multiple size=8 ?php $sql = SELECT * FROM categories; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo option value=, $row['id'], , $row['category'], /optionbr /; } } ? /select The problem nowis to find a way to add a conditional clause above that will insert the option=selected in the output. The input for this comes from: // do categories $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = $idIN book_categories.categories_id = categories.id;; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo$row['id'], br /; } } This may return any number of category ids so the problem is to figure out a way to pass the ids from the above code to the right ids in the first code above. How what do I search to match the two ids? -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php option value=id (id == $row ? selected : ) /option is pretty bad HTML, as attributes should always have a value. Personally, I do something like this as it tends not to confuse the code too much $selected = ($id == $row)?'selected=selected':''; print option value=\$id\ $selected /option; I was unable to get any of the suggestions to work, except in_array(). However, the selected item refuses to become highlighted. code: select name=categoriesIN[] multiple size=8 ?php $sql = SELECT * FROM categories; //$selected = ($id == $row)?'selected=selected':''; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { if (in_array($row['id'], $ccc)) { echo option value=, $row['id'], selected , $row['category'], /optionbr /; } else echo option value=, $row['id'], , $row['category'], /optionbr /; } } ? /select I can't find anything that explains why the selected item is not highlighted. -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php Have you actually looked at the html this produces to see if any of the elements are being marked with the selected=selected attribute? Thanks Ash www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] populate form input option dropdown box from existing data
Ashley Sheridan wrote: On Tue, 2009-06-16 at 18:19 -0400, PJ wrote: Ashley Sheridan wrote: On Tue, 2009-06-16 at 15:48 -0400, PJ wrote: jenai tomaka wrote: You can try like this, $row = stored data; and write the options like this option value=id (id == $row ? selected : ) /option Yuri Yarlei. http://brasil.microsoft.com.br/IE8/mergulhe/?utm_source=MSN%3BHotmailutm_medium=Taglineutm_campaign=IE8 Yuri, I'm still wet behind the ears on this so I don't quite understand what you mean by stored data ; and what does the id (id==$row?selected:) mean? I get the idea that this might be translated into something in the code I have dreamed up - further down. echo option value=, $row['id'], , $row['category'], /optionbr /; I suppose that I must add an if clause to insert the selected option for the categories that are relevant... Gentlemen, I have been diligently studying all this and have just returned my attention to the list, so I've read the replies/inputs and now comment: BTW, I had some problems with the multiple statement - it does not take any parameters; it must simply be stated multiple without quotes. Now, I was happy to learn that it is simpler to populate the insert new books page dynamically from the db. Much shorter neater. It looks to me like the best solution for the edit page is close to what Yuri suggests. Since the edit page is very similar to the insert new books page, I merely need to populate the Select options box slightly differently. This is the code to populate the insert page: select name=categoriesIN[] multiple size=8 ?php $sql = SELECT * FROM categories; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo option value=, $row['id'], , $row['category'], /optionbr /; } } ? /select The problem nowis to find a way to add a conditional clause above that will insert the option=selected in the output. The input for this comes from: // do categories $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = $idIN book_categories.categories_id = categories.id;; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo$row['id'], br /; } } This may return any number of category ids so the problem is to figure out a way to pass the ids from the above code to the right ids in the first code above. How what do I search to match the two ids? -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php option value=id (id == $row ? selected : ) /option is pretty bad HTML, as attributes should always have a value. Personally, I do something like this as it tends not to confuse the code too much $selected = ($id == $row)?'selected=selected':''; print option value=\$id\ $selected /option; I was unable to get any of the suggestions to work, except in_array(). However, the selected item refuses to become highlighted. code: select name=categoriesIN[] multiple size=8 ?php $sql = SELECT * FROM categories; //$selected = ($id == $row)?'selected=selected':''; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { if (in_array($row['id'], $ccc)) { echo option value=, $row['id'], selected , $row['category'], /optionbr /; } else echo option value=, $row['id'], , $row['category'], /optionbr /; } } ? /select I can't find anything that explains why the selected item is not highlighted. -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php Have you actually looked at the html this produces to see if any of the elements are being marked with the selected=selected attribute? Good thinking; I feel dumb all over again... :-( Yeah, view source doesn't show any selected words. Something fishy is going on. -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] populate form input option dropdown box from existing data
Ashley Sheridan wrote: On Tue, 2009-06-16 at 18:19 -0400, PJ wrote: Ashley Sheridan wrote: On Tue, 2009-06-16 at 15:48 -0400, PJ wrote: jenai tomaka wrote: You can try like this, $row = stored data; and write the options like this option value=id (id == $row ? selected : ) /option Yuri Yarlei. http://brasil.microsoft.com.br/IE8/mergulhe/?utm_source=MSN%3BHotmailutm_medium=Taglineutm_campaign=IE8 Yuri, I'm still wet behind the ears on this so I don't quite understand what you mean by stored data ; and what does the id (id==$row?selected:) mean? I get the idea that this might be translated into something in the code I have dreamed up - further down. echo option value=, $row['id'], , $row['category'], /optionbr /; I suppose that I must add an if clause to insert the selected option for the categories that are relevant... Gentlemen, I have been diligently studying all this and have just returned my attention to the list, so I've read the replies/inputs and now comment: BTW, I had some problems with the multiple statement - it does not take any parameters; it must simply be stated multiple without quotes. Now, I was happy to learn that it is simpler to populate the insert new books page dynamically from the db. Much shorter neater. It looks to me like the best solution for the edit page is close to what Yuri suggests. Since the edit page is very similar to the insert new books page, I merely need to populate the Select options box slightly differently. This is the code to populate the insert page: select name=categoriesIN[] multiple size=8 ?php $sql = SELECT * FROM categories; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo option value=, $row['id'], , $row['category'], /optionbr /; } } ? /select The problem nowis to find a way to add a conditional clause above that will insert the option=selected in the output. The input for this comes from: // do categories $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = $idIN book_categories.categories_id = categories.id;; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo$row['id'], br /; } } This may return any number of category ids so the problem is to figure out a way to pass the ids from the above code to the right ids in the first code above. How what do I search to match the two ids? -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php option value=id (id == $row ? selected : ) /option is pretty bad HTML, as attributes should always have a value. Personally, I do something like this as it tends not to confuse the code too much $selected = ($id == $row)?'selected=selected':''; print option value=\$id\ $selected /option; I was unable to get any of the suggestions to work, except in_array(). However, the selected item refuses to become highlighted. code: select name=categoriesIN[] multiple size=8 ?php $sql = SELECT * FROM categories; //$selected = ($id == $row)?'selected=selected':''; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { if (in_array($row['id'], $ccc)) { echo option value=, $row['id'], selected , $row['category'], /optionbr /; } else echo option value=, $row['id'], , $row['category'], /optionbr /; } } ? /select I can't find anything that explains why the selected item is not highlighted. -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php Have you actually looked at the html this produces to see if any of the elements are being marked with the selected=selected attribute? Thanks Ash www.ashleysheridan.co.uk I just cannot find a way to pass the selected fields to the options script. Even if I add the selected to all the fields, they show up in the source code but the fields are still not highlighted. There has to be a way to get this right??? :'( -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit:
Re: [PHP] populate form input option dropdown box from existing data
On Tue, 2009-06-16 at 20:46 -0400, PJ wrote: Ashley Sheridan wrote: On Tue, 2009-06-16 at 18:19 -0400, PJ wrote: Ashley Sheridan wrote: On Tue, 2009-06-16 at 15:48 -0400, PJ wrote: jenai tomaka wrote: You can try like this, $row = stored data; and write the options like this option value=id (id == $row ? selected : ) /option Yuri Yarlei. http://brasil.microsoft.com.br/IE8/mergulhe/?utm_source=MSN%3BHotmailutm_medium=Taglineutm_campaign=IE8 Yuri, I'm still wet behind the ears on this so I don't quite understand what you mean by stored data ; and what does the id (id==$row?selected:) mean? I get the idea that this might be translated into something in the code I have dreamed up - further down. echo option value=, $row['id'], , $row['category'], /optionbr /; I suppose that I must add an if clause to insert the selected option for the categories that are relevant... Gentlemen, I have been diligently studying all this and have just returned my attention to the list, so I've read the replies/inputs and now comment: BTW, I had some problems with the multiple statement - it does not take any parameters; it must simply be stated multiple without quotes. Now, I was happy to learn that it is simpler to populate the insert new books page dynamically from the db. Much shorter neater. It looks to me like the best solution for the edit page is close to what Yuri suggests. Since the edit page is very similar to the insert new books page, I merely need to populate the Select options box slightly differently. This is the code to populate the insert page: select name=categoriesIN[] multiple size=8 ?php $sql = SELECT * FROM categories; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo option value=, $row['id'], , $row['category'], /optionbr /; } } ? /select The problem nowis to find a way to add a conditional clause above that will insert the option=selected in the output. The input for this comes from: // do categories $sql = SELECT id, category FROM categories, book_categories WHERE book_categories.bookID = $idIN book_categories.categories_id = categories.id;; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { echo$row['id'], br /; } } This may return any number of category ids so the problem is to figure out a way to pass the ids from the above code to the right ids in the first code above. How what do I search to match the two ids? -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php option value=id (id == $row ? selected : ) /option is pretty bad HTML, as attributes should always have a value. Personally, I do something like this as it tends not to confuse the code too much $selected = ($id == $row)?'selected=selected':''; print option value=\$id\ $selected /option; I was unable to get any of the suggestions to work, except in_array(). However, the selected item refuses to become highlighted. code: select name=categoriesIN[] multiple size=8 ?php $sql = SELECT * FROM categories; //$selected = ($id == $row)?'selected=selected':''; if ( ( $results = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { if (in_array($row['id'], $ccc)) { echo option value=, $row['id'], selected , $row['category'], /optionbr /; } else echo option value=, $row['id'], , $row['category'], /optionbr /; } } ? /select I can't find anything that explains why the selected item is not highlighted. -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php Have you actually looked at the html this produces to see if any of the elements are being marked with the selected=selected attribute? Thanks Ash www.ashleysheridan.co.uk I just cannot find a way to pass the selected fields to the options script. Even if I add the selected to all the fields, they show up in the source code but the fields are still not highlighted. There has to be a way to get this right??? :'( -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan ---
[PHP] populate form input option dropdown box from existing data
I am having difficulties figuring out how enter retrieved data into a dropdown box for editing. Here's a snippet: ...snip select name=categoriesIN[] multiple size=8 option value=1Civilization/option option value=2Monuments, Temples amp; Tombs/option option value=3Pharaohs and Queens/option... snip As I understand it, I need to put an array ( $categoriesIN[] ) somewhere in the above code... or do I need to insert a series of value selected fields for the values? The closest thing to what I need, seems to be in this post: http://www.hpfreaks.com/forums/index.php?topic=165215 But it doesn't appear too inviting... not sure if they ever got it to work... -- Hervé Kempf: Pour sauver la planète, sortez du capitalisme. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php