How do I fill my combobox in PHP - php

I want to fill my combobox with names from my sql database. I saw some code on W3schools, but I really can't get it to work in my code. I have the combobox filled with select but that's not what I want it to be like. Here is the code:
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('localhost','peter','abc123','my_db');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM user WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Here is my code where I want it to apply to.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="javascript.js">
<link rel="stylesheet" href="layout.css">
<title>Bestuur wijzigen</title>
<link rel="icon" href="images/favicon.png">
</head>
<body>
<ul class="horizontal gray">
<li><a class="active" href="index.php">Bestuur</a></li>
<li>Bestuur wijzigen</li>
<li>Bestuur toevoegen</li>
</ul>
<form action="index.php">
<table class="table" border="1" frame="void" rules="rows">
<tr>
<td><label for="naam">Kies een bestuurslid</label></td>
<td>
<select>
<option value="volvo">Luca Fraser</option>
<option value="saab">Pieter Schreurs</option>
<option value="opel">Wessel Oblink</option>
<option value="audi">Andre Lammers</option>
</select>
</td>
</tr>
<tr>
<td><label for="functie">Functie</label></td>
<td>
<select>
<option value="#" selected="">Voorzitter</option>
<option value="#">Secretaris</option>
<option value="#">Penningmeester</option>
</select>
</td>
</tr>
<tr>
<td><button type="submit" class="button">Opslaan</button</td>
</tr>
</tbody></table>
</form>
</body>
</html>

Here is sample code for one combobox try with this code
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('localhost','peter','abc123','my_db');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
//$sql="SELECT * FROM user WHERE id = '".$q."'"; //your old code
$sql = "SELECT * FROM user WHERE id LIKE = '%$q%'";
$result = mysqli_query($con,$sql);
$name = [];
while($row = mysqli_fetch_array($result)) {
$names[] = $row['FirstName'];
}
mysqli_close($con);
?>
//if your query not working uncomment following line and check then it is working the problem is your query result
// $names = ['jhon','patel','Ameer','Stepan'];
<select>
<?php foreach($names as $name){ ?>
<option value="#"><?php echo $name; ?></option>
<?php } ?>
</select>

Try this simple code. It will give you an idea on how you should create something with MySQL and PHP.
<?php
//your database connection code ...
echo "<select id='functie'>";
while($row = mysqli_fetch_array($result)) {
echo "<option value='".$row['id']."'>".$row['column']."</option>";
}
echo "</select>";
//your additional following codes ...
?>
Modify it to suit your needs.

Related

converting php code to html

