Help - Search - Members - Calendar
Full Version: [resolved] Categories
Codegrrl.com Forums > Script Help > Tutorial Help
Phenix
I was wondering if it was possible to add categories to your "blog" using the BAB tutorials? If there is would anyone be kind enough to tell me how I could do that please?
Amelie
Yes, it is possible.

You will need to add a column called 'category' to your posts table and you will need to add that field into your update page, just like when adding the password field in step 4 of the tutorial. Then you would just type the category into the field when you update your blog (multiple categories are not possible without a complete rewrite of the BAB code, so it will only be single categories).

To create an archive by category, see this thread, except you will need to add one line above where it says print "$category"; - you need to write $category = $_GET['category']; . Also, you should not call the page archive.php if you have called any other page archive.php - call this page something different.

If you get stuck, there are LOADS of threads about BAB categories. I did a quick search and found lots of results - just go to Search and type in 'categories' (making sure you're just searching the Tutorial Help forum). Those should help you out with any problems you encounter. smile.gif
Phenix
Thanks a million! I'll get started soon. biggrin.gif
Phenix
I've been working with adding the categories and surprisingly enough, it was pretty easy!! YAY. The only thing I could really find an answer to was, if its possible to have a link to the page where all the posts of the same category are.

Like for instance, I'm using the categories for keep track of all the books and music I review in certain categories, and one books I review is young adults, I want to link that to all the other posts reviewing books of the same genre. is that possible?

Thanks in advance!
Amelie
Yes, the modified archive page in the thread I linked will help you do that - just link your categories on your blog post as

CODE
category.php?category=<?php echo $row['category']; ?>


Assuming your category archive page is called category.php and your database field is called category.
Phenix
I'm not sure if I did something wrong, but I know its almost working. I think I'm using a different archive though so I'm not sure how I would change it. Right now the archive is

categories.php
CODE

<?php include("/mnt/w0603/d08/s07/b025e6a3/www/vintage/cookiecheck.php");
include($headervar.$skin.$extension);
mysql_connect ('****', '****', '****');
mysql_select_db ('****');

if (!isset($_GET['genre'])) {
    die("Invalid genre specified.");
}
else {
    $category = htmlspecialchars(strip_tags($_GET['genre']));
}

if (get_magic_quotes_gpc()) $genre = stripslashes($genre);
$genre = mysql_real_escape_string($genre);

$result = mysql_query("SELECT timestamp, id, review_title, genre FROM review WHERE genre = '$genre' ORDER BY id DESC");

while ($row = mysql_fetch_array($result)) {
    $date = date("l F d Y", $row['timestamp']);
    $id = $row['id'];
    $review_title = stripslashes($row['review_title']);
    $postgenre = $row['genre'];

    $num_of_posts = mysql_num_rows($result);
    ?>
    
    <div class="div" id="content">
    <p class="h4"> Review Archives </p>
    <p class="content">In <b><u><?php echo $postgenre; ?></b></u> genre we have [ <?php echo $num_of_posts; ?> ] entries </p>
    
    <p> <b>Reviewed On</b> :: <?php echo $date; ?><br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; »<a href="reviews.php?id=<?php echo $id; ?>"><?php echo $review_title; ?></a>
    </p>
    
    
    
    
    </div>
    <?php
}

?>


And I linked it like this

CODE
<a href="/vintage/categories.php?genre=<?php echo $row['genre']; ?>">New Age</a>


And it took me here // http://www.walkinginchrist.org/vintage/cat...cho%20$row['genre'];%20?%3E

its like it almost gets there but doesn't quite make it. Any suggestions. Thanks a lot for your help I really appreciate it. smile.gif
Amelie
First things first, you have named $genre $category at the top.

This:

CODE
else {
    $category = htmlspecialchars(strip_tags($_GET['genre']));
}


Should be

CODE
else {
    $genre = htmlspecialchars(strip_tags($_GET['genre']));
}


Otherwise the script doesn't know what the $genre part refers to, since you actually specified for it to be called $category wink.gif

Secondly, try changing your link to

CODE
<a href="/vintage/category.php?genre=<?php echo $postgenre; ?>">New Age</a>


I'm not sure why it linked your PHP in there... It's supposed to have translated that. If it's still happening after changing your code, can you link us to the page this is happening on?
Phenix
That didn't work either. Not sure exactly what's going on.

// http://www.walkinginchrist.org/vintage/reviews.php?id=1
Amelie
blink.gif I can honestly say I've never seen that happening before :|

Try writing the link in PHP, like so, and see if it works:

CODE
<?php echo '<a href="/vintage/category.php?genre=' . $postgenre . '">' . $postgenre . '</a>'; ?>


You're not including that page as a .html file or anything, are you? It's definitely all .php? It looks to me like it is, but then I can't find any other explanation for it not reading the PHP :\

Oh, wait, are you trying to get this to work from inside your post? I.e., are you writing a post with the category PHP code in it? If so, that won't work - PHP won't read code that comes out of the database.

You need to add the <?php echo $postgenre; ?> part to your main index.php and entry.php files (or whatever your main blog page and single entry pages are called), like you did for the quotes, date and entries.
Phenix
[[ smacks forehead ]] I'll try that, because I was trying to add it via database!

[[edit]]

YAY! That was it! Thank You SO SO SO much! It works now!
Amelie
Hurrah! smile.gif

Yeah, code won't work if it's in your post. HTML will work, but not PHP, since your post is being retrieved from the database using PHP and it can't read the code in the post at the same time as retrieving it, or something like that, heh. Someone else was having this problem, and although there are ways to get PHP working in your post, it can be quite insecure and difficult to get working so I wouldn't recommend it.
Phenix
Thanks for the heads up, I'm glad I know that now!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.