Jump to content

separate blog into categories with tags


dachshund

Recommended Posts

hello all,

 

if i set up a row in my 'blog' table called 'tags' which would contain tags such as 'arts', 'music' etc, how can i use this to be able to separate all the blog posts up into categories.

 

for example if someone clicks the link to see all the 'arts' posts, how can i make that happen?

 

i know i need an 'if', just don't know how to write it.

 

any help would be much appreciated.

 

thanks

Link to comment
Share on other sites

ok i've tried placing it in a couple of places and editing things before coming back here, but i can't get it to work.

 

where should i put it in this lot?

 

// find out how many rows are in the table   
$sql = "SELECT COUNT(*) FROM entries ORDER BY id DESC";  
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);  
$r = mysql_fetch_row($result);  
$numrows = $r[0];  
  
// number of rows to show per page  
$rowsperpage = 8;  
// find out total pages  
$totalpages = ceil($numrows / $rowsperpage);  
  
// get the current page or set a default  
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {  
   // cast var as int  
   $currentpage = (int) $_GET['currentpage'];  
} else {  
   // default page num  
   $currentpage = 1;  
} // end if  
  
// if current page is greater than total pages...  
if ($currentpage > $totalpages) {  
   // set current page to last page  
   $currentpage = $totalpages;  
} // end if  
// if current page is less than first page...  
if ($currentpage < 1) {  
   // set current page to first page  
   $currentpage = 1;  
} // end if  
  
// the offset of the list, based on current page   
$offset = ($currentpage - 1) * $rowsperpage;  
  
// get the info from the db   
$sql = "SELECT * FROM entries ORDER BY id DESC LIMIT $offset, $rowsperpage";  
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
  
// while there are rows to be fetched...  
while ($list = mysql_fetch_assoc($result)) {
$date = date('d/m', strtotime($list['date']));

 

 

 

Link to comment
Share on other sites

ok so far i have

 


// find out how many rows are in the table   
$sql = "SELECT COUNT(*) FROM entries WHERE `tags` LIKE '%,arts,%' ORDER BY id DESC";  
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);  
$r = mysql_fetch_row($result);  
$numrows = $r[0];  
  
// number of rows to show per page  
$rowsperpage = 8;  
// find out total pages  
$totalpages = ceil($numrows / $rowsperpage);  
  
// get the current page or set a default  
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {  
   // cast var as int  
   $currentpage = (int) $_GET['currentpage'];  
} else {  
   // default page num  
   $currentpage = 1;  
} // end if  
  
// if current page is greater than total pages...  
if ($currentpage > $totalpages) {  
   // set current page to last page  
   $currentpage = $totalpages;  
} // end if  
// if current page is less than first page...  
if ($currentpage < 1) {  
   // set current page to first page  
   $currentpage = 1;  
} // end if  
  
// the offset of the list, based on current page   
$offset = ($currentpage - 1) * $rowsperpage;  
  
// get the info from the db   
$sql = "SELECT * FROM entries ORDER BY id DESC LIMIT $offset, $rowsperpage";  
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
  
// while there are rows to be fetched...  
while ($list = mysql_fetch_assoc($result)) {
$date = date('d/m', strtotime($list['date']));
  
?>

 

but this isn't selecting only the posts with 'arts' tag. it shows all posts.

 

Link to comment
Share on other sites

ah perfect, and so simple. i'm such an idiot.

 

any way to do it so that the first tag doesn't have to have a comma before, and the last tag doesn't have a comma after it as well?

 

it works like this ", arts, moving image, animation,"

 

can it work like this "arts, moving image, animation"?

Link to comment
Share on other sites

well the problem is if you use the key word over.

 

if the db is:

 

house , door , awesome , green house

 

then when you search for LIKE '%house%' you'll get stuff tagged with house and green house.

 

I always just do a check and see if it is the first tag in the db, if it is, append a ',' to the beginning. then just have the loop assigning tags add '$tag,' so the last tag is auto there.

 

just my preference tho.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.