Jump to content

Group topics with their categories by using the anchor link.


Trium918

Recommended Posts

I am trying to group topics with their categories by

using the anchor link. There are only two categories called

Computers and Games. There are 6 different topics. Computers

have 5 while Games has only 1.

 

The query below gathers all topics.

<?php
$blog_query ="Select *,DATE_FORMAT(date_posted,'%W,%d %b %Y') as thedate FROM article INNER JOIN categories ON categoryID=catid WHERE DATE_SUB(CURDATE(),INTERVAL 30 DAY)AND artchild='0' ORDER BY date_posted DESC LIMIT 10 ";

$blog_articles = mysql_query($blog_query);
if (mysql_error()) {
die("Query failed<br />$blog_query<br />" . mysql_error());
}
?>

 

This query is pulling the name of the categories from

the database and placing them in links. This is where I

am stuck.

<?php
$category_sql="Select * FROM categories";
$category = mysql_query($category_sql) or die(mysql_error());
$num=mysql_num_rows($category);
if($num>0){
echo "<ul>";
while($row=mysql_fetch_assoc($category)){

echo "<li><a href=\"blog.php\">".$row['category']."</a></li>";
}
echo "</ul>";
}
?>

Link to comment
Share on other sites

What are you on about?

 

Do you mean you want a user to be able to press a sub cat name and see all those topics.

 

I want to be able to select all the articles that

belongs to its category similar to the link below.

Blog Categories

All Categories

Art and Photography

Automotive

Blogging

 

http://blog.myspace.com/index.cfm?fuseaction=blog.home&MyToken=8ca3e229-719f-42ab-9fbc-bc38130e3767

Link to comment
Share on other sites

You first echo out the sub cat, Then format the sub cat with links?cmd=catname then use a select with a like to get the sub cat's displayed.

 

is that what u want.

 

I got something like that working below be I am having trouble

writing a join to pull from the article table.

<?php
if(isset($_GET['action'])){
$action = $_GET['action'];
// This is where I am having problems writing
// a join. categoryID=catid
$blog_query ="Select * FROM article WHERE categoryID='$action'  
AND artchild='0' ORDER BY date_posted DESC LIMIT 10 ";
} else{
// I am trying to write a join similar to this one
// WHERE categoryID=catid
$blog_query ="Select *,DATE_FORMAT(date_posted,'%W,%d %b %Y') as thedate 
FROM article INNER JOIN categories ON categoryID=catid WHERE DATE_SUB(CURDATE(),INTERVAL 30 DAY)AND artchild='0' ORDER BY date_posted DESC LIMIT 10 ";
}

$category_sql="Select * FROM categories";
$category = mysql_query($category_sql) or die(mysql_error());
$num=mysql_num_rows($category);
if($num>0){
echo "<ul>
";
while($row=mysql_fetch_assoc($category)){
$catID = $row['catid'];
echo "<li><a href=\"blog.php?action=$catID\">".$row['category']."</a></li>";
}
echo "</ul>";
}}
?>

Link to comment
Share on other sites

try this ok allways backup.

<?php

if(isset($_GET['action'])){

$action = $_GET['action'];


$blog_query ="Select * FROM article as a,category as b WHERE LIKE '%$action%'  
AND b.id='$action' AND catartchild='0' ORDER BY date_posted DESC LIMIT 10 ";

$search_result=mysql_query($blog_query) or die("mysql_error()");

while($x=mysql_fetch_assoc($search_result)){

echo $x['what_ever'];
} 

$category_sql="Select * FROM categories";

$category = mysql_query($category_sql) or die(mysql_error());

$num=mysql_num_rows($category);

if($num > 0){

echo "<ul>";

while($row=mysql_fetch_assoc($category)){

$catID = $row['catid'];

echo "<li><a href=\"blog.php?action=$catID\">".$row['category']."</a></li>";
}
echo "</ul>";
}
}
?>

Link to comment
Share on other sites

Here is the query that I used. Your method works. Ok, now

what are the advantages and disadvantages of writing a query

this way? I asked because I have no knowledge of wild cards,

and because the category ID is displayed in the URL.

 

Wouldn't this be consider a security breach? I only have two categories

with the numbers 3 & 4.

blog.php?action=3

blog.php?action=4

 

 

<?php
$blog_query ="Select * FROM article as a,categories as b WHERE categoryID LIKE '%$action%'  
AND b.catid='$action' AND artchild='0' ORDER BY date_posted DESC LIMIT 10 ";
?>

Link to comment
Share on other sites

this % means like any character

 

sample

%teng% will be equal to Imtengdude

or its like saying any string having the word teng

 

but if %teng that means any string ending with teng

and if teng% any string beginning with thing

 

so much to explain maybe do some research on the other wild card lol 

 

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.