trying to show the same content in page and also in fancybox - php

Im trying to fill my articles with content that is coming from the database.
I have my articles with: title, image, date, content and with a link (read more) to open a fancybox that will show the same content of this article but in the fancybox.
So I have my div id="show-container" that corresponds to the div that shows the content in fancybox.
This div have display:none in css and it only appears when the user click in the link with #show href and class="fancybox", here:
<a class="fancybox" href="#show">Read more</a>
But I want to show the same content in fancybox so Im fill this "show" div with the same info that I put in the article.
My articles are working fine, each article is with right name,image and content.
But when I click to open the fancybox of each article all the fancyboxs have the same content, that is the content of my first article.
Anyone there know how I can fix this?
$readPost = $pdo->prepare("SELECT * FROM posts ORDER BY date DESC LIMIT 0,4");
$readPost->execute();
$folder = '../images/';
while ($readPostResult = $readPost->fetch(PDO::FETCH_ASSOC))
{
echo '<article id="loop-news">';
echo '<img src="'.$folder.$readPostResult['thumb'].'" title="'.$readPostResult['title'].'"/>';
echo '<h2>';
echo '<a href="#show" class="x" >'.$readPostResult['title'].'</a><br />';
echo '</h2>';
echo '<p>'.$readPostResult['content'].'<a class="fancybox" href="#show">Read more</a></p>';
echo '<div id="show-container">';
echo '<div id="show">';
echo '<h2>'.$readPostResult['title'].'</h2>';
echo '<br />';
echo '<img src="'.$folder.$readPostResult['thumb'].'" title="'.$readPostResult['title'].'"/>';
echo '<p>'.$readPostResult['content'].'<br /></p>';
echo '<a class="button" href="index.html">Back</a>';
echo '</div>';
echo '</div>';
echo '</article>';
}
?>
I think I need to pass the id of each news when I click in my a href:
echo '<p>'.$readPostResult['content'].'<a class="fancybox" href="#show">Read more</a></p>';
Do you know How I can do that?? Because Im already using "#show#" in my link to open the fancybox..

You have to use class for css instead of id

Your ID values are meant to be unique to the element. Because you're generate the content in a loop, you will have multiple duplicate ID values in your elements. Try changing it to this:
$i = 0;
while ($readPostResult = $readPost->fetch(PDO::FETCH_ASSOC))
{
echo '<article id="loop-news-' . $i . '">';
echo '<img src="'.$folder.$readPostResult['thumb'].'" title="'.$readPostResult['title'].'"/>';
echo '<i class="fa fa-download"></i>';
echo '<h2>';
echo '<a href="#show" class="x" >'.$readPostResult['title'].'</a><br />';
echo '</h2>';
echo '<span>'.date('d/m/Y H:i',strtotime($readPostResult['date'])).'</span>';
echo '<p>'.lmWord($readPostResult['content'],270).'<a class="fancybox" href="#show-' . $i . '" id="showFancy-' . $i . '">Read more</a></p>';
echo '<div id="show-container-' . $i . '">';
echo '<div id="show-' . $i . '">';
echo '<h2>'.$readPostResult['title'].'</h2>';
echo '<br />';
echo '<img src="'.$folder.$readPostResult['thumb'].'" title="'.$readPostResult['title'].'"/>';
echo '<p>'.$readPostResult['content'].'<br /></p>';
echo '<a class="button" href="index.html">Back</a>';
echo '</div>';
echo '</div>';
echo '</article>';
++$i;
}
You'll notice, I've created an $i variable outside the while loop, and it appends the value of $i to all your ID elements. Once at the end of the loop, it will increase $i by 1, so that all your IDs in your loop will be unique.

Related

SQL While loop select all rows that have a column checked

