Display users as HTML table - php

I want to display users of wordpress as a table just like
https://stackoverflow.com/users
The recommended function is wp_list_authors, but it is not clear how can customise
the layout.
Say I want to display users in cells of table 5 x 5 with get next page link.
Please advice.
Let me explain a bit.
Unlike get_users_of_blog wp_list_authors does not return an array. If it would - then
having an array I can build any table using foreach. But wp_list_authors builds anchor tags on its own and returns monolith html block. The only option to control layout would be passing some sort before and after tags. But this function does not provide this sort of functionality.

As far as i know, there's no function like get_authors(), but you can do it by a raw SQL query in a custom template:
Update: For pagination
I'm not sure if you can use the built-in WordPress pagination to do that, as the paged param only appear to posts. You could fill a global $post var in a loop or something... There's a lot of approaches, but let's go for the "PHP" one. =D
<?php
$i = 0;
$limit = 25;
$offset = ($o = trim($_GET['offset'])) ? $o : 0;
$users = $wpdb->get_results("SELECT user_nicename ... FROM {$wpdb->users} LIMIT $offset,$limit");
?>
<?php foreach ($users as $user) : ?>
<div class="someclass">
<?php echo $user->user_nicename; ?>
</div>
<?php if ($i++ % $number_of_columns == 0) : ?>
<div class="padder"></div>
<?php endif; ?>
<?php endforeach; ?>
To simulate the table appearance, just float "someclass" left and put a fixed width on it. The "padder" div (float left and width 100%) will ensure that the cells will be aligned by the highest one in that row.
And for the pagination links:
<?php $n = $wpdb->get_var("SELECT count(ID) FROM {$wpdb->users}"); ?>
<?php $o = $offset - $limit; ?>
<?php if ($offset > 0) : ?>
<a class="prev" href="?offset=<?php echo $offset - $limit; ?>">Previous</a>
<?php endif; ?>
<?php $o = $offset + $limit; ?>
<?php if ($o < $n) : ?>
<a class="next" href="?offset=<?php echo $o; ?>">Next</a>
<?php endif; ?>
Code from brain to keyboard. Not tested, again.
Hope it helps.

Related

pagination php is not working, getting stuck on the first one, even tho if i echo the variables that store the page data are correct

I am trying to add pagination to my CRUD PHP app but it's not working properly.
Trying to limit the data from my SQL database to 5 per page, but it's getting stuck to the first page. If I press the second one, stays on the first page data.
$page = (isset($_GET['page']) ? $_GET['page'] : 1);
$perPage = (isset($_GET['per-page']) && ($_GET['per-page']) <= 50 ? $_GET['per-page'] : 5);
$start = ($page > 1) ? ($page * $perPage) - $perPage : 0;
$sql = "select * from movies limit ".$start." , ".$perPage." ";
$total = $db->query("select * from movies")->num_rows;
$pages = ceil($total / $perPage);
And here I am looping through the pages in the HTML code below the PHP one.
<center>
<ul class="pagination">
<?php for($i = 1; $i <= $pages; $i++): ?>
<li>
<?php echo $i; ?>
</li>
<?php endfor; ?>
</ul>
</center>
I am using bootstrap class for pagination which is added with a script in the head. Any idea why I stay at the first page even when I press the second one?
Here is a photo from my web project, in the url bar u can see i am at the second page, but still showing the data from first. In my database i have 10 elements, so it should be 5 and 5.
Looks like you are using the wrong variable name in your link. It should be:
<a href="?page=<?php echo $i; ?>">
(note, page not pages. Also, remove the spaces which get encoded as %20 in the url)
Be vary careful of putting user-submitted data into your query though, google "SQL injection" to find out more.

Implementing a PHP counter inside of a while loop to wrap list items with ul elements

I am having difficulty implementing a PHP counter, to wrap my list elements in unordered list tags.
I want to wrap around every 3 list elements.
I have been trying to use these previous questions as a guide but to little avail.
easier way to get counter from while loop?
php loop counter bootstrap row
<?php
$counter = 0;
echo '<ul class="products latestCourses">';
while ( have_posts() ) : the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php $counter = $counter++; ?>
<?php if ($counter%3 == 0) echo '</ul><ul class="products latestCourses">'; ?>
<?php endwhile; // end of the loop. ?>
This is what I have so far the page template simply contains a list element.
Currently this code is wrapping every list item in an unorder list.
<?php $counter = $counter++; ?>
this line is wrong, use either
<?php $counter++; ?>
or
<?php $counter = $counter +1; ?>
Use this for increment by 1
$counter = $counter + 1;
or
$counter = $counter + n;
where 'n' is the desired number by which you want to increment

Simple pagination for foreach loop

