CSS slide not working with images from database - php

I get a free website layout just to learn and keep training my php skills. The website has some slides in the index, and when it was index.html, getting the images from my computer it worked alright. But now i'm searching the images in the database, and instead of slides different images, it's sliding the same images and putting all the rest under the slides. Enter here to see what i mean gabrielozzy.zz.vc/vertigo.
Well in HTML and CSS it worked alright, why it's wrong now if i didn't change the html/css ?
Here's the code:
<?php
$slides = listarSlides($conexao);
foreach($slides as $slide){
echo'
<section id="dg-container" class="dg-container">
<div class="dg-wrapper">
<img src="images/' . $slide['imagem'] . '"alt="image1" />
<img src="images/' . $slide['imagem'] . '"alt="image2" />
<img src="images/' . $slide['imagem'] . '"alt="image3" />
<img src="images/' . $slide['imagem'] . '"alt="image4" />
<img src="images/' . $slide['imagem'] . '"alt="image5" />
<img src="images/' . $slide['imagem'] . '"alt="image1" />
<img src="images/' . $slide['imagem'] . '"alt="image2" />
<img src="images/' . $slide['imagem'] . '"alt="image3" />
<img src="images/' . $slide['imagem'] . '"alt="image4" />
<img src="images/' . $slide['imagem'] . '"alt="image5" />
<img src="images/' . $slide['imagem'] . '"alt="image1" />
<img src="images/' . $slide['imagem'] . '"alt="image2" />
<img src="images/' . $slide['imagem'] . '"alt="image3" />
</div>
</section>';
}
?>
functions.php:
<?php
function listarSlides($conexao){
$slides = array();
$query = "select imagem from slides order by cod_slides LIMIT 13";
$resultado = mysqli_query($conexao, $query);
while($slide = mysqli_fetch_assoc($resultado)){
array_push($slides, $slide);
}
return $slides;
}
?>
Thanks !

You're trying to loop over the whole div with single image multiple times, where as you have to just loop over the images.
So try to put the section> out of the loop and insert just instance of $silde['imagem'].
try this in your php:
<section id="dg-container" class="dg-container">
<div class="dg-wrapper">
<?php
$slides = listarSlides($conexao);
foreach($slides as $slide){
echo'
<img src="images/' . $slide['imagem'] . '"alt="image1" />
';
}
?>
</div>
</section>

Related

Put values of a foreach loop inside a div

<?php
$recent_posts = wp_get_recent_posts(array('post_type'=>'projet'));
foreach( $recent_posts as $recent ){
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> ';
if ( has_post_thumbnail( $recent["ID"]) ) {
echo get_the_post_thumbnail($recent["ID"],'thumbnail');
}
if (has_excerpt($recent["ID"]) ){
// echo get_the_excerpt($recent["ID"],'the_excerpt');
echo '<p class="excerpt">' . get_the_excerpt($recent["ID"],'the_excerpt') . '</p>';
}
}
?>
Hi, I would like to know how to put all my foreach loop inside a div ? I want each $recent_posts to be inside a div, right now they're one after the other. (btw it's my first post here, excuse me if i'm not clear)
##EDIT##
Here's my html :
<h2>Maquette UX/UI</h2> <img width="150" height="150" src="http://localhost/wordpress/wp-content/uploads/2021/04/vignette_maquette-150x150.jpg" class="attachment-thumbnail size-thumbnail wp-post-image"
alt="" loading="lazy" />
<p class="excerpt">Projet 1</p>
<h2>Intégration Baniera</h2> <img width="150" height="150" src="http://localhost/wordpress/wp-content/uploads/2021/04/vignette_baniera_homepage-150x150.jpg" class="attachment-thumbnail size-thumbnail wp-post-image"
alt="" loading="lazy" />
<p class="excerpt">Projet 2</p>
<h2>Intégration PHP</h2> <img width="150" height="150" src="http://localhost/wordpress/wp-content/uploads/2021/04/order_homepage-150x150.jpg" class="attachment-thumbnail size-thumbnail wp-post-image"
alt="" loading="lazy" />
<p class="excerpt">Projet 3</p>
<p class="excerpt"></p>
</div>
And I want every "group" to be inside a div, right now they're all together
Ok it works now, I wrapped the content of foreach like so :
<?php
$recent_posts = wp_get_recent_posts(array('post_type'=>'projet'));
foreach( $recent_posts as $recent ){
echo '<div class="foreach_wrapper">';
echo '<h2><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a></h2> ';
if ( has_post_thumbnail( $recent["ID"]) ) {
echo get_the_post_thumbnail($recent["ID"],'thumbnail');
}
if (has_excerpt($recent["ID"]) ){
// echo get_the_excerpt($recent["ID"],'the_excerpt');
echo '<p class="excerpt">' . get_the_excerpt($recent["ID"],'the_excerpt') . '</p>';
}
echo '</div>';
}
?>
You can echo a div around the foreach, a little side note, you are using a <li> element but forgot the <ul> wrapper please validate your HTML with a HTML validator.
<?php
echo '<div>'; /* I added this */
$recent_posts = wp_get_recent_posts(array('post_type'=>'projet'));
foreach( $recent_posts as $recent ){
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> ';
if ( has_post_thumbnail( $recent["ID"]) ) {
echo get_the_post_thumbnail($recent["ID"],'thumbnail');
}
if (has_excerpt($recent["ID"]) ){
// echo get_the_excerpt($recent["ID"],'the_excerpt');
echo '<p class="excerpt">' . get_the_excerpt($recent["ID"],'the_excerpt') . '</p>';
}
}
echo '</div>'; /* I added this */
?>

