By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,215 Members | 1,934 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,215 IT Pros & Developers. It's quick & easy.

maintaining relation while POSTing and while fetching.

rpnew
100+
P: 188
Hello friends....
I'm developing one project in PHP-MySql on FC4.....
I'm facing a problem here at one place. Let me describe it....

I've one page in php on which i'm generating a table containing checkboxes for option. These options have suboptions. User need to chose these options and suboptions. with suboption they are getting textboxes in which they can enter values for suboptons. When they submit the form i need to get all these data and accordingly store them into the database.
Now problem comes because all these checkboxes and textboxes are generated dynamically meaning through values in database. So i've made an arrays for all these so that i can get the values of selected checkboxes and textboxes. So the problem is on the second page i cant determine which suboption and textbox value goes to which option means relation is broken i need to maintain this relation so that i can store these values in database maintaining foreign key..

Now let me describe my database a bit so that if you have any other option then this you can suggest me.

In database i've four tables say table1,table2,table3 and table4.

Table1 contains name of OPTION and NUMBER of SUBOPTION it has.
Table2 contains name of SUBOPTIONS and OPTIONS they belongs(foreign key)

Table3 is similar to Table1 and Table4 is similar to Table2(with one field added which will store the value of SUBOPTION coming through TEXTBOXES which user is providing at runtime.)

So what i'm doing is getting the OPTIONS and SUBOPTIONS values from Table1 and 2 respectively and generating form. So when user submits the form i want to store these values into Table3 and 4 respectively.

I told you what i'm doing. so if you can help with that its good and if you have any other idea with GUI as well let me know.
Sep 3 '07 #1
Share this Question
Share on Google+
16 Replies


code green
Expert 100+
P: 1,726
This is a plan, not a problem.
I have to guess your problem is you don't know how to read form values.
Well you use the $_POST array to extract data from a form
Sep 3 '07 #2

rpnew
100+
P: 188
This is a plan, not a problem.
I have to guess your problem is you don't know how to read form values.
Well you use the $_POST array to extract data from a form

Yes you are right that its a plan and
you are wrong a bit that i dont know how to read form values...
well nope..
i know how to use $_POST or $_GET i'm doing that also but my problem is on the second page i'm getting all the values through three different arrays.. and i cant determine relationship between them so that while inserting into the table i can maintain foreign key relation...
And i'm sending and receiving this values as Arrays because all these CHECKBOXES and TEXTBOXES are generated through code according to database values so they'll have same name so to receive all the checked values i've to use Arrays.......

Well if you have any other way to do this let me know.. that will be very helpful

Regards,
RP
Sep 3 '07 #3

rpnew
100+
P: 188
hello friends please help.. im stuck here at this place very badly...


RP
Sep 4 '07 #4

pbmods
Expert 5K+
P: 5,821
Heya, RP.

What do you want your code to do? Give an example.
What is your code doing that you don't want it to do? Give an example.
What is your code *not* doing that it is supposed to? Give an example.
Sep 4 '07 #5

Atli
Expert 5K+
P: 5,058
Hi.

It would also help if you posted the code that generates the form on the first page.
Sep 4 '07 #6

rpnew
100+
P: 188
Heya, RP.

What do you want your code to do? Give an example.
What is your code doing that you don't want it to do? Give an example.
What is your code *not* doing that it is supposed to? Give an example.

What do i've in database?

As i told you earlier i've total of 4 tables...............
say in table1 two columns are there name and noofvariable
table two has name(foreign key to table1), variablename

table3 is same as table1
table4 is same as table2 with one column added that is variablevalue......


now what i'm doing?


from table1 and table2 i'm generating one form which contains checkboxes and textboxes......
like checkboxes for "name" its respective "variablename"(variables) and textboxes for these "variablename" so that user can put values in that.....

what i want.... when user submits i want... whatever the name and variable checked store them to table3 and table4 accordingly..... also if user have provided values in textbox store them into table4...........

problem i'm facing...

As all these fields are generated dynamically means through code.... so i cant provide different names to checkboxes....so they are an array.... like one array for 'name' checkbox one for 'variablename' checkbox and one for 'textfield'....

well through this i can receive all the checked variables and textvalues entered on second page but on second page i cant determine which 'variablename' goes to which 'name'.....

Regarding code
well on first page i'm simply using while loop to generate the form according to database and on second to get the values i'm using foreach loop to access arrays.... so even i want to know that if there is another way to access the array...
i've tried many things so i dont have a exact code now......and frankly i dont have any idea if at all this can be done as on second page you dont which variables are coming and how many(if using different names for all) as there can be as many as 100.... depending upon users selection......
Sep 4 '07 #7

