I'm probably blind to my own mistake again, but why doesn't 2011 become the default selected year? (The -- are for debugging, they show up that the if statement works correctly.)
for($year=1900;$year<=2050;$year++) {
if ($year==date("Y"))
echo "<option value='".$year."' selected='selected'>--".$year."</option>";
else
echo "<option value='".$year."'>".$year."</option>";
}
The selected='selected' should just work. (link) So it is either a stupid mistake(i really don't see one) or some effect from php..
UPDATE:
found the problem.. the ftp didn't overwrite the file properly. Now it works. Thanks for the fast help, when these things happen I always start doubting my own sanity. (cant send this as answer because i have a low rep)
PHP has no effect on the browser behaviour, so if it doesn't work, it is a stupid mistake in the HTML output. ;)
Best thing is checking if the selected variant is actually outputted. Could be that the comparison evaluates to false, so it's always the second line. A little debugging should point out fast what is wrong. Check the output in the browser or var_dump the values of $year and date('Y') to see if they return what you expect.
At first sight I see no error here. Do you have any Javascript that might influence the selection?
CTRL + SHIFT + R if you use firefox.
I had that problem very often too ..
It should be just the cache.
Btw.: You should rather use double quotes for html attributes. That's more "usual". :)
Are you checking the generated HTML? Maybe this code works well (it seems it does), but the problem is in select, or in another place.
Related
I've been teaching myself php & mysql & have found this site invaluable. Thanks to all who answer questions.
I have found several q&a's that are close to this & they HAVE helped to point me in the right direction but I can't quite get this to work.
Here's what I'm trying to do: Pass a constructed SQL query to a second php script so that the results can be displayed in a new window. I can't quite seem to get the syntax right to pass the sql
query ($sql) to open.window correctly. A matter of quotes I
think. I have tried echo instead of print, single quotes with double quotes inside, using ('$sql'), but nothing seems to work.
I do need this to be called by a variable, so that it is after the submit is given. I would prefer not to have a second submit
Help?
if (isset($_POST['NEWWINDOW'])) {
echo "<script>window.open("results.php?sql=$sql)</script>";
}
This will concatinate your $sql-variable with the rest of the string, however doing this you should keep in mind to sanitize your sql in result.php before submiting it to your database.
if (isset($_POST['NEWWINDOW'])) {
echo "<script>window.open('results.php?sql=".$sql."')</script>";
}
Try this
<?php
if (isset($_POST['NEWWINDOW'])) {
echo "<script>window.open('results.php?sql=$sql')</script>";
}
?>
Thank you each for your responses. This query form is private, & I will be the only one using it... (behind firewall), so the risk is minimal. However, the point is well made & I would certainly rather learn to do it the right way than the wrong way. The database is important. Recommendations?
Neither of the two offered suggestions as to syntax work for me.
I made sure & copy the syntax exactly. In both (all three cases) the window.open fails to open a window. I finally cut & pasted to be sure, but the behavior is the same.. the window does not even open.
I am trying to create a dynamic FAQ page. I have the following phtml sample :
<div id="faq">
<!-- Start FAQ "Navigation" -->
<div class="faqBox">
<? foreach($this->aFAQ as $k => $val) : ?>
<?= ($val['mQuestion']); ?>
<?= ($val['mAnswer']); ?>
<? endforeach; ?>
</div>
</div>
Which outputs as follows:
For additional payment options - check or money order, please contact us at iBrandingLevel == 2 ? $this->oStore->getSuppPhone()." Monday to Friday ".$this->oStore->getSuppHoursOpen()." - ".$this->oStore->getSuppHoursClose()." ".$this->oStore->getSuppTimeZone() : "(888) 455-3237 x2 from Monday to Friday 8:00am - 4:30pm MST/Arizona."; ?>
The above text is just the first $val['mAnswer'] (I didnt include the question as that is working properly).
The html is being rendered however obvoiusly the php isn't. the <? and ?> are being removed and just code is displaying. Is there a fix for this? or is my approach fundamentally wrong.
thanks
Your approach is fundamentally wrong, you are outputting PHP code as if it was HTML text and try to execute it.
It is possible to execute code from a string, you can look at the Eval method (http://php.net/manual/fr/function.eval.php) in PHP, but it is not recommended to do this. There are better ways to resolve your specific issues than to output PHP code directly.
What you could do is send a few variables to the view, and use if conditions there.
You could also prepare the full string you need before the view and then all that would be needed is to display it.
To elaborate a little about Eval :
1- If the code you execute within the Eval comes from a user, it is extremely dangerous.
2- If not, there is very often a better solution to the problem, using Eval makes it harder to debug.
Actually, I'm not sure I should answer this.
First, the answer to your request is the mixed eval ( string $code ) php function.
Second, FORGET IT. IMHO, this could be one of the most dangerous things you could think in.
Thanks everybody for the input and resulting discourse. The php code that was being stored in the database was not being input by users, it was all completely internal, however it still shouldn't be there.
I ultimately went through the database and set a %%variablename%% in place of the php code and then upon retrieval I wrote a script that would:
preg_replace("/\%\%variablename\%\%/", $desiredPhpcode, dbRetrievedString).
all instances of %%variablename%%.
It seemed the safer and more sound approach. I don't know if this is an IDEAL approach that anybody else could benefit from if caught in this circumstance or if it 'just works', but I thought I would share.
Thanks Again for the input it helped enormously
PHP is server-side language. Outputting it to client does not make any sense, as there is no one to interpret it.
I was trying to do sprintf("<%s>", "Sat");, but nothing comes out. When you remove the less than symbol, it will start working again. Anyone experience this behavior and whether it expected? as i think it is a bug.
You can even get the same result here with printf.....
http://writecodeonline.com/php/
Your browser is probably rendering it as a tag. View source to confirm.
http://codepad.org/g5FXZAwa
<?php
printf("<%s>", "Sat");
Prints <Sat>
Edit for Yogesh.
<?php
echo sprintf("<%s>", "Sat");
Prints <Sat>
I believe that this happens because <Sat> is interpreted by your browser as a tag.
So I'm trying to do something extremely simple, and after reading through forums, and researching on google I still can't figure out why this is not working. But this is mostly like because I'm still a very much noobie programmer. I'm trying to send information through a url, and having a script pick it up using the $_GET super global.
Here's the link code, in a file called TESTFORM.php:
<p>
Here's a link:
ID
</p>
This is the TESTGET.php script:
<?php
if (isset($_GET['id']))
echo 'it is set<br />';
else
echo 'it is not set<br />';
?>
This yields in a "It is not set" appearing on the page every time. Any thoughts? Are there ghosts in my computer ruining my code? Thanks for taking the time to read through this! Happy coding!
I'm no PHP programmer, but I do know from HTML that computers (especially file names) don't "like" spaces. Try removing the spaces in the id = 5 code.
Your problem is the extraneous space here around the URL parameters:
ID
That will result in PHP seeing the parameter as $_GET["id_"]. The space gets converted into an underscore.
It's always best to use var_dump($_GET); or var_dump($_REQUEST) when you run into such problems. Secondarily it is sometimes helpful to get rid of isset in such cases. Albeit you have a custom error message in place of the language notices intended just for that.
Have you tried to remove spaces in your link?
ID
Code seems fine at a glance, have you tried removing the spaces in
?id = 5 to ?id=5
Well, I've gone over this atleast 30 times, tried as many possible combinations that I could think of, can you spot the syntax error? (I can't, obviously). It doesn't display what it should be, instead it displays the actual html of the page!
The Code:
$ct->data[$key][1] =
'<input id="quantity" name='."items[<?=$product_id;?>]".
'type="text" value="'.$ct->data[$key][1].'"
style="background:#FFFFFF url(qty.png) no-repeat 4px 4px;
Can someone please tell me what I've done wrong? Any help/advice at all is appreciated.
Thanks!
What is this?
name='."items[<?=$product_id;?>]".' type=
I think you meant
name="items[' . $product_id . ']" type=
Using short tags is a very bad practice. It makes code harder to read and it isn't enabled by default on most environments. Which can lead to mistakes like this one.
Always use the full <?php (and not <?) and <?php echo "string" instead of <?="string">. This will prevent many mistakes.
Then, it looks like you're trying to evaluate PHP in strings. echo "echo 'test'"; will never print test, it will always print echo 'test'. It's the same thing for items[<?=$product_id;?>]. First of all, it isn't even a valid PHP syntax and second of all, even if it was really, you can use $product_id without any other modification : items[$product_id]. (edit: actually, I'm not even sure what you're trying to do here).
I'm not going to go over all your code, but it seems like you lack the basics of the language. It may be good to review them!