php foreach loop help - php

I have a foreach loop that builds me a product page basically it out puts my products in rows of 3.
See the code:
foreach ($product_sets as $product)
{
$currentRow = ceil($currentItem / 3);
$currentColumn = $currentItem - (($currentRow - 1) * 3);
if ($number_of_blanks == 2) :
if (($number_of_rows > 1 && $currentRow == ($number_of_rows - 1) && $currentColumn == 2) || ($number_of_rows == 1 && $currentColumn == 1)) :
?>
<li><img src="<?php echo site_url('assets/img/blocks/guarantee.png'); ?>" alt="5 Year Guarantee" width="242" height="156"></li>
<?php
$currentItem++;
endif;
endif;
?>
<li class="<?php if($currentItem % 3 == 0) echo 'endHomeBlock';?>">
<?php $this->load->view('blocks/product_small', array('product' => $product)); ?>
</li>
<?php
$currentItem++;
}
What I am wanting be able to do is place an image (A point of sale) at the end of the first row, and the randomly through the other rows, but maintaining 3 items (including image a point of sale) on a row. I have the image paths in an array called images which looks similar to this,
$images = array(
'iamge1.png',
'image2.png,
'image3.png,
'image4.png,
);
How can I achieve this? I have been scrathing around for a few hours now :(

Sorry I don't have much time in writing you the full code, but the following should work:
<ul>
<li><?php
foreach($items as $i=>$item){
// ...write item...
if(($i % 3)==0 && $i!=0){ // if multiple of 3 and not the first time..
?></li><li><?php
}
}
?></li>
</ul>

So what I would do is create a hashtable of your images and at what points in the table you want to display them. The key of the table would be the index and the value would be the image name.
Assuming $currentItem is zero-based, the first key you would have would be 2 for the third item in the first row.
Then in your loop, check and see if $currentItem is in the hashtable. If it is, print the image and increment $currentItem (and recalculate row and column) and then print $product. If it is not in the hashtable, just print $product like normal.

Related

Display Specific Image Based On SQL Query Result

I have a page that displays products based on a previous user query. The search results contain sets of metadata, most importantly a "product rating" on a 1-10 scale.
Here's the code that pulls the product rating metadata
<?php echo get_post_meta( get_the_ID(), 'product_rating', true ); ?>
This pulls the 'rating' metadata from our dB and simply displays the rating as a numeral (1, 1.5, 2, 2.5 ect).
I'm looking to take the value of the 'product_rating' and set an image to display for each value. A rating of 1 will display one image, while a rating of 2 display a different image ect ect.
The current code I have in place substitutes an image for the value, but the image is displaying multiple times (up to 10 times for a single value). My code is below, using a rating value of "4" as an example.
<?php $rating = get_post_meta( get_the_ID(), 'product_rating', true ); ?>
<?php For($i=4; $i <= $rating; $i++){
echo "<img src='http://www.domain.com/lpimage/lock50.png' />";
}
?>
Is my code close to where I need to be, or am I completely off base for the function I want?
Assuming your rating variable is correctly defined I think you can handle the conditional display of various images as follows:
<?php if ($rating == 1) echo '<img src="urlofimage1.jpg"></img>';
elseif ($rating == 2) echo '<img src="urlofimage2.jpg"></img>';
elseif ($rating == 3) echo '<img src="urlofimage3.jpg"></img>';
elseif ($rating == 4) echo '<img src="urlofimage4.jpg"></img>';
?>
You use the double equal sign when comparing a value, you only use the single equal sign (=) when setting a variable's value.
Use >= and <= for ranges, ie. 1.3 or 2.7

Displaying multiple columns of categories on a specific a custom taxonomy

I'm trying to list all terms on a custom taxonomy but I wanted to at least group them into 3 columns including their children to have a visual balance. Here's what I've done so far. I'm stuck on creating the loop after it reached the maximum term. On what I have, It wrapped all the succeeding items with 'ul' instead of creating a second ul and list the next batch. After it reaches the x amount of term it should create another 'ul' element listing categories in it. There will be a total of 3 columns.
<?php
$get_cats = wp_list_categories( 'echo=0&title_li=&depth=2&hide_empty=0,&taxonomy=industries' );
// Split into array items
$cat_array = explode('</li>',$get_cats);
// Amount of categories (count of items in array)
$results_total = count($cat_array);
// How many categories to show per list (round up total divided by 3)
$cats_per_list = ceil($results_total / 3);
// Counter number for tagging onto each list
$list_number = 1;
// Set the category result counter to zero
$result_number = 0;
?>
<?php echo $cats_per_list ; ?>
<ul class="cat_col" id="cat-col-<?php echo $list_number; ?>">
<?php
foreach($cat_array as $category) {
$result_number++;
if($result_number >= $cats_per_list) {
$list_number++;
echo $category.'</li> </ul> <ul class="cat_col" id="cat-col-'.$list_number.'">';
}
else {
echo $category.'</li>';
}
}
?>
</ul>
The code is very buggy. Just a couple of observations:
Next in the
if($result_number >= $cats_per_list) {
Block you are going to have to reset result_number to 0 since the count starts over again. Your current code would only meet that condition once since $cats_per_list is defined as the average of the total amount. After that it would continue counting up and ALWAYS be >= $cats_per_list
Next: it's quibble but you probably don't need to ceil the result since you are using >=, that operation pretty much does the same thing since 1.5 will meet the criteria of >= 1 as a for instance.
Try this and see if it is any better:
<?php
$get_cats = wp_list_categories( 'echo=0&title_li=&depth=2&hide_empty=0,&taxonomy=industries' );
// Split into array items
$cat_array = explode('</li>',$get_cats);
// Amount of categories (count of items in array)
$results_total = count($cat_array);
// How many categories to show per list (round up total divided by 3)
$cats_per_list = ceil($results_total / 3);
// Counter number for tagging onto each list
$list_number = 1;
// Set the category result counter to zero
$result_number = 0;
?>
<?php echo $cats_per_list ; ?>
<ul class="cat_col" id="cat-col-<?php echo $list_number; ?>">
<?php
foreach($cat_array as $category) {
$result_number++;
if($result_number >= $cats_per_list) {
$result_number = 0;
$list_number++;
echo $category.'</li> </ul> <ul class="cat_col" id="cat-col-'.$list_number.'">';
}
else {
echo $category.'</li>';
}
}
?>
</ul>

Add html word into wp_list_categories

This is my first question, i hope a lot of this site. My friends talk very good about it.
I have a code with categorie list in 3 columns, but i want add word "Asesor " before each category.
Example:
Asesor Category 1
Asesor Category 2
This is my code:
<?php
// Grab the categories - top level only (depth=1)
$get_cats = wp_list_categories( 'echo=0&title_li=&depth=1&hide_empty=0&exclude=1,762,899,951' );
// Split into array items
$cat_array = explode('</li>',$get_cats);
// Amount of categories (count of items in array)
$results_total = count($cat_array);
// How many categories to show per list (round up total divided by 3)
$cats_per_list = ceil($results_total / 3);
// Counter number for tagging onto each list
$list_number = 1;
// Set the category result counter to zero
$result_number = 0;
?>
<ul class="cat_col" id="cat-col-<?php echo $list_number; ?>">
<?php
foreach($cat_array as $category) {
$result_number++;
if($result_number % $cats_per_list == 0) {
$list_number++;
echo $category.'</li>
</ul>
<ul class="cat_col" id="cat-col-'.$list_number.'">';
}
else
echo $category.'</li>';
}
?>
</ul>
I try add this in one line, but don't work:
echo "Asesor" .$category.'</li>';
Anybody can help me please? Really thanks!
I suggest you to use
http://codex.wordpress.org/Function_Reference/get_categories instead.
It will retrieve array of objects by default so you can just loop through and echo what you want

Different css styling for last element of a php array Pt.2

Ok here is my new issue, and a complete description of what I am trying to accomplish. I used the suggestions you gave me and it worked fine, but when I changed the ORDER by information to a different field and the if statement, it went crazy.
Here is what I need, I have two columns on a page, when the array is processed I need it to display results filling each column left to right top to bottom. There is a division line between each item
<div class="centerBoxContentsFeatured centeredContent back vLine " style="width:50%;">
and one under each item
<div class="product-col" >.
If the item is the 3rd, 5th, 7th and so on… it will be on the left and does not need the “vLine “ div before it and if it is the last item(s) it does not need the “product-col” div under it as the last 2 items do not have to have the bottom divider, but if it is only two items on the whole page the bottom divider can stay. What am I doing wrong?
<?php
$count_temp = count ($productid);
for ($i = 0; $i < $count_temp; ++$i) {
$artist = get_the_title();
if (($productartist[$i] == $artist ) && $categoryID[$i] ==1 ) {
?>
//Content//
<?php if (!($i === 0) && !($i % 2) &&
($productid[$i] == 1) &&
( $i === (count ($productid) - 1))) {
echo'<div class="product-col-none" >';
}
else { echo '<div class="product-col" >';}
?>
//Content//
<? if !( $i === (count ($productid) - 1))
{
echo '<div class="centerBoxContentsFeatured centeredContent back vLine " '.
'style="width:50%;">';
}
else {
echo '<div class="centerBoxContentsFeatured centeredContent back " '.
'style="width:50%;">';}
?>
You should write here about your css file code, you are making things complicated and unclear with your html embedded php code.
If you just want a different style for your odd numbered (3,5,7..) items and a different style for your last item then I'll recommend to use css pseudo classes
You can use css pseudo class last-child for your last item.
li:last-child
{
// Your specific style for last item goes here
}
You can also use css pseudo class nth-child(odd) for alternating odd numbered items
li:nth-child(odd)
{
// Your specific style for odd items goes here
}
I hope this is what you are looking for, Why to give a server side load when you can set things done at client end.

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

Categories