1

I am trying to create a dropdown box where it has from 10 - 90, I want each value to stand for the numbers in between (so 10 will hold 11, 12, 13, 14.. and so on and so forth). So if a user clicks on 10 it will display any entries in a database that range from 10 - 19, not just entries with "10" in it.

Is it possible to add multiple values within an option tag? Here is what I have attempted so far.

HTML:

<body>


  <form action="form3.php" method="post">
  <label for ="description">Description:</label>
  <input type="text" name="descrip" /><br />

         <label for="trayheight">Height: </label>
                <select name="trayheight">
                    <option value="">All</option>
                    <option value="10">10</option>
                    <option value="[20:21:22:23:24:25:26:27:28:29]">20</option>
                    <option value="30">30</option>
                    <option value="40">40</option>
                    <option value="50">50</option>
                    <option value="60">60</option>
                    <option value="70">70</option>
                    <option value="80">10</option>
                    <option value="90">90</option>
                </select><br />

         <label for="trayrange">Trayrange: </label>
                <select name="trayrange">
                    <option value="">All</option>
                    <option value="BBQ">BBQ</option>
                    <option value="Dessert">Dessert</option>
                    <option value="Display">Display</option>
                    <option value="Meat">Meat</option>   
                    <option value="Microwave">Microwave</option>
                    <option value="Party">Party</option>
                    <option value="Salad/Wet Pasta">Salad/Wet Pasta</option>
                    <option value="Snacks">Snacks</option>
                    <option value="Standard">Standard</option>
                </select><br />

        <label for ="traytype">Traytype: </label> 
                <select name="traytype">
                    <option value="">All</option>
                    <option value="Open">Open</option>
                    <option value="Cavitised">Cavitised</option>
                    <option value="Lid">Lid</option>
                    <option value="Tray">Tray</option>
                    <option value="Coallition">Coallition</option>
                    <option value="Bowl">Bowl</option>
                    <option value="Hinge pack">Hinge pack</option>
                    <option value="Pot">Pot</option>
                    <option value="Base & Lid">Base and Lid</option>
                    <option value="Rectangular">Rectangular</option>
                    <option value="Specalist">Specialist</option>
                </select><br />

        <label for="trayshape">Trayshape: </label>
                <select name="trayshape">
                    <option value="">All</option>
                    <option value="Rectangular">Rectangular</option>
                    <option value="Oval">Oval</option>
                    <option value="Square">Square</option>
                    <option value="Insert">Insert</option>
                    <option value="Round">Round</option>
                    <option value="Open">Open</option>
            </select><br />
        <input type="submit" value="Submit" /> 
    </form> 

  </body>

PHP:

        <body>

        <?php
            $con = mysql_connect ("localhost", "root", "");
                   mysql_select_db ("delyn_db", $con);

            if (!$con)
                { 
                    die ("Could not connect: " . mysql_error());
                }

            $descrip = mysql_real_escape_string($_POST['descrip']); 
            $width   = mysql_real_escape_string($_POST['width']);
            $depth   = mysql_real_escape_string($_POST['depth']);

            $varHeight= mysql_real_escape_string($_POST['trayheight']);
            $varRange = mysql_real_escape_string($_POST['trayrange']);
            $varType  = mysql_real_escape_string($_POST['traytype']);
            $varShape = mysql_real_escape_string($_POST['trayshape']);
            $varImage = mysql_real_escape_string($_POST['imagename']);

            $sql = "SELECT * FROM delyn WHERE 
                        description LIKE '%".$descrip."%'  
                    AND trayheight LIKE '%".$varHeight."%'
                    AND trayrange LIKE '%".$varRange."%' 
                    AND traytype LIKE '%".$varType."%' 
                    AND trayshape LIKE '%".$varShape."%'";


            $r_query = mysql_query($sql);

                while ($row = mysql_fetch_array($r_query))
                    { 
                        echo '<br /> <img src="   '. $row['imagename'] . '" width="180" length="100">';
                        echo '<br /> Tool Code:   '. $row['toolcode'];
                        echo '<br /> Description: '. $row['description']; 
                        echo '<br /> Tray range:  '. $row['trayrange']; 
                        echo '<br /> Tray type:   '. $row['traytype'];
                        echo '<br /> Tray size:   '. $row['traysize']; 
                        echo '<br /> Tray shape:  '. $row['trayshape'] . '<br />' . '<br />';  
                    }

                if (mysql_num_rows($r_query) <= 0){
                    echo 'No results match your search, please try again';
               }


        ?>
    </body>

Thanks in advance anyone who can help :)

dsgriffin
  • 66,495
  • 17
  • 137
  • 137
