#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2001
    Location
    England
    Posts
    673
    Rep Power
    14

    PHP/javascript problem and stupid clients!


    Right,

    I spend weeks and weeks coming up with a php enabled site that looks good and works well, with a fully integrated backend allowing for full updateability.

    After the initial 'yes, all that functionality is fine but that small square graphic on the homepage - can it be a slightly different shade of lime green?' stage, the client has now decided that one piece of functionality could be improved.

    It's extremely important that this functionality is improved otherwise no-one will go to his site. In fact it's a matter of near life or death. Although it's not, of course, serious enough to pay me extra to change it, even though I did what he wanted in the first place.

    The problem is this:

    In the database are records. Each record has the fields:

    Region
    County
    Town

    as well as other ones.

    At the moment, on page one, I have the code:

    PHP Code:
    <form action="public2.php" method="post">

    <?PHP

    $db 
    mysql_connect("localhost""root");

    mysql_select_db("pyramid",$db);

    $result mysql_query("SELECT DISTINCT county FROM public where location='OP' or location='VOP' order by county",$db);

    echo 
    "<select name=\"county\">";

    while (
    $myrow mysql_fetch_row($result)) {

    echo 
    "<option>",  $myrow[0], "</option>";

    }
    ?>
    </select>
    <input type="submit">
    </form>
    This searches through the records and displays all unique instances of counties in the drop down box.

    When you click on the submit button you are taken to the next page with the code:

    PHP Code:
    <form action="public3.php" method="post">

    <?php

    $db 
    mysql_connect("localhost""root");

    mysql_select_db("pyramid",$db);

    $result mysql_query("SELECT DISTINCT town FROM public where county='$county' order by town",$db);

    echo 
    "<select name=\"town\" id=\"county\">";

    while (
    $myrow mysql_fetch_row($result)) {

    echo 
    "<option>",  $myrow[0], "</option>";

    }
    ?>
    </select>

    <input type="submit">
    </form>
    Obviously this selects all the towns from the database in the specific county that was chosen on the previous page.

    When you click on this submit you are taken to another page which lists all the branches in that town.

    It all works fine, but the client wants the following:

    On the first page is three drop down boxes, region, county and town. When you chose a region, immediately, the county drop down box changes to reflect all the counties in the region and when you select a county, the town box changes to represent all the towns in that county.

    In other words, apart from the additional box, the only change is that instead of pressing submit, it works with OnChange and changes immediately.

    There are plenty of scripts that do this for client side code - please find one attached from a script site on the net.

    Is it at all possible to change this script to reflect the data being added dynamically to the database or should I give up and become a librarian?

    Many thanks

    James

    Ps. I got my money up front, so all that's at stake here really is pride and the happiness of my client.
    Attached Files
  2. #2
  3. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Location
    Canada
    Posts
    28
    Rep Power
    0
    If you're going to take care of the selections onChange, then all the data will have to be loaded into that first page, instead of in stages.

    The best way I can think to handle this would be to select all the region / county / town data on the first page:

    select * from [table] ;

    then use a couple of loops to arrange the results into a multi-dimensional javascript array. (ie. put the php loop between <script></script> tags and use it to print the array definition).

    $qh = mysql_query($query);
    while($rh = mysql_fetch_array($qh))
    {
    print "select['".$rh['region']."']['".$rh['county']."['".$rh['town']."]";
    }

    now, I have to admit that my javascript's a little rusty, so that might not be exactly right (you may have to do a couple of loops, in stages, regions first, then counties, then towns) but something along those lines should build the correct array which can be used to do the auto-updating select boxes.

    Hope this helps
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2001
    Location
    England
    Posts
    673
    Rep Power
    14
    Looking at it, I don't think this is possible with dynamic data.

    In the Javascript, there is the code:

    group[6][0]=new Option("All","All")
    group[6][1]=new Option("Cheshire","Cheshire")
    group[6][2]=new Option("Gtr. Manchester","Gtr. Manchester")
    group[6][3]=new Option("Lancs","Lancs")

    etc

    The group[6] relates to the 6th element of the first drop down - here it has been hard coded as North West England.

    Then the group[6][0], group[6][1], group[6][2] etc stores the counties in North West England.

    With the dynamic data, North West England won't appear until a record is inserted with North West England in the field, thus it can't be a numbered element like group[6] so this won't work?

    Am I right?

    Cheers

    James
  6. #4
  7. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Location
    Canada
    Posts
    28
    Rep Power
    0
    I think you are right. You may have to get even more creative, like using php to write javascript functions each of which contain a hard-coded array (generated from the database), and then using the onChange to call the appropriate function which will poplulate the next select box with its data.

    Devshed had an article dealing with this a while back, here's the link

    http://www.devshed.com/Server_Side/M...ays/page1.html

    I don't know what method the article reccomends, but I'm sure its worth a look
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2001
    Location
    England
    Posts
    673
    Rep Power
    14
    Thanks - I'll have a look....
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Location
    a northern town
    Posts
    74
    Rep Power
    13
    Hi,

    I have a similar requirement (although not using php) based on changing prices of items based on an offer code which may be entered into an order form. This needs to update itself based on the offer code entered into the form.

    The solution I decided on was to submit() a hidden form with the onChange event of the offer code text box, which gets the offer code and associated details from the db and adjusts the price fields and display information accordingly.

    This method may have too many performance overheads for your system but would allow you to use pretty much what you have at the moment but condensed into one page?

    Getting *all* the region, county and town details in one go with php would be more straight forward, but formatting the javascript could be a headache?

    Hope this helps.

    Regards, Ed.
    /* measure twice, cut once */
  12. #7
  13. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2001
    Location
    England
    Posts
    967
    Rep Power
    14
    I have a system that does this kind of thing.. I don't use any queries for the form dropdown generation - instead, I have a perl file running from a cron job, which selects all of the data out of the db every so often and writes all of the data to be populated into the dropdowns to a .src file. Works quite well.

IMN logo majestic logo threadwatch logo seochat tools logo