I have a date of birth, and when the user is over Feb (02), the days should go only to 29. As you can see I'm using $month="1" just to test it. I'm supposed to use PHP only, no JavaScript or anything else. How would i go about making that?
<?php
$month="1"; // <-- currently using this to make it 29,30 or 31 days
print "<select name='day'>";
if ($month==1){
for ($i=0; $i<=28; $i++)
{
$day = 1 + $i;
print "<option value = $day>" . $day ."</option>";
}
}
if ($month==2){
for ($i=0; $i<=29; $i++)
{
$day = 1 + $i;
print "<option value = $day>" . $day ."</option>";
}
}
print "</select>";
print "<select name='month'>";
for ($i=0; $i<=11; $i++)
{
$month = 1 + $i;
print "<option value = $month>" .$month ."</option>";
}
print "</select>";
?>
Once PHP sends stuff to the browser, it is done. It cannot affect the page in any way.
JavaScript, on the other hand, takes over when the browser gets the page. It CAN change the page, and it can even ask the server to do something (in which case PHP may get involved).
In other words, you cannot do what you are asking without JavaScript.
Without Javascript it is just impossible to do.
Related
I would like to get the id of a specific value in my phpmyadmin table.
So, I have got a table with 'id_cal' as an A.I. id, 'mois' representing the month with numbers (e.g 1 for january) and 'annee' representing the year. (see calendar table)
I am trying to set php variable for the month and the year and if they match the current month and year, I want to get this specific id.
I commented the php code where I am having my trouble, here it is :
<?php
include_once('config.php');
$m = idate('n');
$y = idate('Y');
echo $m; echo "\t"; echo $y; echo "<br>"; echo "<br>"; // The result of this is 7 2019
$reponse = $bdd->query('SELECT * FROM calendrier');
while($donnees= $reponse->fetch()){
$mois = $donnees['mois'];
$year = $donnees['annee'];
$id_cal = $donnees['id_cal'];
echo $id_cal;
echo "\t";
echo $mois;
echo "\t";
echo $year;
echo "<br>";
}
// What I am trying to do :
if (($m = $mois) && ($y = $year)){ // If the month and the year are the current month/year
$i = $id_cal; // I want to put the id refering to the current month/year (in my phpmyadmin table) into a new variable
echo "<br>"; // and echo this variable (or use it in other ways)
echo $i; // BUT what I am echoing is 24 representing the number of values in my array
} // How can I only get 7 ? (in this exemple, since we are the 7/2019)
Here is what I am getting in my localhost : echo
I really don't understand why am I not having 7.
Also, I tried this instead of my while:
$donnees= $reponse->fetch();
$mois = $donnees['mois'];
$year = $donnees['annee'];
$id_cal = $donnees['id_cal'];
// But in this cas I am having $i = 1, so it's the same problem.
Many thanks in advance for your response I am quite struggling with this.
It is because id_cal gets overwritten with the new value of id_cal in each iteration of your while statement.
To get the result you want, you could put the if inside the while statement...
while($donnees= $reponse->fetch()){
$mois = $donnees['mois'];
$year = $donnees['annee'];
$id_cal = $donnees['id_cal'];
echo $id_cal;
echo "\t";
echo $mois;
echo "\t";
echo $year;
echo "<br>";
if (($m == $mois) && ($y == $year)){
$my_var_to_use_elsewhere = $id_cal;
}
}
echo "<br>";
echo $my_var_to_use_elsewhere;
My array from a database looks like this:
$client[0]['Name'] = 'Foo';
$client[0]['Claim'][0]['year'] = 2013;
$client[0]['Claim'][1]['year'] = 2014;
$client[0]['Claim'][2]['year'] = 2015;
$client[1]['Name'] = 'Bar';
// no 2013!
$client[1]['Claim'][0]['year'] = 2014;
$client[1]['Claim'][1]['year'] = 2015;
// table headers are name, 2013, 2014, 2015...
foreach($client as $c) {
echo '<tr>';
echo '<td>' . $c['Client']['name'] . '</td>';
foreach($c['Claim'] as $claim) :
echo '<td>' . $claim['year'] . '</td>';
endforeach;
echo '</tr>';
}
Which works fine except when a $client is missing a year then the <td> are not balanced; causing my table to be incorrect.
My goal is to have each $claim['Year'] line up with the appropriate table heading.
I figured I could just add empty keys to each array and re-order them but that seems a bit unnecessary and wondered if there was a better way of doing it.
So an array might look
this would be ideal:
$years[null, 2014, 2015]
whereas currently it is
$years[2014,2015]
Hope this makes sense.
I am assuming you have a start and an end year that you want to display the claims for. Then if you replace the inner for loop with this I think it should work:
$x = 0; //Variable to keep track of what claim we are on.
//Loop through the years.
for($i=$start; $i<=$stop; $i++) {
//If there is a claim for this year, echo it and move on to the next claim.
if($c['Claim'][$x]['year'] == $i) {
echo '<td>' . $c['Claim'][$x]['year'] . '</td>';
$x++;
}
//If not, just echo an empty table cell.
else {
echo '<td></td>';
}
}
This only works if the claims are sorted by year. Please note that I have not tested the code.
I'm working with a multi-language site, and there is an HTML select element with 100 choices. The code looks like this:
<option value="1"><? echo $lang['CARGOTYPE_1']; ?></option>
So system insert into mysql table "type" number "1", or number "2" pr...
But when I select number from mysql, I need to change number to word.
With if, else I can change its meanings:
If($type == 1) { echo $lang['CARGOTYPE_1']; } elseif($type == 2){ echo $lang...
But the problem is, that code will be very long...
Any smart solutions for my problem?
If you know number of choices, use for cycle for that.
for ($i = 1; $i <= 30; $i++) {
echo '<option value="' . $i . '">' . $lang['CARGOTYPE_' . $i] . '</option>';
}
I am not sure what you want... If you want to "echo" something with arguments, you can do something like :
echo $lang['CARGOTYPE_'.$type];
But, is that you need ?
I'm trying to autofill the date function for a dropdown. I thought it would be a fairly straightforward bit of code. I've searched online and fiddled with it for about an hour now and still no luck. Any assistance would be greatly appreciated. :)
$day = 1;
while ($day < 32) {
echo "<option value=/"$day/"> $day </option>";
$day++;
}
In order to escape quotes and double quotes, your slashes need to be backslashes like below.
$day = 1;
while ($day < 32) {
echo "<option value=\"{$day}\"> $day </option>";
$day++;
}
It's also a good idea to wrap variables that aren't appended to a string using . with curly braces {}. If you get in the habit, performing tasks like printing an array value will be much easier later on.
Have you thought about using a for loop?
The for way:
for($day=1;$day<32;$day++)
echo "<option value=\"{$day}\">$day</option>";
Your while way:
$day=1
while($day < 32){
echo "<option value=\"{$day}\">$day</option>";
$day++;
}
You could try doing this on your code. instead of using slashes to scape your double quotes, i dont think they're different but this looks alot cleaner than all those slashes
$day = 1;
while ($day < 32) {
echo "<option value='".$day."'> $day </option>";
$day++;
}
I have a HTML select form to select the day/month/year, and this is generated with PHP. My for loop, for example with the month, looks like this:
$html="<select name=\"".$name."month\">";
for($i=1;$i<=12;$i++)
{$html.="<option value='$i'>$months[$i]</option>";}
$html.="</select> ";
I can't seem to figure out how to set a "default" value to the current month while retaining all of the prior months in the dropdown (I can just set $i=date("n"), but then I lose the ability to select any prior month).
Does anybody know a simple way of setting the default value to be the current day? Thank you very much!
Try this:
<?php
$currentMonth = date("m");
$html = "<select name=\"" . $name . "month\">";
for ($i = 1; $i <= 12; $i++) {
if ($i == $currentMonth) {
$html .= "<option selected='selected' value='$i'>$months[$i]</option>";
} else {
$html .= "<option value='$i'>$months[$i]</option>";
}
}
$html .= "</select> ";
echo $html;
?>