PHP 200+ Form Fields - php

Just to give you a bit of background i have a system where there's certain calculations that need to be calculated in the main section, there'll be various form fields under different tabs, for simplicity sake i have 10 sections, all with 20 drop down boxes and when you change the value of one of the drop downs it's recalculated in the main area (various calculations etc.) now these form values need to be persisted and stored in a database and then saved and reloaded when ever needed.
Logically i don't want the database to return 200records and then set the drop down boxes for them all manually (using maybe a switch statement) as that'll no doubt be a ball-ache.
Any suggestions?

Could you pull the records one or two select boxes ahead via ajax? Ie.
select1 <-- filled
select2 <-- filled
select3 <-- unfilled
select4 <-- unfilled
Once select1 is selected, make an ajax call that fills select 3.

If they are all in 10 sections, you could write queries that only return the 20 values or so you need. Or you could possibly break them into multiple tables, and use a couple relational tables to keep them straight. As far as the calculations, just store those in the database as well, and use ajax or the post submits to recalculate and update them when necessary.

Related

How to store searchable arrays in MySQL

So I've got this form with an array of checkboxes to search for an event. When you create an event, you choose one or more of the checkboxes and then the event gets created with these "attributes". What is the best way to store it in a MySQL database if I want to filter results when searching for these events? Would creating several columns with boolean values be the best way? Or possibly a new table with the checkbox values only?
I'm pretty sure selializing is out of the question because I wouldn't be able to query the selialized string for whether the checkbox was ticked or not, right?
Thanks
You can use the set datatype or a separate table that you join. Either will work.
I would not do a bunch of columns though.
You can search the set easily using FIND_IN_SET(), but it's not indexed, so it depends on how many rows you expect (up to a few thousand is probably OK - it's a very fast search).
The normal solution is a separate table with one column being the ID of the event, and the second column being the attribute using the enum datatype (don't use text, it's slower).
create separate columns or you can store them all in one column using bit mask
One way would be to create a new table with a column for each checkbox, as already described by others. I'll not add to that.
However, another way is to use a bitmask. You have just one column myCheckboxes and store the values as an int. Then in the code you have constants or another appropriate way to store the correlation between each checkbox and it's bit. I.e.:
CHECKBOX_ONE 1
CHECKBOX_TWO 2
CHECKBOX_THREE 4
CHECKBOX_FOUR 8
...
CHECKBOX_NINE 256
Remember to always use the next power of two for new values, otherwise you'll get values that overlap.
So, if the first two checkboxes have been checked you should have 3 as the value of myCheckboxes for that row. If you have ONE and FOUR checked you'd have 9 as the values of myCheckboxes, etc. When you want to see which rows have say checkboxes ONE, THREE and NINE checked your query would be like:
SELECT * FROM myTable where myCheckboxes & 1 AND myCheckboxes & 4 AND myCheckboxes & 256;
This query will return only rows having all this checkboxes marked as checked.
You should also use bitwise operations when storing and reading the data.
This is a very efficient way when it comes to speed. You have just a single column, probably just a smallint, and your searches are pretty fast. This can make a big difference if you have several different collections of checkboxes that you want to store and search trough. However, this makes the values harder to understand. If you see the value 261 in the DB it'll not be easy for a human to immeditely see that this means checkboxes ONE, THREE and NINE have been checked whereas it is much easier for a human seeing separate columns for each checkbox. This normally is not an issue, cause humans don't need to manually poke the database, but it's something worth mentioning.
From the coding perspective it's not much of a difference, but you'll have to be careful not to corrupt the values, cause it's not that hard to mess up a single int, it's magnitudes easier than screwing the data than when it's stored in different columns. So test carefully when adding new stuff. All that said, the speed and low memory benefits can be very big if you have a ton of different collections.

Keeping track of dynamically generated form elements and passing them as POST variables

I have a page that retrieves records from 1 table in a database when a certain condition is met.
What I want to achieve is to provide the use to with an opportunity to update each record displayed using text boxes.
I am having trouble interpreting what logic to proceed with after the user hits the 'submit' button.
Normally, if I'm updating one record (or a static number of records), I will use the apporpriate amount of SQL statements.
Since the amount of records are dynamically generated, what is the best way to update all at once? How would I know which records were retrieved in the first place to update?
FOR EXAMPLE:
OK, We have a table with student ids (ID), names (SNAME), subjects (SUBJ), grade for each subject (GRADE) and general remarks (COMMENTS).
I want to retrieve information about all students that got an 'A', and write UNIQUE congratulatory remarks for each student (such as 'good job', or 'congratulations', or etc.)
I'd retrieve the records and lay them out on the page, with a text box next to each student record for the comments to be entered. Because I don't know how many text boxes to make, I give the text boxes dynamically generated names using the student ID. The user now enters unique comments for each student, and clicks on submit.
Now, how am I supposed to update these records with the values entered in each text box?
I wouldn't know which students were retrieved in the first place - how would I know what names to use? I'm trying to avoid having to execute the query again after submitting - but is there any other way?
Hope this question was not too confusing.
thanks
Further expanding earlier answers:
You need a loop (e.g. foreach) to display and save the textareas. If the names of the textareas include the students ID, you don't need to know the name, because the text is inserted to the database by the primary key (the students ID). You may name your form-elements as array, to iterate over them, for example (where the numbers are the IDs):
<textarea name="comment[2345234]"></textarea>
<textarea name="comment[8957485]"></textarea>
Read it out as described by #evan:
foreach((array)$_POST['comment'] as $studentId => $studentComment)
{
var_dump($studentId, $studentComment);
}
And if you implement this whole thing as self-requesting form (Affenformular in german), you may also use just one single loop to save and output the textareas.
"I don't think you're understanding what I'm trying to ask." Maybe you don't understand the answers, even you stated it. You don't need a students name to save a database record. But if you really want to submit it, you may also use hidden inputs.
Use foreach() to find the values you care about, put them in an array, and process the array.
Expanding on #Ignacio's answer to make it more easily understandable:
foreach($_POST as $name_of_input => $value_of_input)
{
// do stuff - here is something so you can see the results after the submit
echo "$name_of_input :: $value_of_input <br>";
}

