I´m trying to style the following php echo
<a href="" title="<?php
echo"
<div class='test'>".$name."</div>'/n'
<div class='test2'>".$itemLevel."</div>'/n'
";
?>">Test Link</a>
But i think i don´t insert the div class and the '/n' at the right place. Could anyone help me?
You don't echo the HTML content inside the a href's title.
This will work:
<a href=""><?php echo"
<div class='test'>".$name."</div>'/n'
<div class='test2'>".$itemLevel."</div>'/n'
";
?>Test Link</a>
Optimizing your code:
<?php echo '
<a href="">
<div class="test">'.$name.'</div>
<div class="test2">'.$itemLevel.'</div>
Test Link</a>';
You must encode your html in order to put it into the title property
<?php
$title = '<div class="test">'.$name.'</div>' . PHP_EOL .
'<div class="test2">'.$itemLevel.'</div>' . PHP_EOL;
?>
Test Link
Related
Thanks for taking the time. I trying to make some image links inside of a DIV in html. The links are stored in a SQL DB. The links work just fine when outside of this specific div, but I really want to put them inside for styling purposes. The social images div is the one
echo "<div class=column-3>";
echo "<h3 id=\"artistNameLabel\"><strong>$artist_name</strong></h3>";
echo "<img id=\"artistImage\"src=\"imageuploads/$imageFilePath\">";
echo "<p><img alt=\"Artist Instagram\" src=\"images/instagramLogo.png\" width=\"25%\"></p>";//link doesnt work
echo "<p><img alt=\"Artist Streaming Account\" src=\"images/spotifyLogo.png\" width=\"25%\"></p>";//link doesnt work
echo "</div>";
The images appear just fine, but the link doesn't work. Also the text value that is the link is loaded just fine. Thanks for any help
echo "<div class=column-3>";
echo "<h3 id='artistNameLabel'><strong>".$artist_name."</strong></h3>";
echo "<img id='artistImage' src='imageuploads/".$imageFilePath."'>";
echo "<p><a href='".$artistInstagram."' target='_blank'><img alt='Artist Instagram' src='images/instagramLogo.png' width='25%'></a></p>";//link doesnt work
echo "<p><a href='".$artistStreaming."' target='_blank'><img alt='Artist Streaming Account' src='images/spotifyLogo.png' width='25%'></a></p>";//link doesnt work
echo "</div>";
You can try like this way :
$artistInstagram = "https://www.instagram.com/";
$artistStreaming = "https://www.example.com/";
$artist_name = "Test";
$imageFilePath = "https://via.placeholder.com/150";
$html = <<< EOT
<div class=column-3>
<h3 id="artistNameLabel"><strong>$artist_name</strong></h3>
<img id="artistImage"src=$imageFilePath>
<p>
<a href=$artistInstagram target="_blank">
<img alt="Artist Instagram" src="https://via.placeholder.com/150" width="5%">
</a>
</p>
<p>
<a href=$artistStreaming target="_blank">
<img alt="Artist Streaming Account" src="https://via.placeholder.com/150" width="5%">
</a>
</p>
</div>
EOT;
echo $html;
I think this will solve your problem
You need to style you anchor tag with "display:inline-block" and it will work fine. no matter if image is there or not.
So i am trying to pull product data from a database and have it listed out in a nice grid. I have got everything to work and its layout is very nice apart from one issue, the image URL isnt pulling properly:
<?php
while ($row = mysql_fetch_array($query))
$rows[] = $row;
foreach ($rows as $row){
$etitle = $row['title'];
$eprice = $row['price'];
$eurl = $row['prod_url'];
$eimage = $row['image'];
echo ('<div class="col-sm-6 col-md-4">
<div class="card">
<div class="card-image" style="background-image: url("' . $eimage . '");">
<div class="card-image-rating">');
echo ('£' . $eprice);
echo ('
</div><!-- /.card-image-rating -->
</div><!-- /.card-image -->
<div class="card-content">
<h2><a href="' . $eurl . '">');
echo $etitle;
echo ('</a></h2>
</div><!-- /.card-content -->
<div class="card-actions">
<i class="md-icon">favorite</i>
Show More
</div><!-- /.card-actions -->
</div><!-- /.card -->
</div><!-- /.col-* -->');
} ?>
In the source code and on page everything look good apart from their being no image. The column in the database has a URL of the image location in it but in the source code it appears to be removing the / from the path.
Any help would be amazing.
Thanks,
Based on your script, I noticed something goes wrong
<div class="card-image" style="background-image: url("' . $eimage .'");">
you use double quotation marks in style section
your html will look like this
<div class="card-image" style="background-image: url(" ");">
which is shouldn't be like that.
you have to use Apostrophe inside style, since u use quotation mark for style
your html should like this
<div class="card-image" style="background-image: url(' ');">
here, an example for you
https://jsfiddle.net/have_full/kass6ukr/
I am trying to get the title of a post using simple_html_dom the html roots can be seen below the part I am trying to get is titled This Is Our Title.
<div id="content">
<div id="section">
<div id="sectionleft">
<p>
Latest News
</p>
<ul class="cont news">
<li>
<div style="padding: 1px;">
<a href="http://www.example.com">
<img src="http://www.example.com/our-image.png" width="128" height="96" alt="">
</a>
</div>
<a href="http://www.example.com" class="name">
This is our title
</a>
<i class="info">added: Dec 16, 2015</i>
</li>
</ul>
</div>
</div>
</div>
Currently I have this
$page = (isset($_GET['p'])&&$_GET['p']!=0) ? (int) $_GET['p'] : '';
$html = file_get_html('http://www.example.com/'.$page);
foreach($html->find('div#section ul.cont li div a') as $element)
{
print '<br><br>';
echo $url = 'http://www.example.com/'.$element->href;
$html2 = file_get_html($url);
print '<br>';
$image = $html2->find('meta[property=og:image]',0);
print $image = $image->content;
print '<br>';
$title = $html2->find('#sectionleft ul.cont news li a.name',0);
print $title = $title->plaintext;
print '<br>';
}
The issue is here $title = $html2->find('#sectionleft ul.cont news li a.name',0); I assume I am using the wrong selector but I am literally not sure what I am doing wrong..
ul.cont news means "find <news> elements that are a child of ul.cont".
You actually want:
#sectionleft ul.cont.news li a.name
EDIT: For some reason, it seems simple_html_dom doesn't like ul.cont.news even though it's a valid CSS selector.
You can try
#sectionleft ul[class="cont news"] li a.name
which should work as long as the classes are in that order.
If this seems a little hacky, forgive me, but... you can always employ PHP to run a quick .js:
<?php
echo '<script>';
echo 'var postTitle = document.querySelector("ul.cont.news a.name").innerHTML;';
if (!isset($_GET['posttitle'])) {
echo 'window.location.href = window.location.href + "?posttitle=" + postTitle';}
echo '</script>';
$postTitle = $_GET['posttitle'];
?>
<?php
mysql_connect("localhost","username","password") or die (mysql_error());
mysql_select_db("databasename") or die (mysql_error());
$sqlmydata = mysql_query("SELECT id, label, title, info, body FROM table LIMIT 0,10");
// Build the Output Section Here
$outputListdata1 = '';
while($row = mysql_fetch_array($sqldata)){
$id = $row["id"];
$label = $row["label"];
$title = $row["title"];
$info = $row["info"];
$body = $row["body"];
$outputListdata1 .= '<div class="content1">
<a href= "#" >' .$title. '</a>
</div><!-- end of content1 -->
<div class="content2">
' .$info. '
</div><!-- end of content2 -->';
} // close while loop
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>my site</title>
</head>
<body>
<div class="container">
<?php print "$outputListdata1"; ?>
</div><!-- end of container -->
</body>
</html>
look at the line14 in $outputListdata1 where $title is placed in between href:
<a href="#" >' .$title. '</a>
i want to insert a $label variable in place of # inside the href. And so, it should display the result of the variable from my database.
<a href="$label" >' .$title. '</a>
For example:
if my database have the result for the $label variable is karthik means then it should look like(consider variable $title has the result as fan of stack overflow),
<a href="karthik" > fan of stack overflow</a>
i know my question maybe simple or tough but i'm 100% sure you guys come forward with an answer..thanks in advance.
(note: this is my second question so plz excuse it my questioning method is awkward)
You can write php in line with HTML, with the echo command you can output variables/strings into the HTML that will be sent to the user
<a href="<?php echo $var1; ?>" > <?php echo $var2; ?></a>
Just insert the $Label Var in the Attribute:
<?php
$outputListdata1 .= '<div class="content1">
<a href= "' .$label. '" >' .$title. '</a>
</div><!-- end of content1 -->
<div class="content2">' .$info. '</div><!-- end of content2 -->';
?>
You should learn the Basics of PHP, maybe do a Tutorial like this: php-for-the-absolute-beginner.
I just put the link in something like
$SomeLink="index.php";
then use the
echo "<div>$nam Your Password Could not be Found
<br><br>
<a href=$SomeLink>Click and Return to Login Page</a>
</div>";
with the double quotes on the ends it works on IE, Edge and Google Chrome
It isn't working because you encapsulated the variable with a single quote. Use a double quote to retrieve the value of the variable rather than the literal variable text.
$outputListdata1 .= "<div class='content1'><a href='$label'>$title</a></div><div class='content2'>$info</div>";
Hi use it in the same way as you use $title:
$outputListdata1 .= '<div class="content1">
<a href= "'.$label.'" >' .$title. '</a>
</div><!-- end of content1 -->
<div class="content2">
' .$info. '
</div><!-- end of content2 -->';
<a href = "$label" >' .$title. '</a>
Just paste that line of code in line 14 and this will work for you guaranteed ;)
<?php mysql_connect("localhost","username","password") or die (mysql_error());
mysql_select_db("databasename") or die (mysql_error());
$sqlmydata = mysql_query("SELECT id, label, title, info, body FROM table LIMIT 0,10"); // Build the Output Section Here
$outputListdata1 = '';
while($row = mysql_fetch_array($sqldata))
{
$id = $row["id"];
$label = $row["label"];
$title = $row["title"];
$info = $row["info"];
} // close while loop
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>my site</title>
</head>
<body>
<div class="container">
<div class="content1">
<a href= "#" ><?php echo $title;?></a>
</div><!-- end of content1 -->
<div class="content2">
<?php echo $info;?>
</div>
</div><!-- end of container -->
</body>
</html>
Insead of having to alter values for every item in the list as so
<li class="item drawing">
<a class="fancybox" rel="group" href="images/portfolio/skateboard_l.jpg">
<img src="images/portfolio/skateboard.jpg" alt="skateboard"/>
<h3>Skateboard</h3>
</a>
</li>
Is it possible to have something like
item="Skateboard
<li class="item drawing">
<a class="fancybox" rel="group" href="images/portfolio/[item-lowercase][if "item"_l.jpg exists, echo"_l"].jpg">
<img src="images/portfolio/[item-lowercase].jpg" alt="[item-lowercase]"/>
<h3>[item]</h3>
</a>
</li>
So I can just change the item variable for each item in the list rather than all the seperate entries. I assume this would be done using PHP or JS?
Thanks.
You want to use templates I guess.
If you want to do it client side in JS:
underscore
Mustache.js
Handlebars.js
The best solution that I can think of is to build an array (I would use PHP)... then use a while loop to build your list... put it all in a function and call it wherever you need it...
For example:
<?php
function itemList(){
$items=array ("skateboard","rollerskates","scooter","rollerblades");
reset($items);
while (list(, $value) = each($items)) {
echo '<li class="item drawing">';
echo '<a class="fancybox" rel="group" href="images/portfolio/' . $value . '_l.jpg">';
echo '<img src="images/portfolio/' . $value . '.jpg" alt="' . $value . '"/>';
echo '<h3>' . $value . '</h3>'; echo '</a>';
echo '</li>';
}
}
?>
Then in your HTML file (where you want the list to be displayed):
<ul><?php itemList(); ?></ul>
If you put the function in a separate .php file, you have to include it in the HTML document:
<?php include ('/url/of/list.php'); ?>
You can do this in PHP using echo.
For instance, if the image name was stored in $images["myImage"] and the alt was $imageAlts["myImage"
<img src="images/portfolio/<?php echo $images["myImage"]; ?>.jpg" alt="<?php echo $imageAlts["myImage"]; ?>"/>
Looks like AngularJS would help as well. It lets you do stuff like this:
<!doctype html>
<html ng-app>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js"></script>
</head>
<body>
<div>
<label>Name:</label>
<input type="text" ng-model="yourName" placeholder="Enter a name here">
<hr>
<h1>Hello {{yourName}}!</h1>
</div>
</body>
</html>
Smarty is a good template system for PHP. If you'd like to use PHP for templating over JavaScript, just start with one of the tutorials there (or look into other PHP template systems).
A classic way to do this in PHP would be to create an array of items in PHP and then iterate over the array, creating the HTML list items with the appropriate [item-lowercase] entries.
I would consider doing the [if item exists] as part of the process that builds your data array so you don't have to do anything complicated when you build your html. Then, just loop through your array and display whatever is in $theItem.
This is, of course, a simplification.
foreach($itemList as $key=>$theItem){
?>
<li class="item drawing">
<a class="fancybox" rel="group" href="images/portfolio/<?php echo $theItem ?>
<img src="images/portfolio/[item-lowercase].jpg" alt="[item-lowercase]"/>
<h3>[item]</h3>
</a>
</li>
<?php
}