I start web development about 3 weeks ago. Today I ran into this issue where only the first while($query->fetch()) works, and the second one doesn't.
I searched around and I'm supposed to use a $rows array, but I guess I implemented it wrong since it didn't work.
<div class="container-fluid">
<div class="row">
<aside class="sidebar col-md-3 col-md-offset-1 col-md-push-8">
<div class="widget">
<h2>Recent Posts</h2>
<ul>
<?php while($query->fetch()): ?>
<li><?php echo "<a href='post.php?
id=$post_id'>$title</a>" ?></li>
<?php endwhile ?>
</ul>
</div>
</aside>
<div class="col-md-8 posts col-md-pull-4">
<div class="row">
<?php
while($query->fetch()):
$lastspace = strrpos($content, ' ');
?>
<article class="post-excerpt">
<div class="col-md-6 post">
<a href="#">
<img src="../img/blog_posts/<?php echo $image ?>" class="img-responsive">
</a>
<header>
<a href="#">
<h3><?php echo $title ?></h3>
</a>
<p class="post-meta">
<?php echo $category ?>
<span><?php echo $date ?></span>
</p>
</header>
<p>
<?php echo substr($content, 0, $lastspace)."<a href='post.php?id=$post_id'> ..Read More</a>" ?>
</p>
</article>
<?php endwhile ?>
</div>
</div>
<?php
if($prev > 0){
echo "<a href='blog.php?p=$prev'>Prev</a>";
}
if($page < $pages){
echo "<a href='blog.php?p=$next'>Next</a>";
}
?>
</div>
</div>
</div>
Query:
$query = $db->prepare("SELECT id, title, date, image, LEFT(content,500) AS content, category FROM blog INNER JOIN categories ON categories.category_id=blog.category_id order by id desc limit $start, $per_page");
$query->execute();
$query->bind_result($post_id, $title, $date, $image, $content, $category);
if you want to fetching the data twice form result set means you need to use $query->data_seek(0); before second while loop to reset the internal data pointer to 0 . otherwise you will get error .
update 1: use this one before second loop
$query->data_seek(0);
I solved it by using an array in the first loop and then echoing it in a for loop where the second while loop was.
<div class="container-fluid">
<div class="row">
<div class="col-md-8 posts">
<div class="row">
<?php
while($query->fetch()):
$lastspace = strrpos($content, ' ');
$link[] = "<li><a href='post.php?id=$post_id'>$title</a></li>";
?>
<article class="post-excerpt">
<div class="col-md-6 post">
<a href="#">
<img src="../img/blog_posts/<?php echo $image ?>" class="img-responsive">
</a>
<header>
<a href="#">
<h3><?php echo $title ?></h3>
</a>
<p class="post-meta">
<?php echo $category ?>
<span><?php echo $date ?></span>
</p>
</header>
<p>
<?php echo substr($content, 0, $lastspace)."<a href='post.php?id=$post_id'> ..Read More</a>" ?>
</p>
</div>
</article>
<?php endwhile ?>
</div>
<div class="col-md-6 pagination">
<?php
if($prev > 0){
echo "<a href='blog.php?p=$prev' id='previous'>Prev</a>";
}
if($page < $pages){
echo "<a href='blog.php?p=$next' id='next'>Next</a>";
}
?>
</div>
</div>
<aside class="sidebar col-md-3">
<div class="widget">
<h2>About me</h2>
<p>
I'm Yousef. Aspiring web, mobile, and soon-to-be game developer. Well I'm also a geek, a nerd, and I love comic books and comic book accesories. One day I will make the world's coolest website.
</p>
</div>
<div class="widget">
<h2>Recent Posts</h2>
<ul>
<?php
for ($x = 0; $x <= 2; $x++) {
echo $link[$x];
}
?>
</ul>
</div>
</aside>
</div>
Related
I have a 'while' statement on my WordPress website which grabs the latest posts. I want to group every 6 posts in an ordered list. Is it possible to wrap the <li> elements in a <ul> for every 6 items?
I haven't tried anything to do this yet as not sure if possible. My code below is.
<?php elseif(get_row_layout() == 'posts_section'): ?>
<section class="content posts">
<div class="row">
<div class="columns medium-12">
<ul class="medium-block-grid-3 small-block-grid-2" data-equalizer>
<?php
$catquery = new WP_Query('cat='. get_sub_field('category') .'');
?>
<?php
while($catquery->have_posts()) : $catquery->the_post();
?>
<li data-equalizer-watch>
<a href="<?php the_permalink() ?>" rel="bookmark">
<div class="overlay"></div>
<div class="content">
<p><?php the_title(); ?></p>
<div class="text-center"><?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?></div>
</div>
</a>
</li>
<?php
endwhile;
wp_reset_postdata();
?>
</ul>
</div>
</div>
</section>
I cant test the following bit of code since you dont provide any other information, but this is how I would solve this:
<?php elseif(get_row_layout() == 'posts_section'): ?>
<section class="content posts">
<div class="row">
<div class="columns medium-12">
<ul class="medium-block-grid-3 small-block-grid-2" data-equalizer>
<?php
$catquery = new WP_Query('cat='. get_sub_field('category') .'');
?>
<?php
$n = 0; //setting a count
while($catquery->have_posts()) : $catquery->the_post();
if ($n % 6 != 0) {
$n += 1 //keep on increasing till its divisble by 6
?>
<li data-equalizer-watch>
<a href="<?php the_permalink() ?>" rel="bookmark">
<div class="overlay"></div>
<div class="content">
<p><?php the_title(); ?></p>
<div class="text-center"><?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?></div>
</div>
</a>
</li>
<?php
}
if ($n % 6 = 0) {
?>
//end the ul tag here and start a new one
</ul>
<ul class="medium-block-grid-3 small-block-grid-2" data-equalizer>
<?php
}
endwhile;
wp_reset_postdata();
?>
</ul>
</div>
</div>
</section>
The $counter variable is working fine counting the loop, but I need to get the total amount of elements for each loop. How would I go about doing that?
<div id="<?php echo $term->slug; ?>" class="lity-hide resource-pop-up">
<?php
if($the_posts->have_posts()):
$counter = 1;
while($the_posts->have_posts()):
$the_posts->the_post();
//vars
$section_one = apply_filters('the_content', get_field('section_one'));
$section_two = apply_filters('the_content', get_field('section_two'));
$learn_more_link = get_field('learn_more_link');
?>
<section class="pop-up">
<div class="title">
<div class="brand">
<img src="https://via.placeholder.com/125x125" alt="Brand">
<?php the_title('<h3>','</h3>'); ?>
</div>
<aside>
<h4><?php echo $counter; ?>/<?php echo $counter->length; ?></h4>
</aside>
</div>
<div class="row pop-up-content">
<aside class="col-sm-12 col-md-6">
<?php echo $section_one; ?>
</aside>
<aside class="col-sm-12 col-md-6">
<?php echo $section_one; ?>
</aside>
</div>
<div class="learn-more">Learn More</div>
</section>
<?php
$counter++;
endwhile;
wp_reset_postdata();
endif;
?>
</div>
I should expect (number of element)/(total number of elements) or 2/10, basically like saying 2 of 10.
For the number of posts, you need
echo $the_posts->post_count
which is a total of all the posts, as opposed to
echo $counter->length
$counter is only a number and wouldn't have a length property anyway.
I am making a ranking page where I am displaying the username by descending order. The names already appear in the correct order but I am having trouble showing the position on the user, like 1º place, 2º place...
This is my code (simplified):
I tried to do a for loop (in every place I could think of) and print the value of $i, but it doesn't seem to work, either $i has always the same value in every position or all of them in the same position.
<?php
for($i=0;$i<8;$i++){
while($row = mysqli_fetch_assoc($result)) {
$names =$row['username'];
?>
<li>
<a href="#">
<div class="container">
<div class="image">
<svg></svg>
</div>
<div class="content">
<h2><?php print $i; // print the positions ?></h2>
<p><?php print $names ?></p>
</div>
</div>
</a>
</li>
<?php }} ?>
If your SQL orders the users in the correct order, then instead of having a loop, just have a counter and increment it each time (using $i++)...
<?php
$i=1;
while($row = mysqli_fetch_assoc($result)) {
$names =$row['username'];
?>
<li>
<a href="#">
<div class="container">
<div class="image">
<svg></svg>
</div>
<div class="content">
<h2><?php print $i++; // print the positions ?></h2>
<p><?php print $names ?></p>
</div>
</div>
</a>
</li>
<?php } ?>
<?php
$counter=0;
while($row = mysqli_fetch_assoc($result)) {
$names =$row['username'];
$counter++;
?>
<li>
<a href="#">
<div class="container">
<div class="image">
<svg></svg>
</div>
<div class="content">
<h2><?php echo $counter; ?></h2>
<p><?php print $names ?></p>
</div>
</div>
</a>
</li>
<?php } ?>
I am working on an application where i have a slider made in bootstrap but in slider there are two images in one slide show instead of one, so i have wrote a simple select query to get record but the problem is that it showing same images on boxes, i dont know how i can get next data in one loop
here is the code
<div class="carousel-inner">
<?php if($blog) : $counter = 0; foreach($blog as $blogs) : $counter++; ?>
<?php if($counter === 1) : ?>
<div class="item active">
<?php else : ?>
<div class="item">
<?php endif; ?>
<div class="row">
<div class="col-md-6">
<div class="panel">
<div class="panel-heading">
<h3>
<?php echo substr($blogs -> heading, 0, 30); ?> <br> <br> <br>
<p> <?php echo substr($blogs -> description, 0, 300); ?> <br><br>
Read the full story </p><br>
<img src="<?php echo base_url().$blogs -> image; ?>">
</h3>
</div>
</div>
</div>
<div class="col-md-6">
<div class="panel">
<div class="panel-heading">
<h3>
<?php echo substr($blogs -> heading, 0, 30); ?> <br> <br> <br>
<p> <?php echo substr($blogs -> description, 0, 300); ?> <br><br>
Read the full story </p><br>
<img src="<?php echo base_url().$blogs -> image; ?>">
</h3>
</div>
</div>
</div>
</div><!--.row-->
</div><!--.item-->
<?php endforeach; endif; ?>
</div><!--.carousel-inner-->
<a data-slide="prev" href="#blog" class="left carousel-control">‹</a>
<a data-slide="next" href="#blog" class="right carousel-control">›</a>
</div><!--.Carousel-->
the above is the result but i want the second image to be the next data from db
try this:
<div class="carousel-inner">
<?php if($blog) : $counter = 0; foreach($blog as $blogs) : $counter++; ?>
<?php if($counter === 1) : ?>
<div class="item active">
<?php else : ?>
<div class="item">
<?php endif; ?>
<div class="row">
<div class="col-md-6">
<div class="panel">
<div class="panel-heading">
<h3>
<?php echo substr($blogs -> heading, 0, 30); ?> <br> <br> <br>
<p> <?php echo substr($blogs -> description, 0, 300); ?> <br><br>
Read the full story </p><br>
<img src="<?php echo base_url().$blogs -> image; ?>">
</h3>
</div>
</div>
</div>
<?php endforeach; endif; ?>
What outputs if you post this?
I am able to display next and previous post in worspress but unable to show second-previous or third-previous || second-next or third-next like
1 - I want to show this too
2 - this is showing
3 - My Current post
4 - This is showing
5 - I want to show this too
any help would be appreciated.
In the and I am showing you my code so you can judge.
CODE:
<?php $next = get_permalink(get_adjacent_post(false,'',false)); if
($next != get_permalink()) { ?><a href="<?php echo $next; ?>">
<li class="col-xs-12 col-md-4">
<div class="article">
<div class="contain-image">
<?php $nextPost = get_next_post(true); $nextthumbnail = get_the_post_thumbnail($nextPost->ID); echo $nextthumbnail; ?>
</div>
<div class="content">
<div class="double-content">
<div class="information">
<span class="category"><?php echo get_cat_name(1);?></span>
<span class="time"><?php the_time('M j, Y') ?></span>
</div>
<div class="title">
<?php next_post_link('%link', "%title", TRUE); ?>
</div>
<p>
<?php
$Nextpost = get_next_post($id);
echo apply_filters(‘the_content’, $Nextpost->post_content);
?>
</p>
</div>
</div>
</div>
</li>
</a>
<?php } ?>
<?php $prev = get_permalink(get_adjacent_post(true,'',true)); if
($prev != get_permalink()) { ?><a href="<?php echo $prev; ?>">
<li class="col-xs-12 col-md-4">
<div class="article">
<div class="contain-image">
<?php $prevPost = get_previous_post(true); $prevThumbnail = get_the_post_thumbnail($prevPost->ID); echo $prevThumbnail; ?>
</div>
<div class="content">
<div class="double-content">
<div class="information">
<span class="category"><?php echo get_cat_name(1);?></span>
<span class="time"><?php the_time('M j, Y') ?></span>
</div>
<div class="title">
<?php previous_post_link('%link', "%title", TRUE); ?>
</div>
<p>
<?php
$Prevpost = get_previous_post($id);
echo apply_filters(‘the_content’, $Prevpost->post_content);
?>
</p>
</div>
</div>
</div>
</li>
</a>
<?php } ?>
WordPress provides several navigational template tags to make it easy for visitors to surf your pages. There are basically two different types of template tags used for chronological post navigation:
posts_nav_link() – for navigating various archive (non-single) pages
previous_post_link() & next_post_link() – for navigating single-post pages
<?php $posts = query_posts($query_string); if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php previous_post_link(); ?> | <?php next_post_link(); ?>
<?php endwhile; endif; ?>
try this:
global $post;
$post_curr = $post;
//get last post
$post_last1 = get_previous_post();
setup_postdata($post_last1);
//get second last post
$post_last2 = get_previous_post();
setup_postdata($post_curr);
//get next post now
$post_next1 = get_next_post();
setup_postdata($post_next1);
//get second next post
$post_next2 = get_next_post();
//reset current post data
setup_postdata($post_curr);