I have this code, that generates the menu from sql.
<footer class="footer-basic-centered">
<p class="footer-links">
<?php
$top_menuDown = mysqli_query($kapcs, "SELECT * FROM top_menu WHERE latszik = 1 AND (top_menu_place = 1 OR top_menu_place = 0) ORDER BY top_menu_sorrend ASC");
if(mysqli_num_rows($top_menuDown) > 0 )
{
while($top_nav = mysqli_fetch_assoc($top_menuDown))
{
echo '<a class="footer_menu_to_link" href="'.$host.'/'.$top_nav['top_menu_seo'].'" title="'.$top_nav['top_menu_nev'].'"><h5 class="kat_h5">'.$top_nav['top_menu_nev'].'</h5></a>'."\n";
}
}
?>
</p>
<h6 class="copy"><p class="footer-company-name"><?php echo date("Y"); ?> <?php echo $siteName; ?> © - Minden jog fentartva!</p></h6>
</footer>
As you can see it on the image i uploaded, at the first link, the <h5> tag is outside the <a>. The other links are okay.
Whats going on there?
You are putting a h5 in an a that is in a p. That is not going to do what you think.
The p will get closed automatically when you add a new block-level element like a h5.
I am having some issues exploding this title Song Artist – Song Name I am using the following code and having not very much luck.
$title2 = $html2->find('header.section-header h2',0);
$links = $title2->plaintext;
$str = explode ("–", $links);
$artist = preg_replace('#\[[a-zA-Z].*\]#','',$str[0]);
$song = preg_replace('#\[[a-zA-Z].*\]#','',$str[1]);
print '<div class="song"> <div class="options"> <a class="play" href="'.$url.'" data-url="'.$url.'" data-title="'.$artist.'"> </a> <a class="download" href="'.$url.'"> </a> </div> <div class="info"> <a class="direct" href="'.$url.'"> <div class="artist">'.$artist.'</div> <div class="title">A Rainy Night In Harlem (Freestyle)</div> </a> </div> </div>';
It should look like this when I display.
But instead it returns something that looks like this.
I found something that might interest you:
Add echo htmlentities($title)."<br>"; under $title2=$title->plaintext;, IN YOUR ORIGINAL code. Like this
$title2 = $title->plaintext;
echo htmlentities($title)."<br>";
Gives me: (for example:)
<h2 itemprop="name">Chris Brown – You Make Me This Way (I Got You) (LQ)</h2>
No - but a –
That is why the explode didn't work. You might get away with exploding on –
Checked it over here, and it seems to work.
Sorry for all the edits, I had a hard time displaying – :-)
You may use trim to remove any unwanted whitespaces:
<?php
$title2 = $html2->find('header.section-header h2',0);
$links = $title2->plaintext;
$str = explode ("–", $links);
$artist = trim($str[0]);
$song = trim($str[1]);
?>
<div class="song">
<div class="options">
<a class="play" href="" data-url="" data-title=""></a>
<a class="download" href=""></a>
</div>
<div class="info">
<a class="direct" href="">
<div class="artist"><?php echo $artist;?></div>
<div class="title"><?php echo $song;?></div>
</a>
</div>
</div>
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
I'm working on a project in which I want to display randomly any given number of results suppose, I have six <html> tags of image and I only want to display three randomly so that every time we refresh the page it show randomly any three image out of any six
I'm using html code for an example
<html>
<body>
<div class=1>
<a href="http://example1.com">
<div>
<img src="image1.jpg">
</div>
</a>
</div>
<div class=1>
<a href="http://example2.com">
<div>
<img src="image2.jpg">
</div>
</a>
</div>
<div class=1>
<a href="http://example3.com">
<div>
<img src="image3.jpg">
</div>
</a>
</div>
<div class=1>
<a href="http://example4.com">
<div>
<img src="image4.jpg">
</div>
</a>
</div>
<div class=1>
<a href="http://example5.com">
<div>
<img src="image5.jpg">
</div>
</a>
</div>
<div class=1>
<a href="http://example6.com">
<div>
<img src="image6.jpg">
</div>
</a>
</div>
</body>
</html>
Out of this Six Images I only want to show any three Images Each time through php. Is it possible and how can I do that?
Hope you may find any better solution.
Also i want to display Other tags like link inthe image and some more tags so that i can display images in a better way through css so I think it can be done by switch statement more easily
Let's say you have a array with all the images. From that list, we randomly get the keys for 3 of the images. We then through a loop echo out the img tag:
<html>
<body>
<?php
$images = [
'image1.jpg',
'image2.jpg',
'image3.jpg',
'image4.jpg',
'image5.jpg',
'image6.jpg'
];
// Selects 3 random array values and returns the key for each value
$randomkeys = array_rand($images, 3);
// Here we loop through the given index keys from the $images array.
// For each key we will then get the value from $images with the index $key
foreach ($randomkeys as $key) {
// I end with PHP_EOL (End of line) so the source code will look a bit prettier.
echo "<div class=\"image\"><img src=\"{$images[$key]}\"></div>".PHP_EOL;
}
?>
</body>
</html>
If something was unclear, let me know
Edit 1: Added more tags
It's not hard to add more tags to the output. If you know how to echo string and variables you should be able to easy add more tags or change them the way you want.
As you can see in the update I have added the class image to the , and made the link to the same path as the image so when you click it it will just open the image in the same window.
<?php
$images=array('image1.jpg','image2.jpg','image3.jpg','image4.jpg','image5.jpg','image6.jpg');
shuffle($images);
for($i=0;$i<=2;$i++){
echo '<img src="'.$images[$i].'" />';
}
?>
Just expanded Morten's code:
<html>
<body>
<?php
$images = array(
array('img' => 'image1.jpg', 'url' => 'http://example1.com', 'div' => 'class="d1"'),
array('img' => 'image2.jpg', 'url' => 'http://example2.com', 'div' => 'class="d2"'),
array('img' => 'image3.jpg', 'url' => 'http://example3.com', 'div' => 'class="d3"'),
array('img' => 'image4.jpg', 'url' => 'http://example4.com', 'div' => 'class="d4"'),
array('img' => 'image5.jpg', 'url' => 'http://example5.com', 'div' => 'class="d5"'),
array('img' => 'image6.jpg', 'url' => 'http://example6.com', 'div' => 'class="d6"')
);
// Selects 3 random array values and returns the key for each value
$randomkeys = array_rand($images, 3);
// Here we loop through the given index keys from the $images array.
// For each key we will then get the value from $images with the index $key
foreach ($randomkeys as $key) {
// I end with PHP_EOL (End of line) so the source code will look a bit prettier.
echo '<div class="1">' . PHP_EOL . '' . PHP_EOL . '<div ' . $images[$key]['div'] . '>' . PHP_EOL . '<img src="' . $images[$key]['img'] . '">' . PHP_EOL . '</div>' . PHP_EOL . '</div>' . PHP_EOL;
}
?>
</body>
</html>
Result:
https://3v4l.org/OAc6l
Like #AbraCadaver said.
<?php
// vim: set ts=4 sw=4 et:
$myImages = array_map(function($i) { return "/foo$i.jpg"; }, range(1, 6));
foreach (array_map('htmlspecialchars', array_map(function ($i) { global $myImages; return $myImages[$i]; }, array_rand($myImages, 3))) as $image)
echo "<img src=\"$image\"/>\n";
You can keep your src paths in an array. Then you can use array_rand() function to get random array index.
For example:
<?php
$allImages = ["images1.jpg","images2.jpg","images3.jpg","images4.jpg","images5.jpg","images6.jpg"];
//this will return random keys of $allImages array
$randomKeys = array_rand($allImages, 3);
foreach($randomKeys as $key){ ?>
<div><img src="<?php echo $allImages[$key] ?> " ></div>
<?php
}
?>
This is how you can do this using php
In this case, you mean if the page is already prepared ( may be the page is external webpage, or another page which is being already designed) i would use Simple Html Dom. Download and include the folder to the project.
example.html
<html>
<style>
img{
width:100px;
height:100px;
}
</style>
<body>
<div class=1>
<a href="http://example1.com">
<div>
<img src="image (1).jpg" >
</div>
</a></div>
<div class=1>
<a href="http://example2.com">
<div>
<img src="image (2).jpg" >
</div>
</a></div> <div class=1>
<a href="http://example3.com">
<div>
<img src="image (3).jpg" >
</div>
</a></div>
<div class=1>
<a href="http://example4.com">
<div>
<img src="image (4).jpg" >
</div>
</a></div>
<div class=1>
<a href="http://example5.com">
<div>
<img src="image (5).jpg" >
</div>
</a></div>
<div class=1>
<a href="http://example6.com">
<div>
<img src="image (6).jpg" >
</div>
</a></div>
</body>
</html>
myphp.php
/**echo '<style>
img{
width:100px;
height:100px;
}
</style>';**/
//using Simple HTML DOM. This file is in Simple Html Dom folder downloaded
include('simple_html_dom.php');
// get DOM from URL or file
$html = file_get_html('example.html');// you can write your external website file here www.external.com/index.html
// find all link
foreach($html->find('a') as $e)
//echo $e->href . '<br>';
$image_with_link['link'][]=$e->href;
// find all image
foreach($html->find('img') as $e)
//echo $e->src . '<br>';
$image_with_link['image'][]=$e->src;
//for debugging
//echo '<pre>';
//print_r($image_with_link);
//echo '</pre>';
//loop number of times, here i want to display three images
for($i=0;$i<3;$i++){
//get random key from array
$rand=array_rand($image_with_link['image'],1);
//print 3 images with its links
echo '<a href="'.$image_with_link['link'][$i].'" />';
echo '<img src="'.$image_with_link['image'][$i].'" />';
}
Treat the code as one big string. Remove <HTML>, </HTML>, <BODY>, and </BODY> from that string. You can add them back whenever you want. Next, explode the string around "<DIV ". For the array that is created, add "<DIV " to the start of each element. You now have an array containing each div section which has each image and link you want. You can then pick ones at random from that array and insert as needed:
// remove HTML and BODY tags
$remove1 = str_replace("<HTML>", "", $original);
$remove2 = str_replace("<BODY>", "", $remove1);
$remove3 = str_replace("</HTML>", "", $remove2);
$cleaned = str_replace("</BODY>", "", $remove3);
// explode remaining code around "<DIV " so we have each division for each image
$codeArray = explode("<DIV ", $cleaned);
// with our code sectioned in an array, add "<DIV " back to the start
// of each element in the array
for ($x = 0; $x < count($codeArray); $x++){
$codeArray[$x] =. "<DIV ";
}
// the $codeArray now has $x elements of the sections that contain the
// links and images you want.
// You can now randomly grab the div's that you want, by
// Shuffling that array, pick the first X images you want, and then
// echo back out the code you want.
shuffle($codeArray);
echo "<HTML>";
echo "<BODY>";
for ($x = 0; $x < $whateverNumberOfImagesYouWant; $x++){
echo $codeArray[$x];
}
echo "</BODY>";
echo "</HTML>";
I have this php array. Where in my database i have a field called body, and in that field there is some html code. Like this:
<h1>title</h1><img src="http://farm5.staticflickr.com/4075/4788694752_d03557765b_z.jpg" alt=""/>
Here is my php code:
<?php
$q = "SELECT * FROM journals ORDER BY timestamp DESC";
$r = mysqli_query($dbc, $q);
while($journal_list = mysqli_fetch_assoc($r)) { ?>
<div class="col-md-4">
<a class="list-group-item" href="journal.php?id=<?php echo $journal_list['id']; ? >">
<h4 class="list-group-item-heading"><?php echo $journal_list['body']; ´?></h4>
</a>
</div>
<?php } ?>
In the h4 im calling the body field in the database. But i only want the img in that field??
Try phpquery:
$src = phpQuery::newDocumentHTML($journal_list['body'])->find('img')->attr('src');
This is a little hack that you can use
$img = explode('img',$journal['body']);
$final_img = '<img '.$img[1];
Now echo image as
<h4 class="list-group-item-heading"><?php echo $final_img; ?></h4>