Using php to add classes to looped data in WP e-commerce - php

In WP e-commerce, I have the following in my products page template:
<ul id="product-categories">
<?php $counter = 1; ?>
<?php wpsc_start_category_query(array('category_group'=>get_option('wpsc_default_category'), 'show_thumbnails'=> get_option('show_category_thumbnails'))); ?>
<li class="four columns<?php if ($counter % 4 == 1){echo ' alpha';}else if ($counter % 4 == 0){echo ' omega';} ?>">
<?php $counter++ ; ?>
<a title="<?php wpsc_print_category_name(); ?>" href="<?php wpsc_print_category_url();?>">
<?php wpsc_print_category_image('auto', 'auto'); ?>
<span><?php wpsc_print_category_name(); ?></span>
</a>
</li>
<p><?php echo $counter ?></p>
<?php wpsc_end_category_query(); ?>
</ul>
As you can see, for every 4n-3th item, I am trying to add an alpha class, and to every 4nth item, I am trying to add an omega class to my list items, such that the display works with a grid framework.
My issue is, start_wpsc_category_query does not appear to be 'looping' like a standard WP loop, as my echoed counter value remains at 2 for every item, hence no class is added.
Does anyone know how to fix this code to make the counter value increase each time, or have a better (PHP) way of addressing the problem if you've dealt with WP e-commerce before?
Another way of approaching would be to write a foreach loop for WP e-commerce categories, if anyone knows how to do this.
Thanks a million in advance.

Related

How to fix padding being added to css grid element automatically as one side of the grid start increasing more than the other side

Please am new in computer programming and am using php to develop a music website but i ran into a problem to keep occuring in different project that i have being building.
The problem is when ever am using a grid system either a css grid or a bootstrap grid system the grid keeps breaking due to padding to keeps adding automatically to any side of the grid as i uses the foreach loop to loop over many element on any side of the grid layout, this problem will always occur as far as one side of the grid has my content than the other side like maybe my side bar has many content that the main grid content vice versa ... please i don't know what to do, am using a css grid right now.. here are some screeen shot of how it will look..
enter image description here
Here is a sample of how i use my foreach loop to display many content from my database.
<?php $post = new media_data();
$posts = $post->get_posts(5);
$num_rows = $posts->num_rows;
echo '
<div class="show-slide-container">
<?php foreach ($posts as $no => $post) {
<div class="my-slides fade">
<div class="txt-number-sl">
<?php $no + 1 ?> / <?php $num_rows ?>
</div>
<a href="<?php 'single_posts.php?post-slug=' . $post['slug'] ?>">
<img class="img-sl" src="<?php 'static/images/' . $post['image']; ?>"
alt="<?php html_entity_decode(substrwords($post['title'], 230)); ?>">
</a>
<div class="txt-body-sl">
<?php html_entity_decode(substrwords($post['title'], 100)); ?>
</div>
</div>
'
} ?>

Iterating Active class to two row nav bar in WordPress

I'm working with a WordPress site, using the ACF plugin and I have navigation that is in two rows. The first row <li> NEEDS to be class="active" to second row even show up. I can get either the first row to show up as active, or second but not both.
To make things more fun, there is no reliable way to figure this using child-pages (it was tried). Some of nav objects are parent pages and I have no power over that.
And since the goal is to get this working with templates, hardcoding page names isn't an option either..
I somehow have to to "go back" in code if inner <li> is marked as active.
ACF fields:
ACF has upper nav with name, link and sub-navigation repeater.
and sub-navigation repeater has name and link for every sub-page.
In code there is <li> tags inside <li> tags iterated using ACF repeater and while have rows. $lang_code is used to figure from where to pull header texts.
<?php
$lang = get_locale();
$curr_page = get_permalink(); ?>
<nav role="navigation">
<ul class="navigation">
<?php if (have_rows('navigation', $lang_code)) :
while(have_rows('navigation', $lang_code)) : the_row() ?>
<li class="<?php if($curr_page == get_sub_field('link') ){ echo 'active'; } ?>">
<?php the_sub_field('name') ?>
<ul class="sub-navigation">
<?php if (have_rows('sub-navigation', $lang_code)) :
while(have_rows('sub-navigation', $lang_code)) : the_row() ?>
<li class="<?php if($curr_page == get_sub_field('link')){ echo 'active'; } ?>"><?php the_sub_field('name') ?></li>
<?php endwhile; //end sub while
endif; //end sub if ?>
</ul>
</li>
<?php
endwhile; // End navigation while
endif; // End navigation if
</ul>
</nav>
https://imgur.com/a/Ws8YA
Pictures, first one: FAQ is class="active", but since Support is not, nothing is shown. The second picture is while support is class="active", showing second row of links (with support active since they happen to be same page).
I've tried all kinds of parent iterating and counters in nav columns, but I still have no working answer.
I am considering going jQuery + PHP or something like that.
Try using jQuery:
$('.sub-navigation li.active').parents('li').addClass('active');
Code above add .active class to parent <li> of child that has .active class.

