Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

php mysql help needed

  • 28-05-2009 3:06pm
    #1
    Registered Users, Registered Users 2 Posts: 5,473 ✭✭✭


    Hi all, hoping someone can help me out, have chacked around the web a bit but can't seem to find what I am looking for..

    A month a go I decided to learn php mysql and apache.... just something I always wanted to do.... I am a complete novice at this and have been using plenty of books. the best way I thought was to create a website about cars, so thats my goal...
    anyway, I have some mysql databases created and have entered some test data, at the moment I am creating a php script for a form, the form will have two drop down menu items, I have created the first drop down menu but am struggling to create the second one, I want teh second drop down menu to be dependant on the first drop down menu... e.g. some one selects "BMW" in the first menu then I want the second menu to show only the bmw data. Thats my problem any help you can offer would be much appreciated..

    ok.. my databases for this are master_makes (contains primary ID field and column called "master_make" which contains all the manufacturer names) and a second database called "car_makes" ( contains three columns, first an id field, second contains manufacturer name "car_make"and third then contains model name "car_model")..

    here's my php code so far... the first half of it works and produces the right dropdown menu... but the second?????? I am a bit lost...


    <?php
    include("dbconnect.php");
    doDB();



    //get make

    $get_make_sql = "select master_make from master_makes order by master_make";
    $get_make_res = mysqli_query($mysqli, $get_make_sql)
    or die (mysqli_error($mysqli));

    if (mysqli_num_rows($get_make_res) > 0) {
    $display_block .= "<p><strong>Make:</strong><br/>
    <select name=\"sel_master_make\">";

    while ($make = mysqli_fetch_array($get_make_res)) {
    $car_make = $make;
    $display_block .= "<option value=\"".$car_make."\">".
    $car_make."</option>";
    }
    $display_block .= "</select>";
    }

    //get model
    $get_model_sql = "select car_model from car_makes where car_make = $car_make order by car_make";
    $get_model_res = mysqli_query($mysqli, $get_make_sql)
    or die (mysqli_error($mysqli));

    if (mysqli_num_rows($get_make_res) > 0) {
    $display_block .= "<p><strong>Model:</strong><br/>
    <select name=\"sel_car_model\">";

    while ($make = mysqli_fetch_array($get_make_res)) {
    $car_model = $model;
    $display_block .= "<option value=\"".$car_model."\">".
    $car_model."</option>";
    }
    $display_block .= "</select>";
    }


Comments

  • Registered Users, Registered Users 2 Posts: 342 ✭✭adm


    I dont know if you are posting the form correctly but in any case you probably just need to enclose
    the car_make value in single quotes

    orig:
    $get_model_sql = "select car_model from car_makes where car_make = $car_make order by car_make";

    with quotes:
    $get_model_sql = "select car_model from car_makes where car_make = '$car_make' order by car_make";


  • Registered Users, Registered Users 2 Posts: 5,473 ✭✭✭robtri


    adm wrote: »
    I dont know if you are posting the form correctly but in any case you probably just need to enclose
    the car_make value in single quotes

    orig:
    $get_model_sql = "select car_model from car_makes where car_make = $car_make order by car_make";

    with quotes:
    $get_model_sql = "select car_model from car_makes where car_make = '$car_make' order by car_make";

    Thanks for the reply, tried that out and the result is still the same, a blank second box..... its not recognising the variable $car_make as the selected drop down in the first block


  • Registered Users, Registered Users 2 Posts: 1,771 ✭✭✭jebuz


    It's not recognizing the $car_make variable because the $car_make variable is only defined within the scope of your while loop, variables defined within for/if/while blocks are local only to that block unless defined elsewhere.

    You need to understand a little more about the dynamics of php development, simply passing in the $car_make variable does not mean that everytime somebody selects something from the first drop down, the second drop down will update. You need to think about what you want and implement the functionality yourself, it's not magic. I know you're quite new at this but you should brush up on your javascript if you can, it can helps a lot with PHP development, I'll show you how.

    Here is a simple approach I would take...

    Leave the first drop down as it is, and the second drop down should be created empty, it will be waiting for somebody to choose an option in the first drop down.

    Now add a javascript listener to the first drop down (btw use single quotes when constructing the $display_block var, you don't need to escape the double quotes then).
    <select name="sel_car_model" onSelect="loadCars(this.options[this.selectedIndex].value)">;
    

    This means that when somebody selects an option from the drop down, it will call a javascript function called loadCars, and will pass in the value from the drop down menu.

    define the loadCars function in your <head></head> section of the page as follows:
    <script type="text/javascript">
      function loadCars(_make) {
        window.location = "yourfile.php?make=" + _make;
      }
    </script>
    

    All this function does is basically reload the page, but passes the make value in the URL.

    So now at the start of the php file, you need to check if there is a make variable being passed to the page, if there is we can populate the second drop down, if not, do nothing. Place this somewhere at the top in php tags:
    $make = '';
    
    // check to see 'make' is passed to us
    if(isset($_GET['make']) ) {
      $make = $_GET['make']
    }
    

    so finally, we need to do a little check before we write out the second drop down box: If the make variable is set, pass the make into the sql, otherwise just output an empty drop down.
    if($make != '') {
      $get_model_sql = "select car_model from car_makes where car_make ='" . $make . "' order by car_make";
     .... rest of your code here to output select
    }
    else {
      ...write out a blank select with custom option e.g "please select a make above"
    }
    

    That should get your started anyway! Good luck ;)


  • Registered Users, Registered Users 2 Posts: 5,473 ✭✭✭robtri


    Much appreciate the advice... all i can say is crap...another language to undersand... Javascript :)
    will give it a go later tonight when at home


Advertisement