Jump to content

[SOLVED] listing catagories


jacko_162

Recommended Posts

Hi folks,

 

wonder if you can help a .php newbie out.

 

I have a catagories page, that i want to list a load of root catagories and sub catagories on.

 

for example;

 

Root Catagory

Root Catagory

 

These are my root catagories,

 

i want it to list like the following;

 

Root Catagory

        Sub Catagory

        Sub Catagory

 

 

so i set my table up like so;

 

===============

ID (Obviously the Catagory ID)

NAME (Catagory Name)

VIEW (YES/NO to make it viewable on main site)

PARENT (this is the new bit!)

 

i add the following to this table;

-------------------

ID        =  1

NAME    =  Root Catagory

VIEW    =  Yes

PARENT  =  0

-------------------

ID        =  2

NAME    =  Sub Catagory

VIEW    =  Yes

PARENT  =  1

-------------------

ID        =  2

NAME    =  Sub Catagory

VIEW    =  Yes

PARENT  =  1

-------------------

 

i then need a page to show me that PARENT = '0' should come first, then i need to add the sub catagory (in example above PARENT = '1' )

 

how can i code this??

 

Any help appreciated.

Link to comment
https://forums.phpfreaks.com/topic/107136-solved-listing-catagories/
Share on other sites

Why not just do something like:

 

 

index.php

<?php
error_reporting('E_ALL');

$sql = "SELECT `parent` FROM `db_name` . `categories`";
$sql = mysql_query($sql);

if(mysql_num_rows($sql) > 0)) {
  while($row = mysql_fetch_array($sql)) {
    print '<a href="article.php?parent=' . $row['id'] . '">' . $row['name'] . '</a>';
    print '<br />';
  }
}
else {
  print 'No Categories Available.';
}
?>

 

article.php

<?php
error_reporting('E_ALL');

$parent = (isset($_GET['parent'])) : htmlentities(mysql_real_escape_string($_GET['parent'])) ? false;
$child = (isset($_GET['child'])) : htmlentities(mysql_real_escape_string($_GET['child'])) ? false;
$article = (isset($_GET['article'])) : htmlentities(mysql_real_escape_string($_GET['article'])) ? false;

if($parent) {
  $sql = sprintf("SELECT `child` FROM `db_name` . `categories` WHERE `parent` = '%'", $parent);
  $sql = mysql_query($sql);

  if(mysql_num_rows($sql) > 0 ) {
    while($row = mysql_fetch_array($sql)) {
      print '<a href="article.php?child=' . $row['id'] . '">' . $row['name'] . '</a>';
      print '<br />';
    }
  }
  else {
    print 'Invalid ID';
  }
}
else {
  print 'Invalid ID';
}

