Jump to content

[SOLVED] Storing and outputting an array


mattyvx

Recommended Posts

Hi, I'm relatively new to PHP so bare with me :)

 

This might be simple but i've been trawling the net for hours trying to find something.

 

What i want to do is have a dynamically populated dropdown menu where the values are the table names i have in my SQL database.

 

So far i have created the dropdown and can populate from a seperate php file with an array, however i want to get rid of this file and have the values of table names from my database.

 

Here's where i am;

 

this is embedded on my page...

 

  
<?php
include 'scripts/dropdown.php';
     
echo '<SELECT name="town" id="town" style="width:200px;height:25px;font-size:1em;">';

foreach ($town as $key => $town)
{
echo '<OPTION value="'.$town.'"> '.$town.'';
}
echo '</select>';
?> <!-- PHP dropdown box-->

 

the file dropdown.php is:

 

<?php 

$town = array( 
1=> "town1", 
2=> "town2", 
3=> "town3", ); 

sort($town) 

?>

 

So, thats the situation im in, basically i have to manually update the dropphp file for it to work, now heres my attempt at what i want to do... :

 

<?php     
$db_host = 'localhost';       
$db_user = '*';       
$db_pwd = '*';       

$database = 'Customers';       
  
if (!mysql_connect($db_host, $db_user, $db_pwd))       
    die("Can't connect to database");       

if (!mysql_select_db($database))       
    die("Can't select database");       

$sql = "SHOW TABLES FROM $database";
$result = mysql_query($sql);

if (!$result) {
    echo "DB Error, could not list tables\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}

$number = 1;
$town = array();
while ($row = mysql_fetch_array($result)) 
{

echo "{$number}=> \"{$row[0]}\", ");
$number = $number + 1;
}

 

As you can tell i'm getting abit lost.... help?!!!!

Link to comment
https://forums.phpfreaks.com/topic/179915-solved-storing-and-outputting-an-array/
Share on other sites


$query = "SELECT name from table";
while ($row = mysql_fetch_array($query)){
echo "<option value=\"".$row['name']."\" >."$row['name']."</option>";
}

 

you would obviously have to substitute name with whatever the column name is and table with your table, and of course add a where condition or whatever else.

 

Thanks for the quick reply,

 

I've tried using the example you gave but I don't think its quite what im after. Pardon my ignorance if this is easy to change but im not after rows from a specific table, i need the list to be populated with TABLE NAMES FROM my $database. (the table names are all towns so the dropdown would be a list of town)

 

e.g.

$DATABASE = Customers

within $database there are X ammount of tables

I want the drop down to display each table name there is in the $database.

Table 1 = Town a

Table 2 = Town b

Table X = .....

 

Dropdown would display [Town a, Town B, .......}.

 

Thanks in advance.

 

GOT IT!! I was so close too...

 

Your post wasnt exactly what im after but you've pointed me in the right direction. I used my initial query to SELECT NAMES from $DATABASE and then stored these results as in your post by echo-ing them into "OPTIONS".

 

echo '<SELECT name="town" id="town" style="width:200px;height:25px;font-size:1em;">';

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

echo '</select>';
?> <!-- PHP dropdown box-->

 

seems to work a treat!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.