Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable names into variable values 1

Status
Not open for further replies.
Jul 28, 2005
358
FR
Hi all,

don't know if anyone out there can help but I am a bit stumped.

I am developing a rental website for multiple properties and need an availability calendar for the properties. The client side of the calendar is fine and works (it takes booking details for certain days out of a mysql database). What I am having problems with is creating the admin side of it where renters can update their properties availability. At the moment for testing purposes I have entered details into the mysql manually using phpmyadmin.

What I want is a full year or month to be displayed and for the owner to be able to tick radio boxes for each day, submit the data (by month or maybe year - not decided yet) and for that data to be written to the database.

I reckon I need to dynamically create the option name so it is something like 20060503 - ie todays date YYYY/MM/DD.

Anyone know how I could then get the script to automatically parse the form, getting the option name and then inserting either U or B (unavailable or booked) into the database which is set up as a mysql date field (which would be the option name) and an option field which is an enum of U or B.

Thanks

Richard
 
I strongly recommend that you not use dynamic variable names.

There is an obscure feature of PHP that if form field names are formatted the right way, PHP will treat the values of those forms as arrays. For example, if you have the form:

[tt]<form method="post" action="somescript.php">
<input type="text" name="foo[1]"><br>
<input type="text" name="foo[2]"><br>
<input type="text" name="foo[3]"><br>
<input type="submit">
</form>[/tt]

Then when submitted, $_POST['foo'] will itself be an array of three elements, each element containing one of the input values.


To apply this to your question, if I have the HTML:

[tt]<html><body>
<form method="post" action="show_post.php">
2006-06-01<input type="checkbox" name="dates[20060601]"><br>
2006-06-02<input type="checkbox" name="dates[20060602]"><br>
2006-06-03<input type="checkbox" name="dates[20060603]"><br>
2006-06-04<input type="checkbox" name="dates[20060604]"><br>
2006-06-05<input type="checkbox" name="dates[20060605]"><br>
<input type="submit">
</form></body></html>[/tt]

and I check the boxes next to 2006-06-02 and 2006-06-04 then submit the form, $_POST contains:

[tt]Array
(
[dates] => Array
(
[20060602] => on
[20060604] => on
)

)[/tt]

My script can simply use a foreach() loop through $_POST['dates'] and get all the checked values from the keys of the array.



Want the best answers? Ask the best questions! TANSTAAFL!
 
I can certainly see why an array would be easier to cope with. I've actually got the code to almost work without arrays which is bloody amazing because I didn't have a clue earlier!!

I may well change it over to an array though.

Thanks sleepnir, very helpful.

Richard

 
Even if you have it working already, I still recommend changing it to use arrays. Using the appropriate datastructure will always makes coding easier, and I think arrays are the right datastructure to use here.



Want the best answers? Ask the best questions! TANSTAAFL!
 
only problem is that I need the names of the array keys to be able to write to the database.

What I am doing now is keeping the dynamic names for the select then I use an array when the form has been submitted which holds the name of the variables using this code

Code:
<?php

foreach($_POST as $key => $value)
	{
	$keydates=explode("_", $key);
	$keydate= $keydates[0] . "-" .$keydates[1] . "-" .keydates[2];
		echo "$keydate = $value<br />";
	}

?>

at the moment I just want to echo the new dates (after I have changed them from 2006_05_03 format to 2006-05-03 format) I'll do the insert statement once I can figure out why the $keydate=$keydates[0]."-".$keydates[1]."-".$keydates[2]; line is throwing me an error saying there is an unexpected '[' in that line.

Surely, I am referencing an array created by the explode statement and rebuilding a new variable (which will be written to the database).

richard
 
mind that'll be because I'm an idiot!!

Why didn't I notice the c**k up on the last referenc in the array till now!

Thanks Sleepnir, much appreciated!

Richard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top