Jump to content

Recommended Posts

Hi, i have one problem, this is my example:

DROP TABLE IF EXISTS category;
CREATE TABLE IF NOT EXISTS category (
  category_categoryid INT(11) NOT NULL auto_increment,
  category_title VARCHAR(30),
  PRIMARY KEY (category_categoryid)
);

INSERT INTO category (category_categoryid, category_title) VALUES
(1, 'Example Category 1'),
(2, 'Example Category 2'),
(3, 'Example Category 3');

DROP TABLE IF EXISTS subcategory;
CREATE TABLE IF NOT EXISTS subcategory (
  subcategory_subcategoryid INT(11) NOT NULL auto_increment,
  subcategory_title VARCHAR(30),
  subcategory_categoryid INT NOT NULL,
  PRIMARY KEY  (subcategory_subcategoryid),
  FOREIGN KEY(subcategory_categoryid) REFERENCES category (category_categoryid)
);

INSERT INTO subcategory (subcategory_subcategoryid, subcategory_title, subcategory_categoryid) VALUES
(1, 'Example subcategory 1', 1),
(2, 'Example subcategory 2', 1),
(3, 'Example subcategory 3', 2),
(4, 'Example subcategory 4', 2);

DROP TABLE IF EXISTS articles;
CREATE TABLE IF NOT EXISTS articles (
  articles_postid INT(11) NOT NULL auto_increment,
  articles_title VARCHAR(50),
  articles_body TEXT,
  articles_subcategoryid INT,
  PRIMARY KEY  (articles_postid),
  FOREIGN KEY(articles_subcategoryid) REFERENCES subcategory (subcategory_subcategoryid)
);

INSERT INTO articles (articles_postid, articles_title, articles_body, articles_subcategoryid) VALUES
(1, 'This is my first article', 'Small description', 1),
(2, 'This is my second article', 'Small description', 1),
(3, 'This is my 3th article', 'Small description', 2),
(4, 'This is my 4th article', 'Small description', 3);

 

and now ... i want to display results from mysql database just like this:

Example Category 1 (3)

Example subcategory 1 (2)

Example subcategory 2 (1)

Example Category 2 (1)

Example subcategory 3 (1)

Example subcategory 4 (0)

Example Category 3 (0)

 

... category1 (count all articles in subcategories) ...

... subcategory1 of category1 (and count all articles in this category) ...

 

HELP ???

 

... and sorry for my bad english ...

 

...thanks ...

Link to comment
https://forums.phpfreaks.com/topic/161199-solved-simple-php-script-article-script/
Share on other sites

<?php # alco19357
mysql_connect(HOST, USER, PASS);
mysql_select_db(DB);

$query_cats = mysql_query("select * from category");

$cats = array();

while($cat = mysql_fetch_array($query_cats)){
$query_subcats = mysql_query("select * from subcategory where subcategory_categoryid='".$cat["category_categoryid"]."'");
$subcats = array();
$articles = 0;
while($subcat = mysql_fetch_array($query_subcats)){
	$query_articles = mysql_query("select * from articles where articles_subcategoryid='".$subcat["subcategory_subcategoryid"]."'");
	$subcats[$subcat["subcategory_subcategoryid"]] = array("id" => $subcat["subcategory_subcategoryid"], "title" => $subcat["subcategory_title"], "articles" => mysql_num_rows($query_articles));
	$articles = mysql_num_rows($query_articles)+$articles;
}
$cats[$cat["category_categoryid"]] = array("id" => $cat["category_categoryid"], "title" => $cat["category_title"], "subcats" => $subcats, "articles" => $articles);	
}
$echo = '';
$row = 0;
foreach($cats as $_cat){
$row++;
if($row > 1){ $echo .= '<br>'; }
$echo .= $_cat["title"].' ('.$_cat["articles"].')';
foreach($_cat["subcats"] as $_sub){
	$echo .= '<br>  '.$_sub["title"].' ('.$_sub["articles"].')';
}
}
?>

<?php # alco19357
mysql_connect(HOST, USER, PASS);
mysql_select_db(DB);

$query_cats = mysql_query("select * from category");

$cats = array();

while($cat = mysql_fetch_array($query_cats)){
$query_subcats = mysql_query("select * from subcategory where subcategory_categoryid='".$cat["category_categoryid"]."'");
$subcats = array();
$articles = 0;
while($subcat = mysql_fetch_array($query_subcats)){
	$query_articles = mysql_query("select * from articles where articles_subcategoryid='".$subcat["subcategory_subcategoryid"]."'");
	$subcats[$subcat["subcategory_subcategoryid"]] = array("id" => $subcat["subcategory_subcategoryid"], "title" => $subcat["subcategory_title"], "articles" => mysql_num_rows($query_articles));
	$articles = mysql_num_rows($query_articles)+$articles;
}
$cats[$cat["category_categoryid"]] = array("id" => $cat["category_categoryid"], "title" => $cat["category_title"], "subcats" => $subcats, "articles" => $articles);	
}
$echo = '';
$row = 0;
foreach($cats as $_cat){
$row++;
if($row > 1){ $echo .= '<br>'; }
$echo .= $_cat["title"].' ('.$_cat["articles"].')';
foreach($_cat["subcats"] as $_sub){
	$echo .= '<br>  '.$_sub["title"].' ('.$_sub["articles"].')';
}
}
?>

 

thank you :))))))))))))

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.