How do I use PHP, JS and HTML to appropriately handle many-to-many relationship?

Thank you in advance for your help!
I have a large and complicated form with several many-to-many relationships that I'm struggling to deal with. I have, after A LOT of encouragement, created the two appropriate tables for each of those relationships (e.g. one table cars filled with carID's and carName's, and an intersecting table userCars that stores userID's and carID's), no matter the size.
My question is: should my form HTML always be generated by PHP based on those tables? For example, should I have PHP check the cars table and generate one check-box for each row in the table, even though I'll only be starting with five? Is there a better way of doing this besides calling a PHP function onLoad and having that function call a javaScript function?
Also, after the user has submitted the form, how do I store those values? Do I need to go through each POST variable name and compare them to each carName in the table cars in order to get the carID associated with that name, and then make an entry in the userCars table? Or should I just say "if Volvo, carID = 4" because I only have five cars right now!
Again thank you for all of your advice! I would have been totally stuck several times without your generosity!
I agree with Marek that as soon as a php application becomes a litlle bit complicated you are better off with a framework. Preferably one like yii which offers a many-to-many ORM.
That said one of the mistakes a lot of coders make early on is to make a form too complicated.
A good question to ask yourself is whether this form is trying to do too much. It's hard to make a judgement without seeing a link to the actual form.
One way to tackle a problem like this would be to use javascript (jquery:ajax) to update the join table as the user selects a car so that when you submit the form you are only updating the values of the main table.
So a form might look like
Your Name __
Your Age __
Your Favourite Colour __
Cars You have owned
Volvo _ Ford _ Fiat_ Mercedes _
{... these values could dynamically created from an options table ...}
{using jquery each value selected/unselected sends an ajax post to update your user_cars table}
When form is submitted only the user table is updated
However you could also loop through the values of $_POST['cars'][] to update the user_cars table here as well. Which is probably the better solution.
You should make your HTML generated by your database values, or you should make some Javascript that fetches the values from the database, and sets the value in the page based upon those values. Secondarily, if you set the IDs of the form elements to the IDs of the associated entries in the database, then you should just be able to update the database from the set of IDs that were included in the POST.

Checkbox filter using Mysql and PHP

In my application there is a filter option with checkboxes.Checkboxes indicates top five categories and all other categories under another single checkbox.That means total I have six checkboxes(top five + other).When all checked which will fetch all values from database table and when all unchecked which never fetch any values from table.Please anybody give any suggestions for my unchecked case...What will be the condition there?
In the unchecked case i wouldn't do a mysql query - cause it is not necessary and you can save that time. Depending on your application and your code you need to implement a switch here and insert the result on your page as if no result (or entry) has been found. For further details more application code is needed.

PHP - Pulling single value from multidimensional array

this is probably quite simple but I could do with some help.
I am trying to create a small PHP function that will display a single value form a multidimensional array when the user used two dropdown boxes to select the row and column of the array.
So, the user will make a selection from the first dropdown box, which will contain the titles of the rows, and then make a selection from a second dropdown box, which will contain the titles of the columns. Once the selections have been made, the function then needs to output the value for the specific row and column selected.
I thought I had created an array that would work but, sadly no. I have 6 rows and 6 columns in my data table.
Also, is there a JQuery or Javascript alternative?
Just looking for a few pointers to get me on my way.
Thanks in advance,
Micanio
You could either do this on the server-side or through JS.
JS: Have the script update a hidden form field with the value using the onChange() event of the drop downs. Then simply grab that hidden field when the form is posted back to the server (of source always checking for valid data).
PHP: The form will provide the two values $_POST['field1'] and $_POST['field2'] (which of course you will sanitize before using). The script could define a multidimensional array that you could feed those two values into:
$finalValue = $mdArray[$SanitizedField1][$SanitizedField2];
From there just store the $finalValue however you'd like.
$data = array();
for ($i=0;$i<6;$i++) {
$data[$i] = array();
for ($j=0;$j<6;$j++)
$data[$i][$j] = rand(0,100);
}
This should create an array similar to what you have described.
You can then access it like so...
echo $data[0][3];
Your form would need two select fields, both will values 0-5 then you can take the both of them and use them to access a value in your array.
If I understand your question correctly, you need something like a user selects a drop down list. Once selected, the option populates a second list. A real world example would be a user selects on Country to fill in a form and it will populates the States drop down list.
This kind of functionality is usually done with javascript not server side php.
http://javascript.internet.com/forms/country-state-drop-down.html

Categories