how to show table names as combobox use php syntax?

Hi,

Can you please elaborate which table names?

Mysql? ==> show us your codes?
Something else? ==> show us your codes?

this is my code :

<?php
include "connect.php";
$query = "SHOW TABLES FROM `my_database`";

$res = mysql_query($query);
echo "<select name = 'venue'>";
while (($row = mysql_fetch_row($res)) != null)
{
    echo "<option value = '{$row['0']}'";
    if ($selected_venue_id == $row['0'])
        echo "selected = 'selected'";
    echo ">{$row['0']}</option>";
}
echo "</select>";
?>

is this correct?

yah its correct for just showing the tables... but be sure to declare first $selected_venue_id outside or inside the loop =)
then you can have the attribute for <select> onchange

$query = "select * FROM `my_database`";

try this

Member Avatar for diafol

This seems to be the way to me:

include "connect.php";
$query = "SHOW TABLES FROM `my_database`";
$res = mysql_query($query);
$output = "<select name = 'venue'>";
while ($row = mysql_fetch_row($res))
{
    $sel = ($selected_venue_id == $row[0]) ? ' selected="selected"': '';
    $output .= "\n\t<option value = '{$row[0]}'$sel>{$row[0]}</option>";
}
$output .= "\n</select>";

//later on - wherever you need it:

echo $output;

However, you're comparing $selected_venue_id with the table name. Is the venue_id a string or an integer (traditional id values)? It won't stop the script working, you just won't get a selected item in the <select> - well actually, the first <option> will be the default selection.

In the code you post, I can't see $selected_venue_id being initialized anywhere, make sure it is.

how about i change it to :

while ($row = mysql_fetch_array($res)){
    echo "<option value='".$row['0']."'>".$row['0']."</option>";
}

because there are no $selected_venue_id (there are no id) after sql query result:

SHOW TABLES FROM `my_database`

is this will work?

    while ($row = mysql_fetch_array($res)){
    ?>
    <option value="<?php echo $row['0']?>"><?php echo $row['0']?></option>
    <?php }?>

try this may be it will work

Member Avatar for diafol

how about i change it to ... is this will work?

yes. But why haven't you tried it to see for yourself?

it did not work.
so i change to :

<select name="tablename">
<?php
include "connect.php";
   $query = "SELECT * FROM `group`";
   $result = mysql_query($query);
   while ($data = mysql_fetch_array($result)){
      echo "<option value='".$data['groupname']."'>".$data['groupname']."</option>";
   }
?>
</select>

before it, i insert the table that i create into group :

<?php
if(isset($_REQUEST['creategroup'])){
include "connect.php";
$nametabel = $_REQUEST['nametabel'];

mysql_query("CREATE TABLE $nametabel(
id int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
 email VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
 name_customer VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
 name_company VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
 business_line VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
 remark VARCHAR(11) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL
 )")
 or die(mysql_error());

$sql = "INSERT INTO `group` (id, groupname) VALUES ('','$nametabel')";
mysql_query($sql);  

echo "<b>Group $nametabel Created!</b>";
}
?>

i created two query in createtable.php (use form of course), to make it easier when i called it as combobox in show.php. maybe this is to long.

any direct php syntax to show all table in a database as combobox?

Member Avatar for diafol

This may be a better approach as you can apply an id to each table and use them in further code.

This code is correct:

<?php
include "connect.php";
$query = "SHOW TABLES FROM `my_database`";
if($res = mysql_query($query)){}else{die(mysql_error());}
$selectedTable = 'table';
echo "<select name='venue'>";
while ($row = mysql_fetch_row($res)){
    echo "<option value='{$row[0]}' ";
    if ($selectedTable == $row[0]){
        echo " selected='selected'";//<-- added space
    }
    echo ">{$row[0]}</option>";
}
echo "</select>";
?>

Working on it i notice you all have $row['0'] which might be the reason it's empty, it's an integar not a string and make sure you change `my_database` to your actual database

$sql = "INSERT INTO `group` (id, groupname) VALUES ('','$nametabel')";
mysql_query($sql);  

There isn't a field called groupname

$sql = "INSERT INTO `group` (`name_customer`) VALUES ('$nametabel')";
mysql_query($sql);  

Also the security on that page is a little low, update the top to something like this so sql injection can't occur

<?php
$check = str_replace('-','',$_REQUEST['creategroup']);//remove hyphens from string
$check = str_replace('_','',$check);//remove underscores
if(isset($_REQUEST['creategroup']) && ctype_alnum($check)){//check whats left is alphanumeric

@biim

i mean :
i have a database called "my_database"
inside "my_database" there are tables:
1. "my_table1"
2. "my_table2"
3. "my_table3"

in order to get all tables name when i create that tables, i add 2 query : one create table, and another query to insert into a table that called "group". to get tables name, i just called it from table "group".

i am just wondering, is there any direct and simple php syntax that can show :
1. "my_table1"
2. "my_table2"
3. "my_table3"

as a combobox (php) rather i create table "group" and put all tables that i create into "group" to show as a combobox?

"name_customer" is a field in each tables.
"groupname" is a field in table "group" to collect all tables that i create and i want to show as a combobox.

Yes, either my post above for SHOW TABLES or for the table version you can do that groupname table instead which is probably better as it lets you have other tables in the database

<?php
//"groupname" is a field in table "group" to collect all tables that i create and i want to show as a combobox.
include "connect.php";
$query = "SELECT `groupname` FROM `group`";
if($res = mysql_query($query)){}else{die(mysql_error());}
$selectedTable = 'table';
echo "<select name='venue'>";
while ($row = mysql_fetch_assoc($res)){
    echo "<option value='{$row['groupname']}' ";
    if ($selectedTable == $row['groupname']){
        echo " selected='selected'";//<-- added space
    }
    echo ">{$row['groupname']}</option>";
}
echo "</select>";
?>

thank you for all, thanks for the answer. awesome.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.