Jump to content

Category Structure


fixxxer

Recommended Posts

This may sound like a stupid query and ive looked on the net for a tutorial. im trying to build a site that contains information stored in categories. to give an example if i wanted to store information about football i would want a structure like this

 

Football > English > Premiership > Liverpool > Steven Gerrard >

 

the problem im having is how do i store this in a database that links it all together i have an idea on how i can do it but it seems long winded and stupid, basically i want each category to be a link to that section so if they clicked liverpool thet would go to one page and if they clicked the parent category it would be another page.

 

i know this might seem a simple one and im not to bad with php coding would just like an explantion of the most efficent way to do this.

 

if anyone can help id be grateful.

 

thanks

Link to comment
Share on other sites

can someone please help me, its not even the code im looking for just an explanation of the osrt of set up i need e.g database and how i link the different catergories so far ive got :-

 

cat_id

parent_id

cat_name

 

where i can put the parent id and so can create the breadcrumbs by going back through the db but this seemed quite long winded. can someone suggest to me the best way to do this

 

thanks

 

Link to comment
Share on other sites

What you want to do is find a website that helps with Database set-up. 

 

you want to set up reference tables for each catergory:

 

Example: 

refsports

reflocation

 

Then fill these with an unique id and then the information needed.

 

Then in your data table you want to call these using the unique id from each reference table.

Link to comment
Share on other sites

Try this

 

Create some test data first

<?php
$sql = "create table sports
        (
            id int not null primary key, 
            parent int, 
            name varchar (30)
        )";
mysql_query($sql) or die (mysql_error()."<p>$sql</p>");

$sql = "INSERT INTO sports (id, parent, name) VALUES
(1, 0, 'Football'),
(2, 1, 'England'),
(3, 2, 'Premiership'),
(4, 3, 'Man U'),
(5, 4, 'R Giggs'),
(6, 0, 'Rugby'),
(7, 6, 'England'),
(8, 7, 'Div1'),
(9, 8, 'Sale'),
(10, 9, 'J Robinson'),
(11, 4, 'G Nevill'),
(12, 3, 'Chelsea'),
(13, 12, 'T Henri')";

mysql_query($sql) or die (mysql_error()."<p>$sql</p>");
?>

 

Then

<?php
$sql = "SELECT * FROM sports";
$g = new baaGrid($sql);
$g->display();

function crumbs($id, $level=0) {  
    $sql = "SELECT parent, name FROM sports WHERE id = $id";
    $res = mysql_query($sql) or die (mysql_error()."<p>$sql</p>");
    list ($p, $n) = mysql_fetch_row($res);
    
    if ($p != 0) crumbs($p, $level+1);
    echo ($p==0) ? "$n " : "» $n ";    
}

crumbs(3);       // --> Football » England » Premiership 
echo '<br>';
crumbs(10);      // --> Rugby » England » Div1 » Sale » J Robinson 
echo '<br>';
crumbs(5);       // --> Football » England » Premiership » Man U » R Giggs 
echo '<br>';

?> 

Link to comment
Share on other sites

  • 4 years later...
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.