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
https://forums.phpfreaks.com/topic/166836-separate-blog-into-categories-with-tags/
Share on other sites

just seperate the tags with commas, and do something like this

 


$searchterm=$_REQUEST['searchterm'];

$res=mysql_query("SELECT * FROM blogs WHERE `tags` LIKE '%,$searchterm,%'");
while($row=mysql_fetch_array($res));

//etc

 

lemme know

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']));

 

 

 

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.

 

You have to have the same LIKE on your bottom query. That's great that u have it in the count, but if you don't limit the actual result set you're gonna get all of the results

 

the like right after " //get the info from the db "

 

:sweat:

 

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"?

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.