views:

62

answers:

2

Is there a way for me to get a pseudo-ID of a post from the category it belongs to? Let's say I have these posts

post_id | post_title | post_cat
--------+------------+---------
0       | a post     | cat1
1       | a post1    | cat2
2       | a post2    | cat1
3       | a post3    | cat2
...
57      | a post57   | cat2

I want the posts from cat2 and the posts' ids to be relative to the category they were posted in. Something like

post_id | post_title | post_cat | cat_post_id
--------+------------+----------+--------
1       | a post1    | cat2     | 1
3       | a post3    | cat2     | 2
57      | a post57   | cat2     | 3
+1  A: 

Are you trying to achieve something similar to what we have discussed here - http://stackoverflow.com/questions/2585567/creating-a-numerical-order-index-on-a-mysql-table/2585720#2585720

SET @rank=0;
SELECT @rank:=@rank+1 AS cat_post_id, post_id, post_title, post_cat
FROM posts
WHERE post_cat = 'cat2'
ORDER BY post_id DESC;
Ivo Sabev
I think that might work if I were to always get contiguous posts. I'm not sure what to do if my result set only had `a post1` and `a post57` though.
Rfvgyhn
It is not based on the post_id in the table this is iterator, you can put whatever in the WHERE clause and you will always get consecutive numbers for cat_post_id.
Ivo Sabev
Right, and I wouldn't want that. I would still want `a post57` to have cat_post_id = 3.
Rfvgyhn
You can achieve that with subquery, but it is a waste of resources you better add a column with the sequential number you like and then just make your normal select.
Ivo Sabev
A: 

is this in the loop? If it is:

if(is_category(2)) {
     echo the_ID();
     i++;
     echo i++;
}

Now if you are doing this through SQL. You will have to add a column.

Anraiki