formador3
formador3

Reputation: 21

PHP MySQL dropdown menu with DB's Tables

So, I have the following code using PDO method to get all the tables from my DB.
But I need to put it inside a dropdown menu (the ideia it's to display all the records of the selected table so the user can export that records from the table that he selected to an .csv file)
Can anyone help me please?

$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = $pdo->prepare('Show Tables from filecleaner');
$query->execute();

while($rows = $query->fetch(PDO::FETCH_ASSOC)){
     var_dump($rows);
}

Upvotes: 1

Views: 39

Answers (1)

devpro
devpro

Reputation: 16117

You can display all tables name from filecleaner in a select box like

$allTables = array(); // initialize your array
while($rows = $query->fetch(PDO::FETCH_BOTH)){ 
    $allTables[] = $rows[0]; // store all table name in an array. $rows[0] index will give you table name from filecleaner
}

then, display like:

<select>
    <option>Select Table</option>
    <?php
    foreach ($allTables as $key => $value) {
    ?>
        <option><?=$value?></option>
    <?php
    }
    ?>
</select>

Edit:

i think you are not getting $rows[0] due to FETCH_ASSOC, you need to use FETCH_BOTH here to get 0 indexed column number.

Issue in your code is: FETCH_ASSOC returns an array indexed by column name, and you are trying to display with 0 indexed column number.

Reference

Upvotes: 1

Related Questions