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;
?>
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;
Suppose there is a number of item in array.which may be odd or even like I have an array which contain item from a to z Now I want to display that item in table . But As you know That There are 23 alphabets I want to display these alphabets in table which contains only 5 column in the last you got only three alphabets I want to display them in table . In the last I want that there should be three column not 5.
Here is my code i could not get that what should i do?
But the problem I faced in the below code is that the second loop is not correct.
<?php
$arr=array('a','b','c','d','e','f','g','h');
$count= sizeof($arr);
$row=ceil($count/5);
echo "<table border='1'>";
for($r=0;$r<$row;$r++){
echo "<tr>";
for($j=0;$j<=5;$j++){
echo "<td>'".$arr[$j]."'</td>";
}
echo "</tr>";
}
echo "</table>";
?>
My approach uses array_slice to take out pieces of the source and build rows:
$arr=array('a','b','c','d','e','f','g','h');
$offset = 0;
$num_columns = 5; //adjust number of columns
$table_html = "<table border='1'>";
while($slice = array_slice($arr,$offset,$num_columns)){
$offset += $num_columns;
$row_html = '';
foreach($slice as $n) $row_html .= "<td>$n</td>";
$table_html .= "<tr>$row_html</tr>";
}
$table_html .= '</table>';
echo $table_html;
Live demo
Try below code. Declaring number of column required in a variable, so which can be changed any time. closing the tr tag when loop count is same as number of columns to be displayed.
<?php
$arr=array('a','b','c','d','e','f','g','h');
$columnLength = 5;
echo "<table border='1'>";
echo "<tr>";
for($r=0;$r<count($arr) ; $r++){
echo "<td>'".$arr[$r]."'</td>";
if($r + 1 == $columnLength ) {
echo "</tr>";
}
}
echo "</table>";
?>
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 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.
I have a function that take 2 parameters and outputs a select pulldown menu. The first parameter is simply the name, and the second is a parameter ($sel) that holds the selected element (the data is first populated from a db). This works fine, except the if statement (that determine is used to pre-select an option from the database) doesn't seem to work. I've verified there should be a match, and that both $sel and date('g:i ', $i) are both strings..so in principle this should work..but it doesn't.
Can you please take a look and see if there is something I'm doing wrong?
function selectTime_Hour($timeName,$sel){
$start = strtotime('1:00am');
$end = strtotime('12:00pm');
$menu = '<select name="'.$timeName.'">';
for ($i = $start; $i <= $end; $i += 1800)
{
if ($sel==date('g:i ', $i)){
$menu .= '<option selected="selected">' . date('g:i ', $i) . '</option>';
}
else {
$menu .= '<option value="'.date('g:i',$i).'">' . date('g:i ', $i) . '</option>';
}
}
$menu .= '</select>';
return $menu;
}
I'm guessing it's the extra space you're adding in some of those date() calls.
You are populating your <option> tags with values of date('g:i',$i), which I presume you are then storing to the database. However, in your if statement, you are comparing this against date('g:i ',$i), with an extra space after the g:i.