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'], >>>>>> "</option><br />"; } >>>>>> } >>>>>> </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 <select>s. 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 --- pj@xxxxxxxxxxxxx >>>> 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 here, but you should always have an 'id' key > in the array. You need to do some debugging. Do a > print_r($selected_ids) and in your loop later do a echo $row['id']. So > in someone's example above, it should look something like: > > $selected_ids = array(5, 7, 9, 10, 11); > > $row['id'] = 5; > > Then this works: in_array($row['id'], $selected_ids); > > Obviously it doesn't work unless row id is 5, 7, 9, 10, 11. Not so obvious if you read the manual... it is very vague and if you follow the examples to the letter, they do not work! - al least not without better explanation. And here's another goodie -- I cannot get var_dump() to print out nor print_r $selected when there are no categories listed This code: 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['category']."</option>"; } } works but doesn't highlight the correct categories; I have checked the db tables and they are correct; and if I uncomment the var_dump line below --- and the categories are empty, I get an undefined variable for the $selected: // 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) ) >= 0 ) { while ( $row = mysql_fetch_assoc($results) ) { $selected[] = $row['id']; } } else $selected = ""; //var_dump($selected); I suspect there is something wrong with the code for the categories for book id=11 are 6, 14, 20, and 34; but the one field that is highlighted is 11... hmmm ? Ok, so I traced it down to this - the highlighting is coming from the id of the book.id - I fixed that. But in the end, and I think I have the problem fixed, the highlighting is definitely lilnked to the the name attribute - in my case it is categoriesIN[] as that is related to the sessions and the queries in some mysteious way... :-) -- Hervé Kempf: "Pour sauver la planète, sortez du capitalisme." ------------------------------------------------------------- Phil Jourdan --- pj@xxxxxxxxxxxxx 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