PHP, CSS navigation bar coding

So, it would be possible to display subcategory name instead of Category name? BMW Accessories, Merecedes Accessories or Ford Accessories replaces ‘Accessories’ in the example above but there are no subcategories for any other links.

Would the Category->SubCategory->Product still work or would you recommend something easier considering it is only one subcategory for accessories!?

Matt.

Well personally i’d recommend an in-order placement of the expanded menus, but thats me.

It’s perfectly possible to do anything you want with them, you just need to decide on a design before you lay out the code.

I have drawn a diagram to understand the flow-of-clicks in the navigation to work out how many levels of navigation I need and it appears to be 4.

For example

Category: Accessories
Subcategory1: Manufacturer Accessories (ie. BMW, Mercedes)
Subcategory2: Product Series Accessories (ie. BMW 3 Series, BMW Z-Series)
Subcategory3: Product Model Accessories (ie. BMW Z3, BMW Z4)
Subcategory4: Selected Model Product Accessories (ie. BMW Z3 Accessories; indicator, steering wheel, etc.)

I am trying to understand where to start but my main problem is:

  1. Replacing the category name with the subcategory1 name and then replacing the subcategory1 name with the subcategory2 name, etc. depending on what is clicked.
  2. And since the menu contents will change how do I ‘get’ the data since they are not ‘products’ instead they are options until the final level of navigation when they are then accessories but I guess at this final level they could be located using the product.php page.

Would you advise adding additional tables for each of the extra subcategories? Or is there a way I could keep the ‘categoryid’ and somehow choose the associated subcategory name depending on what is clicked? (ie. If you click something in Accessories it is always ‘categoryID’ 8 but the name displayed for this section changes depending what level of navigation the user is within. This might completely mess up the fact that we are selecting an array based on category ID’s since we would be relying on up to 4 different fields?!)

Hope you understand my confusion. Do you have any idea how I can get around this problem. It is only 2 of the 10 (approx.) links in the navigation bar that will have 4 levels. The other 8 will work fine with the code I have so far.

Matt.

I dont understand what you mean by “product.php and categoryview.php would be including the same navigation.php”

I will need separate PHP files called
product.php
categoryview.php

but you suggest these will be included in a navigation.php file??

Matt.

You dont necessarily need seperate PHP files for product and categoryview (Could just be ‘store.php’), but I think you might find it easier to seperate them at the beginning. (Most forum software, for example, has a viewforum.php, a viewthread.php, a post.php etc. Seperate pages for seperate actions).

I’m saying the opposite flow; the navigation.php would be included in both of those files.

OK - A couple more things…

  1. I do not understand the use of “$key” in the last bit of coding - what does this mean/do?

  2. In terms of the ordering of links in the main navigation bar, and in subcategories how do I determine the order of appearance? Is the order exactly the order in the MySQL table? Or is there a way of specifying the order I want them to appear?

Thanks,

Matt.

$cats , when pulled by this query, is a numerical array.

$cats[1] = Phones
$cats[5] = Apparel
etc,
where the number = the categoryId in the database.

foreach($array AS $key => $value)

is a structure that says:
Walk through the array from start to end.
Each time through, set $key to the value of the key in the array entry. Set $value to the value held in that array entry.

So the first time through the loop, $key = 1, $value = “Phones”. The second time, $key = 5, $value = “Apparel”. This also works with associative keyed arrays, the $key would just be a string instead.

You can sort your results pretty easily using the mySQL ORDER BY clause; EG: SELECT * FROM categories ORDER BY name; will put them in alphabetical order by the name field.

And it would be possible to ‘select’ one field from a mySQL table and ‘order’ by another?

Matt.

Certainly. You can order by and select any combination of fields that are in the tables your query references.

You shouldnt need more than 1 if(isset()) per -level of navigation-. (And probably less than that, even)

While it’s true that it will only take milliseconds (or less) to make the if call, you still shouldnt do more than is required.

Hi StarLion,

I have been extending the code you suggested and am finding that the way to design the navigation I need would be to include further “if(isset($GET…)” functions for each subcategory etc… This means there will be at least 50 “if(isset($GET…)” functions and may be about 100.

These “if(isset($GET…)” functions then each have their own “if(isset(selcat1”,2", 3",3" ,4" correspondingly"

I am guessing if the function is false (is not used) this only takes milliseconds to determine. Can you confirm I will not be adding loading time to the web page by using numerous “if(isset…)” functions.

Thanks,

Matt.

The trickiest bit I think will be adding navigation for all accessories for all products. Specifically this means if you are looking at a particular accessory for product 200 of 300 products then all accessories for product 200 will also feature in the navigation bar.

Are you suggesting I should only be using one if(isset()) function for all these different products?? Is that possible? I can only think of using an isset function to determine what product is clicked and then provide the navigation links based on that but this would be an isset function for each product… how can fewer (or 1) isset functions achieve the same outcome??

Matt.

Without knowing the database structure this will be pure guesswork, but…


if(isset($_GET['product']) { //I have cloicked on a product. This product's ID is being stored in that variable.
  $stuff =  $sql->query("SELECT somestuff FROM accessories WHERE productID = ".$sql->real_escape_string($_GET['product']));
//This query will get the accessories associated with the product in question, no matter which product it is.
  while($row = $stuff->fetch_array()) {
    //Output whatever here.
  }
}

1 ISSET, every product covered.

:cool: I’ll try it out.

:cool: Thanks.

:cool: Matt.

I have a problem with the line:

$stuff = $sql->query("SELECT somestuff FROM accessories WHERE productID = ".$sql->real_escape_string($_GET[‘product’]));

It produces a fatal error which reads:

“Fatal error: Call to a member function on a non-object in /home/content/…”

I do not understand what the $sql means or does - maybe it is that which is producing this fatal error? Any ideas?

Matt.

$sql in my code was a mysqli object. If you’re using a mysql procedural call, it would look like
$stuff = mysql_query("SELECT somestuff FROM accessories WHERE productID = ".mysql_real_escape_string($_GET[‘product’]));

StarLion,

Can you just briefly explain why you suggest using _real_escape_string for this part of the navigation and not the previous part of the navigation bar. Is there a very good reason? Or would you recommend I use _real_escape_string in the previous code you suggested too?

All the best,

Matt.

real_escape_string (or sanitization) should be used whenever you use user input in a database query. Always.