Jump to content
MakeWebGames

Recommended Posts

Posted

So, my lecturer gave me this task

Imagine we have a database table of category information as per the below. This shows categories in a hierarchical structure indicated by a parent_id. I.e. a category with a parent_id of 1 is a sub- category of Clothing, whereas a category with a parent_id of 5 is a sub-category of Men. Any category with a sub-category of 0 is a top level category.
 id
parent_id name
1 0 Clothing
2 0 Accessories
3 0 Watches
4 1 Women
5 1Men
6 4 Tops
7 4 Trousers
8 5 T-Shirts
We would like you to create a function that produces a category breadcrumb. This function should use parameters for the required category id and the separator.
For example. If I supplied 8 as the category id and ‘>’ as the separator I would expect the result to be:
Clothing > Men > T-shirts
If I supplied 5 as the category id and ‘/’ as the separator I would expect the result to be:
Clothing / Men
For the purposes of this exercise assume you have a function called getCategory($id) available to supply you with an array of the category values when the category id is passed in.
For example: getCategory(8) would return Array(
      'id' => 8,
      'parent_id' => 5,
      'name' => 'T-Shirts'
)

I’ll be honest with you I don’t even know what his on about? Can someone help me out 😂😂😂

Posted

1 table, 3 columns; id, parent_id, name.
We can make some guesses at what the data might actually be, based on what we see. For example, I see that `id` (the first column) is likely a primary key that auto-increments.
Again, based on the data we've got and the explanation given with it, the parent_id relates to another row in the table if parent_id is greater than 0, or otherwise has no direct relation (and therefore is the parent category).

Your instructions are to write a function that'll display this breadcrumb-style, based on parameters given to said function.
As the question says, assume you've already got a function named getCategory() and it takes 1 parameter; the id.
Can you write, say, showBreadcrumbs()?

If you have a PHP-capable webserver and a DB server available to you, then start playing around (suggested only as I'm aware you're familiar with this)! Create the table with the 3 columns and insert the data as displayed - then query it.
If not, or if you'd like to run this in a php sandbox online for free (hint hint), then write an array of the data instead;
 

<?php
$categories = [
  [
    'id' => 1,
    'parent_id' => 0,
    'name' => 'Clothing',
  ],
  [
    'id' => 2,
    'parent_id' => 0,
    'name' => 'Accessories',
  ],
  // ...
];


There's a couple of "oh, duuuhhh" moments in this exercise. Remember; keep it simple!

Posted

@Magictallguy

@ags_cs4

Thanks guy, both were very useful and I appreciate it!

Now I have a problem the wording is flipped. So when I click on trousers this is what appears. I need it to show 

Trousers > Women > Clothing


I need it to show 

Clothing > Women > Trousers

<?php

    $_GET['id'] =
            (isset($_GET['id']) && is_numeric($_GET['id']))
                    ? abs(intval($_GET['id'])) : '';

if (!isset($_GET['action'])) {
    $_GET['action'] = '';
}
switch($_GET['action']) {
case "view":
    view();
    break;
default: 
    index();
    break;
}
                 
function index() {
    ?><table width = '100%'>
        <tr>
            <th>
                <a href = 'task.php?action=view&id=1'>Clothing</a><br/><br/>
                <a href = 'task.php?action=view&id=4'>Women</a> > <a href = 'task.php?action=view&id=6'>Tops</a> > <a href = 'task.php?action=view&id=7'>Trousers</a><br/><br/>

                
                <a href = 'task.php?action=view&id=5'>Men</a> > <a href = 'task.php?action=view&id=8'>T-Shirts</a>
            </th>
            <th><a href = 'task.php?action=view&id=2'>Accessories</a></th>
            <th><a href = 'task.php?action=view&id=3'>Watches</a></th>
        </tr>
    </table><?php
}
function view() {

if (empty($_GET['id'])) {
        ?>Invalid entry.<?php 
        exit();
}  
$category = array(
    1 => array(
        'id' => 1,
        'parentID' => 0,
        'name' => 'Clothing'
    ),
    2 => array(
        'id' => 2,
        'parentID' => 0,
        'name' => 'Accessories'
    ),
    3 => array(
        'id' => 3,
        'parentID' => 0,
        'name' => 'Watches'
    ),
    4 => array(
        'id' => 4,
        'parentID' => 1,
        'name' => 'Women'
    ),
    5 => array(
        'id' => 5,
        'parentID' => 1,
        'name' => 'Men'
    ),
    6 => array(
        'id' => 6,
        'parentID' => 4,
        'name' => 'Tops'
    ),
    7 => array(
        'id' => 7,
        'parentID' => 4,
        'name' => 'Trousers'
    ),
    8 => array(
        'id' => 8,
        'parentID' => 5,
        'name' => 'T-Shirts'
    ),
    );

function breadcrumber($array, $id){
    static $result = [];  // declare the storage variable without losing elements during recursion
    if(isset($array[$id])) {  // if target exists
        $result[] = $array[$id]['name'];  // store title text
        $parent = $array[$id]['parentID'];  // assign new target
        unset($array[$id]);  // remove possibility of an infinite loop
        breadcrumber($array, $parent);  // recurse
    }
    return $result;
}

echo implode(' > ',breadcrumber(array_column($category, NULL, 'id'), $_GET['id']));
}

?>

 

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.   Paste as plain text instead

  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...