I'm trying to do a featured car section in my used car lot website.
What I want to do is to use a while loop and select only the rows that have a specific column that returns true. For example, in my database, I have a column that is marked true if a checkbox on the submit form is ticked indicating that the car is supposed to be "featured".
In my while loop I only want those cars to appear in this particular section. Using the code below, I can return all vehicles in the inventory but no way to select only the ones with the "featured" column that returns a true value.
<?php
$sql = "SELECT * FROM inventory LIMIT 10";
$result = mysqli_query($conn, $sql);
while ($row = $result->fetch_assoc())
{
echo '<div class="slide">';
echo '<div class="car-block">';
echo '<div class="img-flex">';
echo '<a href="inventory-listing.php?vin='.$row['vin'].'">';
echo '<span class="align-center">';
echo '<i class="fa fa-3x fa-plus-square-o"></i></span></a>';
echo '<img src="images/inventory/'.$row['vin'].'-main.png" alt="" class="img-responsive">';
echo '</div>';
echo '<div class="car-block-bottom">';
echo '<h6><strong>'.$row['year'].' '.$row['make'].' '.$row['model'].'</strong></h6>';
echo '<h6>'.$row['body'].', '.$row['milage'].' miles</h6>';
echo '<h5>$ '.$row['price'].'</h5>';
echo '</div>';
echo '</div>';
echo '</div>';
}
?>
What would I be able to add to my sql query to filter it only the rows where the "featured" column returns true?
Alternative to Goleztrols suggestion, you could just insert an IF clause before echo-ing the results.
<?php
$sql = "SELECT * FROM inventory LIMIT 10";
$result = mysqli_query($conn, $sql);
while ($row = $result->fetch_assoc())
{
if ($row['featured']=="true")
{
echo '<div class="slide">';
echo '<div class="car-block">';
echo '<div class="img-flex">';
echo '<a href="inventory-listing.php?vin='.$row['vin'].'">';
echo '<span class="align-center">';
echo '<i class="fa fa-3x fa-plus-square-o"></i></span></a>';
echo '<img src="images/inventory/'.$row['vin'].'-main.png" alt="" class="img-responsive">';
echo '</div>';
echo '<div class="car-block-bottom">';
echo '<h6><strong>'.$row['year'].' '.$row['make'].' '.$row['model'].'</strong></h6>';
echo '<h6>'.$row['body'].', '.$row['milage'].' miles</h6>';
echo '<h5>$ '.$row['price'].'</h5>';
echo '</div>';
echo '</div>';
echo '</div>';
}
else
{
echo "nothing to be shown";
}
}

while looping w3-quarter in w3-row-padding

Here is the problem that while looping the php in while loop in w3-row-padding of w3 responsive layout . The layout breaks
Here is the source code
<?php
$r=0;
while($r<ceil($fetch_row_count/4))
{ ?>
<div class="w3-row-padding w3-padding-16 w3-center" style="clear:both" id="food">
<?php
while($row=mysqli_fetch_array($res))
{
?>
<div class="w3-quarter">
<img src="admin/uploads/<?php echo $row['image']; ?>" alt="noodles" style="width:50%">
<h3><?php echo $row['title']; ?></h3>
<p><?php echo $row['description']; ?> </p>
</div>
<?php
}
$r++;
}
?>
</div>
Thanks for reply and comments in advance
That bottom div was not being added for each of your padded containers.
The way the code is written you are adding a padded container and then adding your w3-quarter div for each of your result sets and then repeating that a bunch of times with what looks like to me the same set of data in each one.
What you are probably trying to accomplish is just making one padded div and then echo out your result set with the w3-quarter divs inside of it.
Here is your original way with the bottom div corrected:
<?php
$r=0;
while($r<ceil($fetch_row_count/4)) {
echo
'<div class="w3-row-padding w3-padding-16 w3-center" style="clear:both" id="food">';
while($row=mysqli_fetch_array($res)){
echo
'<div class="w3-quarter">' .
'<img src="admin/uploads/' . $row['image'] . '" alt="noodles" style="width:50%">' .
'<h3>' . $row['title'] . '</h3>' .
'<p>' . $row['description'] . '</p>' .
'</div>';
}
$r++;
echo
'</div>';
}
?>
Here is the way I think you are trying to do it: (Just guessing)
<?php
echo
'<div class="w3-row-padding w3-padding-16 w3-center" style="clear:both" id="food">';
$r = 0;
while($row=mysqli_fetch_array($res)){
echo
'<div class="w3-quarter">' .
'<img src="admin/uploads/' . $row['image'] . '" alt="noodles" style="width:50%">' .
'<h3>' . $row['title'] . '</h3>' .
'<p>' . $row['description'] . '</p>' .
'</div>';
$r++;
//I would not actually try to control how many results you add like this.
//I would get the results from a better formatted query.
if($r < ceil($fetch_row_count/4)){
break;
}
}
echo
'</div>';
?>

Random divider image between posts

for my wordpress based website I would like to display a random image between posts, not having an image after the last post.
I thought I could combine an echo inside a php, but it would appear that this is impossible.
Any ideas how to accomplish this ?
<?php
if (($wp_query->current_post + 1) < ($wp_query->post_count)) {
echo '<div class="post-item-divider" align="center"><img src="http://v2.fortherestless.com/images/clouds/<?php echo rand(1,20);?>.png" alt="Random Image" /></div>';
}
?>
Store the number first in a variable then insert it to the image url.
<?php
if (($wp_query->current_post + 1) < ($wp_query->post_count)) {
$image_num = rand(1, 20);
echo '<div class="post-item-divider" align="center"><img src="http://v2.fortherestless.com/images/clouds/' . $image_num . '.png" alt="Random Image" /></div>';
}
?>

Multiple divs with a repeater field of image

