TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Display Modes
Old 06-03-2008, 06:23 PM   #1 (permalink)
The Wanderer
 
codyodell's Avatar
 
Join Date: Nov 2007
Posts: 12
Thanks: 2
codyodell is on a distinguished road
Bug MySQL Category Problem

Hey guys, I'm building a 2-level menu system. My category table has nCategoryId, nParentCategoryId, and strCategory. I am joining this table with my table of posts. When a user clicks a main category I want them to see only the featured posts (tblposts has a boolean flag for featured) for the current category and ALL of it's children.. and I only want to use one query.

Here is what I have currently:

Code:
SELECT PO.strTitle, PO.bFeatured, PO.strPreview, PO.strFile, PO.dateCreated AS tsCreated, DATE_FORMAT(PO.dateCreated, '%b, %D %Y %l:%i %p') AS dateCreated, PO.nPostId, PO.nPostType, PO.strPost, PO.strAbout, PO.nViews, CA.strCategory, CA.nCategoryId, CA.nParentCategoryId FROM tblposts PO INNER JOIN tblcategories CA ON CA.nCategoryId = PO.nCategoryId WHERE 1 = 1 AND PO.bFeatured = 1
Say I have an array of all the category ID's I want to show, how would I incorporate that into this query to only show featured posts for these categories? I am open to different approaches, but I already have the array setup. I've also tried using IN(), but had no luck either.
__________________
The man who invented the wheel was smart. The man who invented the other 3, he was a genius.
codyodell is offline  
Reply With Quote
Old 06-05-2008, 03:40 AM   #2 (permalink)
The Wanderer
 
codyodell's Avatar
 
Join Date: Nov 2007
Posts: 12
Thanks: 2
codyodell is on a distinguished road
Default

No one has any ideas? Let me know if theres any more info about the problem I should post.
__________________
The man who invented the wheel was smart. The man who invented the other 3, he was a genius.
codyodell is offline  
Reply With Quote
Old 06-06-2008, 05:24 PM   #3 (permalink)
The Contributor
 
buggabill's Avatar
 
Join Date: Jan 2008
Location: Maine, USA
Posts: 92
Thanks: 2
buggabill is on a distinguished road
Default

One thing that you could do is take your array and implode it into a string.

php Code:
$strcat = implode(', ', $arrcat);

// For example say you have a sample array
// that looks like this:
//   $arrcat = array(1, 3, 5);
// The first step will take the array
// and implode it into $strcat like so:
// $strcat = '1, 3, 5';
 

Then use that string in the query for the criteria (Note the use of the IN keyword:
php Code:
$sql = 'SELECT PO.strTitle, PO.bFeatured, PO.strPreview, PO.strFile, PO.dateCreated AS tsCreated, DATE_FORMAT(PO.dateCreated, '%b, %D %Y %l:%i %p') AS dateCreated, PO.nPostId, PO.nPostType, PO.strPost, PO.strAbout, PO.nViews, CA.strCategory, CA.nCategoryId, CA.nParentCategoryId FROM tblposts PO INNER JOIN tblcategories CA ON CA.nCategoryId = PO.nCategoryId WHERE CA.nCategoryId IN ('.$strcat.') AND PO.bFeatured = 1';
__________________
-- Bill
"Why is it drug addicts and computer aficionados are both called users?" -Clifford Stoll

Last edited by buggabill : 06-06-2008 at 05:25 PM. Reason: One little 'n'...
buggabill is offline  
Reply With Quote
Old 06-06-2008, 05:35 PM   #4 (permalink)
The Contributor
 
buggabill's Avatar
 
Join Date: Jan 2008
Location: Maine, USA
Posts: 92
Thanks: 2
buggabill is on a distinguished road
Default

BTW...
I made my way over to the snipply site that I saw from your other posts. I like the idea. I am joining now.
__________________
-- Bill
"Why is it drug addicts and computer aficionados are both called users?" -Clifford Stoll
buggabill is offline  
Reply With Quote
The Following User Says Thank You to buggabill For This Useful Post:
codyodell (06-07-2008)
Old 06-07-2008, 09:47 PM   #5 (permalink)
The Wanderer
 
codyodell's Avatar
 
Join Date: Nov 2007
Posts: 12
Thanks: 2
codyodell is on a distinguished road
Default

Thanks, I didn't think of making an array of ALL the categoryId's I needed prior to executing the query, I was trying to dynamically get the main categories and get the children categories through the IN() function.

I would be interested to see if anyone could write a recursive mysql query to search inside children categories infinitely given 1 root categoryId.

As for Snipply, I've been fixing a lot of bugs, adding new stuff. I'll keep everyone updated once I get snipply's blog up, and I should be able to sync my local code in the next week, so that we can actually start using the site (Right now, there are some major bugs making the site unusable). Thanks for signing up, anyone already signed up before I make the site invite-only will automatically get an invite. I'll also be giving out extra invites based off the bug reports/content the users submit, so make sure to send me a bug or a comment/idea if you have one with the 'report a bug' link at the bottom.
__________________
The man who invented the wheel was smart. The man who invented the other 3, he was a genius.
codyodell is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 10:52 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0