The following works fine to show a source image.
<html>
<h3>First Test</h3>
<img src="example1.php" />
</html>
But I wanted to validate user, then only show source image like following,
<html>
<h3>First Test</h3>
<?php
some logic = $usermatch
if($usermatch)
<img src="example1.php" />
?>
</html>
When I try the same it simply doesn't show image and doesn't accept <img src="example1.php" /> inside the PHP code.
I am a beginner and just learning php and html.
Could you please guide me how to make it work?
Thanks.
You have to switch in and out of PHP mode
<html>
<h3>First Test</h3>
<?php if($usermatch) { ?>
<img src="example1.php" />
<?php } ?>
</html>
And some like to use echo statements, but you'll see that you get less help from editors when editing the HTML
<html>
<h3>First Test</h3>
<?php
if($usermatch)
echo '<img src="example1.php" />';
?>
A little change to your code, although this is rather an ugly way to do it.
<?php
some logic = $usermatch
if($usermatch) {
?>
<img src="example1.php" />
<?php
}
?>
Don't miss the php closing and opening tags. Try something like this
<?php
some logic = $usermatch
if($usermatch) : ?>
<img src="example1.php" />
<?php
endif;
?>
Try this:
<?php
some logic = $usermatch
if($usermatch) { ?>
<img src="example1.php" />
<?php
}
?>
The problem is that you are mixing your HTML & PHP code. It is perfectly aceptable to have HTML code in your PHP file, but you need to close the PHP code block before so that the interpreter understands that it is not code you intend to run.
Might as well add to all of the answers:
<html>
<h3>First Test</h3>
<?php echo $usermatch ? '<img src="example1.php" />' : ''; ?>
</html>
Use the following:
<html>
<h3>First Test</h3>
<?php
some logic = $usermatch
if($usermatch) {
print '<img src="example1.php" />'
}
?>
</html>
or you can use another way to print html tags in php:
<html>
<h3>First Test</h3>
<?php
some logic = $usermatch
if($usermatch) {
?>
<img src="example1.php" />
<?php } ?>
</html>
Related
i am unable to concatenate i am not so good at it wondering how would i concatenate in this scenario
<?php $id = getfield('id'); ?>// this is a function to get fields from sql
<html>
<?php <a class="profile" href="profile.php?='$id' ">
<echo ucfirst ($firstname);</a> ?>//i cant seem to get this part
</html>
This is what i have tried so far i did try some other ways to do it but none of them seem to work
This is probably what you are looking for:
<?php $id = getfield('id'); ?> // this is a function to get fields from sql
<html>
<a class="profile" href="profile.php?id=<?php echo $id ?>">
<?php echo ucfirst ($firstname) ?>
</a>
</html>
Or, more compact if you have "short tags" enabled inside php:
<?php $id = getfield('id'); ?> // this is a function to get fields from sql
<html>
<a class="profile" href="profile.php?id=<?= $id ?>">
<?= ucfirst ($firstname) ?>
</a>
</html>
And finally you could inline the assignment, since the variable is used only once:
<html>
<a class="profile" href="profile.php?id=<?= getfield('id') ?>">
<?= ucfirst ($firstname) ?>
</a>
</html>
This should do the trick :
<?php
$id = getfield('id'); // this is a function to get fields from sql
echo '<html>
<a class="profile" href="profile.php?='.$id.' ">'.ucfirst ($firstname).'</a>
</html>';
?>
imagedisplay.php(view)
<html>
<body>
<h3>Your file was successfully uploaded!</h3>
<?php print_r($upload_data); ?> </br>
<?php $str=base_url()."images/".$upload_data['file_name'] ?> </br>
<?php $str=str_replace('http://','',$str) ?>
<?php echo $str; ?>
<img src= '$str'/> </br>
For echo $str; I got the string i need to display th image
but when i pass it to img src.... i am not able to display it on the browswer
Is there any syntactical error or am i missing anything ...pls help?
Just a small syntax problem here.
Embed PHP echo command in the HTML code, like so:
<img src="<?php echo $str; ?>"/> </br>
or embed PHP echo short tags:
<img src="<?=$str?>"/> </br>
In other words: insert the PHP output at the positions, where you need it as HTML content.
I am currently creating a CMS.
Currently I have.
* Saved my images in mysql as app_image
* Saved the images as a URL to where the images are located
But creating MY INDEX PAGE only displays my link as a broken URL.
my code for this page:
<?php
include_once('include/connection.php');
include_once('include/article.php');
$article = new article;
$articles = $article->fetch_all();
?>
<html>
<head>
<title>testing</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="container">
CMS
<ol>
<?php foreach ($articles as $article) { ?>
<li>
<a href="article.php?id=<?php echo $article['app_id']; ?>">
<img src="<?php echo $article['app_image']; ?>" height"100" width"100">
<?php echo $article['app_title']; ?>
</a> -
<small>
Posted: <?php echo date('l jS', $article['article_timestamp'] ); ?>
</small></li>
<?php } ?>
</ol>
<br><small>admin</small>
</div>
</body>
</html>
Can anyone see how I have gone wrong?
Thanks.
OK, I have done simalar thing and it is working just fine.
The code looks similar, and looks fine by me, now, maybe the link indeed is broken (maybe you didn't input the right upload link in DB)
I would go step by step and check that link (check if it is the right link). (with /path/name.ext)
If it is some help here is my case:
I put in DB post_id,post_title,post_contents, post_link
than i get that info with:
$query = $db->prepare ("SELECT bla bla FROM bla bla ORDER BY id DESC")
$query->execute();
$query->bind_result(everything that is selected seperated with ",");
(including $link)
<?php
while($query->fetch()):
?>
<a href="single-post.html" title="">
<img src="../images/<?php echo $link; ?>">
</a>
<?php
}
?>
NOW, the trick I did (to avoid problem is that i put inside DB only the name of file, the upload path is stored directly in HTML ("../images/")
Your code looks similar, and I think it should work, I think the problem is with link.
Var dump can come to the rescue here. Try this to see what the array key values should be set to for each of the elements in $article.
<?php foreach ($articles as $article) { ?>
echo '<pre>'; //just makes it a bit easier to read
var_dump($article); exit;
I am looking for some help using the PHP function. At the moment my website is structured like this:
index.php:
<?php
require_once( 'page_elements.php' );
?>
<body>
<?php echo content();?>
</body>
page_elements.php:
<?php
function content() {
?>
<div class='main'>
<img class='main' src="<?=$ImgName?>"> </img>
</div>
<?php
} ?>
if statement:
if (isset($_SESSION['basket_total']))
{
$basket_total = $_SESSION['basket_total'];
if($basket_total !== '0')
{
$ImgName = 'img/basket.php';
}
else
{
$ImgName = 'img/basket_empty.php';
}
}
What I want to do is to be able to define $ImgName in an if statement that isn't involved in the function content() but if i include it in another file, include 'if_statement.php' or in another function then it doesn't recognise the variable.
Sorry, its my first time structuring a website like this and I am finding it a bit confusing.
Cheers in advance
First of all, you don't close an an "img" tag with another "img" tag ...
function content(){
echo'
<div class="main">
<img class="main" src="'.$imgname.'" alt="" title="">
</div>
';
}
is the proper way of doing things. Now as to your question, I'm having trouble understanding your goal, but do you perhaps mean something a.la ...
function content(){
$imgname = include "file.php";
echo'
<div class="main">
<img class="main" src="'.$imgname.'" alt="" title="">
</div>
';
}
and the if_statement.php would be something like ...
if(isset($_SESSION['basket_total'])){
return $_SESSION['basket_total'];
}else{
return "img/basket.php";
}
This will get around the current issue you are having, but I would do like Ionut Flavius Pogacian suggested above and look into an MVC
<?php
require_once( 'page_elements.php' );
$image_name = "batman.jpg";
?>
<body>
<?php echo content($image_name);?>
</body>
page_elements.php:
<?php
function content($image_name) {
?>
<div class='main'>
<img class='main' src="<?=$image_name?>" />
</div>
<?php
} ?>
Following code will produce unwanted whitespace between icons.
<div>
<img src="icon1.png" />
<img src="icon2.png" />
</div>
I need to keep image tags on single lines because I have some conditions in my .phtml file, it looks something like this:
<div>
<?php if ($condition1) : ?>
<img src="icon1.png" />
<?php endif ?>
<?php if ($condition2) : ?>
<img src="icon2.png" />
<?php endif ?>
</div>
I don't want to have all code messed up on a single line. Is there any solution for situations like this?
Apply font-size:0px; style to your div.
You may use echo to output parts of html code. You'll get something like this
<div>
<?php if (true) :
echo '<img src="icon2.png" />';
endif;
if (true) :
echo '<img src="icon2.png" />';
endif;
?>
</div>