ycoleman Posted March 12, 2007 Share Posted March 12, 2007 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/42336-solved-need-help-with-displaying-subcategories-on-a-webpage-using-php/ Share on other sites More sharing options...
DanDaBeginner Posted March 12, 2007 Share Posted March 12, 2007 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.. Quote Link to comment https://forums.phpfreaks.com/topic/42336-solved-need-help-with-displaying-subcategories-on-a-webpage-using-php/#findComment-205464 Share on other sites More sharing options...
ycoleman Posted March 13, 2007 Author Share Posted March 13, 2007 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/42336-solved-need-help-with-displaying-subcategories-on-a-webpage-using-php/#findComment-206119 Share on other sites More sharing options...
artacus Posted March 13, 2007 Share Posted March 13, 2007 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()); Quote Link to comment https://forums.phpfreaks.com/topic/42336-solved-need-help-with-displaying-subcategories-on-a-webpage-using-php/#findComment-206545 Share on other sites More sharing options...
shoz Posted March 13, 2007 Share Posted March 13, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/42336-solved-need-help-with-displaying-subcategories-on-a-webpage-using-php/#findComment-206594 Share on other sites More sharing options...
ycoleman Posted March 14, 2007 Author Share Posted March 14, 2007 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 ??? Quote Link to comment https://forums.phpfreaks.com/topic/42336-solved-need-help-with-displaying-subcategories-on-a-webpage-using-php/#findComment-206705 Share on other sites More sharing options...
shoz Posted March 14, 2007 Share Posted March 14, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/42336-solved-need-help-with-displaying-subcategories-on-a-webpage-using-php/#findComment-206717 Share on other sites More sharing options...
shoz Posted March 14, 2007 Share Posted March 14, 2007 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()); Quote Link to comment https://forums.phpfreaks.com/topic/42336-solved-need-help-with-displaying-subcategories-on-a-webpage-using-php/#findComment-206723 Share on other sites More sharing options...
ycoleman Posted March 14, 2007 Author Share Posted March 14, 2007 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? ??? Quote Link to comment https://forums.phpfreaks.com/topic/42336-solved-need-help-with-displaying-subcategories-on-a-webpage-using-php/#findComment-207522 Share on other sites More sharing options...
shoz Posted March 14, 2007 Share Posted March 14, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/42336-solved-need-help-with-displaying-subcategories-on-a-webpage-using-php/#findComment-207577 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.