I need a pagination for the output that is generated by a WordPress plugin. The plugin retrieves all products of a certain productgroup from the datebase. The basic code is:
<?php
foreach ((array)$this->view['data']['produkte'] as $p)
{ ?>
...some html code here
}
?>
where $p is an multi-dimensional array that contains the data for the single product.
I'd like to limit the output to, say, 10 products and create a simple pagination for that. The plugin is quite complex and uses nested templates, therefore a custom query is probably not an option in this case.
Would be great if somebody could point me in the right direction. Thank you!
Here's the final code. Thank you very much for your valuable help!!
<?php
$nb_elem_per_page = 3;
$page = isset($_GET['seite'])?intval($_GET['seite']-1):0;
$data = (array)$this->view['data']['produkte'];
$number_of_pages = intval(count($data)/$nb_elem_per_page)+2;
$page_no = $_REQUEST['seite'];
foreach (array_slice($data, $page*$nb_elem_per_page, $nb_elem_per_page) as $p)
{ ?> some HTML here... <?php } ?>
<?php if (count($data) > $nb_elem_per_page) { ?>
<ul id='paginator'>
<?php
for($i=1;$i<$number_of_pages;$i++){
if ($i == $page_no) {?>
<li><?php echo $i ?></li>
<?php }
else { ?>
<li><?php echo $i ?></li>
<?php }} ?>
</ul>
<?php { ?>
Since the default url contains already a query string, I had to modify the code a litte bit. I don't want the current site to have a link in the pagination. This gets me the number of the pagination query string:
$page_no = $_REQUEST['seite'];
I can then easily change the pagination links with a simple if-statement:
if ($i == $page_no) {...}?>
Thanks again!
You may have to tinker this around a bit but that will be something like that :
$nb_elem_per_page = 10;
$page = isset($_GET['page'])?intval($_GET['page']-1):0;
$data = (array)$this->view['data']['produkte'];
$number_of_pages = intval(count($data)/$nb_elem_per_page)+1;
<?php foreach (array_slice($data, $page*$nb_elem_per_page, $nb_elem_per_page) as $p) { ?>
...some html code here
<?php} ?>
<ul id='paginator'>
<?php
for($i=1;$i<$number_of_pages;$i++){?>
<li><a href='./?page=<?=$i?>'>$i</a></li>
<?php}?>
</ul>
You can use array_splice (twice) to get a new array with only the product you need.
Say you want to show 10 items per page and start at page 3:
<?php
$all_products = (array)$this->view['data']['produkte'];
$all_products = array_splice($all_products, 20); //select where to start
$all_products = array_splice($all_products, 0, count($all_products) - 10); //select how many products to show
foreach ($all_products as $p) {
//...some html code here
}
?>
(not tested)
Edit: #Loïc's answer is better as it only uses one array_splice.

Conditional styling for post containers in The Loop

My loop displays posts in two columns using this:
<?php
if (have_posts()): while (have_posts()) : the_post();
$count++;
?>
<?php if ($count == 1) : ?>
<div class="home-ci-row">
<div style="padding: 0px;" class="main-column-item-wrap">
CONTENT OF POST : Title, Thumbnail, Excerpt... etc
</div>
<div class="home-ci-gap"></div><!-- /* the gap */ -->
<?php elseif ($count == 2) : ?>
<div style="padding: 0px;" class="main-column-item-wrap">
CONTENT OF POST : Title, Thumbnail, Excerpt... etc
</div> <!-- main-column-item-wrap -->
</div><!-- /* home-ci-row*/ -->
<?php $count = 0; ?>
<?php else : ?>
// No posts
<?php endif; endwhile; endif; ?>
You can see that the <div class="home-ci-row"> opens in the first count & closes in the second one </div>
so when my loop has an even number of posts works great, but with odd number it doesn't close the div
so My idea is this: If loop has even number
If loop has odd number of posts
By the way, you can do something like:
<?php
$count=0;
while(have_posts()){
if($count%2==0){
echo '<div class="home-ci-row">';
//draw your left div here
}else if($count%2==1){
//draw your gap here
//draw your right div here
echo '</div>';
}
$count++;
}
//close div if count is an odd number
if($count%2==1) echo '</div>';
?>
Is it possible to swap to a for loop? Is this what you need?
for ($i = 0; $i < $numberOfElements; $i++)
{
//if (odd number) && (this is the last element)
if (($i % 0 == 1) && ($i == $numberOfElements - 1))
{
//Special Case
}
else
{
//Normal Case
}
}
Caveat: Watch out for errors, PHP isn't my strongest language
This question was answered on WP Development StackExchange by fischi
Quoting from his answer:
You could do this much easier. As you are making a layout that can be achieved by floats, there is no need to declare a Row every second time.
In my Code example I just youse the $count to determine the Class of the HTML-Element. In combination with the total Number of Posts displayed.
If the total number of posts $wp_query->post_count is reached by the $count AND the total number is odd, I give the Element the Class fullwidth. In the same way, I determine if it is the first or the second (see the IF-Statement).
All I need to do afterwards is output the corresponding Class for each HTML-Element in the Loop. Besides the Class, no Element is diffferent from each other.
I use the Modulo Operator in PHP ( % ) to determine odd/even. It delivers 1 if I use $count % 2 and $count is odd. If you are not sure about this operator, read about it here.
So your code could look like this:
<?php
$count = 0;
if (have_posts()): while (have_posts()) : the_post();
if ( ++$count == $wp_query->post_count && ( $wp_query->post_count % 2 ) == 1 ) {
// if final count is reached AND final count is odd
// full width item
$postclass = "fullwidth";
$opentag = '';
$closingtag = '</div>';
} else if ( ( $count % 2 ) == 1 ) {
// if $count is odd it is the first item in a 'row'
$postclass = "halfwidth first";
$opentag = '<div class="home-ci-row">';
$closingtag = '';
} else {
// second item in a row
$postclass = "halfwidth second";
$opentag = '';
$closingtag = '</div>';
}
?>
<?php echo $opentag; ?>
<div class="main-column-item-wrap <?php echo $postclass; ?>">
CONTENT OF POST : Title, Thumbnail, Excerpt... etc
</div><!-- main-column-item-wrap -->
<?php echo $closingtag; ?>
<?php endwhile; endif; ?>

Equivalent of ExpressionEngine switch tag in wordpress/php?

In ExpressionEngine while in their version of 'the loop' I can add a tag to any element like this:
<li class="{switch='one|two|three|four|five|six'}">
The first iteration of the li will have class one, the next is two, and the loop again after six. I'm needing this similar functionality in a wordpress site, but not sure how to accomplish that. Is there a built in wordpress function or will I need to code some sort of function in php?
Currently, using this in attempt at using #Leonard's solution, but the class 'four' is being repeated over and over instead of cycling
<?php
$argsGallery = array(
'post_type' => 'gallery',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$the_query = new WP_Query( $argsGallery );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();?>
<div class="<?php cycle('four|three|five|two|six|four'); ?> columns">
<div class="thumb">
<a class="jackbox"
data-group="images"
data-thumbnail="<?php the_field('image'); ?>"
data-title="Image One"
data-description="#description_1"
href="<?php the_field('image'); ?>"
>
<div class="jackbox-hover jackbox-hover-black">
<p><?php the_field('image_description'); ?> </p>
</div>
<img
src="<?php the_field('image'); ?>"
alt="responsive lightbox"
/>
</a>
</div>
</div>
<?php
endwhile;
wp_reset_query();
wp_reset_postdata();
?>
Found this question whilst looking for the exact same thing... top of Google 20mins after you posted it. Crazy... anyway!
I've come up with a function that I've tested (albeit quickly) that you can drop in to your functions.php and it works with a standard Wordpress loop. It may need adapting for some needs but hopefully it's a good start point.
It uses the current_post count from the $wp_query array and works out where it needs to be in the cycle values.
function cycle($input, $delimiter = '|', $query = false) {
if($query == false):
global $wp_query ;
$current_post = $wp_query->current_post + 1;
else:
$current_post = $query->current_post + 1;
endif;
$switches = explode($delimiter, $input);
$total_switches = count($switches) ;
$current_set = ceil( $current_post / $total_switches) ;
$i = (($current_post - ($current_set * $total_switches)) + $total_switches) - 1 ;
echo $switches[$i];
}
Then you can use it in a STANDARD loop like so:
<?php cycle('first|second|third|fourth'); ?>
Or you can custom delimit if needs be:
<?php cycle('first*second*third', '*'); ?>
Or if your using it with a CUSTOM wp_query you must use it like this with the query fed in as the third argument:
<?php cycle('first|second|third', '|', $the_query); ?>
I'm sure there is a tidier way to feed in that custom query, I'll keep looking and update if/ when I find a way!
This is known as a cycle function. Unfortunately, PHP does not have such a function natively.
You will need to custom write one, either by:
Creating a function. Here's a good example.
Directly in your loop with a counter and modulus. The following is a contrived example:
$cycles = array('one', 'two', 'three');
for ($i = 0; $i < 9; ++$i) {
echo $cycles[$i % 3];
}
I know enough WP and I never see something like that. Maybe that's why ExpressionEngine is payed (have already built-in solutions like that. In WP you must to do it at hand). Here an idea to make it by your own:
UPDATE: #jason-mccreary 's answer I know that I was wrong. I didn't know nothing about cycles. But here I have a snippet that seems to work like that:
function cycle($chunks)
{
if ( ! is_array($array = explode('|', $chunks)))
return;
static $i = 0;
echo $array[$i ++];
if ($i >= count($array))
$i = 0;
}
?>
<?php for ($i = 0; $i < 9; ++ $i) : ?>
<li class="<?php echo cycle('one|two|three|four|five|six'); ?>"></li>
<?php endfor; ?>
OUTPUT:
<li class="one"></li>
<li class="two"></li>
<li class="three"></li>
<li class="four"></li>
<li class="five"></li>
<li class="six"></li>
<li class="one"></li>
<li class="two"></li>
<li class="three"></li>

Categories