Show Only Posts that been posted in Two Chosen Categories!

Share your CuteNews.RU skins and code snippets.

Show Only Posts that been posted in Two Chosen Categories!

Postby DarkSlim » Thu Jan 14, 2010 4:58 pm

I have been trying to do that for a long time, I tried several things, XFields and more..
Today! I had a breakthrough and I think I have found the solution!

What does it do?
In the include, where you can choose categories, if you put for example: 1,5
It will show all the news from category 1 and all the news from category 5
without any connection between the categories, it will just sort them by date (or whatever you chose)..
Now, have you ever wanted to be able to choose 1,5 and only posts that have been
posted to these two categories 1 AND 5 (even if posted to more other categories too!) will show?
Now you can!! :D

Phew, it took me some time understanding how to sql that so it will work 100% lol

Installation
Open inc/show.news.php

Find:
Code: Select all
       $where[] = 'and';
       foreach($allow_categories as $single_cat){
         $where[] = 'category =~ %'.$single_cat.'%';
         $where[] = 'or';
        }



Replace with:
Code: Select all
                   //DarkSlim's Hack for $bothcat - showing only posts that been posted in both two chosen categories
                       if($bothcat == 'true'){
                              $where[] = 'and';
                              $cat1 = $allow_categories[0];
                              $cat2 = $allow_categories[1];
         $where[] = 'category =~ '.$cat2.','.$cat1.'';
                        $where[] = 'or';
         $where[] = 'category =~ '.$cat1.','.$cat2.'';
         $where[] = 'or';
         $where[] = 'category =~ '.$cat1.',%,'.$cat2.'';
         $where[] = 'or';
         $where[] = 'category =~ '.$cat2.',%,'.$cat1.'';
         $where[] = 'or';
         $where[] = 'category =~ %,'.$cat2.','.$cat1.'';
         $where[] = 'or';
         $where[] = 'category =~ %,'.$cat2.',%,'.$cat1.'';
         $where[] = 'or';
         $where[] = 'category =~ %,'.$cat2.',%,'.$cat1.',%';
         $where[] = 'or';
         $where[] = 'category =~ '.$cat2.',%,'.$cat1.',%';
         $where[] = 'or';
         $where[] = 'category =~ '.$cat2.','.$cat1.',%';
         $where[] = 'or';
         $where[] = 'category =~ %,'.$cat1.','.$cat2.'';
         $where[] = 'or';
         $where[] = 'category =~ %,'.$cat1.',%,'.$cat2.'';
         $where[] = 'or';
         $where[] = 'category =~ %,'.$cat1.',%,'.$cat2.',%';
         $where[] = 'or';
         $where[] = 'category =~ '.$cat1.',%,'.$cat2.',%';
         $where[] = 'or';
         $where[] = 'category =~ '.$cat1.','.$cat2.',%';
         $where[] = 'or';
                       } else {
                        //If $bothcat is not used, this is the normal category check
                          $where[] = 'and';
                          foreach($allow_categories as $single_cat){
                               $where[] = 'category =~ %'.$single_cat.'%';
                                $where[] = 'or';
                           }
                       }


That's it, now in your include you just need to choose
the categories - don't forget you can choose only 2!
The code can be changed to check for 3 too, but for now I'm using only 2. :)

The last thinng you need to add for it to work, add in your
include:
$bothcat = 'true';

As long as $bothcat is true in that include it will do my new both categories check! ;)

Here is an Example include:
Code: Select all
      <?php
        $template = 'MyTemplate';
        $number = 5;
        $category = '1,5';
        $bothcat = 'true';
        include $cutepath.'/show_news.php';
       ?>


Edit: New code, fixed some 'holes' in the code
that might happen when the user have more than 9 categories
now it's near perfect. :D

Hope it helps, if you are using it and want to
donate so I can keep making CNR fixes, hacks and addons
there's a link in my signature, thanks! :)
Last edited by DarkSlim on Thu Feb 04, 2010 4:18 pm, edited 1 time in total.
User avatar
DarkSlim
 
Posts: 298
Joined: Thu Aug 06, 2009 4:18 pm
Location: IL

Postby D72 » Thu Jan 14, 2010 10:44 pm

