SitePoint Sponsor

User Tag List

Page 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 58
  1. #26
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, that makes sense now. So i would need to code it so that if the customer selects "iphone 4" from submenu for the "iphone" link the tables are cross referenced to check the product category.

    The only problem now, which I mentioned above, is making the clicked link appear at the top of the left navigation bar with its' sub menu.

    For example if I have

    Link 1
    Link 2
    Link 3
    Link 4
    Link 5
    Link 6
    Link 7
    Link 8

    AND CUSTOMER CLICKS LINK 8

    they get

    Link 8
    Product 1
    Product 2
    Product 3
    Product 4
    Product 5
    Product 6
    Product 7
    Link 1
    Link 2
    Link 3
    Link 4
    Link 5
    Link 6
    Link 7

    Is this possible? Basically I want the clicked link and its' submenu listed first and then the remaining links in the 'standard order'. The only link out of its' order is the one that was clicked.

    Matt.

  2. #27
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    Rule #1: Pretty much anything is possible, if coded right.

    Something like....(and i'm just pulling this off the top of my head so dont hold me to this being the best way to do it... and in fact i know it isnt because this isnt sanitized, but it's a demonstration)
    PHP Code:
    $res mysql_query("SELECT * FROM categories");
    while(
    $row mysql_fetch_array($res)) {
      
    $cats[$row['categoryid']] = $row['categoryname'];
    }
    if(isset(
    $_GET['category'])) {
     
    $selcat $_GET['category'];
    }
    if(isset(
    $_GET['product'])) {
     
    $prod_info mysql_fetch_array(mysql_query("SELECT * FROM product WHERE productid = ".$_GET['product']));
     
    //Prod_Info now gets stored for use in the main display.
     
    $selcat $prod_info['categoryid'];
    }
    if(isset(
    $selcat)) {
      echo 
    "<a href='categorylist.php?category=".$selcat."'>".$cats[$selcat]."</a>";
      unset(
    $cats[$selcat]); //Gets rid of it for later.
      
    $res mysql_query("SELECT productid,name FROM products WHERE categoryid = ".$selcat);
      while(
    $row mysql_fetch_array($res)) {
       echo 
    "&nbsp;<a href='product.php?product=".$row['productid']."'>".$row['name']."</a>";
      }
    }
    foreach(
    $cats AS $key => $cat)  {
      echo 
    "<a href='categoryview.php?category=".$key."'>$".$cat."</a>";


  3. #28
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Location
    Surrey, UK
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MatthewBOnline View Post
    can 1 PHP file be used if the left navigation bar changes slightly...
    This is exactly what PHP can be used for. It's dynamic, so one PHP file can output all kinds of different things depending on a whole number of variables including what a user has clicked, where they are in the world, whether they've logged in or not, what time of day it is... basically almost anything.
    Martin.

  4. #29
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am trying to understand the code StarLion provided.

    I am not sure what the first bit of code does:

    $res = mysql_query("SELECT * FROM categories");
    while($row = mysql_fetch_array($res)) {
    $cats[$row['categoryid']] = $row['categoryname'];
    }



    And, secondly if I try using the code below I get a Warning in my browser:

    if(isset($_GET['product'])) {
    $prod_info = mysql_fetch_array(mysql_query("SELECT * FROM product WHERE productid = ".$_GET['product']));
    //Prod_Info now gets stored for use in the main display.
    $selcat = $prod_info['categoryid'];


    The Warning says:
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/content/..etc...

    What could be the problem? Is it trying to display an array? And I'm not sure what you meant by "//Prod_Info now gets stored for use in the main display." - as this is a navigation bar, why do we store info for the main display??

    I think, if I can understand the first bit of code and solve the Warning problem, I might be getting somewhere with designing it!

    Look forward to your reply,

    Matt.

  5. #30
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    Okay...
    PHP Code:
    $res mysql_query("SELECT * FROM categories"); //Get everything out of the Categories table.
    while($row mysql_fetch_array($res)) { //For each category...
    $cats[$row['categoryid']] = $row['categoryname']; //Put the name of the category into a numeric array, based on the categoryid.

    So basically, if your category table looked like...

    CategoryID, CategoryName, Discount, .... (etc etc etc)
    1,Phones,0,....
    5,Apparel,5,....
    6,Accessories,0,.....
    .......

    (I skipped 2-4. Maybe you deleted some rows at some point. Who knows. CategoryID is a PRIMARY KEY.)

    Then PHP would hold an array $cats at the end of that code such that:
    $cats[1] = "Phones"
    $cats[5] = "Apparel"
    $cats[6] = "Accessories"

    (Note: numeric arrays in PHP dont 'fill in' gaps in the numeric structure.)

    "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/content/..etc..."

    means that the query returned an error. Try throwing
    PHP Code:
    mysql_error(); 
    on the line above that error.

  6. #31
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've tried adding the mysql_error(); but it still comes up with the same warning.

    Let me check I understand the code:
    You are creating prod_info for a single product? But why are we asking for an array? There will only be one ProductID (taken from the URL using the GET function).

    And then you are making sure the chosen product is in its' correct category, using: $selcat = $prod_info['categoryid'];

    Is this correct or not?

    Matt.

  7. #32
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    fetch_array() tells PHP "Go get a row from that result, and return it as a formatted array, where the field names = array keys"

    So fetch_array()'ing prod_info will mean $prod_info['name'] will hold the name of the product, etc etc, based on the field names in your mysql table.

    $selcat = $prod_info['categoryid'] doesnt do any correcting, all it does is say to PHP 'this is the category of the product i'm looking at'. I use $selcat because there are three scenarios here:

    Scenario 1: No category or product set on the URL. (So i just want a basic list of the categories)
    Scenario 2: A category set on the URL (So I can just read the category in from the URL)
    Scenario 3: A product set on the URL (So I need to read the category in from the product information)

    Then once i deal with reading the category in, I check to see if there is a category (isset($selcat)), and respond accordingly.

  8. #33
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still got the warning message. I was wondering if it was either
    - The quotations are in the wrong place (before the get request)
    - I have 2 tables within one database and I am only accessing the database. I think it is OK to call field names by connecting to the database and not the table names.
    - My database is stored on a separate server to my web site. I am connecting to the database at the top of the page at the other server but perhaps further down the page it has forgotten my connection??

    Other than this - I do not know. Any ideas,

    Matt.

  9. #34
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    If you echo mysql_error(); and get nothing out, then something has gone dramatically wrong.

    Which line is it that is throwing the error? (Compare line numbers) Because there are multiple fetch_array() calls in there.

  10. #35
    SitePoint Member
    Join Date
    Apr 2011
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP is a general-Purpose language development produce web pages. HTML Source and interpreted by a web server PHP processor module. It has evolved to include a command-line interface capability and can be used in standalone graphical applications.


    <snip/>
    Last edited by Mittineague; Apr 18, 2011 at 16:59. Reason: Please wait until you get your signature for your links.

  11. #36
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I started again and this time it is working; not sure what I did wrong last time.

    The only problem now is the 'remaining categories' when loaded in the browser have dollar signs "$" in front of them. How do I get rid of the "$" in front of the category names? Does this have something to do with .$key, as I'm not sure where this comes from.

    And my other query is:
    In the code we use the get function for: 'category' and 'product'
    However, when we write the code in this script we use only 'productid' with product.php page and only ".$key." with categoryview.php page.
    If the code asks for both towards the top of the script, why do we only enter one in our fields towards the bottom of the coding??

    Look forward to your comments.

    Matt.

  12. #37
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Following from the previous message I have found and deleted the extra '$' you added in the final line. (Deleted first "$" in "$".$cat."</a>") It was probably a typo you made in the final line.

    But I'm still confused about the use of the 'get' function. Why we ask for both and then use only one in each URL we create (see last/previous message).

    Matt.

  13. #38
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    Because you only need 1.
    Remember the scenarios.

    A user could:
    Just have clicked on your store (No Category or Product ID's yet.) [user is now on yourstorename.php, which is loading the navigation script]
    Clicked on a category, but hasnt chosen a product (No Product ID, but we have a Category) [user is now on categoryview.php, which is loading the navigation script]
    Clicked on a product. (The URL-based category is redundant at this point, because we have a categoryID attached to the product.) [user is now on product.php, which is loading the navigation script]

    Now. As to why 'we only use X with Y'... both product.php and categoryview.php would be including the same navigation.php (or whatever you called it) to generate the navigation window. Thus, we must account for both eventualities. Notice the structure, though; if a user on product.php decided for s***s and giggles to throw &category=1 on the end of the URL, the page wouldnt mess up - it would detect the category on the URL, store it, then immediately override it by noticing there's a productID on the URL, and use that information instead.

  14. #39
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am going to design an example so that I can see it working before I design it for a web site. I'll get back to you if I encounter some issues because at the moment I can only get the navigation working when both productid and category are passed through the URL.

    At the moment, I have something else I'd like to design but I am not sure it is possible with this navigation code we have here. There will be part of the left navigation bar that needs to change depending on the link clicked: To get the idea across here is an example: They start with...

    BMW
    Mercedes
    Ford
    Accessories

    User clicks 'accessories' and gets:

    Accessories
    BMW accessories
    Mercedes accessories
    Ford accessories
    BMW
    Mercedes
    Ford

    User clicks 'BMW accessories' and gets (category name changes from Accessories to BMW accessories and associated sub menu appears):

    BMW accessories
    Spare wheel
    New Indicator
    Rear View Mirror
    New Seat
    Radio
    Accelerator Pedal
    BMW
    Mercedes
    Ford

    Notice that the navigation categories change as do the sub menus as the user gets deeper into accessories. I think it would be a compromise too far to not include the individual accessories within the navigation bar and it needs breaking down into manufacturer so I see no way around this and I am trying to work out how the code we have can be modified in this small way? Is this likely to be possible?

    All the best,

    Matt.

  15. #40
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    It's not too hard to modify - you're simply adding another layer to the structure. Instead of Category->Product, you're now just making it Category->SubCategory->Product.

    Each subcategory belongs to a Category.

    There is now an additional scenario:
    User has chosen a subcategory, but not a product.

  16. #41
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  17. #42
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    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.

  18. #43
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  19. #44
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  20. #45
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    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.

  21. #46
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  22. #47
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    $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.

  23. #48
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And it would be possible to 'select' one field from a mySQL table and 'order' by another?

    Matt.

  24. #49
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    Certainly. You can order by and select any combination of fields that are in the tables your query references.

  25. #50
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •