what is wrong with this piece of code? html + php - php

i've a simple function, which has two parameters, one for the image url, and other for the attributes for the image
function image_found($url,$attributes)
{
if(#getimagesize($url))
{
echo '<img src="'.$url.'" '.$attributes.'/>';
}
else
{
echo '<img src="'.base_url().'/site_images/image_not_found.svg" '.$attributes.'/>';
}
}
now what i'm trying to do is create a clickable image, if the image is found, now this is the html code
echo '<div class="panel-body">';
echo '<div class="col-md-12 col-lg-12 col-sm-12 text-center">';
$url = base_url().'product_images/'.$result->product_image.'.'.$result->image_type;
$attributes = 'height="200px" width="100%"';
echo ''.image_found($url,$attributes).'';
echo '</div>';
echo '</div>';
and this is the output i'm getting
<div class="panel-body">
<div class="col-md-12 col-lg-12 col-sm-12 text-center">
<img src="http://localhost/nsc/product_images/7908076366784972032090.jpg" height="200px" width="100%"/>
</div>
</div>
i don't know what is wrong here, i'm using bootstrap

Just use return statements instead of echo in your function and your problem should be solved ;-)

The better way is verify if your image exists (remove #) and then return (instead of echo):
...
if(file_exists('your/path/to/image'))
return '<img src="'.$url.'" '.$attributes.'/>';
else
return '<img src="'.base_url().'/site_images/image_not_found.svg" '.$attributes.'/>'
...

When you need to return a value from a function, use return statement instead of echo
When echo is used the output immediately gets printed instead of getting returned to the place where the function call is. Here is an illustration.
function printer(){
echo 'second';
}
echo 'first'.' '.printer().' '.'last';
The Output:
secondfirst last
This is the exact same thing happening with your code. The echo in image_found() gets printed as
<img src="http://localhost/nsc/product_images/7908076366784972032090.jpg" height="200px" width="100%"/>
The rest of the echo statement gets printed as
So using a return statement should solve your problem

Related

Display and check for multiple key in php echo

I am trying to output a key value from JSON data and want to check for multiple keys.
I want to display the title of a movie and or tv show.
if($channel=="multi" AND !empty($movies)){
foreach ($movies as $movie) {
if(!empty($movie['poster_path'])){
echo '<div class="mov">
<a href="'.$movie['media_type'].'/'.$movie['id'].'">
<img src="https://image.tmdb.org/t/p/w185'.$movie['poster_path'].'">
<h2 class="title">'.$movie['title'].'</h2>
</a>
</div>';
}
}
}
whereas <h2 class="title">'.$movie['title'].'</h2> also needs to check for <h2 class="title">'.$movie['name'].'</h2>
How can I check for both 'title' and 'name' in the same go?
I am pretty new to all this and learning on the go. So please correct me as well if there are any mistakes in my coding!
If you're trying to get $movie['title'] or $movie['name'] (because you're unsure what the variable will be called) you can use an if statement. (This is not a pretty way of writing it -- but it is the clearest way).
if($channel=="multi" AND !empty($movies)){
foreach ($movies as $movie) {
if(!empty($movie['poster_path'])){
echo '<div class="mov">
<a href="'.$movie['media_type'].'/'.$movie['id'].'">
<img src="https://image.tmdb.org/t/p/w185'.$movie['poster_path'].'">
<h2 class="title">';
//AN IF STATEMENT TO CHOOSE EITHER TITLE OR MOVIE
if(!empty($movie['title']) {
echo $movie['title'];
} else {
echo $movie['name'];
}
echo .'</h2>
</a>
</div>';
}
}
}
Alternatively, you could also make $movie['title'] equal to $movie['name'] if it is not blank.
if($channel=="multi" AND !empty($movies)){
foreach ($movies as $movie) {
if(!empty($movie['poster_path'])){
// MAKE MOVIE TITLE = MOVIE NAME IF IT IS NOT BLANK
if(!empty($movie['name'])){
$movie['title'] = $movie['name'];
}
echo '<div class="mov">
<a href="'.$movie['media_type'].'/'.$movie['id'].'">
<img src="https://image.tmdb.org/t/p/w185'.$movie['poster_path'].'">
<h2 class="title">'.
$movie['title'].
'</h2>
</a>
</div>';
}
}
}
There are usually many ways to solve a problem. Think through the logic. If this than that. If not this, than that instead.

Column div moving below the column div with largest height above

I am using PHP loop to create and display articles (as 'col' divs) from the MySQL database. If I use 3 "col-6" divs, the 3rd one moves below both the above divs. I want it to stay just close to the div above it(the first col-6 div in this case). How can I achieve this?
I have figured out that this is a problem with bootstrap columns. Also, I cannot use absolute positioning in this case.
while($row=mysqli_fetch_array($run_query)){
echo '<div class="col-md-6 col-xl-4 blogColumn">';
echo '<a class="articleLink" href="show.php?blogId=';echo $row['id'].'" target="_blank">';
echo '<article>';
echo '<header>';
if($row['file_id']==null){
echo '<img class="img-fluid rounded focus" src="https://i.ibb.co/ZNDm012/logo.jpg"/>';
} else{
$fileId=$row['file_id'];
$q="SELECT * FROM uploads WHERE id='$fileId'";
$run_q=mysqli_query($con,$q) or die(mysqli_error($con));
$res=mysqli_fetch_array($run_q);
$path="uploads/".$res['name'];
if($res['type']=='image'){
echo '<img class="img-fluid rounded focus" src="'.$path.'"/>';
} else {
echo '<video class="articleVideo" src="'.$path.'" controls="controls">';
echo '</video>';
} }
echo '<h2>'.$row['title'].'</h2>';
echo '</header>';
echo '<p>'.substr($row['description'],0,100).'... Read More'.'</p>';
echo '</article>';
echo '</a>';
echo '<hr/>';
echo '</div>';}?>```
The full width of the page is defined by col-12.
So if you want to have 3 same width columns next to each other use col-md-4
<div class="col-md-4">...</div>
<div class="col-md-4">...</div>
<div class="col-md-4">...</div>
This will result in 3 equal width columns next to each other.

Nesting HTML/PHP statement inside another PHP statement

I have an IF/ELSE statement and I would like to print out some images that I am getting from my Drupal site. I can't figure out how to print those IMG tags without getting errors.
This is what I have so far:
<?php
$field = field_get_items('node', $node, 'field_visitor_image');
if($field){
<img src="<?php print image_style_url('lead_teaser', $node->field_visitor_image['und'][0]['uri']); ?>">
}
else
{
<img src="<?php print image_style_url('lead_teaser', $node->field_banner_image['und'][0]['uri']); ?>">
}
?>
You have to break out of PHP mode when you start outputting HTML.
if($field){
?>
<img src="<?php print image_style_url('lead_teaser', $node->field_visitor_image['und'][0]['uri']); ?>">
<?php
}
Use echo and string concatenation:
if ($field) {
echo '<img src="' . image_style_url('lead_teaser', $node->field_visitor_image['und'][0]['uri']) . '">';
}
You cannot nest
<?php > inside another <?php >.
One option for you could be to concatenate using ".".

Displaying an <img> tag inside a div class

I have a problem here that deals with the displaying of a photo inside a variable within a div class.
Here's my code below and help me find solutions. Thanks
if($newimage){
$url = 'http://www.client.jaobuilders.com/uploads/profile_picture/upload_photos/$newimage';
} else {
$url = 'http://www.client.jaobuilders.com/images/blank_photo.jpg';
}
return '
<div class="comment">
<div class="avatar">
'.$link_open.'
<img src="'.$url.'" width="50px" height="50px"/>
'.$link_close.'
</div>
$newimage is a variable and the value will depend on the user who logged in.
I really don't know what to do. Help me.
You have a problem with quotes - some of them are lacking. Also you did not close <div> tag:
return '
<div class="comment">
<div class="avatar">
'.$link_open.'
<img src="'.$url.'" width="50px" height="50px"/>
'.$link_close.'
</div>
</div>';
I can only hope this return statement is enclosed in some kind of function or method. Or at least it is the only return statement in a file that has been properly included somewhere (like $my_divs = include('some_file.php');).

PHP function problem

My PHP function for sidebar element generation looks like that
function makeSidebarEl ($side, $name, $lang, $db)
{
$title='title_'.$lang;
$txt='txt_'.$lang;
$query=$db->query("SELECT $title, $txt FROM sidebar WHERE side='$side' AND name='$name'");
$result=$query->fetch_array(MYSQLI_BOTH);
$title=makeTitle($result[$title], $lang, $db);
$txt=makeTxt($result[$txt], $lang, $db);
echo '<div class="nav">'.$title.$txt.'</div>'."\n";
}
But i'm getting result something like this
<div class="nav"></div>
<div class="nav_1"><img border="0" src=core/design/img/left_nav.png alt="" height="25px"/></div>
...
I mean it opens and closes <div class="nav"></div> at the begining of the every element but in fact function must echo result within this div:
echo '<div class="nav">'.$title.$text.'</div>'."\n"; How to fix that problem?
UPDATE
function makeTitle($title) {
echo '<div class="nav_1"><img border="0" src=core/design/img/left_nav.png alt="" height="25px"/></div>
<div class="nav_2">'.$title.'</div>
<div class="nav_3"><img border="0" src=core/design/img/left_nav.png alt="" height="25px"/></div>
<div style="clear:both;"></div>';
}
function makeTxt($txt) {
echo '<div id="parts" class="parts_txt">'.$txt.'</div>';
}
makeTitle() and makeText() should return strings rather than echo them.
As it is now, they echo text when they are called and return NULL, which is then concatenated into string when you call echo '<div class="nav">'.$title.$txt.'</div>'."\n";, i.e., that line evaluates to echo '<div class="nav">'.NULL.NULL.'</div>'."\n";
You need to replace echo to return at functions makeTitle and makeTxt

Categories