Ok, so I have the php echo working and pulling specific url's from the mysql database but this string that I got from an example is adding in single quotes to my href. So instead of being localhost/newdesign/about.php it is localhost/newdesign/'about.php'.
here is the code:
<p>
<?php
echo '' . $row['url'] . '';
?>
</p>
Thank You
It would be simpler if you use <?php echo only around the variables, not the literal HTML parts as well. It also looks like part of the onclick got lost when you were copying to SO.
<p><?php echo $row['url'] ?></p>
No need for the 2nd apostrophe.
"' . $row['url'] . '"
Related
I have this php line which works fine:
echo "<p>" . $post['message']. "</p>";
But I want to change it so it will link to my page (not to a single post). So it should look like that.
echo "<p>" . $post['message']. "</p>";
I have tried a lot many proposition gathered on different website, but each time I am getting an error.
Any idea ?
Thanks a lot!
Using single and double quotes, you avoid escaping issues. Try this:
echo '<p>'. $post['message']. '</p>';
i see that you didn't escaped from double quote that closes href attribute:
echo "<p><a href=\"https://www.facebook.com/rscmovement\" target=\"_blank\">"
I guess You have missed the back slash () before " after www.facebook.com/rscmovement.
"https://www.facebook.com/rscmovement\" "\"target=\"_blank\">" will
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>
This is the code I am working with:
echo '' . $post_type->labels->singular_name . '' .$markup;
It currently links correctly but shows the entire http string before the link.
Example: http://www.blah.com/blah/blahPortfolio. When it should be just "Portfolio".
Fresh eyes on this would be so helpful.
There are two errors that I can see:
<?php bloginfo('template_directory'); ?> If you are using echo it means <?php tag is already open so use only bloginfo('template_directory')
There are two closing tag for a tag (i.e. />) You use only one (i.e. >)
So the code will be like this:
echo '' . $post_type->labels->singular_name . '';
Try this code chunk and let me know --
echo '' . $post_type->labels->singular_name . '';
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.
I have some php code $_SESSION['username'] and I need to display it using html with something like <p>the username here</p>. I heard you might be able to do this using echo in PHP but Im not sure how. Thanks for any help.
echo '<p>' . $_SESSION['username'] . '</p>';
or
<p><?php echo $_SESSION['username'] ?></p>
You should really run it through htmlspecialchars to avoid breaking your HTML syntax (for example if the user's name contains </p> or something, or if they attempt an XSS attack):
echo '<p>' . htmlspecialchars($_SESSION['username']) . '</p>';
If I understood you correctly, I believe you are looking for:
<p><?php echo $_SESSION['username']?></p>
<?php echo '<p>'.$_SESSION['username'].'</p>'; ?>
Check out the documentation:
http://www.php.net/manual/en/tutorial.useful.php