Web Analytics Made Easy -
StatCounter Passing a dynamic variable... - CodingForum

Announcement

Collapse
No announcement yet.

Passing a dynamic variable...

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Passing a dynamic variable...

    This is what I am trying to do. I want a user to be able to pick a general number range within a drop down menu - and then pass that variable so that when the recordset is pulled up dynamically on the next page - it will pull up products only within that general number range.

    For example:

    I want one variable to pull up anything less than or equal to 1500 - I named the variable L1500
    I want one variable to pull up anything between 1500 and 3000 - I named that variable B1500
    I want the last variable to pull up anything equal to or above 3000 - I named that variable A3000
    The name of the drop-down menu is 'lumens'

    So, on the page that the variables I sent to - I need to assign those variables those numbers right? I'm just not sure how to do that. This is what I thought I would need:

    if ($lumens == L1500)
    $lumens<=1500;
    elseif ($lumens == B1500)
    $lumens > 1500 && $lumens <3000;
    elseif ($lumens == A3000)
    $lumens>= 3000;

    But that isn't working - so obviously something is wrong. Can anyone help me with this?
    Thanks!

  • #2
    Welcome here!

    I don't understand why you need the variables ...
    Say your formfield (the dropdowns is name="lumens")

    On the page where you post your page to, you read the selected option and use it as a parameter inside theselect-statement. Like

    PHP Code:
    switch ($_POST['lumen']){
       case 
    1:
          
    $condition 'lumens<=1500';
          break;
       case 
    2:
          
    $condition 'lumens BETWEEN 1500 AND 3000';
          break;
       case 
    3:
          
    $condition 'lumens > 1500';
          break;
       default:
          echo 
    'illegal fieldvalue posted';
    }

    $sql "SELECT var1, var2 FROM table WHERE " $condition ;
    ... 
    I'm assuming that the variable you want to check against in your products table is also called 'lumens'

    But maybe i just understand the problem ...
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

    Comment


    • #3
      I'm not sure I understand where this goes. Does this go on the initial page where the user can select the option from the drop-down menu?

      Yes, the variable/column name I want to check against in my Db is called 'lumens'

      Comment


      • #4
        Unktehi,

        I think raf has got it correct.

        Where his code goes (at least the switch statement) is in place of your if/else statement.

        Then wherever you were going to perform your query (the select statement) on your DB, you should ensure that you include the $condition variable in the where clause. This will limit your query to those selected by the drop down box from your form.

        All this stuff is on the server-side, in the php code. When your form presses the submit button, it should have method="post" and action="someFile.php", and it is this someFile.php that will contain this code.

        One thing to note is that (and I'm not sure about this), $condition may need to be declared above the switch statement. Also, if you have some other values to select from in the drop down you will have to update your switch statement accordingly since you sort of need something in your $condition variable because I don't think you can have an empty where clause.

        Hope that helps,
        Sadiq.

        Comment


        • #5
          Unktehi,
          I'm not sure I understand where this goes. Does this go on the initial page where the user can select the option from the drop-down menu?
          It's sad69 explained and like i said in my initial post:
          "On the page where you post your page to, you read ..."
          You put it at the top of that page if you like
          $sql will then contain the complete sql-stetement that you can execute like
          $result=mysql_query($sql,$link) or die (Queryproblem);

          sad69,
          $condition doesn't need to be declared.
          And if there were other option, then the witch doesn't need cheging since they will then be codered by the default clause. If there are other options, then it's not certain that you will run the update ... There nothing much sensible we can say about it.
          Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

          Comment

          Working...
          X