Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
It's been a long time since I have done php so sorry for the silly question.
This is my current code, I'm trying to save the URL as a variable so that I can insert it into the echo, but it doesn't seem to work as nothing appears:
<?php ob_start();
echo get_post_meta($post->ID, 'oldurl', true);
$old_url = ob_get_contents();
ob_end_clean();
?>
<?php echo do_shortcode('[fbcomments][fbcomments url="$old_url" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>
I have echoed $old_url and can see that it has the correct value, but how do I insert the value into the echo do_shortcode with url="$old_url"?
This doesn't work either:
<?php echo do_shortcode('[fbcomments][fbcomments url="echo $old_url;" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>
You'll need to switch your quotes around. Single quotes print everything out as-is. Double-quotes will process the variables. Also, echo is not needed within an echo.
<?php echo do_shortcode("[fbcomments][fbcomments url='$old_url' width='375' count='off' num='3' countmsg='wonderful comments!']"); ?>
Another way to do it without switching your quotes is to break out of the statement:
<?php echo do_shortcode('[fbcomments][fbcomments url="'.$old_url.'" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>
Variables are not replaced in single quotes ...
<?php echo do_shortcode('[fbcomments][fbcomments url="' . $old_url . '" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>
Singles quotes doesn't allow variables parsing.
For example :
$var = 'Hello';
echo 'The content of my var is : $var';
// Will output : "The content of my var is : $var"
echo "The content of my var is : $var";
// Will output : "The content of my var is : Hello"
So you have to use double quotes or use the concatenate operator : .
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I wrote this code
<html>
<head>
<title> page 1</title>
<body>
<style>
a{
margin-left:10px;
}
</style>
<?php
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con,"uoh");
$q = " SELECT * FROM student WHERE id = 201102820" ;
$result = mysqli_query($con , $q ) ;
if($row = mysqli_fetch_array($result)){
if ($row["major"]=="computer engineerig"){
echo "welcome ". $row["name"];
echo '<img src="tran.png"/>';
}
}
?>
</body>
</html>
but when I run it show me like this
Welcome stephen ICON
the icon(picture) that I put comes in front of the text .
Can I do like this
welcome stephen
ICON
I want the icon (picture) comes under the text.
HTML have a tag called Break :) you should echo this :
echo "welcome ". $row["name"];
echo "<br />";
echo '<img src="tran.png"/>';
you need a line break
echo "welcome ". $row["name"] . '<br/>';
echo '<img src="tran.png"/>';
or
echo '</p>' . "welcome ". $row["name"] . '</p>';
echo '<img src="tran.png"/>';
<a> and <img> are inline elements by default. See this article : CSS display: inline vs inline-block
So you have to put the ICON block in a block element to see a separation between the name and the icon.
Like this for example :
if ($row["major"]=="computer engineerig")
{
echo "welcome ". $row["name"];
echo '<p><img src="tran.png"/></p>';
}
Or adding a <br/> like suggested #Noor Adnan
<?php
//here you can add php
?>
<p>welcome <?php echo $row["name"]; ?> </p>
<br />
<img src="tran.png"/>
<?php
// here you can add your php
?>
If you Separate your HTML and PHP then you can easily add css and HTML inside your PHP.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
The simplest question but I can't get it working... What's wrong with the way I'm trying to add an image to this php file?
<?php header("HTTP/1.0 404 Not Found"); ?>
<?php defined('C5_EXECUTE') or die("Access Denied."); ?>
<h1 class="error"><?php echo t('Page Not Found')?></h1>
<?php echo t('We could not find a page at this address.')?>
<?php if (is_object($c)) { ?>
<br/><br/>
<?php $a = new Area("Main"); $a->display($c); ?>
<?php } ?>
<?php
echo "<img src="img.jpg">"
?>
<?php echo t('Back to Home')?>.
The file named img.jpg sits in the same directory as this .php file. When it runs, I see this error: Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in line 21 where line 21 is echo "<img src="img.jpg">".
instead of :
echo "<img src="img.jpg">";
you can do:
echo "<img src='img.jpg'>";
or
echo '<img src="img.jpg">';
or even escape the quote:
echo "<img src=\"img.jpg\">";
Two, or possibly three, things are wrong with the way you're adding an image.
You need to use different kinds of quotation marks (" vs '), otherwise they cancel each other out.
You need a ; to end the line in PHP
Your image path may be broken. If img.jpg is not in the same directory as the PHP script, it won't work.
Replace:
"<img src="img.jpg"/>"
with
"<img src='img.jpg'/>";
If the problem is with your image path, try using an absolute path (src="http://example.com/your/path/img.jpg") instead of the relative path (src="img.jpg"). If that works, then it means that the relative path was wrong.
You have a wrong quotation on the img line and then I suggest keeping it all in PHP. You can replace your code with this code which is more easy to read and maintain:
header("HTTP/1.0 404 Not Found");
defined('C5_EXECUTE') or die("Access Denied.");
echo '<h1 class="error">'. t('Page Not Found') .'</h1>';
echo t('We could not find a page at this address.');
if (is_object($c)) {
echo '<br /><br />';
$a = new Area("Main");
$a->display($c);
}
echo "<img src='img.jpg'>";
echo ''. t('Back to Home') .'.';
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Please let me know how to writte the below code so as to work because as it is it doesn't work
echo "<a href='$row['url']'>$row['link_text']</a>";
Write so you can read it next time. Also syntax highlight is better this way:
echo '' . $row['link_text'] . '';
You are using ' twice, so you need to escape them or just remove them in this case:
echo "<a href='$row[url]'>$row[link_text]</a>";
When you have to insert complex variables like array values inside strings, usually printf or sprintf is more clear and less error-prone.:
printf("<a href='%s'>%s</a>", $row['url'], $row['link_text']);
This will work:
echo "<a href='".$row['url']."'>".$row['link_text']."</a>";
Also this:
echo "<a href='{$row['url']}'>{$row['link_text']}</a>";
It's personal preference.
It's because you've put a ' inside another '.
echo "<a href='{$row['url']}'>{$row['link_text']}</a>";
or
echo "<a href='" . $row['url'] . "'>" . $row['link_text'] . "</a>";
Choose the one more to your liking.
You can try with.
echo "<a href='".$row['url']."'>".$row['link_text']."</a>";
Or
echo "<a href='{$row['url']}'>{$row['link_text']}</a>";
Or
echo ''.$row["link_text"].'';
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I need some little help on a simple thing!
I have this table in my database, where I have a number of images stored, called "covers".
And then I have a table in HTML that displays the covers of this MySQL table inside one of it's cells.
Like this:
<td><a href="movie.php?id=<?php echo $idt + 1; ?>">
<?php echo '<img src="'.htmlentities($idt + 1['cover'], ENT_QUOTES, 'UTF-8').'" alt="Cover" style="max-width:300px;max-height:300px;" />';; ?></a></td>
And this is the code before
$req = mysql_query("select id, name, year, genre, cover from movies");
$dnn = mysql_fetch_array($req);
$idt = $dnn['id'];
But why doesn't it work when I try to dynamically change id by putting this?
$idt + 1;
In order to output data from multiple rows, I suggest using a loop like this:
while ($row = mysql_fetch_assoc($req)) {
?><td>
<a href="movie.php?id=<?php echo $row['id']; ?>">
<img src="<?php echo htmlentities($row['cover'], ENT_QUOTES, 'UTF-8'); ?>" alt="Cover" />
</a>
</td><?php
}
You need to fetch a whole set of results and not just one id and increment it in the view. You should do a foreach loop on $idt like that:
foreach($dnn as $row){
$id=$row['id'];
$cover=$row['cover'];
$genre=$row['genre'];
//etc...
//now echo html with vars like this:
echo "<img src=\"$cover\"/>";
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying my best to describe this. I'd basically like to be able to have something like this in a url:
mydomain.com/?&name=Courtney
The name variable being Courtney. I would like to print it onto the page so when visited with the name=Courtney it'd state "Hello Courtney!" or "Hello Taylor!" Depending on who it is, if neither, don't display their name and just say "Hello!" I'm hoping someone here knows how to use a name in a url variable and is welling to share.
Simple in essence, but, I'm not sure how simple it is in code.
Directly from the PHP website:
echo 'Hello ' . htmlspecialchars($_GET["name"]) . '!';
if you want to check if the variable is available:
if (isset($_GET["name"])) {
echo 'Hello ' . htmlspecialchars($_GET["name"]) . '!';
} else {
echo 'Hello!';
}
<?php
if($_GET['name']){
echo "Hello " . $_GET['name'] . "!";
}else{
echo "Hello!";
}
?>
use
if (isset($_GET['name'])) {
echo "Hello ".$_GET['name']."!";
}
You could do it all in one line with a Ternary Operator
// Collect name if present //
$name = (isset($_GET['name'])) ? ' ' . ucwords(htmlspecialchars($_GET['name'])) : '';
// Echo output //
echo "Hello{$name}!";
// Output if name present //
Hello Courtney!
// Output if no name present //
Hello!
I'll provide a quick example. Complicated subject, simple code.
<?php
if(isset($_GET['name'])){
$name = $_GET['name'];
echo 'Hello ' . $name;
}
else{
echo 'No name supplied'
}
?>
if you want to use the current page url then after page load you can use
var address= document.URL
then extract the name (as url in your question is like 'mydomain.com/?&name=Courtney')
var name= address.substring(address.lastIndexOf('=') + 1)
if(name.length>0)
{alert("Hello "+name)}
else
alert("Hello")