Jump to content
Sign in to follow this  
WesleyA

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

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?

Share this post


Link to post
Share on other sites

You can use the SQL query SHOW TABLES to return a list of tables.

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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?

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...