Jump to content

Tag Cloud - two tables problem


EternalSorrow

Recommended Posts

I recently acquired a tag cloud code from Prism Perfect for use with the simple article database I created.

 

I have two tables the tag must query:

The catjoin table holds category_id and entry_id.  It is used to show the COUNT for instances of category use per entry_id.

The category table holds category_id and the actual category_name.

 

The creator stated that one could use two tables, but I've found that when joining the two tables the category_name from category, which is the second table, isn't queried.

 

Only an empty space appears where the $category_name should be.  I've tried using variations in the SELECT area and WHILE area, but either it doesn't work or I receive an error message concerning the ARRAYs.

 

Here's the code I slightly modified for my own use:

<?php
mysql_connect(localhost,user,pw);
@mysql_select_db(db) or die( "Unable to select database");

$query = "SELECT category_id AS tag, COUNT(entry_id) AS quantity
FROM catjoin 
JOIN category USING (category_id)
GROUP BY category_id
ORDER BY category_id ASC";

$result = mysql_query($query);

while ($row = mysql_fetch_array($result)) {
    $tags[$row['tag']] = $row['quantity'];
}

$max_size = 250; // max font size in %
$min_size = 100; // min font size in %

$max_qty = max(array_values($tags));
$min_qty = min(array_values($tags));

$spread = $max_qty - $min_qty;
if (0 == $spread) { // we don't want to divide by zero
    $spread = 1;
}

$step = ($max_size - $min_size)/($spread);

foreach ($tags as $key => $value) {

    $size = $min_size + (($value - $min_qty) * $step);

    echo '<a href="categories.php?category_name='.$category_name.'" style="font-size: '.$size.'%"';
    echo ' title="'.$value.' article(s) tagged with '.$key.'"';
    echo '>'.$category_name.'</a> ';

}
?>

Link to comment
https://forums.phpfreaks.com/topic/140404-tag-cloud-two-tables-problem/
Share on other sites

Try this as your first query:

 

$Query 	= 	sprintf
		('
			SELECT
					c.*,ci.*
			FROM c.category
			LEFT JOIN ci.catjoin
			USING (c.category_id)
			GROUP BY c.category_id
			ORDER BY c.category_id ASC
		');
$result = mysql_query($Query) or die("Line error on " . ___LINE___ . "<br>".mysql_error());

It merely gives the following error message:

 

Line error on ___LINE___

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.category_id) GROUP BY c.category_id ORDER BY c.category_id ASC' at line 2

The error message remains the same.  Here's the modified Query code in full (I've placed a simple "or die" command in the result line):

$Query = sprintf
("SELECT c.*, ci.* FROM c.category
LEFT JOIN ci.catjoin USING (c.category_id)
GROUP BY c.category_id
ORDER BY c.category_id ASC");

$result = mysql_query($Query)  or die(mysql_error());

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.