LiamHorizon
  • 171
  • 2
  • 3
  • 10
  • 2
    I don't think you need to put the range of values in the option tag. Why can't you just implement the business logic on the server. So, if they select 10, in your PHP code you can translate that into 10-19 in the query – Mark Sherretta Oct 11 '12 at 13:16
  • Because what I am doing is, 10 will stand for a 100. So when it searches for 10 (100) it will look within the database for the first records that have either 10,11,12 or anything up to 19 in(So in theory it will find anything within the first 100). I am experimenting with searching through a database without having to get the exact results. – LiamHorizon Oct 11 '12 at 13:19
  • off topic, it is pretty clear to me you are starting out with php. please use [PDO](http://www.php.net/manual/en/intro.pdo.php), or at least [Mysqli](http://www.php.net/manual/en/intro.mysqli.php) Using the mysql API is a bad habit you don't want to pick up. – Asad Saeeduddin Oct 11 '12 at 13:30

3 Answers3

1

Simple answer: you cannot store multiple values in one option tag value.

The simplest and safest solution would be to store one value in the option value (e.g. 10) and then treat this as a range from the value set to the next multiple of 10:

'AND col >= '.$value.' AND col < '.$value+10

A second solution would be to set the value to be a range like 10-19 and then on the server side:

list($min,$max) = explode('-',$value);
... 'AND col >= '.$min.' AND col <= '.$max ...

Another solution would be to store a JSON encoding of an array containing the values you wish to encapsulate in the one option and then decode the value on the server side:

'AND col IN ('.implode(',',json_decode($value)).')'

I personally would avoid the JSON approach as it is overkill for this kind of problem.

Mitch Satchwell
  • 4,770
  • 2
  • 24
  • 31
  • Why is everyone jumping to JSON encoding here? What's wrong with simple notations like "10-19" and "10,11,12,13"? – IMSoP Oct 11 '12 at 13:37
  • I just tried the second option: 'list($min, $max) = explode('-',$value); $sql .= "SELECT * FROM delyn WHERE description LIKE '%".$descrip."%' AND trayheight >= '.$min' AND trayheight <= '.$max'";' And now the forms don't return anything. Hmmm. – LiamHorizon Oct 11 '12 at 13:48
  • From a glance, you are missing a . after $min. Also my answer is abstracted away from your code and offers generalised solutions to the problem. I don't believe $value is relevant to your code, for example. – Mitch Satchwell Oct 11 '12 at 13:51
  • I just can't get it to return any result at all. Sorry the value was $varHeight, even then it still doesn't return anything. – LiamHorizon Oct 11 '12 at 14:04
  • I am even returning BOOLEAN areas now... _mysql_fetch_array() expects parameter 1 to be resource, boolean given_ haha. – LiamHorizon Oct 11 '12 at 14:06
  • As well as changing it to $varHeight, from the snippet you have provided there should be a . after $min to join the strings and you have a ' in place of a . after $max. – Mitch Satchwell Oct 11 '12 at 14:06
  • I am still getting two BOOLEAN errors for the while and if loops. I can't wrap my head around why. `list($min, $max) = explode(':',$varHeight); $sql = "SELECT * FROM delyn WHERE description LIKE '%".$descrip."%' AND trayheight >= '.$min.' AND trayheight <= '.$max.";` – LiamHorizon Oct 11 '12 at 14:16
  • `list($min, $max) = explode(':',$varHeight); $sql = "SELECT * FROM delyn WHERE description LIKE '%$descrip%' AND trayheight >= $min AND trayheight <= $max";` This should work providing the input is in the form `10:19`. In your example you are mixing single and double quotation marks. If you have any further problems with syntactical issues like this I would recommend opening a new question as this is becoming off-topic and out of scope of the original question. – Mitch Satchwell Oct 11 '12 at 14:20
  • @MitchS Just entered it and I am still getting the same boolean errors. The value in the form is the same as [10:19] and there doesn't seem to be any syntax errors. :( – LiamHorizon Oct 11 '12 at 14:29
0

I am assuming you are retrieving this integers from the PHP source code. In that case, just store this logic somewhere in your database, php file or some sort of a config file in order to keep this logic server-sided. It is not possible to store multiple values in one option, that's the idea of an option. Or you would have to store it as a json_encode string, but I am not sure if that is what you want.

It would safer as well to put this on your server-side, I am not sure what you are creating, but safety is always a pre.

More about json encoding

Jelmer
  • 2,663
  • 2
  • 27
  • 45
  • Can't an option stand for more than one value though? So 20 stands for anything from 20-29? How would you implement this server-side within the php? – LiamHorizon Oct 11 '12 at 13:21
  • @LiamHorizon No, an option has a single value, which is a string. It is entirely up to you to give meaning to that string using whatever PHP code you want - the string "a,b,c" could be a list of three values using `explode()`, but it could just be the string "a,b,c". – IMSoP Oct 11 '12 at 13:27
0

The value of an <option> can be any string; it's up to your server-side code to interpret that string appropriately.

Rather than always using the string-match operator LIKE in your SQL, you need to choose appropriate conditions for each of your columns.

For instance, set your value to be a range specified by min and max: <option value="11-20">foo</option> and then use <= and >= in your SQL (beware of "fence-post errors"!)

list($min,$max) = explode('-', $_POST['trayheight']);
$sql .= 'And trayheight >= ' . intval($min) . ' And trayheight <= ' . intval($max);
IMSoP
  • 89,526
  • 13
  • 117
  • 169