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
} ?>
Related
I would like to remove the else statement in the following code.
How shall I convert the if statement, so it still works (route is correct/ logo is being shown)?
<div class="logo a">
<a href="<?=$site_url;?>">
<?php if(isset($settings['site_logo_image']) && $settings['site_logo_image'] != '') { ?>
<img src="<?=$site_url;?>_img/logo.png" class="image_logo" border="0" height="40" alt="<?=(isset($settings['site_logo']) ? $settings['site_logo'] : 'Hello');?>" />
<?php } else { ?>
<?=(isset($settings['site_logo']) ? $settings['site_logo'] : 'Hello');?>
<?php } ?>
</a>
</div>
If I leave out the if prefix, it's not working.
I am not a pro in php and actually have to do with frontend stuff...
Thanks
It's a bit tricky to read php interspersed with HTML. Once you get the knack of it, though, it's straightforward. Delete everything from else { to the next }. Like this.
<div class="logo a">
<a href="<?=$site_url;?>">
<?php if(isset($settings['site_logo_image']) && $settings['site_logo_image'] != '') { ?>
<img src="<?=$site_url;?>_img/logo.png" class="image_logo" border="0" height="40" alt="<?=(isset($settings['site_logo']) ? $settings['site_logo'] : 'Hello');?>" />
<?php } ?>
</a>
</div>
Pro tip A language-aware IDE like VSCODE or PhpStorm will alert you immediately if you make a mistake doing this sort of thing.
I've been editing the following block of code and now when I use it then the page will not load but I also don't get anything in the error log either. I am guessing this has happened due to a missing or misplaced character somewhere but have looked and looked over it and cannot spot what it is! does it stand out to anyone?
<?php if ($logourldecoded != "") { ?>
<?php if ($companyname != "") { ?>
<img src="images/site-logo-high-res.png" alt="logo" style="max-height:91px; max-width:372px; min-height:91px;" />
<?php } else { ?>
<div class="companynameclass" style="font-size: <?php echo $data[font_size]; ?>;">
<?php echo $companyname; ?>
</div>
<?php } else { ?>
<img src="<?php echo $logourldecoded; ?>" alt="logo" style="max-height:91px; max-width:372px; min-height:91px;" />
<?php } ?>
I also know it's not that any of the data I'm calling with $NAME_HERE being incorrect or missing as if i just echo them all indiviually they are exactly as expected and before my edits where showing.
Missing } to close the inner IF
<?php
if ($logourldecoded != "") {
?>
<?php
if ($companyname != "") {
?>
<img src="images/site-logo-high-res.png" alt="logo" style="max-height:91px; max-width:372px; min-height:91px;" />
<?php
} else {
?>
<div class="companynameclass" style="font-size: <?php echo $data[font_size]; ?>;">
<?php echo $companyname; ?>
</div>
<?php
// the missing line
}
?>
<?php
} else {
?>
<img src="<?php echo $logourldecoded; ?>" alt="logo" style="max-height:91px; max-width:372px; min-height:91px;" />
<?php
}
?>
You might consider not placing <?php ..?> tags on each line, it makes the code almost completely unreadable, and whats unreadable is by definition unmaintainable
Currently I am trying to loop through a directory grabbing all 14 file names and generate some html using the file names. I have run into some issue of creating HTML elements within PHP, and right now I'm just trying to generate an HTML div with an image in it and a line of text underneath 14 times.
I have tried putting empty php tags before and after the HTML elements, which didn't work because PHP does not allow php elements within another php element.
<?php
$dir = "classes/1961/*";
foreach(glob($dir) as $file)
{
<?php
?>
<div class="cell-1-9">
<img src="images/image.jpg">
<p>Dylan Miller</p>
</div>
<?php
?>
}
?>
I need another way of generating HTML elements within PHP.
You just need to close the <?php tag in order to revert back to html:
<?php
$dir = "classes/1961/*";
foreach(glob($dir) as $file)
{
?>
<div class="cell-1-9">
<img src="<?php echo $file ?>">
<p>Dylan Miller</p>
</div>
<?php
}
?>
<?php
$dir = "classes/1961/*";
foreach(glob($dir) as $file)
{
?>
<div class="cell-1-9">
<img src="images/image.jpg">
<p>Dylan Miller</p>
</div>
<?php
}
?>
Try the HEREDOC syntax.
I'm pasting the user1477388's corrected code: the closing HTML; must NOT be indented.
<?php
$dir = "classes/1961/*";
foreach(glob($dir) as $file)
{
echo <<<HTML
<div class="cell-1-9">
<img src="{$file}">
<p>{$file}</p>
</div>
HTML;
}
?>
As shown in the example in my MVC project, PHP-One, you can do something like this using what's known as HEREDOC syntax:
<?php
foreach($Model as $movie)
{
echo <<<HTML
<li class="list-group-item">
<strong>{$movie->Title}</strong> ({$movie->Rating}) - {$movie->ReleaseDate}
</li>
HTML;
}
?>
Adapted for your code, try something like:
<?php
$dir = "classes/1961/*";
foreach(glob($dir) as $file)
{
echo <<<HTML
<div class="cell-1-9">
<img src="{$file}">
<p>{$file}</p>
</div>
HTML;
}
?>
Ref. https://github.com/DominicArchual/Php-One#define-your-view
Footnote: Learning MVC will make you a better developer; also more valuable.
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>
Hi i would like to set several languages on my site, a have good looking php script but i have a problem with trigger it, my knowledge of php is very poor
language.php
header ("content-type: text/html; charset=utf-8");
if (isset($_GET['lang'])) {
$langID = $_GET['lang'];
setcookie('lang', $langID, time()+(3600*24*365));
}
elseif (isset($_COOKIE['lang'])) {
$langID = $_COOKIE['lang'];
}
else {
$langID = 'Pl';
}
and my index.php
<body>
<div id="languages">
<img src="images/en.png" />
<img src="images/pl.png" />
</div>
<div id="content">
<div id="tresc">
<?php if($langID == 'Pl'): ?>
<div class="editable" id="polska">
<h1>Head</h1>
<p>first land</p>
</div>
<?php else: ?>
<div class="editable" id="angielska">
<h1>Head</h1>
<p>second lang</p>
</div>
<?php endif; ?>
</div>
</div>
</body>
what I should put here to trigger script
<div id="languages">
<img src="images/en.png" />
<img src="images/pl.png" />
</div>
<img src="images/en.png" />
<img src="images/pl.png" />
And, for the sake of all that is holy, please check that the vale of $_GET['lang'] is acceptable before putting it in the cookie:
if (in_array($_GET['lang'], array('pl','en','fr')) {
$langID = $_GET['lang'];
} else {
// throw an error
}
If you don't do this your script will not be secure.