Loop through PHP function, display userdata from Mysql - limit to display to 25 items and add next button

I have this PHP function, which I use to display my userdata. If run once it will basicly display a div with the userdata in it.
function listings($fornavn, $efternavn, $email, $adresse, $tlf, $postnr, $city, $fodselsdag, $brugerid,$bartype,$idbar)
{
?>
<div class="container">
<span class="records" id="id-<?php echo $brugerid;?>">
<div class="customer bidDiv clearfix">
<?php if ($bartype=='temp_vip') { ?>
<ul>
<li>
<span class="actionSpan" id="<?php echo $brugerid;?>" value="<?php echo $idbar;?>">
<a class="edit-opt" id="godkend" href="#">GODKEND</a>
</span>
</li>
<li>
<span class="actionSpan" id="delete-<?php echo $brugerid;?>" value="<?php echo $bartype;?>">
<a class="delete-opt" id="delete" href="#">Afvis</a>
</span>
</li>
<?php }else{ ?>
<ul>
<li>
<span class="actionSpan" id="delete-<?php echo $brugerid;?>" value="<?php echo $bartype;?>">
<a class="delete-opt" id="delete" href="#">Slet</a>
</span>
</li>
<li>
<a class="edit-opt" href="editform.php?id=<?php echo $brugerid."&bartype=".$bartype;?>" rel="facebox">Rediger</a>
</li>
<?php if ($bartype =='vip'){?>
<li>
<a class="print-opt" href="print.php?id=<?php echo $brugerid;?>" rel="facebox">Print</a>
</li>
<?php }else{
// Dont render vip link
}}?>
</ul>
<p class="contact-data">
<?php echo $email;?><br>
Tlf.: <?php echo $tlf;?>
</p>
<div class="base-data">
<h4><?php echo ucwords($fornavn)." ".ucwords($efternavn);?></h4>
<p>Fødselsdag <?php echo $fodselsdag;?></p>
<address><?php echo ucwords($adresse) ." ". $postnr ." ". ucwords($city);?></address>
</div>
</div><!-- end customer -->
</div>
</span>
<?php
I mostly use this function in a loop, to display all users from my MySQL database, who has a specific relation to something else.
This is all working great. But currently it would pull all results at once. Right now I only have 5 members, so its no problem, but say I get 800 or 2000 members, the list of users would get very long.
So I want to implement some kind of limit on how many users it displays, but still be able to browse through all the users. Many sites use something like pages and split up the results that way. Links like:
[1] [2] [3] [Last page>>]
I can't figure out how to start doing this? How would I proceed?
Also looked into this Jquery plugin:
http://andersonferminiano.com/jqueryscrollpagination/
But it keeps reloading my PHP file which results in the Loops being restarted, and it simply displays the results over and over again endlessly.
Can someone help me with the logic behind creating this? Or better, point me in a direction where I could use the jquery pluging above - where it only loads the loop one time, and renders the results as I scroll.
Any help or pointers will be greatly appreciated!
Thanks
Jquery pagination by default will only break-up the given table into several tabs on the page, that is all the data is actually loaded. There are some that let you do an AJAX request to fetch the next page, to work in conjunction with your PHP code.
Ideally, you want to limit the query using LIMIT (number of rows in the result object) and OFFSET (Start from row X ) which will give you number of records starting from your offset row up until the limit, and then use logic on the PHP side to determine the maximum number of pages.
$page = $_POST["page"] * 25; // make sure that page 0 is the starting page or calculate
$SQL = "SomeQueryText LIMIT 25 OFFSET '$page'" ;
$result = query ($SQL);

Different <li> class based off of category or tag in Wordpress

My goal is to have action icons in lists that correspond to how the list item is tagged or categorized.
For example, if I have a list item that is a webinar it should have a webinar icon next to it, the next item in the list might be a white paper, which should have a white paper icon next to it. The population of the icons should be controlled by how the list item is tagged or categorized.
I don't know where to start; any suggestions are appreciated. Thanks!
EDIT:
Thought it might be helpful if I show the list I'm wanting to modify - technically, the items that I want to modify are in the span class=meta" section, but I'm open to using whatever method worls:
<ul class="sub_nav">
<li class="ui-state-default ui-corner-top ui-tabs-selected ui-state-active last">
<a href="#resource_center" title="Resources II">
Featured Resources
</a>
</li>
</ul>
<div id="resource_center">
<?php
$resources = get_posts("post_type=resource&posts_per_page=2&cat=31&tag=featured");
?>
<?php foreach ($resources as $key => $resource): setup_postdata($resource); ?>
<?php if ($key === 0): ?>
<?php endif ?>
<p><span class="meta"><?php echo apply_filters('get_the_date',mysql2date('m.d.Y', $resource->post_date), 'm.d.Y') ?></span>
<?php echo $resource->post_title ?> – <?php echo strip_tags(get_the_excerpt()) ?></p>
<?php endforeach; ?>
<span class="more">Read More</span>
</div>
Just name all your icons after the tags they correspond to and put them in the same folder on your server (let's say http://www.yoursite.com/tagicons)
In your loop, just iterate the meta tag inside an image tag
<img src="http://www.yoursite.com/tagicons/{$tag}.png" />
Paste the code you're using to iterate your list items if you need more help.
Cheers
-D
EDIT:
I se you're using wordpress.
Refer to http://codex.wordpress.org/Function_Reference/wp_get_post_tags
to see how to get the tags you're looking for.
If you are generating the list inside the WordPress loop you can add the category as a class to the list element. For example:
...loop started
$categories = get_the_category();
$ids = '';
foreach ($categories as $category) {
$ids .= ' ' . $category->slug;
}
echo '<li class="' . $ids '">This item</li>';
...more loop
Then utilize CSS to style the list block.
While I think both of these solutions would have worked, I decided to go with a third solution I discovered as I researched options to meet my use case. This one was ideal because I was able to seamlessly fit it into my existing code structure and because I have a relatively low number of resources that I need to add the featured image to.
I added the code below, which basically uses the post's featured image as a left-aligned thumbnail.
<?php if ( has_post_thumbnail()): ?>
<?php
$thumb_id = get_post_thumbnail_id($resource->id);
$args = array(
'p' => $thumb_id,
'post_type' => 'attachment'
);
$thumb_image = get_posts($args);
$thumb_caption = $thumb_image->post_excerpt;
?>
<?php if (!empty($thumb_caption)): ?>
<div class="caption"><?php echo $thumb_caption ?></div>
<?php endif ?>
<?php the_post_thumbnail('sidebar-thumb'); ?>
<?php endif; ?>
Followed by this code snipped to grab the image and put it by the list item:
<?php echo get_the_post_thumbnail($id, 'thumbnail', array()); ?>
Here's a screen shot of my test site list section after I added the code - it's exactly what I was looking for:
Thanks for the suggestions and help, it got me moving in the right direction!

How to Show Accurate Category Post Count?

I've got some code in my blog sidebar that is designed to show the number of posts in a particular category. It works, kind of. It will only show up to the number 5 and then it stops updating the post count. Any ideas on how to fix?
Here's the code for one of the categories:
<?php
$count_posts = get_posts('post_type=post&category_name=Software')
?>
<li id="nav1">
<a class="<?php if ($current_cat == get_cat_ID('Software')) echo "software-active active"; ?>" href="<?php echo get_category_link(get_cat_ID('Software')) ?>" title="Software">Software (<?php echo count($count_posts) ?>)</a>
Here's a link to the blog. thanks for your help!
Change:
get_posts('post_type=post&category_name=Software')
To:
get_posts('post_type=post&category_name=Software&numberposts=-1');

Categories