drop down populated by database

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jsd219

    #1

    drop down populated by database

    Hello, hopefully someone can help me with this: I currently have a php
    file that pulls a query from the field "category" from my database.
    this file is called category.php. I am trying to populate a drop down
    box in another page (called add_subpage.php ) from the results of the
    category page. the code below is on the add_subpage.php file: (It was
    my best guess and does not work)

    <select name="category" >
    <option value="CAT"<?ph p include 'category.php' ?>></option</select>

    Can anyone tell me how to make this drop down populate from the
    category php file?

    I really appreciate any help!! Thank you

    God bless
    jason

  • Geoff Berrow

    #2
    Re: drop down populated by database

    Message-ID: <1181238806.840 842.284510@p47g 2000hsd.googleg roups.comfrom
    jsd219 contained the following:
    ><select name="category" >
    ><option value="CAT"<?ph p include 'category.php' ?>></option</select>
    >
    >Can anyone tell me how to make this drop down populate from the
    >category php file?
    >
    We need to see category.php.

    If this file produces an array (called say $categories) then you could
    do something like this:

    <select name="category" >
    foreach ($categories as $category){
    echo "<option >$category</option>\n";
    }
    ?>
    </select>
    --
    Geoff Berrow (put thecat out to email)
    It's only Usenet, no one dies.
    My opinions, not the committee's, mine.
    Simple RFDs http://www.ckdog.co.uk/rfdmaker/

    Comment

    • jsd219

      #3
      Re: drop down populated by database

      On Jun 7, 1:11 pm, Geoff Berrow <blthe...@ckdog .co.ukwrote:
      Message-ID: <1181238806.840 842.284510@p47g 2000hsd.googleg roups.comfrom
      jsd219 contained the following:
      >
      <select name="category" >
      <option value="CAT"<?ph p include 'category.php' ?>></option</select>
      >
      Can anyone tell me how to make this drop down populate from the
      category php file?
      >
      We need to see category.php.
      >
      If this file produces an array (called say $categories) then you could
      do something like this:
      >
      <select name="category" >
      foreach ($categories as $category){
      echo "<option >$category</option>\n";}
      >
      ?>
      </select>
      --
      Geoff Berrow (put thecat out to email)
      It's only Usenet, no one dies.
      My opinions, not the committee's, mine.
      Simple RFDshttp://www.ckdog.co.uk/rfdmaker/
      Thank you, i have one more question now. :-) i need to see the results
      from my table where column Category has anything in it. I am assuming
      i would use a WHERE statement but i don't know how to tell it to look
      for anything in the field.

      God bless
      jason

      Comment

      • Geoff Berrow

        #4
        Re: drop down populated by database

        Message-ID: <1181247194.230 303.237710@q69g 2000hsb.googleg roups.comfrom
        jsd219 contained the following:
        >Thank you, i have one more question now. :-) i need to see the results
        >from my table where column Category has anything in it. I am assuming
        >i would use a WHERE statement but i don't know how to tell it to look
        >for anything in the field.
        Something like this:

        $categories=arr ay();
        $sql="SELECT DISTINCT category FROM table WHERE category!=''";
        $result=mysql_q uery($sql);
        while($row=mysq l_fetch_assoc($ result)){
        $categories[]=$row['category'];
        }

        --
        Geoff Berrow (put thecat out to email)
        It's only Usenet, no one dies.
        My opinions, not the committee's, mine.
        Simple RFDs http://www.ckdog.co.uk/rfdmaker/

        Comment

        • Toby A Inkster

          #5
          Re: drop down populated by database

          Geoff Berrow wrote:
          $sql="SELECT DISTINCT category FROM table WHERE category!=''";
          Although tempting to use, SELECT DISTINCT is normally a sign of a place
          where things could be done better.

          SELECT MIN(category) FROM table WHERE category!='' GROUP BY category;

          will achieve the same effect, but on most databases will run slightly
          faster. This is because SELECT DISTINCT effectively fetches rows and then
          discards them, whereas GROUP BY only fetches the needed rows to begin with.

          If you don't believe me, try using the "EXPLAIN" feature on your database
          to see which is faster.

          --
          Toby A Inkster BSc (Hons) ARCS
          [Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
          [OS: Linux 2.6.12-12mdksmp, up 104 days, 18:07.]

          URLs in demiblog

          Comment

          Working...