0

I have a php function that gets a list of authors from a database...I am trying to get this list to display on a drop down menu, so when the user goes to select by Author it shows up as a list of Authors form the data base...I don't want to input the authors manually but I want it to use the function to populate the list..Here is my getAuthors function

  <?php
  function getAuthors($db, $isbn)
 {
 $query = 'SELECT first_name, last_name
FROM books_authors
 INNER JOIN authors ON authors.author_id = books_authors.author_id
 WHERE isbn = :isbn
 ORDER BY author_ordinal';
  $statement = $db->prepare($query);
 $statement->bindValue(':isbn', $isbn);
$statement->execute();
 $authors = $statement->fetchAll();
 $statement->closeCursor();
  $authorString = "";
 $count = 1;
  foreach ($authors as $author)
 {
 $authorString = $authorString . $author['first_name'] . ' ' .   $author        ['last_name'];
if ($count < $statement->rowCount())
  $authorString = $authorString . ", ";
 $count++;
 }
 return $authorString;
  }
  ?>

and here is my drop down menu

<select name ="searchtype">
            <option value="author"> By author: </option>
1
  • This has been asked (and answered) so many times already. Did you really search for an answer? Take a look at the "Related" topics. Commented Nov 3, 2016 at 11:23

1 Answer 1

0
   if(stcmp($ishtml,"yes")== 0)
{ ?>
    <select name ="searchtype">
                <option value="author"> By author: </option>
    <? } foreach ($authors as $author)
         {
         $authorString = $authorString . $author['first_name'] . ' ' .   $author        ['last_name'];
        if ($count < $statement->rowCount())
          $authorString = $authorString . ", ";
         $count++;
if(stcmp($ishtml,"yes")== 0)
{
        ?>  <option value="<? echo $authorString; ?>"> <? echo $authorString; ?> </option>
        <?
}
         }

Just change your foreach code with mine and it should work, you can add a default parameter to function to ask for displaying html code or not.
Change your function header with this:

function getAuthors($db, $isbn,$ishtml="no")
 {
... code below
}


So now to get the print of the dropdown you need to call your function with a additional parameter of "yes". Thats it

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.