div img src not displaying random image

I have code for a random image in the php section of my page
$imagesDir = 'images/eggs/';
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$randomImage = $images[array_rand($images)];
it will display an image with this code
echo "<img src='$randomImage'>";
but when i try to do this in the body section of the page like this
<body>';
}
function template_body_above()
{
global $context, $settings, $options, $scripturl, $txt, $modSettings;
echo !empty($settings['forum_width']) ? '
<div id="wrapper" style="width: ' . $settings['forum_width'] . '">' : '', '
<div class="forest">
<div id="egg1"></div>
</div>
<div id="header"><div class="frame">
<div id="baloon1">
<img src="egg.php"/> //tried including seperate php code
<img src="<?php echo $randomImage ?>"> //php on same page
</div>
I am showing for the image source ...%3C?php...
Any help on where I'm going wrong ?
Try this way:
function template_body_above()
{
global $context, $settings, $options, $scripturl, $txt, $modSettings;
echo ( (!empty($settings['forum_width'])) ? '<div id="wrapper" style="width: ' . $settings['forum_width'] . '">' : '') .
'
<div class="forest">
<div id="egg1"></div>
</div>
<div id="header"><div class="frame">
<div id="baloon1">
<img src="' . $randomImage . '">
</div>

Display MySQL DB results in two different slides with RoyalSlider

I am using the RoyalSlider plugin (dimsemenov.com/plugins/royal-slider) to display the first 6 items from a MySQL database in the slider using PHP. I am using the LIMIT function to restrict each slide to 3 items so that the first slide displays items 1-3 from the DB and the second slide shows items 4-6. This works but it uses two separate SELECT / LIMIT queries which adds to page load time. Is it possible to merge the two separate LIMIT queries into one query?
The code I have so far is:
HTML / PHP
<div class="royalSlider rsDefault">
<div class="rsContent">
<ul>
<?php
$qry = mysql_query("SELECT * FROM properties LIMIT 0,3");
while($property = mysql_fetch_array($qry)) {
echo '<li><a href="/property.php?id='.$property['property_id'].'">
<img class="img-main" src="/files/'.$property['property_id'].'-1.jpg" title="" alt=""/>
<h2>'.$property['property_name'].'</h2>
<h3>'.$property['property_location'].'</h3>
</a>
</li>';
}
?>
</ul></div>
<div class="rsContent">
<ul>
<?php
$qry = mysql_query("SELECT * FROM properties LIMIT 3,3");
while($property = mysql_fetch_array($qry)) {
echo '<li><a href="/property.php?id='.$property['property_id'].'">
<img class="img-main" src="/files/'.$property['property_id'].'-1.jpg" title="" alt=""/>
<h2>'.$property['property_name'].'</h2>
<h3>'.$property['property_location'].'</h3>
</a>
</li>';
}
?>
</ul></div></div>
Please, try this code:
<div class="royalSlider rsDefault">
<div class="rsContent">
<ul>
<?php
$qry = mysql_query("SELECT * FROM properties LIMIT 0,6");
$i=1;
while ($i<=3 && $property = mysql_fetch_array($qry)) {
$i++;
echo '<li><a href="/property.php?id=' . $property['property_id'] . '">
<img class="img-main" src="/files/' . $property['property_id'] . '-1.jpg" title="" alt=""/>
<h2>' . $property['property_name'] . '</h2>
<h3>' . $property['property_location'] . '</h3>
</a>
</li>';
}
?>
</ul>
</div>
<div class="rsContent">
<ul>
<?php
while ($property = mysql_fetch_array($qry)) {
echo '<li><a href="/property.php?id=' . $property['property_id'] . '">
<img class="img-main" src="/files/' . $property['property_id'] . '-1.jpg" title="" alt=""/>
<h2>' . $property['property_name'] . '</h2>
<h3>' . $property['property_location'] . '</h3>
</a>
</li>';
}
?>
</ul>
</div>
</div>

getting 6 pics on one line

I am trying to have multiple lines of pictures with 6 pics in each line. Currently there are 4 pics on each line and I am not sure how to make it so I have 6. Any help would be very appreciated!
Here is my code:
<?php
$pageTitle = "Bright Punch Love";
$section = "home";
include('inc/products.php');
?>
<?php include('inc/header.php'); ?>
<div id="top"><img src="top.png" width="1337"></div>
<div class="shirts">
<ul class= "shirts-section">
<?php
foreach($products as $product_id => $product) {
echo "<li>";
echo '<a href="shirt.php?id=' . $product_id . '">';
echo '<img src="' . $product["img"] . '" width= 200 alt="' . $product["name"] . '">';
echo '<p id="details">$30 View Details</p>';
echo "</a>";
echo "</li>";
}
?>
</ul>
</div>
<?php include('inc/footer.php'); ?>
<div id="bottom-index"><img src="bottom-index.png" width="1337"></div>
I would suggest to use % instead of px. Also you use the width tag. Try to avoid using css in your html code. Use class="..." instead and include your css file in the head tag. I make the file now in the head, because it is easier for you. But I would recommend to make an file just for the css.
<head>
<style type="text/css">
.picture{
width : 15%;
}
</style>
</head>
echo '<img src="' . $product["img"] . '" class=\"picture\" alt="' . $product["name"] . '">';
Use PHP's array_chunk function to short your answer. Just Try.
$products_new = array_chunk($products, 6);
foreach($products_new as $product_id => $product) {
//Your code
}

conditional logic in the header.php

So I am using Wordpress and I have to have a specific logo on a specific page. From research I have to use conditional logic to swap the existing logo with another depending on the current page. Everything I have tried seems to just break the theme.. Any help on guiding me in the correct direction? So basically every page except page_id=79 would have the same logo in the header.
<a id="logo" href="<?php echo home_url(); ?>">
<?php
if(!empty($options['use-logo'])) {
$default_logo_id = (!empty($options['retina-logo'])) ? 'id="default-logo"' : null;
echo '<img '.$default_logo_id.' alt="'. get_bloginfo('name') .'" src="' . $options['logo'] . '" />';
if(!empty($options['retina-logo'])) echo '<img id="retina-logo" alt="'. get_bloginfo('name') .'" src="' . $options['retina-logo'] . '" />';
} else { echo get_bloginfo('name'); }
?>
</a>
<?php if ( is_page(79) ) { ?>
What to displayed on page 79.
<?php } else { ?>
What will be displayed everywhere else.
<?php } ?>
This should work.
Try using get_queried_object_id();
<a id="logo" href="<?php echo home_url(); ?>">
<?php
if(!empty($options['use-logo']) && get_queried_object_id() != 79) {
$default_logo_id = (!empty($options['retina-logo'])) ? 'id="default-logo"' : null;
echo '<img '.$default_logo_id.' alt="'. get_bloginfo('name') .'" src="' . $options['logo'] . '" />';
if(!empty($options['retina-logo'])) echo '<img id="retina-logo" alt="'. get_bloginfo('name') .'" src="' . $options['retina-logo'] . '" />';
} else { echo get_bloginfo('name'); }
?>
</a>
The url of your logo image is contained within $options['logo']. You should be able to modify this in the admin section of your WordPress installation (try looking in "Appearance -> Header").

Categories