0

Hello i am able to use a drop down list to show rows from a table but what i am wanting to is to populate the drop down list with the 2 tables i have in my database.

Example: i am currently listing members from the table members

bob jake chris

but what i am wanting to do is to list the tables

members cars

<?php
// check for errors
ini_set('display_errors', 1);

//calls connection
require_once('connection.php');
?>

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title></title>
</head>
<body>
    <h1> View Table(s) </h1>

    <?php
    // calls get results method to list members    
    $ResultSet = getResults("members");

    echo "<table border='1' cellpadding='6'>";
    echo "<tr> <th>Id</th> <th>First Name</th> <th>Second Name</th> <th>Age</th>     <th>Email</th>";

    foreach ($ResultSet as $row) {
        echo "<tr>";
        echo "<td>" . $row ['id'] . "</td>";
        echo "<td>" . $row['first_name'] . "</td>";
        echo "<td>" . $row['second_name'] . "</td>";
        echo "<td>" . $row['age'] . "</td>";
        echo "<td>" . $row['email'] . "</td>";
        echo "</tr>";
    }

    echo "<table>";
    ?>
    <br/>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post'>   
        <p>Choose a member to view:</p>
        <select name='members' id="ddmembers">       
            <?php
            $results = getResults('members');
            if ($results) {
                foreach ($results as $row) {
                    echo '<option value="' . $row['id'] . '">' . $row['first_name'] . '</option>';
                }
            }
            else
                echo '<option value="0"0"> No Data</option>';
            ?>

        </select>
        <input type="submit" id="submit" value="Submit"/>
        <br/> 
        <br/>
    </form>

    <?php
//Message for what has been selected
    if (isset($_POST['members'])) {
        echo 'You have selected Member ' . $_POST['members'];
    }
    ?>
</body>
</html>
Bob Marks
  • 139
  • 4
  • 14

1 Answers1

1

Try using information_schema database

See the following post for more information

Get table names using SELECT statement in MySQL

EDIT : Without using information_schema, you can do something like

<?php
 $dbname = "DATABASE_NAME";
 $sql = "SHOW TABLES FROM $dbname";
 $result = mysql_query($sql);
 $tableNames= array();

 while ($row = mysql_fetch_row($result)) {
  $tableNames[] = $row[0];
 }


 echo '<select name="tables" id="tables">';     
 foreach ($tableNames as $name){
   echo '<option value="' . $name . '">' . $name . '</option>';
 }
 echo '</select>';
?>

2 times action :

  • Save all names into array
  • Iterate this array and print options

Of course, if you prefer, you can do this with 1 time action

Community
  • 1
  • 1
azerto00
  • 1,001
  • 6
  • 18