Jump to content

Recommended Posts

I have two tables subcat and category

 

structure

subcat

id_subcat|id_of_cat|subcat_name| and for

main categories

ID_cat|cat_name|

 

now what I want to display them as

Main category

subcategory1

subcategory2

Main category2

subcategoryX

etc...

 

Here's the code I'm using but doesn't work,it doesn't display anything.

 

<?php
	$query = "
		SELECT ID_cat, id_subcat, subcat_name, cat_name 
	  FROM subcat, category 
	  WHERE ID_cat=id_subcat";
	$result = mysql_query($sql);

	$lastCat = null;
	while($row = mysql_fetch_assoc($result)) {
	  # Check if you need to print the category_name
	  if($lastCat != $row['ID_cat']) {
		# Not the same category as the last one!
		# Print it and save the ID
		$lastCat = $row['ID_cat'];
		echo "<br />". $row['ID_cat'];
	  }

	  # Print the sub category
	  echo "<br />". $row['subcat_name'];
	}
?>

Link to comment
https://forums.phpfreaks.com/topic/225014-categories-and-subcategories-display/
Share on other sites

The code you posted won't work because you are using the $query variable to make the query, but putting a non-existent $sql variable into the mysql_query() function.

 

Are you developing and debugging your code on a system with error_reporting set to E_ALL (or a -1) and display_errors set to ON so that php would help you by reporting and displaying all the errors it detects? You would save a TON of time on simple mistakes in your code.

Ok thanks for that tip, I solved the issue with undefined variable $sql (it should be $query)

 

There is still this warning for which I can't find the solution... googleing right now.

mysql_fetch_assoc() expects parameter 1 to be resource, boolean given

 

warning is located at

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

If you do a search for that error message, you will find that it generally means that your query failed due to an error, which is probably the reason Pikachu2000 asked this -

Have you tried to run that query in phpMyAdmin to see what it returns?

 

Make sure your query executes correctly in phpMyAdmin first.

After executing query in phpMyAdmin It showed up this error

#1052 - Column 'ID' in field list is ambiguous

 

I googled for this error and I've found this

It means that ID and/or Title exist in both tables

 

Doesn't mysql/php sepereates from uppercase and downcase ? Because structure of both tables is almost the same

 

Categories

|ID||cat_name|

 

Subcategory

|id||id_main| subcat_name|

 

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.