Results 1 to 4 of 4

Thread: Drop Down Menus

  1. #1
    Join Date
    Jun 2004
    Posts
    5

    Unanswered: Drop Down Menus

    Is there a way to create a drop down menu using php and html to show a list of data in a mysql database table.

  2. #2
    Join Date
    Jan 2004
    Location
    India
    Posts
    168

    Thumbs up

    Ya there are ways to that. But I would like to know more about what exactly you need. As your requirement is not that clear, I am guessing that you want the data of a perticular field. the code sample will be like this:

    $sql="select * from table";
    $result=mysql_query($sql);
    if($result)
    {
    echo "<select name='type'>";
    while($row=mysql_fetch_array($result))
    {
    echo "<option value="\".$row['field2']."\">$row['field2']</option>";
    }
    echo "</select>";
    }

    hope this would do. If you still have doubt feel free to get back to us.

    Regards
    Rajesh

  3. #3
    Join Date
    Jun 2004
    Posts
    5

    I tried it....

    Here is the code:

    <html>
    <head>
    <title> Our List of Jokes </title>
    </head>
    <body>
    <?php

    // Connect to the database server
    $dbcnx = @mysql_connect('localhost', 'root', '');
    if (!$dbcnx) {
    die( '<p>Unable to connect to the ' .
    'database server at this time.</p>' );
    }

    // Select the jokes database
    if (! @mysql_select_db('jokes') ) {
    die( '<p>Unable to locate the joke ' .
    'database at this time.</p>' );
    }



    $sql="select JokeText from jokes";
    $result=mysql_query($sql);
    ?>
    if($result)
    {
    echo "<select name='text'>";
    while($row=mysql_fetch_array($result))
    {
    echo "<option value="\".$row['JokeText']."\">$row['JokeText']</option>";
    }
    echo "</select>";
    }
    ?>

    <p> Here are all the jokes in our database: </p>
    <blockquote>
    <?php

    // Request the text of all the jokes
    $result = @mysql_query('SELECT JokeText FROM Jokes');
    if (!$result) {
    die('<p>Error performing query: ' . mysql_error() .
    '</p>');
    }

    // Display the text of each joke in a paragraph
    while ( $row = mysql_fetch_array($result) ) {
    echo('<p>' . $row['JokeText'] . '</p>');
    }

    ?>
    </blockquote>
    </body>
    </html>

    And this is the result:

    if($result) { echo ""; while($row=mysql_fetch_array($result)) { echo "$row['JokeText']"; } echo ""; } ?>
    Here are all the jokes in our database:

    Why did the chicken cross the road? To get to the other side!

    Knock knock. Who's there? Me! Me, who?...

    Jokes are supposed to be funny.

    Wow, this actually works!

    Test

    I can enter from my blackberry

    I can enter from my blackberry


    ####################
    Please let me know if you see my error.

    Thanks

  4. #4
    Join Date
    Jan 2004
    Location
    India
    Posts
    168
    Quote Originally Posted by ccc2001
    Here is the code:
    <?php

    // Connect to the database server
    $dbcnx = @mysql_connect('localhost', 'root', '');
    if (!$dbcnx) {
    die( '<p>Unable to connect to the ' .
    'database server at this time.</p>' );
    }

    // Select the jokes database
    if (! @mysql_select_db('jokes') ) {
    die( '<p>Unable to locate the joke ' .
    'database at this time.</p>' );
    }



    $sql="select JokeText from jokes";
    $result=mysql_query($sql);
    *****************************************
    ?>
    *****************************************

    if($result)
    {
    echo "<select name='text'>";
    while($row=mysql_fetch_array($result))
    {
    echo "<option value="\".$row['JokeText']."\">$row['JokeText']</option>";
    }
    echo "</select>";
    }
    ?>

    <p> Here are all the jokes in our database: </p>
    <blockquote>
    <?php

    // Request the text of all the jokes
    $result = @mysql_query('SELECT JokeText FROM Jokes');
    if (!$result) {
    die('<p>Error performing query: ' . mysql_error() .
    '</p>');
    }

    // Display the text of each joke in a paragraph
    while ( $row = mysql_fetch_array($result) ) {
    echo('<p>' . $row['JokeText'] . '</p>');
    }

    ?>
    </blockquote>
    </body>
    </html>
    You can see one "?>" ie th closing tag of php is there just before the if($result)
    this makes you code to eliminate the code inside the if condition.

    Remove that and try that.
    Regards
    Rajesh

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •