I created a database with 5 categories and created a page to display the available categories that I pull from the database. The problem is that I have 4 divs that have the following class ( col-md-3 ) and one that is ( col-md-6 ).
Code for gathering the first 5 categories ( col-md-3 ):
<?php
$this->db->limit(4);
$categories = $this->db->get_where('category', array('parent' => 0))->result_array();
foreach ($categories as $key => $category):
?>
<div class="gallery-item">
<div class="grid-item-holder">
<div class="listing-item-grid">
<img src="<?php echo base_url('uploads/category_thumbnails/').$category['thumbnail'];?>" alt="" />
<div class="listing-counter">
<span>2</span>Locations
</div>
<div class="listing-item-cat">
<h3><?php echo $category['name']; ?></h3>
<p><?php echo $category['name']; ?></p>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
Code for gathering another 1 category ( col-md-6 ):
<?php
$this->db->limit(1);
$categories = $this->db->get_where('category', array('parent' => 0))->result_array();
foreach ($categories as $key => $category):
?>
<div class="gallery-item gallery-item-second">
<div class="grid-item-holder">
<div class="listing-item-grid">
<img src="<?php echo base_url('uploads/category_thumbnails/').$category['thumbnail'];?>" alt="" />
<div class="listing-counter">
<span>2</span>Locations
</div>
<div class="listing-item-cat">
<h3><?php echo $category['name']; ?></h3>
<p><?php echo $category['name']; ?></p>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
With this code, I get a display of categories but instead of displaying 5 categories, it repeats one of the displayed 4. How can I fix the code so that the display automatically continues to display the categories in all divs? If I forgot to write something, correct it or ask, I will update the question.
It looks like you are getting the first 4 items for the first foreach loop and then getting the first one again for the second loop. Would it make sense to get all 5 and process them in one single foreach loop only adding 'gallery-item-second' on the 5th item, something like ...
<?php
$this->db->limit(5);
$categories = $this->db->get_where('category', array('parent' => 0))->result_array();
$index = 0;
foreach ($categories as $key => $category):
$index++;
if ($index == 5) {
echo '<div class="gallery-item gallery-item-second">';
} else {
echo '<div class="gallery-item">';
}
?>
<div class="grid-item-holder">
. . .
</div>
</div>
<?php endforeach; ?>
Or if you must have the second loop then get 5 items and only show the 5th.
And to make sure you get the same 5 you could use a sort criteria when you get the items.
The problem is resolved with math:
I have added the next code:
...
$num = 5;
$i = 0;
foreach ($categories as $key => $category):
if ($i < ($num - 1)) {
// show div col-md-3
} else {
// show div col-md-6
}
endif;
Related
I'm working on a Mega Menu for WooCommerce Product Category. I'm able to get the list of all Subcategories using the code below;
$parent_id = 37; //ID of the Parent Category
$subCat_of_parent = get_terms('product_cat',array('child_of' => $parent_id));
Then used in a html structure as below;
<div class="row">
<div class="col-md-6">
<ul>
<?php
foreach ($subCat_of_parent as $subcat) {
?>
<li>
<?php echo $subcat->name; ?>
</li>
<?php
}
?>
</ul>
</div>
</div>
This worked by getting all the list of subcategory of the parent category in this format;
<div class="row">
<div class="col-md-6">
<ul>
<li><a>1st subcategory</a></li>
<li><a>2nd subcategory</a></li>
<li><a>3rd subcategory</a></li>
<li><a>4th subcategory</a></li>
<li><a>5th subcategory</a></li>
<li><a>6th subcategory</a></li>
</ul>
<div>
</div>
what i want to achieve is after the 3rd subcategory, it should break and continue on a new column, so that i can get something like this;
<div class="row">
<div class="col-md-6">
<ul>
<li><a>1st subcategory</a></li>
<li><a>2nd subcategory</a></li>
<li><a>3rd subcategory</a></li>
</ul>
<div>
<div class="col-md-6">
<ul>
<li><a>4th subcategory</a></li>
<li><a>5th subcategory</a></li>
<li><a>6th subcategory</a></li>
</ul>
<div>
</div>
How can i achieve this? Thanks for your help in advance
You can use array_chunk() to divide the array into groups of 3.
<div class="row">
<?php
$chunks = array_chunk($subCat_of_parent, 3);
foreach ($chunks as $group) {
print '<div class="col-md-6">';
print '<ul>';
foreach ($group as $subcat) {
print '<li>';
//to-do
print '</li>';
}
print '</ul>';
print '</div>';
}
?>
</div>
You need to emit the inner <div class="col-md-6"><ul> and the </ul></div> part every three categories.
Here is the pseudocode:
Emit <div class="row">
Set a counter, something like $i = 0
Start your for loop, foreach ($subCat_of_parent as $subcat) {
Now say if ($i == 0), emit the start div tag and start ul tag.
Emit your list item
$i = ($i + 1) % 3
Now say if ($i == 0), emit the close ul tag and close div tag.
If the number of subcategories is not a multiple of three, you need extra logic at the end to make sure the last group is properly closed off.
<?php
$arr = array(1, 2, 3, 4,5,6);
$count = 0;
?>
<div class="row">
<?php
foreach ($subCat_of_parent as $subcat)
{
// echo "count =".$count;
if($count%3 == 0)
{
echo ('<div class="col-md-6">
<ul>');
}
$count++;
?>
<li>
<?php echo $subcat->name; ?>
</li>
<?php
if($count%3 == 0)
{
echo ('
</ul></div>');
}
}
?>
</div>
Below code is working, You can use this.
<?php
$subCat_of_parent = array('1st sub', '2nd sub', '3rd sub', '4th sub', '5 sub', '6 sub', '7 sub');
$subCat_of_parent = array_chunk($subCat_of_parent, 3);
?>
<div class="row">
<?php
foreach ($subCat_of_parent as $subcats) {
?>
<div class="col-md-6">
<ul>
<?php
foreach ($subcats as $subcat) {
?>
<li><?php echo $subcat; ?></li>
<?php
}
?>
</ul>
</div>
<?php
}
?>
</div>
Trying to display the custom posts on my archive page within a bootstrap row containing 3 columns then starting a new row, got the code but new to PHP and dont know where to put the content.
<?php
//Columns must be a factor of 12 (1,2,3,4,6,12)
$numOfCols = 3;
$rowCount = 0;
$bootstrapColWidth = 12 / $numOfCols;
?>
<div class="row">
<?php
foreach ($rows as $row){
?>
<div class="col-md-4"<?php echo $bootstrapColWidth; ?>">
<div class="thumbnail">
<img src="user_file/<?php echo $row->foto; ?>">
</div>
</div>
<?php
$rowCount++;
if($rowCount % $numOfCols == 0) echo '</div><div class="row">';
}
?>
</div>
<div class="embed-container">
<?php the_field('podcast_embed_link'); ?>
</div>
<h3><?php the_title(); ?></h3>
<p><b><?php echo $date->format( $format_out );?></b></p>
<p><?php the_field('description'); ?></p>
<?php if( get_field('thumbnail') ): ?>
<img src="<?php the_field('thumbnail'); ?>" />
<?php endif; ?>
<?php endwhile; // end of the loop. ?>
</div>
</div>
</div><!-- #content -->
Here is the code for the page archive.podcasts.php, where would i add the custom fields within the row loop?
First of all, you don't need to close and open row tag each 3 items. If you leave the code like this:
<div class="row">
<?php
foreach ($rows as $row){
?>
<div class="col-md-<?php echo $bootstrapColWidth; ?>">
<div class="thumbnail">
<img src="user_file/<?php echo $row->foto; ?>">
</div>
</div>
<?php
}
?>
</div>
you will get the same effect, but without the separation that a row tag involves. Notice that the line involving "col-md-4" has already changes in order to not create wrong col size usage.
In this part of code:
<div class="col-md-4"<?php echo $bootstrapColWidth; ?>">
You must get wrong bootstrap class like col-md-41, col-md-412.
Correct you code by this way:
<div class="col-md-<?php echo $bootstrapColWidth; ?>">
With php print_r I got final result as :
Array ( [0] => 31 [1] => 21 ) Array ( [0] => 33 [1] => 27 )
And I want result to be like 31,21,33,27.
I am stuck up. Please guide me through.
Thanks,
Vikram
**Please bare with lengthy stuff. **
I have 2 main categories and need to display 3 fourth level children on home page. Categories are like :
Category > Clothing.
Category > Clothing > Mens.
Category >> Clothing > Mens > Top.
Category > Clothing > Mens > Top > Jeans.
CODES
<div class="container products" data-aos="fade-down">
<div class="page-titles" data-aos="fade-up">
Products
<hr />
</div>
<!--page-titles-->
<?php
$homepage_products=get_post_meta($post->ID, 'homepage_products', TRUE);
$homepage_parent=explode(',', $homepage_products);
global $parent_one;
global $x;
$x=1;
foreach($homepage_parent as $parent_one)
{
$parent_one_title = get_the_title( $parent_one );
$content_post = get_post($parent_one);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
?>
<div class="row clothing">
<div id="<?php echo $x++; ?>" class="col-xs-12 col-sm-12 col-md-4 col-lg-4
<?php if($x % 2)
{
echo 'col-md-push-8 col-lg-push-8';
}
?>
">
<?php echo get_the_post_thumbnail($parent_one, 'medium_large', array('class' => 'img-'.$x)); ?>
</div>
<!--col-4 img-->
<div class="col-xs-12 col-sm-12 col-md-8 col-lg-8
<?php if($x % 2)
{
echo 'col-md-pull-4 col-lg-pull-4';
}
?>
">
<h2><?php echo $parent_one_title; ?></h2>
<?php echo $content; ?>
<!-- child pages of <?php echo strtolower($parent_title); ?> starts-->
<div class="row">
<?php
$parent_two=get_pages( array('parent'=>$parent_one,'child_of'=>$parent_one) );
foreach($parent_two as $parent_two_ids)
{
$parent_two_id=$parent_two_ids->ID;
$parent_two_id.=",";
$parent_three=get_pages( array('parent'=>$parent_two_id) );
$str_id = array();
foreach($parent_three as $parent_three_ids)
{
$parent_three_id=$parent_three_ids->ID;
$parent_three_id.=',';
$parent_four=get_pages( array('parent'=>$parent_three_id) );
$four_ids=array();
foreach($parent_four as $parent_four_ids)
{
$four_id=$parent_four_ids->ID;
$four_ids[]=$four_id;
}//parent_four_ids
// HERE IS MY ISSUE //
echo implode(',', array_merge($four_ids) );
// HERE IS MY ISSUE //
//$ids=implode(',',$four_ids);
//$idx=$ids;
//$idv=preg_replace('#\s+#',',',trim($idx));
//echo $idv;
//$str_id[]=$idx;
//$str_idx=implode(',',$str_id);
//echo $str_idx;
$content=apply_filters('the_content', $parent_four_ids->post_content);
$c_length = 80;
if (strlen($content) > $c_length)
{
$content = wordwrap($content, 80);
$i = strpos($content, "\n");
if ($i) {
$content = substr($content, 0, $i);
}
}
?>
<!-- <div class="col-xs-12 col-sm-12 col-md-3 col-lg-3 product">
< ?php echo get_the_post_thumbnail($parent_four_ids, 'medium_large'); ?>
<a href="< ?php echo get_permalink($parent_four_ids);?>">
<h4>< ?php echo $parent_four_ids->post_title; ?></h4>
</a>
< ?php echo $content; ?>
</div><!--col 3 #< ?php echo $parent_four_ids->ID; ?>-->
<?php
}//parent_three_ids
} // parent_two_ids
?>
</div>
<!-- row category - slider- child pages of <?php echo strtolower($parent_one_title); ?> ends-->
</div>
<!-- col-6 content - child pages-->
</div>
<!--row <?php echo strtolower($parent_title); ?>-->
<? } //home_parent ?>
</div>
<!--container products-->
Just as iainn said above:
php > echo implode(',', array_merge([31, 21], [33, 27]));
31,21,33,27
If you wanted a single array and not a string, remove the implode() call.
I'm trying to figure this out for hours but I can't. I just want to show the second post and skip the first one, just like the Wordpress offset function. I'm using AW blog extension for Magento and I want to skip the first post in the recent blog posts. Below is my modified code showing one post recent post in a homepage block. I just want to create another block that will show the second recent post. :(
<?php $posts = $this->getPosts(); ?>
<div id="messages_product_view">
<?php Mage::app()->getLayout()->getMessagesBlock()-> setMessages(Mage::getSingleton('customer/session')->getMessages(true)); ?>
<?php echo Mage::app()->getLayout()->getMessagesBlock()->getGroupedHtml(); ?>
</div>
<?php
foreach ($posts as $post):
if ($i++ >= 1) break;
?>
<div class="postWrapper">
<div class="postTitle">
<h3><a href="<?php echo $post->getAddress(); ?>" ><?php echo $post->getTitle(); ?></a></h3>
</div>
<div class="postContent"><?php echo $post->getPostContent(); ?></div>
</div>
<?php endforeach; ?>
I will recommend you to check the count and after printing it stop the loop, otherwise it will loop the entire array to the end and print just the second iteration.
$i=0;
foreach ($posts as $post){
if ($i==1) {
//print your div here
$i++; //added here after edit.
continue;
}else if ( $i>1 ){
break;
}
$i++;
}
This way it will only iterate twice.
Try changing:
foreach ($posts as $post):
if ($i++ >= 1) break;
to:
//set $i outside of loop
$i=0;
foreach ($posts as $post):
if ($i==0) {
$i++;
//skip the first record
continue;
}
Try this -
<?php $i=1;
foreach ($posts as $post):
if ($i > 1) {
?>
<div class="postWrapper">
<div class="postTitle">
<h3><a href="<?php echo $post->getAddress(); ?>" ><?php echo $post->getTitle(); ?></a></h3>
</div>
<div class="postContent"><?php echo $post->getPostContent(); ?></div>
</div>
<?php }
$i++;
endforeach; ?>
What I need to do that my latest posts go automatically to top of the page? Now latest post go bottom of the ealier posts.
php:
<div id="container">
<div id="blog">
<div class="grid_9 float-left">
<?php foreach($posts->results as $post): ?>
<div class="post box_shadow">
<h2><?php echo $post->title ?></h2>
<small><?php echo date('d-m-Y',strtotime($post->date)) ?></small>
<p><?php echo Str::limit_word($post->content, 40); ?></p>
<?php echo Lang::line('home.blog_read_more', array(), $lang)->get() ?>
</div>
<?php endforeach ?>
<?php echo $posts->links()?>
</div>
</div>
<div id="footer" class=" box-shadow">
<?php echo stripcslashes($setting->footer)?>
</div>
</div>
I am still newbie so could you give advice what line I need modified or add and where?
If it is exactly the opposite of what your looking for try running array_reverse on it before hitting the loop.
That's how your array is sorted. options: reverse the array or do a for loop starting from the last index going to 0.
Something similar to below:
`$posts->results` has all of your posts
Replace the foreach with a for.
$count = count($posts->results);
for($i = $count-1; $i <= 0; $i--) {
$post = $posts->results[$i];
//display post
}
The simplest but probably less efficient way is to reverse the array right before the foreach loop.