Results 1 to 6 of 6

Thread: Simple for you... hell for me!

  1. #1
    Join Date
    Apr 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Simple for you... hell for me!

    Anyone that knows php:

    Small (simple) project from a novice:

    I want to have a page refresh the data based on a drop down. I know little php, but it's what my sever supports.

    I have the mysql table with data already and I have gotten as far as having my dropdown update from the table (cool)

    here is my code:
    Code:
    <? 
    $db_username = "mydbname"; // I chaged the real thing here...
    $db_password = "mypw"; 
    $db_hostname = "mysql.mysite.com"; 
    
          
    $db_connect = mysql_connect($db_hostname, $db_username, $db_password)or die("Unable to connect to MySQL"); 
    $db_select = mysql_select_db("mydb", $db_connect)or die("Unable to select Database"); 
    
    // below is where I have the dropdown get options from a field "team1"
    
    $result = mysql_query("SELECT DISTINCT `team1`,'team2' FROM `sg1`WHERE `team1` NOT LIKE '%bye%' ORDER BY `team1` ASC ");
    echo "<select name=home1 value=''>Home1</option>";
    	while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
    echo "<option value=$nt[team2]>$nt[team1]</option>";
    /* Option values are added by looping through the array */
    }
    echo "</select><br>";// Closing of list box 
    
    // lost from here: how do I have the above dropdown output based on it's value
    
    $result = mysql_query("select * from sg1"); 
    while ($row = mysql_fetch_array($result)) { 
    echo "Match Start Date: $row[start_date]<br>Playing Dates: $row[match_dates] <br> Zone Details: $row[zone] Group: $row[zone_tittle]<br> Home Team: $row[team1] vs. $row[team2] at Grounds: $row[ground]<br><br><br>"; 
    } 
    
    mysql_close($db_connect);
    So all I want is to have the user select a couple dropdowns and have the data output based on the selections....

    any help is a lot of help... Thanks.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Here's a rough outline--

    echo "<option value=$nt[team2]>$nt[team1]</option>";
    For this line... you need to notice that the value is $nt[team2].

    Now... I'm not sure how this works.

    If you have a form, just make it like this:
    <form action="" method="post">
    (current page/"post" method... could be get... but that displays the values in the address bar... like index.php?var=value)
    (and a close tag after the dropdown list.)

    The output IS ALWAYS based on the value. That's the point.

    All you need to do is add that form tag.
    OR find javascript (onChange, I believe) to refresh the page based on that value, but the form setup would be easier... your choice.

    Once the page is refreshed, you will have a value sent.

    Since your select's name is "home1"**, then you will get a value sent as variable "home1".
    (**ADD QUOTES AROUND THE NAME! not sure if they're required, but certainly more compatible)

    To do something about that sent value.
    $_POST['home1'] is the value.
    So.. for example,
    if ($_POST['home1'] == "teamsomething") {
    //do stuff!
    }

    Note: $_POST and $_GET work the same way, but are based on the method of the form. As above, post is hidden, but get is sent in the address bar.
    your choice.
    (advantage of get is that you could link to "...index.php?var=value" and the page would work... you don't NEED to send a form. might also be easier to make it work with javascript if you choose that method)

    That work?

  3. #3
    Join Date
    Apr 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Wow... no wonder you are called: Professor

    so I understand the "concept" but the code.... it is 'out there' for me...

    I'll do as u suggest:
    using the $_GET method, could you give a rough outline of the code... and perhaps I could go from there (as in the actual syntax/code) as I am very new to PHP

    The concept, if I understand, is that after the user selects the options via say selection.php, he can hit 'submit' (simple and no onchange stuff) and then php calls another page like results.php

    Therefore: what does that code look like in selection.php, and what does the code in results.php look like.... (copy from my code in the 1st message for speed....)

    I can see light at the end of the tunnel....

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Haha. I noticed my title change today too. I just post a lot, trying to be helpful
    Learning myself...
    this stuff shouldn't be above your head if you play around enough.

    The concept, if I understand, is that after the user selects the options via say selection.php, he can hit 'submit' (simple and no onchange stuff) and then php calls another page like results.php
    Yep. Done. *remember to add a "submit" button... so they can get there... you don't have one right now.

    Well... you're asking me to write a code for something I don't really understand. i'm not sure what you actually want it to do. I can give you a rough outline, though.

    All you need on the selection.php page is what you have, 'cause that includes the select menu. That's what you need... the form. Add whatever else, and get the values of the menu. Looks like you've got that working.
    add form tags, like above, and a submit button-- <input type="submit" value="Click to submit"> (before the form close tag, with a line break or whatever if you want)

    (use POST OR GET, your choice... using GET as you said that above... just replace all with POST if that's what you want)

    On results.php... here:
    PHP Code:
    <?
    $sel 
    $_GET['home1'];
    if (
    $sel == "stuff") {
         
    //do stuff
         
    }
    elseif (
    $sel == "somethingelse") {
         
    //do something else
         
    }
    elseif (
    $sel == "whatever") {
         
    //do whatever
         
    }
    else {
         
    //display default/error message (etc) if there are  no matches above.
         
    }
    ?>
    Make more sense?

  5. #5
    Join Date
    Apr 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    You gave me wings!

    As vague as the example I gave, and with your guide... I made it work.

    Thanks... and I do appreciate it!

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Cool. Glad to help.

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
  •