well that's some...
i don't know yet if i ever will use this as i don't see the advantage right now.
But you never know. Will give it a change to try next time.
Last edited by D72 on Thu Jan 14, 2010 11:23 pm, edited 1 time in total.
User avatar
D72
 
Posts: 336
Joined: Thu Feb 22, 2007 12:05 am
Location: NL

Postby DarkSlim » Thu Jan 14, 2010 11:21 pm

D72 wrote:well that's some...
i don't yet if i ever will use this as i don't see the advantage right now.
But you never know. Will give it a change to try next time.


hehe kk, I'm using it for example, in my site I have catgories
like news, reviews,previews, articles and wii, ps3, xbox360, nds, psp, pc etc'
Then, I have reviews page for the wii and reviews page for the ps3 and so on
so with this, I'll be able to choose reviews + wii for the wii reviews page
that way - wii news or articles won't show in there, only reviews, and
other reviews for other platform won't show too, just the part the I asked for. :)

Can be usefll in other situations too ^^
User avatar
DarkSlim
 
Posts: 298
Joined: Thu Aug 06, 2009 4:18 pm
Location: IL

Postby D72 » Thu Jan 14, 2010 11:25 pm

i got to see an example, i think i'm a bit lost right now.
And i'm very curious about your site. tried to visit the url in your signature, but that's definitely not a gaming website
User avatar
D72
 
Posts: 336
Joined: Thu Feb 22, 2007 12:05 am
Location: NL

Postby typeman » Thu Jan 14, 2010 11:59 pm

At first, I was like "eh?" :S But now I get it! :)

Fantastic post! :) I'll be sure to try and implant this :)
typeman
 
Posts: 102
Joined: Sat Oct 17, 2009 7:38 am

Postby DarkSlim » Sun Jan 17, 2010 8:25 am

Thank you, take your time, use it when you
find out it can solve your needs, for me I was
needing this for a long time! :)
User avatar
DarkSlim
 
Posts: 298
Joined: Thu Aug 06, 2009 4:18 pm
Location: IL

Postby DarkSlim » Thu Feb 04, 2010 4:19 pm

Added new parts to the code! If you are using it please replace
it with the new one, it solves a 'hole' in the code that might happen
when user have more than 9 categories, now it should be near perfect. :D
User avatar
DarkSlim
 
Posts: 298
Joined: Thu Aug 06, 2009 4:18 pm
Location: IL

Postby typeman » Thu Feb 04, 2010 8:12 pm

DarkSlim wrote:Added new parts to the code! If you are using it please replace
it with the new one, it solves a 'hole' in the code that might happen
when user have more than 9 categories, now it should be near perfect. :D


Great to see you posting again! :P Thought you had left us :(

Anywho, thank you for the fix :)
typeman
 
Posts: 102
Joined: Sat Oct 17, 2009 7:38 am

Postby DarkSlim » Sun Feb 07, 2010 12:05 pm

No problem :D

I'm still here, but lately I have more on my mind, work
and of course finishing building my website, so I'll keep
checking here daily, hopefully when I'll get my site
up & running I'll have more time for making more stuff
for CNR plus the new version! ;)
Either way, whenever I find something useful I'll always
share it here with you all ^^
User avatar
DarkSlim
 
Posts: 298
Joined: Thu Aug 06, 2009 4:18 pm
Location: IL

Postby scottdallas » Sun Mar 14, 2010 7:46 pm

Hm interesting and nice hack lol it's always confused me that original cutenews you could include as many multiple categories as you wanted but cutenews.ru always messed up when you included more than one. So, I had given up on the idea of using more than one category. Funny that now you have fixed it up so we can include up to 2! lol but i can see this being useful. I'm going to add the hack to my installation. I designed my site to never need more than one category on a page at a time just because of that problem! lol now i can do more stuff, thanks darkslim
Everyday I'm tumblin http://scodal.tumblr.com
User avatar
scottdallas
 
Posts: 2209
Joined: Thu May 04, 2006 6:46 pm
Location: US

Postby DarkSlim » Sun Mar 14, 2010 8:47 pm

Thanks!

It is an extremly helpful hack! it was crucial for my site
I was working on it for a long time and at last I made it hehe :)
User avatar
DarkSlim
 
Posts: 298
Joined: Thu Aug 06, 2009 4:18 pm
Location: IL


Return to Skins and snippets



Who is online

Users browsing this forum: No registered users and 1 guest

cron