dachshund Posted July 21, 2009 Share Posted July 21, 2009 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 Quote Link to comment Share on other sites More sharing options...
seventheyejosh Posted July 21, 2009 Share Posted July 21, 2009 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 Quote Link to comment Share on other sites More sharing options...
dachshund Posted July 21, 2009 Author Share Posted July 21, 2009 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'])); Quote Link to comment Share on other sites More sharing options...
dachshund Posted July 21, 2009 Author Share Posted July 21, 2009 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. Quote Link to comment Share on other sites More sharing options...
dachshund Posted July 23, 2009 Author Share Posted July 23, 2009 if anyone could provide any insights at all it would be much appreciated. thanks Quote Link to comment Share on other sites More sharing options...
seventheyejosh Posted July 23, 2009 Share Posted July 23, 2009 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 " Quote Link to comment Share on other sites More sharing options...
dachshund Posted July 23, 2009 Author Share Posted July 23, 2009 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"? Quote Link to comment Share on other sites More sharing options...
seventheyejosh Posted July 24, 2009 Share Posted July 24, 2009 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.