Connecting Tech Pros Worldwide Help | Site Map

PHP / MY SQL design problem...

Newbie
 
Join Date: Oct 2009
Posts: 2
#1: Oct 2 '09
Hello,
I'm kinda newbie on all this and I hate databases (lol)... but I'm trying to figure out the easiest way to save on a database the following form:

-----------------------------------------------
| Country | Service#1 | Service#2 | Service#3 |
-----------------------------------------------
|Canada | Yes | Yes | No |
-----------------------------------------------
|USA | Yes | No | No |
-----------------------------------------------
|Spain | No | Yes | No |
-----------------------------------------------
|Brazil | Yes | Yes | Yes |
-----------------------------------------------

The thing here is that the user is gonna be able to mark the countries in which it provides services and it has to indicate which services provides there...
The options of services is the same for all the users as well as the countries, the user just have to select the country and the services, therefore user 1 and user 2 may o may not have the same services on the same country.
Don't forget that all the countries of the world are listed on the page, therefore we're talking about 150 check boxes for all the countries plus 4 or 5 more per country for the services....
The idea is taht later on we I can serch either by service, by country or by user, or a combination of one or more values, but I have not idea how to make it easy to save all this on a database without writing a book full of if's lol...
Thanks in advance.,..
TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 913
#2: Oct 2 '09

re: PHP / MY SQL design problem...


Databases are your friend.
One option is to have about 750 columns
---------------------------------------------------------------------------------
| User | AU1 | AU2 | AU3 | AU4 | AU5 | US1 | US2 | US3 | US4 ...
---------------------------------------------------------------------------------

Which is probably inefficient, but will get you one table with all values.

Another option is to store choices as text and process later (NULL if country not selected, or the services separated by commas if country selected):
---------------------------------------------------------------------------------
| User | AU | US | ...
| 1 | 1,0,0,1,0 | 0,1,0,1,1 | ...
| 2 | NULL | 0,1,0,1,0 | ...
---------------------------------------------------------------------------------
That will require you to separate the strings pulled from the DB when you do want to find some particular choice which could be too much effort, making the former choice more attractive.

How many users are you expecting?

And welcome to Bytes ;)
Newbie
 
Join Date: Oct 2009
Posts: 2
#3: Oct 2 '09

re: PHP / MY SQL design problem...


Thanks TheServant,
I understand what you saying in both cases, but I'm expecting (hopefully) around 500 / 750 users if not more... now, I can probably go with option 2, after some tweaking...
Now the question is pretty much how do I pass those values from my form to a database... the code is gonna be a book pretty much if we take into considetaion that we have 30+ countries per form and 4 or 5 services per country.... you know what I mean ?...

How do I tell the data base that the user click or check US / AU / NZ / GB / CA / BR / UY / AR and PY and some fo the services for each individual country and not MX / CL / SP / CH that were in between those other countries in the form ?....
Am I complicating things ?... I guess the database part I ahve it clear, but is just the logic in between my form and the database that is killing me at this point
TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 913
#4: Oct 2 '09

re: PHP / MY SQL design problem...


Well I think that you need to use arrays with loops.
So to make the form, you need your array with all your countries:
Expand|Select|Wrap|Line Numbers
  1. $countries = array ( 'US', 'AU', 'NZ', ... );
Then you need to use a loop in a table like:
Expand|Select|Wrap|Line Numbers
  1. <form name="myform" method="post" action"...">
  2. <table>
  3. <?php
  4. foreach( $contries as $country ) {
  5. $co = $country; // eg. AU
  6. $s1 = $country . "1"; // eg. AU1
  7. $s2 = $country . "2"; // eg. AU2
  8. $s3 = $country . "3";
  9. $s4 = $country . "4";
  10. echo("
  11. <tr>
  12. <td><input type="checkbox" name="$co" /></td>
  13. <td><input type="checkbox" name="$s1" /></td>
  14. <td><input type="checkbox" name="$s2" /></td>
  15. <td><input type="checkbox" name="$s3" /></td>
  16. <td><input type="checkbox" name="$s4" /></td>
  17. </tr>
  18. ");
  19. }
  20. ?>
  21. </table>
  22. </form>
  23.  
Now when that sends it will send it all in a $_POST[] array which you can cycle through in a similar fashion to make a nice long insert command.

Expand|Select|Wrap|Line Numbers
  1. $query_string = "INSERT INTO mytable(";
  2. foreach($countries as $country) {
  3. $co = $country; // eg. AU
  4. $s1 = $country . "1"; // eg. AU1
  5. $s2 = $country . "2"; // eg. AU2
  6. $s3 = $country . "3";
  7. $s4 = $country . "4";
  8.  
  9. $query_string .= "$co, $s1, $s2, $s3, $s4,";
  10. }
  11. /* Need to remove last comma from string */
  12.  
  13. $query_string .= " VALUES ";
  14. foreach($countries as $country) {
  15. $co = $_POST[$country]; // eg. AU
  16. $s1 = $_POST[$country . "1"]; // eg. AU1
  17. $s2 = $_POST[$country . "2"]; // eg. AU2
  18. $s3 = $_POST[$country . "3"];
  19. $s4 = $_POST[$country . "4"];
  20.  
  21. $query_string .= "$co, $s1, $s2, $s3, $s4,";
  22. }
  23. /* Need to remove last comma from string */
  24.  
  25. mysql_query($query_string);
  26.  
Now that is *extremely messy and there is probably a better way to do it, but it will give you some ideas on the logic atleast. I don't know if that works, but look at the logic of building a statement using loops to save you coding! This way, if you ever wanted to add a country, you need to add a column to your database, and change the $countries variable only. Check this thread as others might have quicker/neater ideas.
Reply