SitePoint Sponsor

User Tag List

Results 1 to 11 of 11

Thread: populate a list box with field names

Hybrid View

  1. #1
    SitePoint Evangelist dscriptor's Avatar
    Join Date
    Oct 2005
    Location
    in front of my computer
    Posts
    571
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question populate a list box with field names

    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!
    happy is the man that finds wisdom....wisdom in {PHP}.


  2. #2
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,768
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please post an example of what kind of result you'd like.
    John

  3. #3
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    712
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess your after something like:

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

  4. #4
    SitePoint Evangelist dscriptor's Avatar
    Join Date
    Oct 2005
    Location
    in front of my computer
    Posts
    571
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    something like this:

    SORT THE TABLE BY:
    and then, when i click it, it has a url link to sort the table by the selected item in the list....i hope it makes sense...
    happy is the man that finds wisdom....wisdom in {PHP}.


  5. #5
    SitePoint Evangelist dscriptor's Avatar
    Join Date
    Oct 2005
    Location
    in front of my computer
    Posts
    571
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sir matrix,

    thanks for the reply...

    the PHP script is not in the actual HTML file...the query is in PHP script, separated from the HTML.... how will i call the result of the query to populate the <select name="whatever">

    thanks....
    happy is the man that finds wisdom....wisdom in {PHP}.


  6. #6
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    712
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are u hunting for something like:

    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>
    then catch the variables in script.php place them in the sql query to order the results?

    hth

  7. #7
    SitePoint Evangelist dscriptor's Avatar
    Join Date
    Oct 2005
    Location
    in front of my computer
    Posts
    571
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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...
    happy is the man that finds wisdom....wisdom in {PHP}.


  8. #8
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    712
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  9. #9
    SitePoint Evangelist dscriptor's Avatar
    Join Date
    Oct 2005
    Location
    in front of my computer
    Posts
    571
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    ok thanks for the posts...
    now, how can i put the url link to each item in the listbox?
    happy is the man that finds wisdom....wisdom in {PHP}.


  10. #10
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    712
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  11. #11
    SitePoint Evangelist dscriptor's Avatar
    Join Date
    Oct 2005
    Location
    in front of my computer
    Posts
    571
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the posts and for your patience...thanks.. it really helped me a lot!
    happy is the man that finds wisdom....wisdom in {PHP}.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •