Jump to content

Recommended Posts

Hi I'm not sure this problem can be solved by pure PHP and HTML but here goes.

 

I'm creating an online database app for a dog grooming business. The problem I'm trying to solve at the moment is this:

  • Each booking is linked to one customer
  • Each customer may have more than one dog
  • The customer may bring more than one dog to be groomed

 

The problem I'm having is how to get this information into the database. I've spent quite a lot of time on the design side of the database and I'm confident it's structure is a workable one.

 

At the moment the user can select a customer from a drop down list. The ID of this customer gets put into the booking table.

 

Along with this the time, date etc all get put in. Easy. However I'm not sure how I can let the user select which dogs are linked to the booking.

 

What I'm looking for is:

 

  • The user selects a customer
  • A list of the customers dogs is pulled from the MySQL database
  • The user can select as many dogs as they like from this list

 

Any help is greatly appreciated

Link to comment
https://forums.phpfreaks.com/topic/47450-dynamic-options/
Share on other sites

you don't need ajax. this is an example of something similiar i did for a client of mine. i basically built a bridal registry and integrated it with an existing 3rd party shopping cart. please forgive it's simple design, it was the first thing i ever coded in php. i hope the example helps.

<?php

        echo "<TABLE BORDER=\"0\"><TR>\n";
        echo "<TD align=\"left\" valign=\"bottom\">";

        echo "<form action=\"". $_SERVER['php_self'] ."?regID=". $regID ."\" method=\"post\">\n";
        echo "<SELECT NAME=\"categories\">\n";
        echo "<OPTION VALUE=\"\">Choose a Category\n";

        /*query category info*/
        $cat_sql = mysql_query("SELECT * FROM categories") or die(mysql_error());

        /*populate dropdown menu from the shopping cart product table*/
        while($cat_row = mysql_fetch_array($cat_sql)){
                echo "<OPTION VALUE=\"". $cat_row['id'] ."\"". (($_POST['categories'] == $cat_row['id']) ? (" SELECTED") : ("")) .">". $cat_row['name']. "\n";
        }

        echo "</SELECT>\n";

        echo "<input type=\"submit\" value=\">>\">\n";
        echo "</form>\n";
        echo "</TD>";

       /*
        *if a category has been chosen,
        *use this switch statement to populate
        *the item list that falls under the
        *chosen category
        */
        if(isset($_POST['categories']) && !empty($_POST['categories'])){
                switch($_POST['categories']){
                        case $_POST['categories']:
                                /*get the items that are in that category*/
                                $item_sql = mysql_query("SELECT id, name, cat FROM items WHERE cat = ". $_POST['categories'] ."") OR die(mysql_error());

                                echo "<TD align=\"left\" valign=\"bottom\">";
                                echo "<form action=\"addItem.php?regID=". $_GET['regID'] ."\" method=\"post\">\n";
                                echo "<SELECT NAME=\"items\">\n";
                                echo "<OPTION VALUE=\"\">Select an Item\n";

                                /*populate the matching category's item list here*/
                                while($item_row = mysql_fetch_array($item_sql)){
                                        echo "<OPTION VALUE=\"". $item_row['id'] ."\"". (($_POST['items'] == $item_row['id']) ? (" SELECTED") : ("")) .">". $item_row['name'] ."\n";
                                }
                                echo "</SELECT>\n";
                                echo "<input type=\"hidden\" name=\"categories\" value=\"". $_POST['categories'] ."\">\n";
                                echo "<input type=\"submit\" value=\">>\"><br />\n";
                                echo "</form>\n";
                                echo "</TD>";

                                break;

                        default:
                                break;
                }
        }
       /*
        *the $_POST['item'] value is set within the switch statement.
        *none of this below is displayed without the user first
        *selecting a category, then through the switch statement
        *selecting an item within that category.
        *if an item isn't set, don't display anything.
        *if an item has been selected, show the form for
        *quantity requested, and still needs
        */
        if(isset($_POST['items']) && !empty($_POST['items'])){
                echo "<form action=\"". $_SERVER['php_self'] ."?regID=". $regID ."\" method=\"post\">";
                echo "<TD align=\"right\" valign=\"bottom\">\n";
                echo "<B>Quantity Requested:</B> ";
                echo "<input type=\"text\" name=\"qty_req\" size=\"3\"><br />\n";
                echo "<B>Still Needs:</B> ";
                echo "<input type=\"test\" name=\"still_needs\" size=\"3\">\n";
                echo "</TD></TR>";
                echo "<TR><TD COLSPAN=\"3\" align=\"right\">\n";
                echo "<input type=\"submit\" value=\"Submit\"></TD></TR></TABLE>\n";
                echo "<input type=\"hidden\" name=\"categories\" value=\"". $_POST['categories'] ."\">\n";
                echo "<input type=\"hidden\" name=\"items\" value=\"". $_POST['items'] ."\">\n";
                echo "</form>\n";
        }
         echo"</TD></TR></TABLE>\n";

Link to comment
https://forums.phpfreaks.com/topic/47450-dynamic-options/#findComment-231539
Share on other sites

use AJAX

I have considered this but tbh I have used very little AJAX and would not know where to start with this. Plus I like to handle things server side if possible.

you don't need ajax. this is an example of something similiar i did for a client of mine. i basically built a bridal registry and integrated it with an existing 3rd party shopping cart. please forgive it's simple design, it was the first thing i ever coded in php. i hope the example helps.

 

 

This looks like what I'm trying to achieve. Only difference is I'd need to use something like a 'select multiple' instead of a select for the second set of options. I'm going to look into doing it this way.

Link to comment
https://forums.phpfreaks.com/topic/47450-dynamic-options/#findComment-231566
Share on other sites

It will be server side handling with ajax, its the same just a smoother update to the page

 

I understand it would be server side to an extent. It still means some client side handeling. For instance what if the user had Javascript disabled. I'm not sure if this is the right stance to have but my personal preference is for maximum compability. If write some PHP code and this does the processing, I know that this will work with every browser and with users with javascript disabled. Although as you say, the smoothness is lost.

 

This is why I feel the PHP route better.

Link to comment
https://forums.phpfreaks.com/topic/47450-dynamic-options/#findComment-231585
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.