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.

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

Reply via email to