Jump to content

tables instead of rows in a <select> choice form.


WesleyA

Recommended Posts

i found this script online: http://stackoverflow.com/questions/8022353/how-to-populate-html-dropdown-list-with-values-from-database

    <?php     $conn = new mysqli('localhost', 'username', 'password', 'database')      or die ('Cannot connect to db');     $result = $conn->query("select id, name from table");     echo "<html>";     echo "<body>";     echo "<select name='id'>";     while ($row = $result->fetch_assoc()) {                  unset($id, $name);                  $id = $row['id'];                  $name = $row['name'];                   echo '<option value="'.$id.'">'.$name.'</option>';     }     echo "</select>";     echo "</body>";     echo "</html>";     ?> 

But what I want is to get the table names from the database. Does anyone know how?

Link to comment
Share on other sites

Yes, the result of SHOW TABLES can be fetched with the fetch_assoc(). Most likely the table name is in the array with index 0, but just to be sure, use print_r() to see what is actually returned.

Link to comment
Share on other sites

Actually I had that already.

 

This is a part of my own script

    <html>    <!-- make connection -->    <?php    $sql = "SHOW TABLES";    if (!$result = $conn->query($sql)){	die('There was an error running the query[' .$conn->error. ']');		    }    while($row = $result->fetch_assoc()){	echo "<center>";	$reeks = implode(" " , $row);	echo $reeks;		echo "<br>";	echo "</center>";    }    mysqli_close($conn);				?>		<body>		<table style="width: 100%;">    <tbody>    <tr>    <td>    <div class="lefcol"> <center>    <br><br><br><br><br><br>    Choose here which option you want<br>    <br><br>    <?php       ?>	         <form method="post">       <select name="col2" >	      <?php 	if (is_array($row)) //	{		foreach ($row as $value) : ?>					        <option value="<?php echo $value ?>"> <?php echo $value ?></option> 	        <?php endforeach; }?>        </select>    	<br><br><br>       			<input type="submit" value="click" />        </form>    <!-- continue html choice -->    <br>    </center>    </html>

so I'm able to print or echo the database tables. Now the thing is that I want these tables in the select tag.

Edited by WesleyA
Link to comment
Share on other sites

That's just a matter of looping through the database results and printing <option> tags for them. Are you really having trouble with that?

Link to comment
Share on other sites

I believe it is possible, but the thing is that there seems to be a difference in getting rows from a databases and getting tables.

 

Getting a row like id or name in a table has an identification variable.

 

When you use SHOW TABLES you get all the tables but its a list, an array.

Edited by WesleyA
Link to comment
Share on other sites

but the thing is that there seems to be a difference in getting rows from a databases and getting tables.

It's not any different, you're just not printing the column names, only the values. The implode function does not use the array keys. Use var_dump or print_r to see the structure of the array so you can see what the keys are. Or, look up the MySQL manual for show tables to see what it returns.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...