SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    87
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Calling a Range From MySQL Table With Drop Down Menu

    Good evening from Palo Alto, California. I have four items in a MYSQL database table. Each has 2 fields: name and weight, as shown here:

    Bob 149
    Sue 98
    Jack 155
    Sally 110


    I want users to be able to visit a drop-down menu and ask to see the names associated with one of three weight ranges: up to 100, between 100 and 150, and over 150. If, for example, they selected 100-150, I'd like the code to return:

    Sally
    Bob

    But I'm stuck on how to do this. I imagine that the HTML page with the drop down menu would look something like:

    <HTML>
    <HEAD></HEAD>
    <BODY>
    <form action="generatelist.php" method="POST">
    Show me the names of people with weights of
    <select name="weight" size="1">
    <option value> </option>
    <option value="upto100">up to 100</option>
    <option value="100-150">100-150</option>
    <option value="150+">over 150</option>
    </select>
    <INPUT TYPE="submit" value="Search"></FORM>
    </BODY>
    </HTML>

    I also imagine that "generatelist.php" should look something like:

    <?
    $weight = $_POST['weight'];
    mysql_connect("ip.address.goes.here", "sql@nameofmysite.com", "password") or die(mysql_error());
    @mysql_select_db("nameofdatabase") or die( "Unable to select database");
    $query="SELECT * FROM landforsale WHERE weight='WeightRange'";
    $result=mysql_query($query);
    $num=mysql_numrows($result);
    mysql_close();
    $i=0;
    while ($i < $num) {
    $weight=mysql_result($result,$i,"weight");
    $name=mysql_result($result,$i,"name");
    ?>
    <? echo "$name";?><br><br>
    <?
    ++$i;
    }
    ?>

    Please, do you have some guidance for me? Thank you!

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try out this code which working fine with me although this might not be the good way if someone expert finds easy and short way.
    PHP Code:
    if($_POST['Submit']){
        
    $weight $_POST['weight'];
        
        if(
    $weight == "100"){
            
    $param " AND weight<100";
        }
        else if(
    $weight == "100-150"){
            
    $param " AND weight>100 AND weight<150";
        }else if(
    $weight == "150"){
            
    $param " AND weight>150";
        }else{
            
    $param "";
        }
        
    $sql "SELECT * FROM tbltest WHERE 1 $param";
        
    $result mysql_query($sql);
        while(
    $rows mysql_fetch_array($result)){
            echo 
    $rows['name1'] . "<br>";
        }

    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Test</title>
    </head>
    <body>
    <form name="frm1" method="post">
    <table width="350" border="0" cellspacing="1" cellpadding="3">
      <tr>
        <td colspan="2" align="left" valign="middle">Select Weight </td>
        </tr>
      <tr>
        <td align="right" valign="middle">&nbsp;</td>
        <td><select name="weight" size="1">
            <option value="0">Select</option>
            <option value="100">Up To 100</option>
            <option value="100-150">100-150</option>
            <option value="150">Over 150</option>
          </select>    </td>
      </tr>
      <tr>
        <td align="right" valign="middle">&nbsp;</td>
        <td><input type="submit" name="Submit" value="Send Mail"></td>
      </tr>
      <tr>
        <td colspan="2" align="left" valign="middle">&nbsp;</td>
      </tr>
    </table>
    </form>
    </body>
    </html>
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    87
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Other suggestions? And thanks, rajug!

    Thank you, rajug, very much for your reply. The code seems not to be working for me. I wonder -- does someone see a simpler solution?
    Thank you!


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
  •