php simple concatenation problem - php

Here is a code sample of PHP function which prints HTML links. For some reason there is problem with the title attributte of the a tag(' games' isn't concatenated). For example if I have $gameCategorie = '3D' I get <a title='3D'>3D games</a> I want to get <a title='3D games'>3D games</a>
foreach($gamesCategories as $gamesCategorie){
$gameContent = $gamesCategorie.' games';
echo '<li><a title='.$gameContent.'>';
echo $gameContent;
echo '</a></li>'. PHP_EOL;
}
Ideas about improving the quality of the code and tutorials about HTML generation by PHP are also appreciated.

All valid xhtml should have attributes enclosed in speachmarks. Try this
foreach($gamesCategories as $gamesCategorie){
$gameContent = $gamesCategorie.' games';
echo '<li><a title="'.$gameContent.'">';
echo $gameContent;
echo '</a></li>'. PHP_EOL;
}

Related

having difficulty echo iframe with advanced custom fields wordpress

Hi guys this is a pretty dumb question im really bad with php and this is basically not working and outputting the wrong thing can someone please help me.
if (get_field("gallery")){
while (has_sub_field("gallery")){
if (get_row_layout() == "video"){
echo '<ul class="bxslider">';
echo '<li>';
echo '<iframe src="'.the_sub_field("videos").'" frameborder="0"></iframe>';
echo '</li>';
}
heres a screenshot of whats happening as you can see the URL is not in the iframe src
Load the_sub_field("videos") into a variable first then include that in the string you want to echo out.
$myVar = the_sub_field("videos");
echo '<iframe src="' . $myVar . '"....
Of course this will not work if you are not correctly return a string from the_sub_field("videos") so make sure that function actually returns a string.

Echoing PHP within HTML

I am currently trying to create a shopping cart for my website and I have images of products stored in a database and I want to include them within <img src> . By putting $get_row[imagesrc] within the src. I need to know the correct way to add it to the below code as I dont fully understand the ' and . tags
echo '<p>'.$get_row['name'].'<br/>'.$get_row['description'].'<br/>'.$get_row['imagesrc'].
'<br/>£'.number_format($get_row['price'],2).'Add</p>';
This should achieve what you're looking for:
echo '<p>'.$get_row['name'].'<br/>'.$get_row['description'].'<br/><img src="'.$get_row['imagesrc'].'" /><br/>£'.number_format($get_row['price'],2).'Add</p>';
The ' character defines a string literal when it is wrapped around a series of characters.
The . character is used for concatenating strings for output or storage.
echo '<p>'.$get_row['name'].'<br/>'.$get_row['description'].'<br/><img src="'.$get_row['imagesrc'].'"><br/>£'.number_format($get_row['price'],2).'Add</p>';
. concatenates two strings, and ' is wrapped around a string.
so
echo 'Hello '.'World'; // Shows Hello World
I'd split yours up to make it easier to read:
echo '<p>';
echo $get_row['name'].'<br/>';
echo $get_row['description'].'<br/>';
echo '<img src="'.$get_row['imagesrc'].'" /><br/>';
echo '£'.number_format($get_row['price'],2);
echo 'Add';
echo '</p>';
But it all looks OK.
echo '<p>'.$get_row['name'].'<br/>
<img src="'.$get_row['imagesrc'].'" alt="'.$get_row['name'].'"><br/>
<br/>£'.number_format($get_row['price'],2).'
Add</p>';`
echo '<img src="'.$get_row['imagesrc'].'">';
Try that.
A specific answer has been given:
echo '<img src="'.$get_row['imagesrc'].'">';
Nonetheless, it's worth adding that you should:
You should escape output - with htmlspecialchars() or otherwise.
echo '<img src="' . htmlspecialchars($get_row['imagesrc']) . '">';
Read the documentation on PHP Strings.
Check out this way of including PHP in your HTML. It's much easier to read and maintain. The last line in the paragraph is your image tag.
<p>
<?php echo $get_row['name']; ?><br/>
<?php echo $get_row['description']; ?><br/>
<?php echo $get_row['imagesrc']; ?><br/>
£<?php echo number_format($get_row['price'],2); ?>
Add
<img src="<?php echo $get_row['imagesrc']; ?>" />
</p>

using ' and " in php syntax [duplicate]

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.

Adding A Dynamic Link In Php

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.

How to rewrite this php function into HTML-emeddable function <?php ?>

Could someone convert this line of code to be readable by HTML?
echo '<h3>'. $r['title'] .'</h3>';
into something like this:
<?php echo...blah blah blah ?> /* To display the title in HTML */
I am sure I am not doing it right, that's why it's still not working :(.
Edit: There seems to be a confusion here. I am not going to modify the original php function. What I need to do is call it to my HTML page, to display the Title of the page
function r($text, $level = 3)
{
$tag = 'h' . $level . '>';
return '<' . $tag . $text . '</' . $tag;
}
Thanks for the downvote. The given question is totally unclear and constantly edited.
Ah you mean?
<php echo "<h3>$r['title']</h3>"; ?>
could be an answer to this unclear question
Save the result into a variable.
<?php $title = '<h3>'. $r['title'] .'</h3>';?>
<?php echo $title; ?>
Not exactly sure what you're asking, but you can't use PHP code within an HTML page.
The line
<?php echo '<h3>'. $r['title'] .'</h3>'; ?>
Within a PHP file, will print out the contents of $r['title'], within <h3> tags.
There is no function involved; $r is an associative array variable and title is a key to a particular value.

Categories