There is a problem in this code I can not detected
<?php echo "<a href ='$rows['Link']'> .$rows['UploadName']</a> "; ?>
Do you find you have a solution???
Thank you very much.
My guess is that your problem is that it isn't writing out the data in $rows['Link'] ... if that is the case, then your solution is to change it to {$rows['Link']} ... actually, you'll probably want to change both, since it looks like you started doing string concatenation and then switched halfway through.
So:
<?php echo "<a href ='$rows['Link']'> .$rows['UploadName']</a> "; ?>
becomes:
<?php echo "<a href ='{$rows['Link']}'>{$rows['UploadName']}</a> "; ?>
See: The PHP Manual on Variable Parsing in strings
It should be:
<?php echo "<a href ='{$rows['Link']}'>{$rows['UploadName']}</a>"; ?>
Or:
<?php echo "<a href ='{$rows['Link']}'>" . $rows['UploadName'] . "</a>"; ?>
There's a problem in parsing variables in the string. Use curl braces:
<?php echo "<a href ='{$rows['Link']}'> .{$rows['UploadName']}</a> "; ?>
Take a look to this php.net page, under "variable parsing".
More alternatives:
<?php echo '' . $rows['UploadName'] . ''; ?>
or
<?=('' . $rows['UploadName'] . '')?>
Another alternative (that I tend to prefer, given I know that both 'Link' and 'UploadName' are valid indices of $row.
<?=$rows['UploadName']?>
I'm not sure what that does for readability for most people, but on color-coded IDEs, it tends to help, because the HTML isn't just seen as one giant ugly single-colored string.
Related
This question already has answers here:
php - insert a variable in an echo string
(10 answers)
Closed 3 years ago.
All I want to do is echo out a PHP variable inside a <li> tag. Refer to the code below. What's the proper format? Mine is not working.
$row['cat_title'];
echo "<li> {$cat_title} </li>";
If you need to output a value of a particular array key (e.g. $row['cat_title']) in HTML, you have quite a few options.
You can use the concatenation operator (.):
echo '<li>' . $row['cat_title'] . '</li>';
You can use variable interpolation with simple string parsing (note that in this syntax, quotes around the array key must be omitted):
echo "<li>$row[cat_title]</li>";
You can use variable interpolation with complex string parsing (using {}) which is actually not necessary here, but useful for more interpolating more complex expressions. With this syntax, quotes around the array key should be included.:
echo "<li>{$row['cat_title']}</li>";
You can output plain HTML and use the echo shortcut syntax <?= to output the value (only do this if you are already outputting HTML, not if you are currently in a <?php tag; that would be a syntax error.):
<li><?= $row['cat_title'] ?></li>
You can use printf (thanks to Elias Van Ootegem's comment for reminding me of this; I should have included it to begin with). sprintf can be used if you want to save the result to a variable instead; printf will output it immediately:
printf('<li>%s</li>', $row['cat_title']);
The first argument of printf is a format string, where %s is a string conversion specification that will take the value of $row['cat_title'] when printf is executed.
There are other ways, but these are the most common.
You need to reference the right variable. You're on the right track, though.
echo "<li> {$row['cat_title']} </li>";
echo '<li>'.$var.'</li>';
OR
echo "<li>$var</li>";
OR
<li><?=$var?></li>
If the file is mostly PHP in the file then:
<?php echo "<li>" . $row['cat_title'] . "</li>"; ?>
Or conversely if it's mostly HTML in the file then:
<li><?php echo $row['cat_title']; ?></li>
It sounds to me that it's most likely the second option you are looking for.
Another alternative:
<li><?php echo $row['cat_title']; ?></li>
Either of these methods will work...
// Use single quotes and periods like this...
echo '<li>'. $row['cat_title'] .'</li>';
OR
// Use double quotes like this...
echo "<li> $row['cat_title'] </li>";
This question already has answers here:
php - insert a variable in an echo string
(10 answers)
Closed 3 years ago.
All I want to do is echo out a PHP variable inside a <li> tag. Refer to the code below. What's the proper format? Mine is not working.
$row['cat_title'];
echo "<li> {$cat_title} </li>";
If you need to output a value of a particular array key (e.g. $row['cat_title']) in HTML, you have quite a few options.
You can use the concatenation operator (.):
echo '<li>' . $row['cat_title'] . '</li>';
You can use variable interpolation with simple string parsing (note that in this syntax, quotes around the array key must be omitted):
echo "<li>$row[cat_title]</li>";
You can use variable interpolation with complex string parsing (using {}) which is actually not necessary here, but useful for more interpolating more complex expressions. With this syntax, quotes around the array key should be included.:
echo "<li>{$row['cat_title']}</li>";
You can output plain HTML and use the echo shortcut syntax <?= to output the value (only do this if you are already outputting HTML, not if you are currently in a <?php tag; that would be a syntax error.):
<li><?= $row['cat_title'] ?></li>
You can use printf (thanks to Elias Van Ootegem's comment for reminding me of this; I should have included it to begin with). sprintf can be used if you want to save the result to a variable instead; printf will output it immediately:
printf('<li>%s</li>', $row['cat_title']);
The first argument of printf is a format string, where %s is a string conversion specification that will take the value of $row['cat_title'] when printf is executed.
There are other ways, but these are the most common.
You need to reference the right variable. You're on the right track, though.
echo "<li> {$row['cat_title']} </li>";
echo '<li>'.$var.'</li>';
OR
echo "<li>$var</li>";
OR
<li><?=$var?></li>
If the file is mostly PHP in the file then:
<?php echo "<li>" . $row['cat_title'] . "</li>"; ?>
Or conversely if it's mostly HTML in the file then:
<li><?php echo $row['cat_title']; ?></li>
It sounds to me that it's most likely the second option you are looking for.
Another alternative:
<li><?php echo $row['cat_title']; ?></li>
Either of these methods will work...
// Use single quotes and periods like this...
echo '<li>'. $row['cat_title'] .'</li>';
OR
// Use double quotes like this...
echo "<li> $row['cat_title'] </li>";
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get useful error messages in PHP?
Ive started on part of my new year resolution and decided to learn php, as part of it im trying to parse in an xml feed, and echo out the name of the events wrapped in <a> tags linking them back to the events page on the xml feed's site.
I think ive got it all in but i cant seem to see why this isnt working im just getting a blank page, if some one could point me in the right direction it would be much appreciated, cheers
<?php
// F1 W/H xml feed
$xml = simplexml_load_file('http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=5&marketSort=HH&filterBIR=N');
foreach ($xml->response->williamhill->class->type as $type) {
$type_attrib = $type->attributes();
echo "<h2>".$type_attrib['name']."</h2>"; //Title - in this case f1 championship
} ?>
<ul>
<?php
foreach($type->market as $event) {
echo "<li>";
echo "<a href="$event_attributes['url']">";
echo $event_attributes['name'];
echo "</a>";
echo "</li>";
}
?>
</ul>
echo "<a href="$event_attributes['url']">";
try changing that line to
echo "<a href=\"".$event_attributes['url']."\">";
The Php parser is pretty funny about this. Usually you pick one and just stick to it, or use both single quotes and double quotes as you please. Just remember that strings with double quotes are parsed for variables.
$hello = "Hello";
echo "$hello master";
is the same as
$hello ="Hello";
echo $hello.' master';
When you are testing your PHP scripts, you'll find it useful to switch on errors - then PHP will actually tell you why it isn't showing you anything:
error_reporting(E_ALL);
Normally you will have missed a ; or mis-typed a variable name.
in your case the error is here:
echo "<a href="$event_attributes['url']">";
You have accidentally ended the string with a double quote, so PHP thinks the string ends here:
echo "<a href="
This is where using single-quotes can be very handy because your double quotes won't then close the string.
echo '<a href="' . $event_attributes['url'] . '">';
The main difference between single and double quotes in PHP is that double quotes has special clever parsing rules and single quotes doesn't. For example:
$myVar = "BLAH";
echo "Example $myVar"; // Example BLAH
echo 'Example $myVar'; // Example $myVar
In your unordered list, you should use a dot to concatenate your string, and escape your double quotes like this:
echo "<a href=\"".$event_attributes['url']."\">";
Instead of
echo "<a href="$event_attributes['url']">";
Your example throws and error because you haven't used proper string concatenation. However, even with correct concat, it would render as <a href=http://someurl>, and you'd need to add the double quotes according to html standard. Hence you have to double quote.
if you want to not be troubled by having to switch between using a ' or a " then i suggest using the php alternative syntax php alternative syntax
with the given code it would look like
<?php
// F1 W/H xml feed
$xml = simplexml_load_file('http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=5&marketSort=HH&filterBIR=N');
foreach ($xml->response->williamhill->class->type as $type) {
$type_attrib = $type->attributes();
echo "<h2>".$type_attrib['name']."</h2>"; //Title - in this case f1 championship
} ?>
<ul>
<?php foreach($type->market as $event):?>
<li>
<a href="<?php echo $event_attributes['url']; ?>">
<?php echo $event_attributes['name']; ?>
</a>
</li>
<? endforeach;?>
</ul>
one advantage this would bring is that it would produce cleaner code since you can clearly distiguish your php code from your html which is the presentational part at the price writing all those other <?php ?> and as what others would claim a performance degradation. the choice is yours
Change
echo "<a href="$event_attributes['url']">";
for
echo "<a href=".$event_attributes['url'].">";
You are missing the periods in your second echo, where you have your $event_attributes['url']
<?php
foreach($type->market as $event) {
echo "<li>";
echo "<a href=".$event_attributes['url'].">";
echo $event_attributes['name'];
echo "</a>";
echo "</li>";
}
?>
I would recommend you to enable your error log, it would allow you to know the line with problems in any of your scripts.
I have been using the following to add a dynamic link on a page I am writing, it works ok and appears how it should on the page but I cant help but think that I am going a bit backwards with the way its written as it looks messy. What is the correct way to write it, as if I put it all in one line it doesn't work ?..
echo '<a href="./customer-files/';
echo $customerID;
echo '/';
echo $filename->getFilename();
echo '">';
echo $filename->getFilename();
echo '</a>';
Try with
echo "{$filename->getFilename()}";
Here there is the documentation with a lot of examples of how to concatenate output.
I'd approach it like this:
$safe_customer_id = htmlspecialchars(urlencode($customerID));
$safe_filename = htmlspecialchars(urlencode($filename->getFilename()));
$safe_label = htmlspecialchars($filename->getFilename());
echo "$safe_label";
I would go with this:
$fn = $filename->getFilename();
$link = $customerID . '/' . $fn;
echo ''.$fn.'';
If you're using a template layer, it is even better to break out into PHP only when you need to:
<a href="./customer-files/<?php
echo $customerID . '/' . $filename->getFilename()
?>">
<?php echo $filename->getFilename() ?>
</a>
This way, your IDE will correctly highlight your HTML as well as your PHP. I've also ensured that all PHP is in single-line blobs, which is the best approach for templates (lengthy statements should be banished to a controller/script).
Concatenation is your friend. Use a . to combine multiple string expression into one.
echo ''.$filename->getFilename()/'';
Even better way would be
$filename = $filename -> getFilename(); //cache the filename
echo "<a href='/$customerId/$filename'>$filename</a>";
// ^ On this echo NOTICE that variables can be DIRECTLY placed inside Double qoutes.
Ok, it's Wordpress related and I know about Wordpress Stack Exchange, but I'm asking here because this is mostly a PHP question.
I want my code to display something or nothing using if statement.
The problem is I'm going to have a variable and bloginfo('template_directory') in-bulit function.
I wrote this:
<?php if (!empty($instance['example']))
echo "<li><img src="?><?php bloginfo('template_directory') ?><?php echo "/images/example.png /></li>"; ?>
It works fine until $instance['example'] is not empty, when is - it still displays the template directory link including images/example.png.
Any ideas?
I've tried " . bloginfo('template_directory') . " but doesn't seem to work.
PHP if statements that do not have braces { } will only evaluate the first line thereafter. To resolve this,
<?php if (!empty($instance['example'])) {
echo "<li><img src="?><?php bloginfo('template_directory') ?><?php echo "/images/example.png /></li>"; } ?>
Try that and see if it works for your needs. All I did was insert the braces so that your if statement spans all of your arguments.
You forgot to add the : after the if statement to make an if endif; block. Alternatively use the standard curly brackets to enclose all your commands in the if statement.
Currently it's only checking if for the first echo command.
Try this code:
<?php if (!empty($instance['example']))
echo "<li><img src=".get_bloginfo('template_directory')."/images/example.png /></li>";
?>
Try this
<?php if (!empty($instance['example'])) {
echo "<li><a href=". $example ."><img src="?><?php bloginfo('template_directory') ?>
<?php echo "/images/example.png /></a></li>";
}
?>
I'd personally use.
<?php
if( !empty( $instance['example'] ) )
echo '<li><img src="' . get_bloginfo('stylesheet_directory') . '/images/example.png" alt="" border="0" /></li>';
?>
First we add those missing attribute quotes.
Second we use the stylesheet path to ensure it points to the correct location for a child theme.
Third we call get_bloginfo to get a return value for the echo statement.
Fourth, i also added a border="0" to the image, borders aren't usually wanted for an image inside a link and also added an alt tag, because it will at least help pass HTML validation, even if you leave it empty.
Same answer as others, but with better formatted code:
<?php
if(!empty($instance['example']))
{
echo '<li><a href=' . $example . '><img src=' . bloginfo('template_directory') . '/images/example.png /></a></li>';
}
?>
Added brackets, removed unnecessary opening/closing php tags, and converted strings to single quotes since there are no variables or special characters contained within them that need processing.