PHP img alt attribute [duplicate] - php

This question already has answers here:
CodeIgniter img tag
(3 answers)
Closed 9 years ago.
I want to include an image alt attribute using PHP.
I currently have the following code...
<?php echo img(IMAGEPATH . 'logo.png'); ?>
How would I go about adding the alt attribute - "my awesome alt" - to this image using PHP so that when I go into view source I see something like this for the image?
<img src="http://examplesite.com/img/logo.png" alt="my awesome alt"/>
I am using codeigniter.

<?php echo img(array('src'=>'image/picture.jpg', 'alt'=> 'alt information')); ?>

Try like
<?php echo img(IMAGEPATH . 'logo.png',array('alt'=> 'alt information')); ?>

Related

PHP - onClick not working and calling "echo" Video and Image SRC using PHP tag [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
How can I combine two strings together in PHP?
(19 answers)
Closed 4 years ago.
I have a problem because I cannot call my data that I want to retrieve on a video src and image src. here is my code. image and video src are not showing.
can someone give me the correct syntax, please? thanks
few questions, do I need to concatenate something? do my rows[''] are concatenate correctly?
but my main problem here is I cannot click or change any videos that are retrieved or stored. how can I fix the onClick? thanks
<?php
echo '<li>
<a href="javascript:void();" onClick="document.getElementById("vid_frame").src="images/promvid/pal/<?php row['videos'] ?>">
<span class="vid-thumb">
<img width=72 src="images/promvid/philippines.jpg"/<?php row['image'] ?>
</span>
<div class="desc">Philippines<?php row['title'] ?>
</div></a></li>';
?>
MY ERROR
Parse error: syntax error, unexpected 'videos' (T_STRING), expecting
',' or ';
You cannot use PHP tags inside PHP tags you need to concatenate the string part with variable using echo.
<?php
echo '<li>
<a href="javascript:void();" onClick="document.getElementById("vid_frame").src="images/promvid/pal/'.$row['videos'].'">
<span class="vid-thumb">
<img width=72 src="images/promvid/philippines.jpg"/'.$row['image'].' </span>
<div class="desc">Philippines'.$row['title'].' </div></a></li>';
?>
This below code may help you.
In php we need use the variable by using $
echo '<li>
<a href="javascript:void();"
onClick="document.getElementById("vid_frame").src="images/promvid/pal/"'.$row["videos"].'>
<span class="vid-thumb">
<img width=72 src="images/promvid/philippines.jpg/"'.$row['image'].'>
</span>
<div class="desc">Philippines'.$row['title'].'</div>
</a>
</li>';
You are using bad practice and run into errors. You cannot have <?php ?> tags within <?php ?> tags.
Write clean code and make good practice to a habit.
PHP is an embedded language. Do not try to generate all HTML by PHP echo. Embed peaces of PHP code into a surrounding HTML template. You can even close PHP tags within a block of loop constructs.
Variables standing alone in a PHP tag are not output, except you are using shot-open tags <?=$variable?>. Short-open tags should not be used since most server configurations do not enable them.
Inline JavaScript is old school and considered to be bad practice. First of all inline on-handlers might be removed from the standards some day. Use event listeners instead.
You have block elements div inside the inline element a. This is valid only in HTML5. Ensure that your document type explicitly is HTML5 by prepending <!DOCTYPE html> as the very first line in the document.
Links doing nothing has been widely discussed if the href attribute should be # or javascript:void(0);. I tend to the latter. void is a keyword, however, the function style is fine. Regardless to the code style there has to be an argument.
even better: Do not use non-linking links at all. Use links with a valid fallback URL instead. In the event handler you can call the preventDefault() method wich will prevent the href action, i.e. location change.
PHP site:
<head>
<title>Non-Empty Title</title>
<body>
<ul>
<?php
// fake query result
$rows =
[
[
'videos' => 'phillippines.mpeg4',
'image' => 'philippines.jpg' ,
'title' => 'Philippines' ,
'noscript-frame-page' => 'philippines.html',
],
[
'videos' => 'usa.mpeg4',
'image' => 'usa.jpg' ,
'title' => 'USA' ,
'noscript-frame-page' => 'usa.html',
]
];
// fake fetch row
foreach ($rows as $row)
{
?>
<li>
<a class="video-ref"
href="<?php echo $row['noscript-frame-page'];?>"
data-video-src="images/promvid/pal/<?php echo $row['videos'];?>"
target="video-frame"
>
<span class="vid-thumb">
<img width=72 src="images/promvid/"<?php echo $row['image'];?>
</span>
<div class="desc"><?php echo $row['title'];?>
</div>
</a>
</li>
<?php
}
?>
</ul>
<noscript>
<iframe id="video-frame" name="video-frame" src="start-video-page.html"></iframe>
</noscript>
<div id="debug">DEBUG OUTPUT</div>
<script src="my-script.js"></script>
my-script.js
document.addEventListener('DOMContentLoaded', evt =>
{
"use strict";
const VideoLinkListener = evt =>
{
const videoSrc = evt.currentTarget.getAttribute('data-video-src');
if(videoSrc)
{
document.getElementById('debug').innerHTML = videoSrc; // document.getElementById('vid_frame').src=videoSrc;
// do not change location to link's href
evt.preventDefault();
}
};
document.querySelectorAll('a.video-ref').forEach( link => link.addEventListener('click', VideoLinkListener) );
});

Put php value in the end of a link [duplicate]

This question already has answers here:
How to echo inside html tags
(6 answers)
Closed 5 years ago.
Script 1.
add_action('asgarosforum_after_post_author', 'my_function_asgaros_cabinet', 10, 1);
function my_function_asgaros_cabinet($author_id) {
$userData = get_userdata($author_id);
if (!empty($userData) && !empty($userData->description)) {
echo $userData->description;
}
}
Script 2.
I want to put this in script 1 and put ''description'' in the end of that link.
How to do that and whats the right way/code? Cause i think my code is wrong.
<iframe src="http://test.com/me.php?sid='echo $userData->description'"></iframe>
Try with adding the php tag:
<iframe src="http://test.com/me.php?sid=<?php echo $userData->description?>"></iframe>
or
You can try to write the all iframe tag with php:
echo '<iframe src="http://test.com/me.php?sid='. $userData->description .'"></iframe>'

How to echo in variable url [duplicate]

This question already has answers here:
php - insert a variable in an echo string
(10 answers)
Closed 6 years ago.
I'm relatively new to php & was wondering if I can pass echo statement within variable. I have this in wordpress trying to retrieve profile picture using variable id to be able to integrate in wp comment section. Tried this.
$idd= author_details(get_the_ID());
$authordata = get_userdata( $idd );
$au=$authordata->display_name;
$authorAvatar = '<img src="http://www.example.com/photos/<?php echo getimg($au);?>" class="avatar user-1-avatar avatar-28 photo" width="40" height="40" alt="profile pic" >';
Here is the result image url I'm getting:
http://www.example.com/photos/%3C?php%20echo%20getimg($au);?%3E
It's apparent that I cannot pass echo statement within variable, but the sad thing is I'm unable to find solution for this simple stuff.
Note: The code works perfectly when they are put outside variable.
Voluntary help will be highly appreciated.
Thanks.
You don't need the PHP tags in the middle.
$var = getimg($au);
$authorAvatar = '<img src="http://www.example.com/photos/'.$var.'" class="avatar user-1-avatar avatar-28 photo" width="40" height="40" alt="profile pic" >';

PHP include() gets error [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
I have 35.php that includes common menu for my website. 36.php page should display the menu by using include()
Why doesn't this work?
My error:
35.php is shown below;
<?php
echo "Home
PHP
HTML
CSS <br />";
?>
36.php is shown below;
enter code here
<html>
<body>
<?php include("35.php"); ?>
<p>About Menu is imported from 35.php file</p>
</body>
</html>
You missused the ". They include the string but you also used them for the href. But that closes the string
echo "<a href="
This is where the String ends for PHP and it wants either a ; fo finish the statement or a . to connect it to another string.
So this is what you could to to solve it:
echo 'Home
PHP
HTML
CSS <br />';
With switching the "to a ' The string symbol is now the ' and therefor the " can be used int he string normaly.
Otherwise you could also escape the " like this \" inside the string:
echo "Home ...

Get All Strings between tags and echo them? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Get content between two strings PHP
Ok, here is my delima:
here is an example of my $content :
<!--nextpage-->First Title Here<!--nexttitle-->
some more text here
and here and here and here
a little more here!
<!--nextpage-->Second Title Here<!--nexttitle-->
some more text here
and here and here and here
a little more here!
<!--nextpage-->Third Title Here<!--nexttitle-->
some more text here
and here and here and here
a little more here!
How could I run a function that finds all "get_all_strings_between()" and echo them at the top of the $content:
**First Title Here** / **Second Title Here** / **Third Title Here**
and then my content HERE!
THanks!!
If I understood your question correctly this regexp will do it:
function get_all_strings_between($content) {
preg_match_all('/<!--nextpage-->([^<]*)<!--nexttitle-->/', $content, $m);
return implode(' / ', $m[1]);
}
// then you echo get_all_strings_between($content) where you need it
If I'm incorrect, please provide more info what are these nextpage and nexttitle tags? Are they appear like this literally?

Categories