i´m trying to display multiple divs with the images of a repeater field (ACF plugins).
For example Div 1 with image 1 - Div 2 with image 2 - etc...
with this markup:
<div id="project" class="item">
<a href="#">
<img src="img/project1.jpg" alt="project1" width="240" height="173">
</a>
<div class="art_title">
<p>SWEET LIFE #1</p>
</div>
<div class="mask"></div>
</div>
So, repeating it thru the html I displayed all the images, but now i´m integratin it in the wordpress and I have the following problem:
I´m using the repeater field to get all the images, so with this code:
<?php $slides = get_field('project_thumbnails');
// Grabs the array
// Check if there is any data in the array before looping
if($slides) {
//we need to close this div
echo '<div id="project_slider" class="item">';
foreach($slides as $s) {
echo '<div class="aimagediv" >'; //adding the start tag of the div
echo '<a href="#">';
echo '<img src="'.$s['project_thumb'].'" alt="" />';
echo '</a>';
echo '</div>'; //CLOSING THE DIV JUST ADDED
}
echo '<div class="art_title">';
echo '<p>SWEET LIFE2</p>';
echo '</div>';
echo '<div class="mask">';
echo '</div>';
echo '</div>'; //closing the first div,not sure if you want this, its optional
}
?> <?php endwhile; // end of the loop. ?>
I get the images of the repeater but it displays all into the same div, it doesn´t create a new div id=¨project". So it shows like this:
And the markup created on the page it´s like all the images are into the same div.
<div id="project" class="item">
<a href="#">
<img src="img/project1.jpg" alt="project1" width="240" height="173">
<img src="img/project1.jpg" alt="project1" width="240" height="173">
</a>
<div class="art_title">
<p>SWEET LIFE #1</p>
</div>
<div class="mask"></div>
</div>
What i´m doing wrong?
<?php $slides = get_field('project_thumbnails');
// Grabs the array
// Check if there is any data in the array before looping
if($slides) {
//we need to close this div
foreach($slides as $s) {
echo '<div id="project_slider" class="item">';
echo '<div class="aimagediv" >'; //adding the start tag of the div
echo '<a href="#">';
echo '<img src="'.$s['project_thumb'].'" alt="" />';
echo '</a>';
echo '</div>'; //CLOSING THE DIV JUST ADDED
echo '<div class="art_title">';
echo '<p>SWEET LIFE2</p>';
echo '</div>';
echo '<div class="mask">';
echo '</div>';
echo '</div>'; //closing the first div,not sure if you want this, its optional
}
}
?> <?php endwhile; // end of the loop. ?>

Display multiple image on separated div with repeater field

I´m trying to display a list of div with images using the repeater field of ACF.
this is the code i´m using:
<?php $query = new WP_Query( 'post_type=artworks_post&posts_per_page=-1&order=DESC' ); ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php
$slides = get_field('project_thumbnails'); // Grabs the array
// Check if there is any data in the array before looping
if($slides) {
echo '<div id="project_slider" class="item">';
foreach($slides as $s) {
echo '<a href="#">';
echo '<img src="'.$s['project_thumb'].'" alt="" />';
echo '</a>';
}
echo '<div class="art_title">';
echo '<p>SWEET LIFE2</p>';
echo '</div>';
echo '<div class="mask">';
echo '</div>';
}
?>
<?php endwhile; // end of the loop. ?>
The problem with this is that it displays all images in the same div, instead of display every image of the repeater field in a new div.
what i´m doing wrong?
Im not a php expert but,i think is just a matter of missing closing tags for the first div, also im gonna put another div within the loop, it looks like this:
<?php $slides = get_field('project_thumbnails');
// Grabs the array
// Check if there is any data in the array before looping
if($slides) {
//we need to close this div
echo '<div id="project_slider" class="item">';
foreach($slides as $s) {
echo '<div class="aimagediv" >'; //adding the start tag of the div
echo '<a href="#">';
echo '<img src="'.$s['project_thumb'].'" alt="" />';
echo '</a>';
echo '</div>'; //CLOSING THE DIV JUST ADDED
}
echo '<div class="art_title">';
echo '<p>SWEET LIFE2</p>';
echo '</div>';
echo '<div class="mask">';
echo '</div>';
echo '</div>'; //closing the first div,not sure if you want this, its optional
}
?> <?php endwhile; // end of the loop. ?>
Another alternative would be this (just figuring out what's the resulting layout you're expecting):
<?php $slides = get_field('project_thumbnails');
// Grabs the array
// Check if there is any data in the array before looping
if($slides) {
//we need to close this div
foreach($slides as $s) {
echo '<div id="project_slider" class="item">';
echo '<a href="#">';
echo '<img src="'.$s['project_thumb'].'" alt="" />';
echo '</a>';
echo '</div>'; //CLOSING THE DIV JUST ADDED
}
echo '<div class="art_title">';
echo '<p>SWEET LIFE2</p>';
echo '</div>';
echo '<div class="mask">';
echo '</div>';
}
?> <?php endwhile; // end of the loop. ?>
Hopefully this would be enough.

Categories