if($child) {
  $sql = sprintf("SELECT * FROM `db_name` . `articles` WHERE `cat_id` = '%s';
  $sql = mysql_query($sql);

  if(mysql_num_rows($sql) > 0) {
    while($row = mysql_fetch_array($sql)) {
      print '<a href="article.php?article=' . $row['id'] . '">' . $row['name'] . '</a>';
      print '<br />';
    }
  }
  else {
    print 'Invalid ID';
  }
}
else {
  print 'Invalid ID';
}

if($article) {
  $sql = sprintf("SELECT * FROM `db_name` . `articles` WHERE `id` = '%s' LIMIT 1", $article);
  $sql = mysql_query($sql);

  if(mysql_num_rows($sql) > 0) {
    if($obj = mysql_fetch_object($sql)) {
      print '<a href="article.php?article=' . $obj->id . '"><h1>' . $obj->name . '</h1></a>';
      print '<p>' . $obj->content . '</p>';
    }
  }
  else { 
    print 'Invalid ID';
  }
}
else {
  print 'Invalid ID';
}
?>

 

Obviously you'll need to adjust it to meet your requirements.

thank you,

 

i have used this for cat.php

 

<? include("includes/connect.php") ?>
<?php

$categories = mysql_query("select * from $table13 WHERE parent = '0' ORDER BY name ASC") or die(mysql_error()); // Select all the root categories and order them alphabetically

while($category = mysql_fetch_array($categories)) // Start the loop for root categories
{
echo '<h2><a href="category.php?id=' . $category['id'] . '">' . $category['name'] . '</a></h2>'; // Display category name

$subcategories = mysql_query("SELECT * FROM $table13 WHERE parent = '" . $category['id'] . "' ORDER BY name ASC") or die(mysql_error()); // Same as the query above but this time the parent is the root category that has just been echoed

echo '<ul>';

while($subcategory = mysql_fetch_array($subcategories)) // Start the loop for subcategories
{
	echo '
	<li><a href="category.php?id=' . $subcategory['id'] . '">' . $subcategory['name'] . '</a></li>'; // Display subcategory. Link to category.php
}

echo '</ul>';
}
?>

 

and it displays how i need it to.

 

thank you again.

another quick question;

 

can anyone help me convert the above code into this page;

 

<? include("includes/header.php") ?>
<?php

$categories = mysql_query("select * from $table13 WHERE parent = '0' ORDER BY name ASC") or die(mysql_error()); // Select all the root categories and order them alphabetically

while($category = mysql_fetch_array($categories)) // Start the loop for root categories
{
echo '<a href="category.php?id=' . $category['id'] . '">' . $category['name'] . '</a>'; // Display category name

$subcategories = mysql_query("SELECT * FROM $table13 WHERE parent = '" . $category['id'] . "' ORDER BY name ASC") or die(mysql_error()); // Same as the query above but this time the parent is the root category that has just been echoed

echo '<br>';

while($subcategory = mysql_fetch_array($subcategories)) // Start the loop for subcategories
{
	echo '
	<li><a href="category.php?id=' . $subcategory['id'] . '">' . $subcategory['name'] . '</a></li>'; // Display subcategory. Link to category.php
}

echo '</ul>';
}
?>

 

much appreciated.

sorry i ment this code;

 

<title>Cpanel - Catagory List</title>
<? include("includes/header.php") ?>
<table width="100%" border="0" cellspacing="1" cellpadding="4">
  <tr valign="top">
    <td width="80%"><table width="75%" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td width="4" height="4" bgcolor="#f2f3f5"><img src="images/top_left_corner.gif" width="4" height="4" /></td>
        <td height="4" background="images/top_menu.gif" bgcolor="#f2f3f5"><img src="images/top_menu.gif" width="1" height="4" /></td>
        <td width="4" height="4" bgcolor="#f2f3f5"><img src="images/top_right_corner.gif" width="4" height="4" /></td>
      </tr>
      <tr>
        <td width="4" background="images/left_menu.gif" bgcolor="#f2f3f5"><img src="images/left_menu.gif" width="4" height="1" /></td>
        <td bgcolor="#f2f3f5"><table cellpadding="3" cellspacing="1" border="0" width="100%">
          <tr> </tr>
          <tr>
            <td width="60%"><b><span class="style8 style4"><strong><u>Catagory Name</u></strong></span></b></td>
            <td width="25%"><div align="center" class="style8"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><u>Show?</u></font></div></td>
            <td width="15%"></td>
            <?
// Query to pull information from the "Catagories" Database
$result = mysql_query("select * from $table13 order by view DESC");
while ($row = mysql_fetch_object($result)) {
?>
          </tr>
          <tr>
            <td width="60%"><?
echo $row->name; ?>
            </td>
            <td width="25%"><div align="center"><span class="style7">
              <?
echo $row->view; ?>
            </span></div></td>
            <td width="15%"><div align="center"><font size="2"><b><a href="editcat.php?ID=<? echo $row->id; ?>"><img src="images/edit.gif" border="0" /></a> <a href="remove.php?ID=<? echo $row->id; ?>&db=catagory"><img src="images/del.gif" border="0" /></a></b></font>
                        <? } ?>
            </div></td>
          </tr>
        </table></td>
        <td width="4" background="images/right_menu.gif" bgcolor="#f2f3f5"><img src="images/right_menu.gif" width="4" height="1" /></td>
      </tr>
      <tr>
        <td width="4" height="4" bgcolor="#f2f3f5"><img src="images/bottom_left_corner.gif" width="4" height="4" /></td>
        <td height="4" background="images/bottom_menu.gif" bgcolor="#f2f3f5"><img src="images/bottom_menu.gif" width="1" height="4" /></td>
        <td width="4" height="4" bgcolor="#f2f3f5"><img src="images/bottom_right_corner.gif" width="4" height="4" /></td>
      </tr>
    </table>
        <div align="right"></div></td>
  </tr>
</table>

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.