Jump to content

Recommended Posts

advance thank you ...

 

 

 

I wanted to create a re usable function for dates.

 

But at the same time, i want to change the variable name of $date_select_name

 

within the function result, to the variables i changed it to and show the result after posting.

 

look at the code your see what i mean.

 

 

 

 

example dose not do what i want it like the

variable of $date_select_name=$select1.$select2.$select3; not changing.

 

 

i fort if i use this

 

form_date($date_select_name="test3");

 

now variable $date_select_name becomes $test3 then echo it after posting.


<?php


if(isset($_POST['submit'])){

$test1=$_POST['test1'];

echo $test1;
}


function form_date($date_select_name){


$date_select_name=$select1.$select2.$select3;


echo" <select name='select1'>";


echo"<option value=' '>Day</option>";


$d=range(1,31);
$m=range(1,12);
$y=range(2009,2011);


for($i=0; $i<count($d); $i++){

	$day=$d[$i];


	echo "<option value='$day'>$day</option>";

}

echo"</select>";


echo" <select name='select2'>";


echo"<option value=' '>Month</option>";


for($i=0; $i<count($m); $i++){


	$month=$m[$i];


	echo "<option value='$month'>$month</option>";

}

echo"</select>";


echo" <select name='select3'>";


echo"<option value=' '>Year</option>";


for($i=0; $i<count($y); $i++){


	$year=$y[$i];


	echo "<option value='$year'>$year</option>";

}


echo"</select>";

return;


}


echo "<form method='POST' action=' '>";

echo" USER A";

form_date($date_select_name="test1");

echo "<br>";

echo" USER B";
form_date($date_select_name="test2");

echo"<br>";

echo" USER C";
form_date($date_select_name="test3");

echo "<br><br>";

echo"<input type='submit' name='submit' value='Test Me'>";


echo "</form>";

?>

 

 

 

 

basic way it works

 

but that no good i need to change the variable name with the function.

 


<?php


if(isset($_POST['submit'])){


$date_select_name=$_POST['select1'].$_POST['select2'].$_POST['select3'];

echo $date_select_name;

}


function form_date(){



  echo" <select name='select1'>";


echo"<option value=' '>Day</option>";


$d=range(1,31);
$m=range(1,12);
$y=range(2009,2011);


for($i=0; $i<count($d); $i++){

	$day=$d[$i];


	echo "<option value='$day'>$day</option>";

}

echo"</select>";


echo" <select name='select2'>";


echo"<option value=' '>Month</option>";


for($i=0; $i<count($m); $i++){


	$month=$m[$i];


	echo "<option value='$month'>$month</option>";

}

echo"</select>";


echo" <select name='select3'>";


echo"<option value=' '>Year</option>";


for($i=0; $i<count($y); $i++){


	$year=$y[$i];


	echo "<option value='$year'>$year</option>";

}


echo"</select>";

return;


}


echo "<form method='POST' action=' '>";


form_date($date_select_name);


echo"<input type='submit' name='submit' value='Test Me'>";


echo "</form>";

?>

 

 

i thort, with a function, when you return a variable name,

you get the result buy echo it out.

 

i am telling the function to return

this:

 

$date_select_name=$_POST['select1'].$_POST['select2'].$_POST['select3'];

 

does not work .

 

 

example have a look very strange.

<?php


if(isset($_POST['submit'])){



echo $date_select_name;

}


function form_date($date_select_name){


$date_select_name=$_POST['select1'].$_POST['select2'].$_POST['select3'];


      echo" <select name='select1'>";


echo"<option value=' '>Day</option>";


$d=range(1,31);
$m=range(1,12);
$y=range(2009,2011);


for($i=0; $i<count($d); $i++){

	$day=$d[$i];


	echo "<option value='$day'>$day</option>";

}

echo"</select>";


echo" <select name='select2'>";


echo"<option value=' '>Month</option>";


for($i=0; $i<count($m); $i++){


	$month=$m[$i];


	echo "<option value='$month'>$month</option>";

}

echo"</select>";


echo" <select name='select3'>";


echo"<option value=' '>Year</option>";


for($i=0; $i<count($y); $i++){


	$year=$y[$i];


	echo "<option value='$year'>$year</option>";

}


echo"</select>";

return($date_select_name);


}


echo "<form method='POST' action=' '>";


form_date($date_select_name);


echo"<input type='submit' name='submit' value='Test Me'>";


echo "</form>";

?>

Let say the user had 5 rooms now every room has it own,

database column and date.

 

well because i can not change the select name via the function it very hard task.

 



<?php


if(isset($_POST['submit'])){


$date_select_name=$_POST['select1'].'/'.$_POST['select2'].'/'.$_POST['select3'];

echo $date_select_name;

}