code green
Expert 100+
P: 1,726
As all these fields are generated dynamically means through code.... so i cant provide different names to checkboxes
If the checkboxes are generated within a loop [PHP]for($c=0; etc...)[/PHP]Then you can create a name for the checbox dynamically
[PHP]echo '<input type="checkbox" name="mybox'.$c.'"id="'.$c.'">Choice '.$c;[/PHP] If this idea does not suit then it is possible to send an array as a url variable.
It needs to be declared as such in HTML.
Can't remember how but it is simple
Sep 5 '07 #8

rpnew
100+
P: 188
If the checkboxes are generated within a loop [PHP]for($c=0; etc...)[/PHP]Then you can create a name for the checbox dynamically
[PHP]echo '<input type="checkbox" name="mybox'.$c.'"id="'.$c.'">Choice '.$c;[/PHP] If this idea does not suit then it is possible to send an array as a url variable.
It needs to be declared as such in HTML.
Can't remember how but it is simple
well yes i'm doing that... but my problem is to maintain relation between two arrays to store it into the tables..

however.. dont worry.. i've solved my problem at last.....

thanks for the reply.....

RP
Sep 5 '07 #9

code green
Expert 100+
P: 1,726
Glad you solved your problem.
but my problem is to maintain relation between two arrays to store it into the tables..
What about a 2d array?
Sep 5 '07 #10

rpnew
100+
P: 188
Glad you solved your problem.
What about a 2d array?
Well i've three arrays actually to manage.. to maintain their relation what i've done is....
The first one is aa array which contains values of CHECKBOXES..
The second one is also an array of CHECKBOXES(values of checkboxes)
The third is an array of TEXTBOXES..

Now i've given the name of the TEXTBOXES as content(value) of the second array(CHECKBOXE) and For name of the Second array i've taken content(value) of the first array(CHECKBOX)... and passed the first array across the pages....

So on the second page i'm already getting first array.. to get value of second array i'm using first array value and to get the third one i'm using value of the second array.. in that way by passing only one array i can access all three arrays... and relation is also maintained..

RP
Sep 6 '07 #11

code green
Expert 100+
P: 1,726
You could possibly improve this by using an associative aray and naming the keys the same as checkbox/textbox name
Sep 6 '07 #12

pbmods
Expert 5K+
P: 5,821
Heya, RP.

You may find this document of some interest.
Sep 6 '07 #13

rpnew
100+
P: 188
You could possibly improve this by using an associative aray and naming the keys the same as checkbox/textbox name

hi......
well thanks for your suggestion..... but i dont know how to use associative array so if you can give me some information it will be useful....however i'll also try to search it on net..

Thanks,
RP
Sep 6 '07 #14

rpnew
100+
P: 188
Heya, RP.

You may find this document of some interest.

Thanks to you as well..

RP
Sep 6 '07 #15

code green
Expert 100+
P: 1,726
A little example using associtive arrays
[PHP]$assocArray = array();
$nBoxes = 3;
for($c=0;$c<$nBoxes;$c++)
{
$name = 'box'.$c; creates a string called 'box' with a numeric suffix
$value = 'Any string '.1;

$assocArray[$name] = $value;
echo '<input type="textbox" name="'.$name.'id="'.$c.'value="'.$value.'>';
}
//Creates an array such
assocArray ('box0'=> 'Anystring 0','box1'=> 'Anystring 1','box2'=> 'Anystring 2')
//

//Read array
foreach($assocArray as $key=>$value)
{
echo 'Name = '.$key;
echo ' Value = '.$value.'<br>';
}
//Will output
Name = box0 value = Anystring 0
Name = box1 value = Anystring 1
Name = box2 value = Anystring 2
//Or at least it should do [/PHP]
Sep 6 '07 #16

rpnew
100+
P: 188
A little example using associtive arrays
[PHP]$assocArray = array();
$nBoxes = 3;
for($c=0;$c<$nBoxes;$c++)
{
$name = 'box'.$c; creates a string called 'box' with a numeric suffix
$value = 'Any string '.1;

$assocArray[$name] = $value;
echo '<input type="textbox" name="'.$name.'id="'.$c.'value="'.$value.'>';
}
//Creates an array such
assocArray ('box0'=> 'Anystring 0','box1'=> 'Anystring 1','box2'=> 'Anystring 2')
//

//Read array
foreach($assocArray as $key=>$value)
{
echo 'Name = '.$key;
echo ' Value = '.$value.'<br>';
}
//Will output
Name = box0 value = Anystring 0
Name = box1 value = Anystring 1
Name = box2 value = Anystring 2
//Or at least it should do [/PHP]

hmm well thats an useful example..

Thanks,
RP
Sep 7 '07 #17

Post your reply

Sign in to post your reply or Sign up for a free account.