PHP - Print position of user in ranking page - php

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 } ?>

Related

How to echo the total of increment counter?

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.

While($query->fetch()) Twice

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>

Not able to align horizontal slider in a row

I have a horizontal carousel that is working fine, here is the code for it
Now i wish to fetch the data from database and display it that corousel. Following is the code that i used
<div class='row'>
<div class='col-xs-12'>
<div class="carousel slide media-carousel" id="media">
<div class="carousel-inner">
<div class="item active">
<div class="row">
<?php foreach($student as $student): ?>
<div class="col-md-3">
<a class="thumbnail" href="#"><?php echo $student->fullname;?></a>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
<a data-slide="prev" href="#media" class="left carousel-control">‹</a>
<a data-slide="next" href="#media" class="right carousel-control">›</a>
</div>
</div>
</div>
but the view is distorted and instead of all the slides sliding in one row, the view is coming like this
That's because unless the row is active, the other .item row gets the property display:none; If you were to remove the display:none; it should work as you want.
Create the class="row" inside the loop. Also keep in mind that the screen consists of 12 units only, so if you create a row of more than 12 units it will go to next line automatically. As you have created the loop that creates "col-md-3" for more than 4 times which sums more than 12, so it moves to next line.
For reference view this
As you have not given the php code, i am giving u sample of it
The array that you are getting divide it into a group of 4 (or as many as you want) like given below
$query = $this->db->get('student');
$r = $query->result();
$s = (array_chunk($r, 4));
return $s;
And then on the code you have given, change into following
<div class="carousel-inner">
<?php foreach($s as $key => $per_student): ?>
<?php if($key=='0'): ?>
<div class="item active">
<?php else: ?>
<div class="item ">
<?php endif; ?>
<div class="row">
<?php foreach($per_student as $student): ?>
<div class="col-md-3">
<a class="thumbnail" href="#"><?php echo $student->fullname;?></a>
</div>
<?php endforeach; ?>
</div>
</div>
<?php endforeach; ?>
</div>

Bootstrap 3 column with fetch array

I have the site set up to have 3 columns with image, under the image a title, and under title the description x 3.
If i was to input the words using php all shows fine but when i use the fetch array it breaks the columns and puts each one on a different row. My current code is:
<!-- /.row -->
<?
while($row = mysql_fetch_array($result)){
?>
<!-- Projects Row -->
<div class="row">
<div class="col-md-4 portfolio-item">
<a href="#">
<img class="img-responsive" src="../assets/img/tienda/<? echo $row["images"];?>" alt="">
</a>
<h3>
<? echo $row["top"];?>
</h3>
<p><? echo $row["desc"];?></p>
</div>
</div>
</div>
<hr>
<!-- /.row -->
<? } ?>
This answer will handle the possibility that more than 3 results will be returned by PHP, which would necessitate another row of 1-3 results. You'd have to add a .clearfix to your CSS that did clear:both which would safely clear the floats contained within the col-md-4.....and besides, it's just good practice to ensure floats don't propagate.
<?
$inc = 0; //This is your incrementor
while($row = mysql_fetch_array($result)){
if ($inc == 0) {
?>
<div class="row">
<?
}
?>
<div class="col-md-4 portfolio-item">
<a href="#">
<img class="img-responsive" src="../link/<? echo $row["images"];?>" alt="">
</a>
<h3>
<? echo $row["top"];?>
</h3>
<p><? echo $row["desc"];?></p>
</div>
<?
if ($inc == 0} {
?>
<div class="clearfix"></div>
</div>
<?
}
$inc = ($inc == 2)? 0 : $inc++; //Increment unless value = 2, then set to zero
}
?>
<hr>
try
<!-- /.row -->
<div class="row">
<?
while($row = mysql_fetch_array($result)){
?>
<!-- Projects Row -->
<div class="col-md-4 portfolio-item">
<a href="#">
<img class="img-responsive" src="../assets/img/tienda/<? echo $row["images"];?>" alt="">
</a>
<h3>
<? echo $row["top"];?>
</h3>
<p><? echo $row["desc"];?></p>
</div>
<!-- /.row -->
<? } ?>
</div>
<hr>

How to divide categories in rows of three columns

I have a foreach loop which retrieves the subcategories of the current category.
<?php $_categories = $this->getCurrentChildCategories(); ?>
<?php foreach ($_categories as $_category): ?>
<div class="item">
<?php echo $this->htmlEscape($_category->getName()) ?>
</div>
<?php endforeach; ?>
I want to divide the results in rows of three columns. The desired format is then:
<div class="row">
<div class="item">
content
</div>
<div class="item">
content
</div>
<div class="item">
content
</div>
</div>
<div class="row">
<div class="item">
content
</div>
<div class="item">
content
</div>
<div class="item">
content
</div>
</div>
...
---------------EDIT----------------
My code is now as follows. How and where do i close the row div?
<?php $_categories = $this->getCurrentChildCategories(); ?>
<?php foreach ($_categories as $_category): ?>
<?php if ($i % 3 == 0) { ?>
<div class="row">
<?php } ?>
<div class="item">
<h2><?php echo $this->htmlEscape($_category->getName()) ?> »</h2>
</div>
<?php $i++; endforeach; ?>
---------------EDIT 2----------------
Getting closer. This is the result:
<div class="category-list">
<div class="row"></div>
<div class="row">
<div class="item">
content
</div>
<div class="item">
content
</div>
<div class="item">
content
</div>
</div>
<div class="row">
<div class="item">
content
</div>
<div class="item">
content
</div>
<div class="item">
content
</div>
</div>
<div class="row">
<div class="item">
content
</div>
<div class="item">
content
</div>
<div class="item">
content
</div>
</div>
</div>
How do I get rid of the empty row?
The code is now as follows:
<div class="category-list">
<?php
$i=0;
echo '<div class="row">';
$_categories = $this->getCurrentChildCategories();
foreach($_categories as $_category):
{
if($i %3 ==0)
{ ?>
</div>
<div class="row">
<div class="item">
<h2><?php echo $this->htmlEscape($_category->getName()) ?> »</h2>
</div>
<? }
else
{ ?>
<div class="item">
<h2><?php echo $this->htmlEscape($_category->getName()) ?> »</h2>
</div>
<? }
$i++;
}
endforeach;
?>
</div>
---------------EDIT 3----------------
Solved by hiding the empty row via CSS.
You have to introduce az iterator (?) variable for example $i = 0
if ($i % 3 == 0) { //modulo
// use for whatever you want
}
In every cycle (maybe at the end of it, it depends on your real solution) you have to increment $i (++$i);
Hope this helps
<?php
$i=0;
echo"<div class="row" >
foreach($category as $cat)
{
if($i %3 ==0)
{
echo"</div><div class='row'> <div class='item'> Content </div>";
}
else
{
echo"<div class='item'> Content </div>";
}
$i++;
}
echo"</div>";
?>

Categories