function form_date(){

echo "<form method='POST' action=' '>";


  echo" <select name='select1'>";


echo"<option value=' '>Day</option>";


$d=range(1,31);
$m=range(1,12);
$y=range(2009,2011);


for($i=0; $i<count($d); $i++){

	$day=$d[$i];


	echo "<option value='$day'>$day</option>";

}

echo"</select>";


echo" <select name='select2'>";


echo"<option value=' '>Month</option>";


for($i=0; $i<count($m); $i++){


	$month=$m[$i];


	echo "<option value='$month'>$month</option>";

}

echo"</select>";


echo" <select name='select3'>";


echo"<option value=' '>Year</option>";


for($i=0; $i<count($y); $i++){


	$year=$y[$i];


	echo "<option value='$year'>$year</option>";

}


echo"</select>";


echo"<br><br>";


echo"<input type='submit' name='submit' value='Test Me'>";




echo "</form>";


return;


}


for($i=1; $i<6; $i++){

echo " ROOM $i";

form_date();

}
?>

 

 

This is my Problem,

 

I got a web site, that users can fill in a form, and say how many room's they got,

now each room has also got dates,

 

so i need a way to show a form with the dates, that the user can update the dates according to to the room they got.

 

do you get me.

 

i was trying to create a function with dates so i can re use it to change the select value="name"

Something like

<?php


if(isset($_POST['submit']))
{
   echo '<pre>'.print_r($_POST, true).'</pre>';

   echo '<hr />';
}


function add_date_form($r)
{
    $form   = '';
    $days   = range(1,31);
    $months = range(1,12);
    $years  = range(2009,2011);

    $form .= "  <select name=\"r[$r][d]\">
    <optgroup label=\"Day\">\n";

    foreach($days as $d)
    {
        $form .= "        <option value=\"$d\">$d</option>\n";
    }

    $form .= "    </optgroup>\n  </select>
  <select name=\"r[$r][m]\">
    <optgroup label=\"Month\">\n";

    foreach($months as $m)
    {
        $form .= "    <option value=\"$m\">$m</option>\n";
    }

    $form .= "    </optgroup>\n  </select>
  <select name=\"r[$r][y]\">
    <optgroup label=\"Year\">\n";

    foreach($years as $y)
    {
        $form .= "    <option value=\"$y\">$y</option>\n";
    }

    $form .= "    </optgroup>\n  </select>\n";

    echo $form;
}


echo "<form action=\"\" method=\"post\">\n";

for($i=1; $i<6; $i++)
{
   echo " ROOM $i";

   add_date_form($i);

   echo '<hr />';
}

echo "  <input type=\"submit\" name=\"submit\" value=\"Go\" />
</form>";

?>

Your fantastic.

 

thank you so much wildteen your such a grate programmer and always helped cheers.

 

Now the problem is how can i use that with a database .

 

 

database example.

 

room

/////

 

room_id

user_id

number_of_rooms

 

date_busy

date_id

user_id

date_busy

 

 

let say

 

room_id = 01

user_id = 10

number_of_rooms=3

 

date_id = 06

user_id = 10

room_number = 1

date_busy = any date you want.

 

let say there 4 entry hypercritically

 

how do i get the array or select name to update the correct room for that date.

 

When you call the add_date_form you pass it the room id.

 

To process the form, you'd use

if(isset($_POST['submit']))
{
   foreach($_POST['r'] as $room_id => $date)
   {
       echo 'Date for room #'.$room_id.'<br />'.
            'Day: '.$date['d'].'<br />'.
            'Month: '.$date['m'].'<br />'.
            'Year: '.$date['y'];

       echo '<hr />';
   }
}

ok i understand that,

 

so what all this for please..

 

<select name=\"

 

r <<<<<<<<< ? //carriage return ?

[$r] >>>>> ? this is the array but it got [] ?

[y] <<<<< ? // [y] year while showing the array i think?

\">

 

like i say thank you.

 

It just constructs the $_POST array.

 

r stands for room

$r is the room id you pass to the function

d, m and y stands for day, month and year

 

so if you pass then function the room id of 101 it'll produce the following:

<select name="r[101][d]">
...
  </select>
  <select name="r[101][m]">
...
  </select>
  <select name="r[101][y]">
...
  </select>

well that that,

 

thank you so much well said and presented i say.

 

i just didnt understand the the array in the select name="" were i got stuck.

 

maybe me keep reading post's got me this far ..

 

thank you wildtenn your the MAN SOLVED

 

if there anything else any one wants to throw at me, to learn go for it, it all helps.

 

i got the biggest white board in the world.

 

still understanding thorpe examples they where grate. (2 years old)

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.