PHP Noob needs assistance

Discussion in 'PHP' started by thewindmaster, Jun 9, 2006.

  1. #1
    I know almot nothing about PHP but I am making a stab at this. I have a PHP/MySQL script than I am editing and integrating into a site. I have done a decent job so far or formatting the output.

    However, I have come to a point where I need to estblish/call a variable from the SQL database to make the format look right and I do not know what to do.

    The script is an inventory script. DB consists of products that have ID, category, description etc.

    To display all products in catagory I have (index.php?cat=2) this shows all product in category 2 etc. I wanted some variable html based on category and did the following which works.

    <? if (($cat >= 1) && ($cat <= 13)) {
    include("../menus/menu1.php");
    }else if (($cat >= 21) && ($cat <= 23)) {
    include("../menus/menu2.php");
    }else if (($cat >= 15) && ($cat <= 17)) {
    include("../menus/menu3.php");
    }else if (($cat >= 18) && ($cat <= 20)) {
    include("../menus/menu4.php");
    }else if ($cat == 24) {
    include("../menus/menu5.php");
    } ?>


    The problem comes in that I also have a product.php to display the actually product. It uses (product.php?id=3) to display product 3 etc. I want to display the same variable html base on the category but the $cat variable does not get established on the product.php page. I need to know what code I can use to establish the $cat variable based on the product id on the product.php page.

    The current PHP code on the product page is:

    mysql_query("UPDATE product SET viewed=viewed+1 WHERE id=$id");
    $qr1 = mysql_query("SELECT * FROM product WHERE id=$id");
    $prod = mysql_fetch_object($qr1);


    Thanks in advance for any assistance.
     
    thewindmaster, Jun 9, 2006 IP
  2. TheHoff

    TheHoff Peon

    Messages:
    1,530
    Likes Received:
    130
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Each product belongs in a category so the product table certainly has the necessary category ID stored in it. Your job is to extract that field from the table and use it as the $cat variable.

    Right here, the code loads in the product data:

    $qr1 = mysql_query("SELECT * FROM product WHERE id=$id");
    $prod = mysql_fetch_object($qr1);

    So now you have an array named $prod that contains all of the data about that product. Inside of that array there will be a category id. It could be called

    $prod['cat'] or $prod['category'] or $prod['categoryid'] or $prod['catid']

    depending on the developer's adherence to naming conventions. Once you find out what that is called, you can set the $cat variable to equal the same thing... essentially finding out what category the product is in.

    $cat = $prod['categoryid'];
     
    TheHoff, Jun 9, 2006 IP
  3. thewindmaster

    thewindmaster The Man with the Plan

    Messages:
    845
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks Hoff. That actually did not work but It got me thinking in the right direction. I used this:

    $cat = stripslashes($prod->cat);

    and it worked.
     
    thewindmaster, Jun 9, 2006 IP
  4. TheHoff

    TheHoff Peon

    Messages:
    1,530
    Likes Received:
    130
    Best Answers:
    0
    Trophy Points:
    0
    #4
    There ya go, glad it got you pointed the right way.
     
    TheHoff, Jun 9, 2006 IP