SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Oct 2005
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dependable combos in PHP and mysql.Plz help.

    Hi

    i am a newbie in php and mysql and am stuck with combos. I have two dependable combos say categories combo and subcategories combo. When a user selects a

    category then corresponding subcategories get loaded after that and the second combo gets populated. I have other fields too so there will be one final

    submit at the end.

    Please send me a working example if anyone has it or has a better approach to this problem as i am completely new to PHP.

    This is a part of it that i got online and was trying it. Please help me with it.

    THE CODE STARTS HERE
    <?php

    global $onpage;
    $onpage = $_SERVER['PHP_SELF'];

    // Connect to the test datbase on localhost
    // That's where we created the countries table above
    mysql_connect('localhost','finaluser','sp');
    mysql_select_db('dbtest');

    // Query the countries table and load all of the records
    // into an array.
    $sql = 'SELECT * FROM category';
    $res = mysql_query($sql) or die(mysql_error());
    while ($rec = mysql_fetch_assoc($res)) $categories[] = $rec;

    ////pasted form code here
    echo "<form action=\"<?=$onpage?>\" method=\"post\" name=\"cat\">";
    echo '<SELECT name="cat">';
    echo "<OPTION value=\"\" SELECTED>----- Choose your category first ----</OPTION>\n";
    foreach ($categories as $c)
    {
    if ($c['id'] == $_GET['id'])
    echo "<OPTION value=\"{$c['catid']}\" >{$c['catname']}</OPTION>\n";
    //echo "<OPTION value=\"{$c['catid']}\" SELECTED>{$c['catname']}, {$c['catname']}</OPTION>\n";
    }
    echo '</SELECT>';

    echo $c['catid'];

    echo "/////////////////////////////";
    //second combo box2
    $sql2 = 'SELECT * FROM subcategory';
    $res2 = mysql_query($sql2) or die(mysql_error());
    while ($rec = mysql_fetch_assoc($res2)) $subcategories[] = $rec;
    echo '<SELECT name="subcat">';
    echo "<OPTION value=\"\" SELECTED>----- Choose your sub category ----</OPTION>\n";
    foreach ($subcategories as $c2)
    {
    if ($c2['id'] == $_GET['id'])
    echo "<OPTION value=\"{$c2['subcatid']}\" >{$c2['subcatname']}</OPTION>\n";
    //echo "<OPTION value=\"{$c['catid']}\" SELECTED>{$c['catname']}, {$c['catname']}</OPTION>\n";
    }
    echo '</SELECT>';
    echo '</form>';

    //echo $forms["subcat"];
    ?>

    Please help me with this as i am stuck with it and cannot go further.
    thanks in advance.
    seema

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    bugs

    What error messages are you getting?


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
  •