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++;
}
Related
I'm setting up a while function that adds four every time it runs. But I get this weird error.
Notice: A non well formed numeric value encountered in.
I would like to fix this issue only I couldn't find any info regarding this problem in the instance of a while loop. Thank you in advance.
$x= 2012;
while($x <= 2084){
echo "Schrikkeljaar:" . $x+=4 ."<br>";
}
Wrap it in parenthesis:
echo "Schrikkeljaar:" . ($x += 4) . "<br>";
$x= 2012;
while($x <= 2084){
echo "Schrikkeljaar:" . ($x += 4) . "<br>";
}
Live Example
Repl
Php is trying to add "4<br>" to x. The easiest way to solve this is by adding that before:
$x= 2012;
while($x <= 2084){
$x+=4;
echo "Schrikkeljaar:$x<br>";
}
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.
Is there a way to print php variables inside a HTML tag?
Like this:
<?php
for ($e = 1; $e <= 30; $e++) {
for ($i = 1; $i <= 40; $i++) {
print ('<div id="map"><a>$i</a></div>'); // <----I'm adding the divs here, but I want to add a text inside each of them
}
}
?>
Your call:
echo ''.$title.'';
or
echo "<a href='$href'>$title</a>";
or
<?= $title ?>
I prefer the first one, because I think it is the most readable. But it is up to you.
Using your example:
print ('<div id="map"><a>'.$i.'</a></div>');
or
print ("<div id=\"map\"><a>$i</a></div>"); // note the double quotes to allow PHP to parse the string
or
<div id="map"><a><?= $i ?></a></div> // but you really should do this!
To better clarify you SHOULD NEVER rely on short tags (the last option). Every time you do that god will kill a kitten and abuse a unicorn.
In this case take the variable out of the string and concatenate it inside.
print ('<div id="map"><a>'.$i.'</a></div>');
This is pretty basic stuff so I suggest you go through some tutorials or read the manual again.
I'm not sure what text you want to add there, you can have any other variables in there, same how you're printing the $i .. except that single quotes do not evaluate variables inside the string, only double quotes do ..
so:
$foo = "bar";
print "$foo" => "bar"
print '$foo' => "$foo"
You're using single quotes to output your variable. using double quotes may work instead, but I prefer using string concatenation(. operator)
print ("<div id=\"map\"><a>$i</a></div>");
Or
print ('<div id="map"><a>' . $i . '</a></div>');
Use " instead of ' when you want to use variables in a string.
<a href='<?php echo "http://www.stackoverflow.com"; ?>' target='_blank'>test</a> (Syntax maybe off)
EDIT
<?php
for ($e = 1; $e <= 30; $e++) {
for ($i = 1; $i <= 40; $i++) {
print ('<div id="map"><a>'. $i .'</a></div>');
}
}
?>
Note the '. $i .' part.
I randomized a set of questions using the following code:
for($i=0; $i < count($nwi); $i++)
$itemorder[$i] = $i;
shuffle($itemorder);
$_SESSION["itemorder"] = $itemorder;
A few pages later, a portion of the questions are presented:
for ($i=0; $i<40; $i++) {
if ($i % 10 ==0) echo $header;
echo '<tr class="';
if(in_array($itemlabel[$_SESSION["itemorder"][$i]], $errors)) echo 'skip';
elseif ($i % 2 == 0) echo 'even';
else echo 'odd';
echo '">';
echo '<td>' . ($i + 1) . ".</td><td>" . $itemtext[$_SESSION["itemorder"][$i]] . '</td>';
for ($j = 1; $j <= 6; $j++) {
echo "\n" . '<td';
if ($j == 6) echo ' style="background-color:#999;"';
echo '><input type="radio" name="';
echo $itemlabel[$_SESSION["itemorder"][$i]];
echo '" value="';
echo $j; //value of the input
echo '"';
if ($_POST[$itemlabel[$_SESSION["itemorder"][$i]]] == $j) echo " checked";
echo '></td>';
}
At the end of the survey, I am trying to put the answers to the questions (which should range in value from 1-8) into my SQL database:
"INSERT INTO surveydata
(id, agree_pppg_01,agree_pppg_02,agree_pppg_03,....
VALUES
('$_SESSION[id]', '$_SESSION[itemorder][0]',
'$_SESSION[itemorder][1]', '$_SESSION[itemorder][2]',
'$_SESSION[itemorder][3]',...
I am getting only zeros in my SQL database regardless of how I answer the questions. Any suggestions?
Well, first I don't see anything assigning anything to the session values, but the issue with your code is in this pattern: '$_SESSION[itemorder][1]'. First, I would make sure that MySQL is expecting a varchar there and not an int. If it is an int, good form would be to make sure it isn't quoted.
More importantly, though, when you have an associative array in PHP, you need to make sure PHP expects that.
This
$a = array("hi"=>array("world"=>0)); echo "$a[hi][world]";
Outputs
Array[world]
Put braces around the lookup to make sure it knows to treat it as an array, and then put quotes around all string indexes:
// note the braces and quotes
$a = array("hi"=>array("world"=>"here")); echo "{$a["hi"]["world"]}";
Outputs
"here"
But, I wonder if you wouldn't be better off just using implode:
$columns = implode(',', $_SESSION['itemorder']);
$query = "INSERT INTO surveydata
(id, agree_pppg_01,agree_pppg_02,agree_pppg_03,....
VALUES ('{$_SESSION["id"]}', $columns )";
I do feel obliged to point out that that system does not seem scaleable, and column names like agree_pppg_02 are not descriptive. You may want to go to the codereview stackexchange site to see if they can't offer tips on database design.
I have set to constants, a start year and an end year,
so i have built a while loop on the condition that
if the start year is < than the current year increment until true.
the problem i have is that instead of it going up like this:
1999,2000,2001,2002,2003,2004
it goes like:
1999,2001,2003,2005,2007,2009
Here is my code:
function yearCount()
{
$yearBegin = START_YEAR;
$yearCurrent = CURRENT_YEAR;
while($yearBegin < $yearCurrent){
echo "<option value=\"".$yearBegin++."\">".$yearBegin++."</option>";
}
}
any ideas would be highly appreciated.
You are incrementing the value twice:
echo "<option value=\"".$yearBegin++."\">".$yearBegin++."</option>";
Each $yearBegin++ increments it by one.
Use a for loop instead:
for ($yearBegin = START_YEAR; $yearBegin < CURRENT_YEAR; $yearBegin++)
{
echo "<option value=\"".$yearBegin."\">".$yearBegin."</option>";
}
Using a for loop is usually the way to do this,
for($year=START_YEAR;$year<=CURRENT_YEAR;$year++)
{
//User the $year here
}
your problem with the code is that your calling $yearBegin++ 2 times within the while loop, causing it to increment twice.
using the for loop is much cleaner then as incrementing is done within the expression for you
function yearCount()
{
$yearBegin = START_YEAR;
$yearCurrent = CURRENT_YEAR;
while($yearBegin < $yearCurrent){
$this_year = $yearBegin++;
echo "<option value=\"".$this_year."\">".$this_year."</option>";
}
}
use only one time ++ , increment
echo "<option value=\"".$yearBegin."\">".$yearBegin++."</option>";
You increment $yearBegin twice, one time in the value part, one time in the display part ...
You need to change it so it only increments once
You increment it twice, once setting it as a value, and the second time displaying it in option tag