How can I skip multiple strings from php array? - php

I have the categories: 2D, 3D, Photo, Animation, which become buttons.
How can I skip the 3D and Animation from being displayed? on this particular page when running foreach. For example, the buttons that will appear have to be: All, 2D, Photo.
I tried to find a solution, but no luck.
What I managed to remove was only a string, but not multiple ones, with:
$key = array_search('3D',$terms);
unset($terms[$key]);
This is my code that I want to apply the changes to:
$terms = get_terms("portfolio_category",$cat_arguments);
if($terms):
?> <!-- category menu, this -->
<div class="row categories-p">
<ul class="col-sm-12">
<li><?php _e("All","um_lang"); ?></li>
<?php
sort($terms);
foreach($terms as $term):?>
<li>
<?php echo $term->name; ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>

You can use php's in_array, here's a basic example:
Assuming you have the following:
$categories = ['2D', '3D', 'Photo', 'Animation'];
And your skip list looks like this:
$skipList = ['3D', 'Animation'];
Then your render function will look like this:
iterate over category list
if the item is not in the skip list, render
foreach ($categories as $category) {
if (!in_array($category, $skipList)) {
// Display the category button
// e.g. echo "<button>{$category}</button>";
}
}

Related

menu with while and foreach loop

I am stuck with my current knowledge of PHP. I want to make a menu where the info is stored into a database. What I want to achieve is the following:
<?php
$names= array();
$aliases= array();
$items = array();
?>
<?php while($row = mysqli_fetch_array($menu, MYSQLI_ASSOC)){
$names[] = $row['page'];
$aliases[] = $row['alias'];
$items[] = $row['item'];
}?>
<div id="menu">
<ul>
<?php foreach($names as $page_name){
<?php echo $page_name;?>
?>
*** IF THERE ARE SUB MENU ITEMS, SHOW THEM ***
<ul>
<li>
*** NAME OF SUB ITEM***
</li>
</ul>
</ul>
</div>
I have absolutely no idea if i am on the right pad. The foreach works fine for the names or aliasses only, but i want to sort of combine them. Then i want to check if there is a submenu for the hover, but also set the parent item on class "active".
Since it is hard for me to explain it in English, please ask me any question needed so i can provide more info.
Thanks in advance!
First of all, you don't need three different arrays, only $names array would be enough. Second, change your while() loop in the following way,
while($row = mysqli_fetch_array($menu, MYSQLI_ASSOC)){
$names[] = array('page_name' => $row['page'], 'alias' => $row['alias'], 'item' => $row['item']);
}
This will create a multidimensional array comprising of name, alias and sub-items.
And finally, display the entire menu in the following way,
<div id="menu">
<ul>
<?php foreach($names as $page_array){ ?>
<?php echo $page_array['page_name']; ?>
<?php
if(!empty($page_array['item'])){
?>
<ul>
<li>
<?php echo $page_array['item']; ?>
</li>
</ul>
<?php
}
} ?>
</ul>
</div>
You are overcomplicating things:
<?php
echo '<div id="menu">
<ul>';
while($row = mysqli_fetch_array($menu, MYSQLI_ASSOC)){
$names[] = $row['page'];
$aliases[] = $row['alias'];
$items[] = $row['item'];
echo '<li class="pagename">'.$row['page'].'';
if ('checkforsubitem') {
echo '<ul>
<li>
'.$row['name_of_subitem'].'
</li>
</ul>';
}
}
echo '</li>
</ul>
</div>';
?>
However, this assumes some things about the submenu-bits. Normally, you would either fetch those items within the same query, and have them available to you via $row, but you can also do separate queries for those items based on a connection from the $row['id'] or something, and pull those subitems that corresponds to that id, and run another while-loop around the secondary <li>-items.

How to group posts by category using PHP & MySql?

I’ve tried using GROUP BY in my query but that only returns one post per category. This is an example of what is returned with my current setup. What I need is for all posts with the same category to be grouped together, can someone point me in the right direction? Thanks for your time.
Like suggested by Charlotte, you could group your posts in an associative array, with the categories as keys.
<?php
// Connecting to database
$categories = array(); // Create an empty array
while ($post = mysqli_fetch_array($result)) {
if(!array_key_exists($post['category'], $categories)) { // Check if the category is already present
$categories[$post['category']] = array(); // Create an array indexed with the category name
}
$categories[$post['category']][] = $post; // Add the post to the category
}
?>
Now you have to iterate twice : to display the categories and to display each post in the category.
When I have to deal with nested foreach to render html, I preferably use inline foreach.
The rendering of the posts should look like :
<?php foreach ($categories as $category => $posts) : ?>
<h2><?php echo $category; ?></h2>
<?php foreach ($posts as $post) : ?>
<a href='article.php?id=<?php echo $post['post_id']; ?>' ><?php echo $post['title']; ?></a>
<p>Posted on <?php echo date('d-m-y h:i:s',strtotime( $post['posted'] ) );?> In <a href='category5.php?id=<?php echo $post['category_id']; ?>' ><?php echo $post['category']; ?></a>
</p>
<?php endforeach; ?>
<hr/>
<?php endforeach; ?>
Edit: I ometted the opening and closing php tags in the first block of code.
If you assemble the two blocks of code in your php file, it should work.

removed foreach item doesn't keep count correctly

<ul>
<?php foreach (array_slice($items, 0, 3) as $item): ?>
<?php if (!isset($item->story)): ?> <!-- hide story updates -->
<li>
<?php if (isset($item->message)) echo $item->message; ?>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
I am displaying a news feed, and only want to display the items without a story update. So i am using !isset to check if a story item is present, and hide the item if it is.
This works perfectly, but now I am trying to display 3 results in the feed, but i think the story items are still being counted in the foreach, so when the last 3 updates are story items, nothing displays in the feed.
I have read this question which looks like what I am trying to do, but I dont understand where he has got the $elementkey variable from, and what it does?
How do I remove the item from the foreach, and remove its count so 3 items will display?
This should work for you:
(Here i just added a $count variable which i only increment if a message get's posted. And after 3 i break the foreach)
<ul>
<?php $count = 0;
foreach ($items as $item): ?>
<?php if ($count >= 3) break; if (!isset($item->story)): ?> <!-- hide story updates -->
<li>
<?php if (isset($item->message)) {
echo $item->message;
$count++;
}
?>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>

Wordpress - How can I loop into two different DIVS without repeating the DIVs

I am having a little trouble trying to figure out how I can accomplish this: I have a container with two colums in it, a right and a left column. Each column will have 3 posts with a short excerpt stacked in it. Ideally I would like to add one into the right and one into the left until it reaches 3 rows of posts(although if that proves to be alot more of a difficult task than I am fine with posting to the first column than the second. I am able to loop through the posts and add them to one column, but I am having trouble figuring out how one will loop into two separate divs, without repeating the divs. Here is how I am attempting it, so far this code will loop into the left column.
The first left side is using the loop to display the title(excerpt will come after) and the right side is just using html. Can anyone help me figure this one out?
<h2>Popular posts</h2>
<h3>Showing the most popular posts.</h3>
<div class="col4 inner-left">
<ul class="popular-list">
<?php
query_posts('meta_key=post_views_count&orderby=meta_value_num&order=DESC&posts_per_page=10&post_type=pop');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<li>
<a href=<?php the_permalink(); ?>><?php the_title(); ?></a>
<p>Popular post. This is a popular post, it really is.</p>
</li>
<?php
endwhile; endif;
wp_reset_query();
?>
</ul>
</div>
<div class="col4 inner-right">
<ul class="popular-list">
<li>
This is a popular post
<p>Popular post. This is a popular post, it really is.</p>
</li>
<li>
This is a popular post
<p>Popular post. This is a popular post, it really is.</p>
</li>
</ul>
</div>
You just need to apply logic for how you want the posts organized before you start iterating them.
Here's an example (this isn't functional, just a quick illustration):
<?php
$col1 = array();
$col2 = array();
$posts = array('one', 'two', 'three', 'four', 'five', 'six');
foreach ($posts as $post) {
$count++;
$count % 2 === 1 ? array_push($col1, $post) : array_push($col2, $post);
}
?>
<div class="col-1">
<?php
foreach ($col1 as $post) {
echo '<div class="post">'.$post.'</div>';
}
?>
</div>
<div class="col-2">
<?php
foreach ($col2 as $post) {
echo '<div class="post">'.$post.'</div>';
}
?>
</div>
This produces the output:
<div class="col-1">
<div class="post">one</div>
<div class="post">three</div>
<div class="post">five</div>
</div>
<div class="col-2">
<div class="post">two</div>
<div class="post">four</div>
<div class="post">six</div>
</div>
As you can see, now the elements of the $posts array are organized in both columns in an alternating fashion.

How to get subcategories under a custom menu on Magento

I have successfully created a custom drop-down-menu for each specific category I needed but one of them needs to load the subcategory within a subcategory and I can't get it to work.
The working code without the "subcategory within a subcategory" is the following but I need to find out how to add the 3rd level on this code.
<!-- Vending -->
<?php $main = Mage::getModel('catalog/category')->load(355) ?>
<li class="eight"><?php echo $main->getName(); ?>
<?php $children = Mage::getModel('catalog/category')->getCategories(355); ?>
<ul class="nav_static">
<?php foreach ($children as $category): ?>
<li>
<a href="<?php echo $category->getRequestPath(); ?>">
<?php echo $category->getName(); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</li>
<!-- END - Vending -->
Well, as I have suggested in https://stackoverflow.com/a/14586422/653867 you have to load a category object for your second level categories:
$cat = Mage::getModel('catalog/category')->load($category->getEntityId());
then you can access its children by executing
$children = $cat->getChildrenCategories();
The $children variable is a collection of type Mage_Catalog_Model_Resource_Category_Collection, and you can iterate through it to output the next level categories
I think, your code can be improved a bit if you called getChildrenCategories() on your $main in the first place. You wouldn't have to load every child in a loop, which can be performance punishing. Instead use this (and it can actually be improved even further with recursive calls, but such setup would include creating extra blocks, which might be too much hassle for this particular case):
<?php $main = Mage::getModel('catalog/category')->load(355) ?>
<li class="eight"><?php echo $main->getName(); ?>
<?php $children = $main->getChildrenCategories(); ?>
<ul class="nav_static">
<?php foreach ($children as $category): ?>
<li>
<a href="<?php echo $category->getUrl(); ?>">
<?php echo $category->getName();
$subCategories = $category->getChildrenCategories();
foreach ($subCategories as $subCat) {
/**
*your code to output the next level categories
*/
}
?>
</a>
</li>
<?php endforeach; ?>
</ul>
</li>

Categories