cakephp, how to save data in two tables? - php

i have a simple form with two input fields and i would like to save the data from one field inside one table and the other one inside the second table.
to save the data i use $this->Room->save($this->data)
any ideas how this can be done?
thanks
edit:
one is rooms the other one is roomates the common key i want to use is is id_rooms and id_roomates

For Room build an array like below. It is just an example, set it according to your actual fields.
$this->data['Room']['id_rooms'] = $this->data['Room']['id']
$this->data['Room']['abc'] = $this->data['Room']['xyz']
Then save data to room table: $this->Room->save($this->data).
Next build an array for second table, say Roomtitle, as below:
$this->data['Roomtitle']['id_rooms'] = $this->data['Room']['id']
$this->data['Roomtitle']['abc'] = $this->data['Room']['xyz']
and save it: $this->Roomtitle->save($this->data).

Related

PHP Form: Want to have a drop down selection with two values that post to different fields in the form

Example, I want the drop down value to be 1278|Toy Name
I would like to then separate via the | and spost the id "1278" in an id field
and "Toy 2" in a toy field.
I am actually using FormTools (formtools.org) for all of my forms and this is one thing I just can not get my head around.
A little background: This is for a client and needs to be this way. they need the ID submitted as well as the Toy Name into separate Db tables.
Any help would be greatly appreciated.
You could split the submitted value based on the | and use the retrieved values to insert into the database.
$toyInput = $_POST["toyInput"];
$toySplit = explode("|", $toyInput);
$toyID = $toySplit[0];
$toyName = $toySplit[1];
// Now insert $toyID and $toyName into your DB in separate fields.
Be aware that this has no validation, a user could easily put fake ID's, names, or a value with no "|" and cause your script to error out. Be sure to add extra checks for these cases!
You can also use list that assign values to a list of variables.
$toyInput = $_POST["toyInput"];
list($toyID, $toyName) = explode("|", $toyInput);

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 to Store Multi Select Box in php?

am having select box with multiple options, like option1,option2,option3,option4,option5.Now i want to select more than one option.
For Example: Select option1,option5,option3.and click submit.after submitting the select box need to show the selected items.
in which format to be stored in mysql and what's the exact coding to do that?please help me to get rid of this problem...
You could serialize the data in and out.
Use name[] as your name which results in an array in PHP.
$_POST['name'];
serialize($name);
//now insert into DB
Do the opposite to retrieve:
unserialize($name);
But to be honest you are better off looking at more granular methods of storing the data, rather than trying to stuff it all into one field in a database. You could take the array and loop through it storing each choice in a separate row or field.
foreach($name as $value){
//insert $value into db
}
use implode
$var = implode ( "|", $_POST['selectname']);
Try this:
http://www.aleixcortadellas.com/main/2009/03/20/492/

store more than one value into a db field to be retrieved as separate values

Is it possible to store more than one value and query the database and assign each value as a separate entity?
For example, I have a database with a column wallpaper can I store say this into it.
wall_1.png, wall_2.png, wall_3.png
and then query the database and pull out 3 different variables that hold the wallpaper name.
so I'd have $defaultWall = "wall_1.png" $wall2 = "wall_2.png" $wall3 = "wall_3.png" <-- being pulled from the database.
also I have a table called settings, the user can set all their settings here. One column is for their wallpaper preference for their profile. I would like for them to have a series of uploaded wallpapers on hand if they choose to change later on. So by doing this I wanted to store an array of wallpaper urls into the db and have a tiny image preview gallery pull each wallpaper for processing and resizing from the database.
Yes, you can. But more than one value in table field doesn't satisfy to the Normalization Forms.
Try to create one table with wallpapers for this row.
The correct way, from DDL logic, is to define another table for wallpaper images, which have foreign keys that link them to the row of that table which has the Wallpaper column in your case.
If you don't find any reason to do so, and you ARE using only PHP, use serialize and unserialize php functions to access that data from the field. You can store any data structures that way.
Take a look at the expand() function in PHP!
http://www.php.net/manual/en/function.explode.php
Note
I don't think this is the best way to handle such values, if you want to be able to link any amount of wallpapers (or none) to an object I would choose to use another table.
create table tblMain (ItemId, Name, etc)
create table tblWallpapers (WallpaperId, WallpaperName)
create table tblMainUsesWallpapers (Wallpaperid, ItemId)
then just select the values like so:
select * from tblWallpapers w
inner join tblMainUsesWallpapers muw
on w.WallpaperId = muw.WallpaperId
inner join tblMain m
on muw.ItemId = m.ItemId
where m.ItemId = x

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