tags:

views:

2023

answers:

6

I am trying to include a value from a database table within the value element of an input field.

This is what I have, but it is not working

?><input type="text" size="10" value="<?=date("Y-m-d", 
strtotime($rowupd['upcoming_event_featured_date']))?>" name="upcoming_event_featured_date" 
id="keys"/><?php

I have done this before, but I usually print it out like this

print '<input type="text" size="10" value="'.date("Y-m-d", 
strtotime($rowupd['upcoming_event_featured_date'])).'" name="upcoming_event_featured_date" 
id="keys"/>';

What is the appropriate way of doing this (without using print '';?

A: 

You can use the short_open_tag ini directive to turn on the <?= shortcut for printing.

If that is not available, you must use either print or echo to accomplish this.

You can try:

ini_set('short_open_tag', true);
Devon
A: 

Are you sure that short_open_tag is enabled? http://us.php.net/echo

Totty
A: 

You can do

[...] ?><input 
    type="text" 
    size="10" 
    value="<?php echo date("Y-m-d", strtotime($rowupd['upcoming_event_featured_date'])) ?>" 
    name="upcoming_event_featured_date" 
    id="keys"/>
<?php [...]

if you don't have short_open_tag enabled in your PHP configuration.

Stefan Gehrig
+2  A: 

It's a good idea to always use full php tags, because that will keep your app from breaking if you move to a different server or your config is changed not to allow short tags.

?>
<input type="text" size="10" value="<?php
echo(date("Y-m-d", strtotime($rowupd['upcoming_event_featured_date'])));
?>"name="upcoming_event_featured_date" id="keys"/><?php

Also, note that you are missing the ; from the end of your php code.

You may find it better to keep the whole thing in PHP too, and just echo() out the HTML, as that will keep from having to switch back and fourth from php to html parsing.

Eli
one-line echoes don't need a semicolon.
Nathan Strong
They may not be completely necessary, but It's still a good idea to have them.
Eli
A: 

If you find yourself doing this often you may want to look into a templating system for PHP, such as Smarty.

Keith Twombley
+1  A: 

As some other replies have mentioned, make sure short_open_tag is enabled in your php.ini. This lets you use the <?= syntax. A lot of people recommend not using short tags, since not all servers allow them, but if you're sure you won't be moving this to another server, I think it's fine.

Besides that, I don't know of any technical reason to choose one way over the other. Code readability should be your main focus. For example, you might want to set your value to a variable before outputting it:

$featured_date = date("Y-m-d",strtotime($rowupd['featured_date']));

?><input type="text" value="<?=$featured_date?>" name="featured_date" /><?php

In fact, I'd try to do as little processing as possible while you're in the middle of a block of HTML. Things will be a lot cleaner if you define all your variables at the beginning of the script, then output all of the HTML, inserting the variables as needed. You're almost getting into templating at that point, but without needing the overhead of a template engine.

Zac