hi all,
supposed i have a db table:
|id|name|lastname|address|email
i want to populate a list box with the field names...and put a url each item in the list...
how will i do it in PHP?
please help..thanks!
| SitePoint Sponsor |





Please post an example of what kind of result you'd like.
John




I guess your after something like:
hthPHP Code:<select name="whatever">
<option value="">Choose</option>
<?php $sql = mysql_query("SELECT * FROM table ORDER BY field ASC"); while ($row = mysql_fetch_array($sql)) { ?>
<option <?php echo $_POST['field']==$row['field'] ? 'selected' : ''?> value="<?php echo $row['url']; ?>"><?php echo $row['field']; ?></option>
<?php } ?>
</select>




Are u hunting for something like:
then catch the variables in script.php place them in the sql query to order the results?PHP Code:<form action="../script.php" method="post">
<select name="field">
<?php $sql = mysql_query("SELECT * FROM table ORDER BY field ASC"); while ($row = mysql_fetch_array($sql)) { ?>
<option value="<?php echo $row['field']; ?>"><?php echo $row['field']; ?></option>
<?php } ?>
</select>
<select name="order">
<option value="ASC">ASC</option>
<option value="DESC">DESC</option>
</select>
<input type="submit" name="Submit" value="Submit" />
</form>
hth



you are quite right...but do not want to put the PHP query or script lines inside the html file where select is displayed..
what i have to do is create a query in another PHP file to get the list of fields...and then the result will be displayed as list items in the <select> which is in another HTML file....i hope that makes it clear...![]()




Well im pretty sure you need the php between the html to populate the the list/menu
I cant see any other way you can acheive this ..
Whats the problem with the php in there anyway?
also .. use the php_self function and run the entire script within the same page.
hth




you dont ..
you post the form and catch the post variables in the next part of the script, use the variables in your query to create the sort etc ..
Altho I dont think u want to be populating the list/menu from the database, it looks like you want the table field headings in the list menu .. there for something like:
PHP Code:<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<select name="fields">
<option value="id">ID</option>
<option value="name">Name</option>
<option value="address">Address</option>
<option value="email">Email</option>
</select>
<select name="order">
<option value="ASC">ASC</option>
<option value="DESC">DESC</option>
</select>
<input type="submit" name="Submit" value="Submit" />
</form>
<?php
$sql = mysql_query("SELECT * FROM table ORDER BY ".$_POST['fields']." ".$_POST['order']."");
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$name = stripslashes($row['name']);
$address = stripslashes($row['address']);
$email = stripslashes($row['email']);
echo 'Your results here';
}
?>
hth
Last edited by _matrix_; Feb 16, 2006 at 17:32.
Bookmarks