I have been having hard time trying to convert my php code to html for 5 hours and at this point, I'm really burned out :X.
Here's my Php code
<?php
$con=mysqli_connect("localhost","dbuser","pw","dbname");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM tablea");
echo "<table border='1' >
<tr>
<th>id</th>
<th>Subject_Code</th>
<th>date</th>
<th>name</th>
<th>description</th>
<th>Other_Details</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['Subject_Code'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td width='600' class='table_width'>" . $row['description'] . "</td>";
echo "<td width='600' class='table_width' align='center'>" . $row['Other_Details'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
and here's what i have done so far for HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>database connections</title>
</head>
<body>
<?php
$username = "dbuser";
$password = "pw";
$host = "localhost";
$database= "dbname";
$connector = mysql_connect($localhost,$dbuser,$pw,dbname)
or die("Unable to connect");
echo "Connections are made successfully::";
$selected = mysql_select_db("test_db", $connector)
or die("Unable to connect");
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM tablea ");
?>
<table border="2">
<thead>
<tr>
<th>id</th>
<th>Subject_Code</th>
<th>date</th>
<th>name</th>
<th>description</th>
<td>Other_Details</td>
</tr>
</thead>
<tbody>
<?php
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['Subject_Code']; ?></td>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo $row['Other_Details']; ?></td>
</tr>
<?php mysql_close($connector); ?>
</body>
</html>
little Help here please, Thanks !!
EDIT: seems like some people are not understanding my question. My php is working fine so i want to convert the php code into html. Basically, i want my table from database to show up using HTML table.
you not close while;
so change code to :
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>database connections</title>
</head>
<body>
<?php
/////////////////////////////////// change \\\
$username = "dbuser";
$password = "pw";
$host = "localhost";
$database= "dbname";
$connector = mysql_connect($localhost,$username,$password)
or die("Unable to connect");
echo "Connections are made successfully::";
$selected = mysql_select_db($database, $connector)
or die("Unable to connect");
/////////////////////////////////// end change \\\
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM tablea ");
?>
<table border="2">
<thead>
<tr>
<th>id</th>
<th>Subject_Code</th>
<th>date</th>
<th>name</th>
<th>description</th>
<td>Other_Details</td>
</tr>
</thead>
<tbody>
<?php
while ($row = mysql_fetch_array($result)) :
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['Subject_Code']; ?></td>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo $row['Other_Details']; ?></td>
</tr>
<?php endwhile;?>
<?php mysql_close($connector); ?>
</body>
</html>
Actually your problem is as you said like html version there is no variable like $localhost but your passing the parameter to connect mysql etc. below error code shows the variable mismatch of your code.
Error code :
<?php
$username = "dbuser";
$password = "pw";
$host = "localhost";
$database= "dbname";
$connector = mysql_connect($localhost,$dbuser,$pw,dbname);
^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^
//here there is no variable like what you passing to connect mysql that's problem here
?>
solution :
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>database connections</title>
</head>
<body>
<?php
$con=mysqli_connect("localhost","dbuser","pw","dbname");
// Check connection
if(mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM tablea");
echo "<table border='1' >
<tr>
<th>id</th>
<th>Subject_Code</th>
<th>date</th>
<th>name</th>
<th>description</th>
<th>Other_Details</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['Subject_Code'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td width='600' class='table_width'>" . $row['description'] . "</td>";
echo "<td width='600' class='table_width' align='center'>" . $row['Other_Details'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
Here's another version that makes use of PDO - by far the superior of php connectors in terms of maintainability and versatility. I have the same code working under MySQL, SQLite and SQLServer - the only thing that changes is the connection string.
You'll note I pull all of the results at once. I also make use of the fact that we get back an array. Each element of that array is a row. Each row is an array of cells. This greatly reduces the code needed to output a result-set. We just need two forEach loops and that's it.
<?php
$host = 'localhost';
$userName = 'dbuser';
$password = 'pw';
$nameOfDb = 'dbname';
$nameOfTable = 'tablea';
$pdo = new PDO('mysql:host='.$host, $userName, $password);
$pdo->query("use " . $nameOfDb);
// desired results requested explicitly, so when we get an array for the row's data, the elements will be in the same order
// - not required if the order of the columns in the database matches the desired display order. (we could just use 'select *' then)
//$query = $pdo->prepare('select * from '.$nameOfTable);
$query = $pdo->prepare('select id, Subject_Code, date, name, description, Other_Details from '. $nameOfTable);
$query->execute();
$query->setFetchMode(PDO::FETCH_ASSOC);
$rows = $query->fetchAll();
?><!doctype html>
<html>
<head>
</head>
<body>
<table>
<thead>
<tr>
<th>id</th><th>Subject_Code</th><th>date</th><th>name</th><th>description</th><th>Other_Details</th>
</tr>
</thead>
<tbody><?php
forEach($rows as $curRow)
{
echo '<tr>';
// use this one if you want to display the columns in the same order they are returned in the query results
// AND you'd like to display all columns in the result
forEach($curRow as $curCell)
echo '<td>'.$curCell.'</td>';
// otherwise, you can request the desired elements by name
//
// echo '<td>' . $curCell['id'] . '</td>'
// echo '<td>' . $curCell['Subject_Code'] . '</td>'
echo '</tr>';
}
?></tbody>
</table>
</body>
Download HTTrack Website Copier and install and run the soft. Run your script and paste your URL in web copier software. You will get HTML output in your desire folder

php mysql dropdown value select

Currently the following code displays the filters for the individual publishers ie if I select DC I will only see the DC titles.
The issue is that from the current code I cannot get it to display all publishers from the dropdown menu.
<div id="main">
<form method="post" action="">
<div id="search_query" >
<select name="make" size="0">
<option value="all">All Publishers</option>
<option value="DC">DC</option>
<option value="Marvel">Marvel</option>
<option value="Image">Image</option>
</select>
<input type="submit" name="submit" value="submit">
</div>
</form>
<div id="main_container">
<?php
$db_con = mysql_connect('127.0.0.1','root','root');
if (!$db_con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('work', $db_con);
if(isset($_POST['submit']))
{
$make = mysql_real_escape_string($_POST['make']);
$sql = sprintf("SELECT * FROM products WHERE publisher= '$make' ");
$result = mysql_query($sql);
echo "<table width= 970 border=1>
<tr>
<th width='120' scope='col'>Title</th>
<th width='170' scope='col'>Publisher</th>
<th width='185' scope='col'>Price</th>
<th width='126' scope='col'>Desc</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>".$row['title']."</td>";
echo "<td>". $row['publisher'] . "</td>";
echo "<td>". $row['price'] ."</td>";
echo "<td>". $row['desc'] ."</td>";
echo "</tr>";
}
echo "</table>";
}
else
{
$sql = sprintf("SELECT * FROM products");
$result = mysql_query($sql);
echo "<table width= 970 border=1>
<tr>
<th width='120' scope='col'>Title</th>
<th width='170' scope='col'>Publisher</th>
<th width='185' scope='col'>Price</th>
<th width='126' scope='col'>Desc</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>".$row['title']. "</td>";
echo "<td>". $row['publisher'] . "</td>";
echo "<td>". $row['price'] ."</td>";
echo "<td>". $row['desc'] ."</td>";
echo "</tr>";
}
echo "</table>";
}
mysql_close($db_con);
?>
In your query, it's searching for publisher = $make where $make = "all".
Your database table probably doesn't have any record with publisher = "make".
I'd suggest you to add an if else condition here:
if (isset($_POST['submit'])) {
$make = mysql_real_escape_string($_POST['make']);
if ($make != "all") {
$sql = sprintf("SELECT * FROM products WHERE publisher= '$make' ");
} else {
$sql = sprintf("SELECT * FROM products");
}
/* Other code */
}

List & Delete records in mysql using php error

I have written a PHP code. I am not able to retrieve the results from database and delete as well. On Submit it just gives a blank page without throwing any error. I am new to this so please, reply even if u think its a silly question. Refer to the code and suggest me some changes which will make my code work.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<title>EDIT SCREEN</title>
<form action="test4.php" method="post">
<ul>
<li>
Employee ID:</br>
<input type="text" name="eid">
</li>
<li>
<input type="submit" value="SUBMIT">
</li>
</ul>
</form>
</body>
</html>
//test.php
<?php
define('DB_NAME', 'test');
define('DB_USER', '**');
define("DB_PASSWORD", '**');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
$db_selected = mysql_select_db(DB_NAME, $link);
if(isset($_POST['ok'])){
$value1 = $_POST['eid'];
$res = mysql_query("SELECT * from 'add' WHERE empid = '".$value1."'");
echo "<table border='1'>
<tr><th>Name</th>
<th>EmployeeID</th><th>Address</th></tr>";
while($row = mysql_fetch_array($res))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['empid'] . "</td>";
echo "<td>" . $row['desig'] . "</td>";
echo "<td><a href='test5.php?del=$row[empid]'>Delete</a></td>";
echo "</tr>";
}
echo "</table>";
}
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
mysql_close();
?>
//test5.php
<?php
define('DB_NAME', 'test');
define('DB_USER', '');
define("DB_PASSWORD", '');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
$db_selected = mysql_select_db(DB_NAME, $link);
$value1 = $_POST['del'];
mysql_query("DELETE FROM add WHERE empid = '$value1'")
?>
<?php
if(isset($_POST['ok'])){
$value1 = $_POST['eid'];
$res = mysql_query("SELECT * from `add` WHERE empid = '".$value1."'");
echo "<table border='1'>
<tr><th>Name</th>
<th>EmployeeID</th><th>Address</th></tr>";
while($row = mysql_fetch_array($res))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['empid'] . "</td>";
echo "<td>" . $row['add'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
New Code
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<title>EDIT SCREEN</title>
<form action="result.php" method="post">
<ul>
<li>
Employee ID:</br>
<input type="text" name="eid">
</li>
<li>
<input type="submit" value="SUBMIT" name="ok">
</li>
</ul>
</form>
</body>
</html>
<?php
mysql_connect("localhost","root","");
mysql_select_db("fdd");
if(isset($_POST['ok'])){
$value1 = $_POST['eid'];
$res = mysql_query("SELECT * from `jobs` WHERE id = '".$value1."'");
echo "<table border='1'>
<tr><th>Name</th>
<th>EmployeeID</th><th>Address</th></tr>";
while($row = mysql_fetch_array($res))
{
echo "<tr>";
echo "<td>" . $row['job_date'] . "</td>";
echo "<td>" . $row['client_code'] . "</td>";
echo "<td>" . $row['department'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
output
Before querying the database you have to make a connection first. Follow the following code snippet example to first create a connection with mysql and then query the database.
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform queries
mysqli_query($con,"SELECT * FROM Persons");
mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Glenn','Quagmire',33)");
mysqli_close($con);
?>
try this code, Don't use id directly in mysql query, before using your id directly to mysql query pass this from mysql_real_escape_string, it will clear id from sql injection. see SQL injection, REF,
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<title>EDIT SCREEN</title>
<form action="test4.php" method="post">
<ul>
<li>Employee ID:</br><input type="text" name="eid"></li>
<li><input type="submit" value="SUBMIT"></li>
</ul>
</form>
</body>
</html>
********* test4.php file ***********
<?php
$value1 = mysql_real_escape_string($_POST['eid']);
if($value1)
{
$Connection = mysql_connect('localhost','root','') or die(mysql_error());
$ConnectionDB = mysql_select_db($Connection) or die(mysql_error());
$res = mysql_query("SELECT * from `add` WHERE empid = '".$value1."'") or die(mysql_error());
echo "<table border='1'>
<tr>
<th>Name</th>
<th>EmployeeID</th>
<th>Address</th>
</tr>";
while($row = mysql_fetch_array($res))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['empid'] . "</td>";
echo "<td>" . $row['add'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
NOTE: When your code work then remove this "or die(mysql_error())" with ";" it is only used in production mode.

Creating HTML table with php output

I am trying to get the php output into an HTML table. The mysql code outputs the data from the table pet, but when i add the table code as shown below it stops working and gives errors. How do I get the table pet output into an HTML table? thanks
<html>
<head>
<title> php test script - hope this works </title>
</head>
<body>
<h1>php & mysql connection</h1>
<hr>
<?php
$db_host = "localhost";
$db_username = "vistor";
$db_pass = "visitor";
$db_name = "test";
$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$query = $db->query('SELECT * FROM pet');
echo "<table border = '2'>"
<tr>
<th>id</th>
<th>name</th>
</tr>
while ($row = $query->fetch())
{
echo "<tr>";
echo "<td>" . $row['id'] ."</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "</tr>;
}
echo "</table>";
?>
</body>
</html>
Your issues lies in your formatting and confusion between the use of the echo command and non PHP wrapped HTML. Code should read as follows when properly formatted.
<?php
$db_host = "localhost";
$db_username = "vistor";
$db_pass = "visitor";
$db_name = "test";
$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$query = $db->query('SELECT * FROM pet');
?>
<html>
<head>
<title> php test script - hope this works </title>
</head>
<body>
<h1>php & mysql connection</h1>
<hr>
<table border = '2'>
<tr>
<th>id</th>
<th>name</th>
</tr>
<?php
while ($row = $query->fetch())
{
echo "<tr>";
echo "<td>" . $row['id'] ."</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "</tr>";
}
?>
</table>
</body>
</html>
<html>
<head>
<title> php test script - hope this works </title>
</head>
<body>
<h1>php & mysql connection</h1>
<hr>
<?php
$db_host = "localhost";
$db_username = "vistor";
$db_pass = "visitor";
$db_name = "test";
$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$query = $db->query('SELECT * FROM pet');
?>
<table border ="2">
<tr>
<th>id</th>
<th>name</th>
</tr>
<?php
while ($row = $query->fetch())
{
?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['price']; ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
Your quoting is wrong.
echo "<table border = '2'>
<tr>
<th>id</th>
<th>name</th>
</tr>";
while ($row = $query->fetch())
{
echo "<tr>";
echo "<td>" . $row['id'] ."</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "</tr>";
}
echo "</table>";
The quote at the end of the first echo line belongs after the </tr>. And the last echo "</tr>"; line was missing a closing quote.
You should be able to see these problems from the syntax highlighting in the question.

Populate my drop down list with tables and not rows in php?

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>
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

Categories