Jump to content

[SOLVED] Need help with displaying subcategories on a webpage using PHP


ycoleman

Recommended Posts

Hello.  I am creating an online store website and I need help with displaying information for the products for this store.  I am connected to the database  I am having trouble with getting subcategories from the database to display on my page called "SubCategory.php" when I click on each link from my "category.php" page. My category page is fine but it's just my subcategory page that I am having problems with.  :-\

 

This is the code from my "Category.php" page:

 

<?

  $sql = "SELECT * FROM tblCategory";

          $result = @mysql_query($sql,$db) or die(mysql_error());

 

while ($row = mysql_fetch_array($result)) {

$CatID = $row['CatID'];

$CategoryName = $row['CategoryName'];

echo "<a href='SubCategory.php?CatID=" . $CatID . "'>" . $CategoryName ."</a><br>";

}

?>

 

 

And this is the code for the "SubCategory.php" page:

 

<?

//assign the query string

$thisCatID = $_GET['CatID']

 

?>

 

 

<?

//get subcategories

$sql ="SELECT * FROM tblSubCategory";

$result = @mysql_query($sql,$db) or die(mysql_error());

$sql_2 ="SELECT * FROM tblSubCategory WHERE CatID = `".$thisCatID."`";

$result = @mysql_query($sql_2,$db) or die(mysql_error());

 

 

 

while ($row = mysql_fetch_array($result)) {

$CatID = $row['CatID'];

$SubCategoryName = $row['SubCategoryName'];

$thisCatID =$row['CatID'];

 

echo "<a href='SubCategory.php?CatID=" . $CatID . "'>" . $SubCategoryName ."</a><br>";

}

 

 

?>

 

Link to comment
Share on other sites

:)

your code here:

 

--->$result = @mysql_query($sql,$db) or die(mysql_error());

$sql_2 ="SELECT * FROM tblSubCategory WHERE CatID = `".$thisCatID."`";

--->$result = @mysql_query($sql_2,$db) or die(mysql_error());

 

--->while ($row = mysql_fetch_array($result)) {

-----------------------------------------------------------

--->youre fetching 2 $result... thats why youre having this problem..

 

 

Link to comment
Share on other sites

Ok I got rid of the 2 result but now I get an error that says "Query is empty"  from this code:

 

<?

//assign the query string

$thisCatID = $_GET['CatID']

 

?>

 

 

<?

//get subcategories

 

$result = @mysql_query($sql,$db) or die(mysql_error());

$sql ="SELECT * FROM tblSubCategory WHERE CatID = `".$thisCatID."`";

 

 

while ($row = mysql_fetch_array($result)) {

$CatID = $row['CatID'];

$SubCategoryName = $row['SubCategoryName'];

//$thisCatID =$row['CatID'];

 

//echo "<a href='SubCategory.php?CatID=" . $CatID . "'>" . $SubCategoryName ."</a><br>";

}

 

 

?>

Link to comment
Share on other sites

LOL. You can't run the query before you build it.

 

$result = @mysql_query($sql,$db) or die(mysql_error());

$sql ="SELECT * FROM tblSubCategory WHERE CatID = `".$thisCatID."`";

 

change to

 

$sql ="SELECT * FROM tblSubCategory WHERE CatID = `".$thisCatID."`";

$result = @mysql_query($sql,$db) or die(mysql_error());

 

Link to comment
Share on other sites

You also haven't assigned a value to variable "$db". it should hold I assume, the return value from a successful call to mysql_connect. Remove each "@" entry, you should not be suppressing errors while developing and after going live you should disable the display of errors, log and handle errors in a different way.

 

Change each <? entry to <?php. Short tags are not enabled on all servers. Also Put the following at the top of your script.

 

error_reporting(E_ALL);
ini_set('display_errors', '1');

 

You can check the manual for info on what each function does. If you're still having problems post all output and the code you're using in its current form.

 

Note that these types of questions should be posted in the PHP-Help forum and if you encounter additional PHP problems after this, post the questions there.

Link to comment
Share on other sites

Now I get an error message that says

 

"Notice: Undefined index: CatID in /home/ycoleman/public_html/Assignment4/SubCategory.php on line 107

Unknown column '' in 'where clause'"

 

Line 107 is this code on the bottom.  I was told to put this code

 

<?php

//assign the query string

$thisCatID = $_GET['CatID']

 

?>

 

but it gives me that error

???

Link to comment
Share on other sites

You should use isset() in the manner shown below before making the assignment

 

<?php
if (isset($_GET['catID']))
{
    $thisCatID = (int)$_GET['catID'];
}
else
{
    //here you do whatever should be done when a catID isn't present.
}
?>

 

Type Casting

 

Note that $_GET['catID'] will only exist if the url is similar to "http://www.com/page.php?catID=valuehere"

 

Php Security Guide

Link to comment
Share on other sites

As for the WHERE clause error, change the mysql_query calls to the following and post the errors again. Note that I've made a post above in the event you missed it.

 

$sql = 'query here';
$result = mysql_query($sql) or die($sql."<br />\n".mysql_error());

Link to comment
Share on other sites

For the "else" statement, I am not sure what to put between there.  Will it be echo $thisCatID =0; if query can't find catID?  ???

 

The else block should do whatever you think appropriate when the catID is not present.

 

<?php
if (isset(....))
{
    //look for subcategories
   SELECT * FROM table WHERE ...
    
    //if no subcategories are found display a message informing the user  
    if (!mysql_num_rows($result))
    {
        echo 'no subcategories found';
    }
    else
    {
        //display subcategories
    }
}
else
{
    /**
     * redirect to the list of categories from which the user can choose to view its subcategories.
     * You only need to redirect if the page displaying the categories is a separate script.
     *
     * You can alternatively echo a message stating that no categories have been found that match 
     * the selection even though none was made. What I'm trying to bring across is that you can do 
     * whatever you decide is best. You can even restructure the code if what you'd like to do requires it.
     */
}
?>

 

If you have any trouble, post what you've tried and explain what you're attempting to do.

Link to comment
Share on other sites

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.