Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 01-31-2012, 07:49 PM Re: Category Display...
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
Hi all,

I am trying this all from scratch again...Talk about persistency...Well that's what I am all about.

Ok, Here's where I am struggling again...I have a categories.php page and a subcategories.php page. The categories page has a very simple query on it to display the categories, and it works just fine.. The subcategories page on the other hand does not work fine. What I want the subcategories.php to display is the subcategories for the id associated with the categories. Here's what my database looks like:

catiid type int(11) auto_increment (primary key)
parentid type int(11)
name type (varchar(255)
description type text
cat_image_url type texr

Note: I have included both categories and subcategories.php files in a zip attachement as well a a screenshot expalining how the link to the subcategories should work.

Thank you in advance!
-Brian
Attached Files
File Type: zip categories_1328057264283.zip (596 Bytes, 8 views)
File Type: zip subcategories_1328057304598.zip (449 Bytes, 4 views)
File Type: zip ss_1328057862544.zip (22.3 KB, 7 views)
__________________
Made2Own

Please login or register to view this content. Registration is FREE

Last edited by Brian07002; 01-31-2012 at 08:23 PM..
Brian07002 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-31-2012, 09:44 PM Re: Category Display...
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
~ bump ~
__________________
Made2Own

Please login or register to view this content. Registration is FREE

Last edited by Brian07002; 02-01-2012 at 06:53 PM..
Brian07002 is offline
Reply With Quote
View Public Profile
 
Old 02-02-2012, 06:39 AM Re: Category Display...
miki86's Avatar
Extreme Talker

Posts: 185
Location: print_r($serbia);
Trades: 0
Maybe a Switch statement for subcategories?
PHP Code:
switch(categoryID) {
    case 
1//display this subcategories
    
break;
    case 
2//display this subcategories
    
break;
    
//...

I didn't read what you wrote on previous pages, but if i understood, this is one way to do it.

Last edited by miki86; 02-02-2012 at 06:41 AM..
miki86 is online now
Reply With Quote
View Public Profile
 
Old 02-02-2012, 09:19 AM Re: Category Display...
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
Quote:
Originally Posted by miki86 View Post
Maybe a Switch statement for subcategories?
PHP Code:
switch(categoryID) {
    case 
1//display this subcategories
    
break;
    case 
2//display this subcategories
    
break;
    
//...

I didn't read what you wrote on previous pages, but if i understood, this is one way to do it.
Hey miki,

I haven't tried the switch statement before, but after looking at some examples I don't think that is what I should be using in this case because of the large amount of subcategories.

Thanks for that option though
-Brian
__________________
Made2Own

Please login or register to view this content. Registration is FREE
Brian07002 is offline
Reply With Quote
View Public Profile
 
Old 02-02-2012, 10:18 AM Re: Category Display...
miki86's Avatar
Extreme Talker

Posts: 185
Location: print_r($serbia);
Trades: 0
You have relative ID's stored in your db as i understand.
Use that ID to load different subcategories.

Example:

categories.php
PHP Code:
// You can do a loop here if you have to many categories
<a href="subcategories.php?catid=1">Category 1</a>
<
a href="subcategories.php?catid=2">Category 2</a>
<
a href="subcategories.php?catid=3">Category 3</a>
<
a href="subcategories.php?catid=4">Category 4</a
subcategories.php
PHP Code:
if(isset($_GET['catid']) && ($_GET['catid'] != NULL)) $categoryID $_GET['catid'];
else 
$categoryID 1;
$query "SELECT * FROM subcategories WHERE parentID = {$categoryID}";
$result mysql_result($query);

while(
$row mysql_fetch_assoc($result)) {
    echo 
$row[];

Something like that should work.

EDIT:
btw im looking at your files and i noticed this in your subcategories.php:
PHP Code:
$sub_cats $_GET['parentid'];

$query "SELECT * FROM categories WHERE parentid != 0"
You are passing a parentid value, but you are not using it in your sql

Your query should look like this:
PHP Code:
$sub_cats mysql_real_escape_string($_GET['parentid']);

$query "SELECT * FROM categories WHERE parentid = {$sub_cats}"

Last edited by miki86; 02-02-2012 at 10:25 AM..
miki86 is online now
Reply With Quote
View Public Profile
 
Old 02-02-2012, 10:29 PM Re: Category Display...
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
Quote:
Originally Posted by miki86 View Post
You have relative ID's stored in your db as i understand.
Use that ID to load different subcategories.

Example:

categories.php
PHP Code:
// You can do a loop here if you have to many categories
<a href="subcategories.php?catid=1">Category 1</a>
<
a href="subcategories.php?catid=2">Category 2</a>
<
a href="subcategories.php?catid=3">Category 3</a>
<
a href="subcategories.php?catid=4">Category 4</a
subcategories.php
PHP Code:
if(isset($_GET['catid']) && ($_GET['catid'] != NULL)) $categoryID $_GET['catid'];
else 
$categoryID 1;
$query "SELECT * FROM subcategories WHERE parentID = {$categoryID}";
$result mysql_result($query);

while(
$row mysql_fetch_assoc($result)) {
    echo 
$row[];

Something like that should work.

EDIT:
btw im looking at your files and i noticed this in your subcategories.php:
PHP Code:
$sub_cats $_GET['parentid'];

$query "SELECT * FROM categories WHERE parentid != 0"
You are passing a parentid value, but you are not using it in your sql

Your query should look like this:
PHP Code:
$sub_cats mysql_real_escape_string($_GET['parentid']);

$query "SELECT * FROM categories WHERE parentid = {$sub_cats}"
Hi Miki,

I've tried to use your code posted above, and I am getting this error:

Code:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /var/www/subcategories.php on line 17
Line 17:
PHP Code:
while($row=mysql_fetch_assoc($result)) { 
I do have the closing curly brace btw. I just posted the line with the error, and I did put 'name' in the $row[]; part in the while loop, assuming that will give me the names of the subcategories for each of the categories in the database. Yes, I also do have subcategories in the subcategories table.

It might be better if you could offer an sql file that I can import to get the database structure you offering me. Otherwise, I am having a total brain freeze trying to figure this out...I am pretty good with navigation, but until I have the coding part understood, I am a total n00b. Give me credit, I am still trying though.

Thanks again for the help in advance!
-Brian
__________________
Made2Own

Please login or register to view this content. Registration is FREE

Last edited by Brian07002; 02-02-2012 at 10:44 PM..
Brian07002 is offline
Reply With Quote
View Public Profile
 
Old 02-03-2012, 10:12 AM Re: Category Display...
Skilled Talker

Posts: 50
Trades: 0
This should work.

I've done no checking to see if the id is passed across.

You'll need to add you connection file again.

Categories
Code:
<html>
<head><title>Reunite My Site - Category Navigation Page</title>
<style type="text/css">

#main_content {

background-color: #ccc;
border: 1px solid #000;
color: #000;
padding: 5px;
}
</style>

<?php 

// Turn on error reporting
error_reporting(E_ALL);
ini_set('display_errors','on');

/* Connect String */
$dbHost = "localhost";
    $dbUser = "root";
    $dbPass = "root";
    
    $con = mysql_connect($dbHost,$dbUser,$dbPass);
    if (!$con)
        {
        die('could not connect: ' .mysql_error());
        }
        
    mysql_select_db("TESTDB", $con);

/* Output Main Categories */

$query = "SELECT * from categories where parentid = 0";
$result = mysql_query($query);

echo " <div id=\"main_content\" ";

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

echo "<p>";
echo "<a href=\"subcategories.php?id=$row[id]\">$row[name]</a>";
echo "</p>";
}
echo "</div>";


?>
Sub cat
Code:
<H3> Subcategories Page: </H3>

<?php 

// Turn on error reporting
error_reporting(E_ALL & ~E_NOTICE);
ini_set('display_errors','on');

$dbHost = "localhost";
    $dbUser = "root";
    $dbPass = "root";
    
    $con = mysql_connect($dbHost,$dbUser,$dbPass);
    if (!$con)
        {
        die('could not connect: ' .mysql_error());
        }
        
    mysql_select_db("TESTDB", $con);

$sub_cats = $_GET['id'];

$query = "SELECT * FROM categories WHERE parentid = $sub_cats";
$result = mysql_query($query);

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

echo "<p>";

echo $row['name'];
echo "</p>";

}
?>
HullBorn is offline
Reply With Quote
View Public Profile
 
Old 02-03-2012, 05:00 PM Re: Category Display...
miki86's Avatar
Extreme Talker

Posts: 185
Location: print_r($serbia);
Trades: 0
Quote:
I am getting this error:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /var/www/subcategories.php on line 17
That means that your query is not ok.
Try this and see what it comes out:
PHP Code:
$query "SELECT * FROM categories WHERE parentid = {$sub_cats}";
$result mysql_query($query);
if(!
$result) {echo mysql_error(); die();} 
Quote:
It might be better if you could offer an sql file that I can import to get the database structure you offering me.
I can't give you any sql file but i can explain you how it works.
You have a db table with the names of parent categories, each of them has a unique ID, you also have a db table with subcategories which looks like this:
Code:
ID---parentid---subcategory_name
1----1----------Subcategory 1
2----1----------Subcategory 2
3----2----------Subcategory 3
4----3----------Subcategory 4
5----3----------Subcategory 5
So basically parentid in subcategory table is the ID of the category table.
You just need to select all rows with the proper ID.
miki86 is online now
Reply With Quote
View Public Profile
 
Old 02-03-2012, 10:34 PM Re: Category Display...
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
OK, I just got in from work not too long ago..Had to work a double today so I am quite tired, so I will try tonight and if all fails, I will double / triple check it tomorrow. Thank you both!

-brian
__________________
Made2Own

Please login or register to view this content. Registration is FREE
Brian07002 is offline
Reply With Quote
View Public Profile
 
Old 02-04-2012, 09:37 AM Re: Category Display...
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
Quote:
Originally Posted by HullBorn View Post
This should work.

I've done no checking to see if the id is passed across.

You'll need to add you connection file again.

Categories
Code:
<html>
<head><title>Reunite My Site - Category Navigation Page</title>
<style type="text/css">

#main_content {

background-color: #ccc;
border: 1px solid #000;
color: #000;
padding: 5px;
}
</style>

<?php 

// Turn on error reporting
error_reporting(E_ALL);
ini_set('display_errors','on');

/* Connect String */
$dbHost = "localhost";
    $dbUser = "root";
    $dbPass = "root";
    
    $con = mysql_connect($dbHost,$dbUser,$dbPass);
    if (!$con)
        {
        die('could not connect: ' .mysql_error());
        }
        
    mysql_select_db("TESTDB", $con);

/* Output Main Categories */

$query = "SELECT * from categories where parentid = 0";
$result = mysql_query($query);

echo " <div id=\"main_content\" ";

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

echo "<p>";
echo "<a href=\"subcategories.php?id=$row[id]\">$row[name]</a>";
echo "</p>";
}
echo "</div>";


?>
Sub cat
Code:
<H3> Subcategories Page: </H3>

<?php 

// Turn on error reporting
error_reporting(E_ALL & ~E_NOTICE);
ini_set('display_errors','on');

$dbHost = "localhost";
    $dbUser = "root";
    $dbPass = "root";
    
    $con = mysql_connect($dbHost,$dbUser,$dbPass);
    if (!$con)
        {
        die('could not connect: ' .mysql_error());
        }
        
    mysql_select_db("TESTDB", $con);

$sub_cats = $_GET['id'];

$query = "SELECT * FROM categories WHERE parentid = $sub_cats";
$result = mysql_query($query);

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

echo "<p>";

echo $row['name'];
echo "</p>";

}
?>
Thank you Hullborn.

That was *exactly* what I was trying to accomplish! You have done a super job! I appreciate your work. It's always the small things that make a big difference!

:: Edit ::

Miki86, I appreciate your effort as well, only I am not the best when it comes to finding syntax mistakes, so even though the code you posted may have been on the right track, I could not figure it out...Seems like I will be studying up more on my syntax errors.

Thanks again Hullborn!
-Brian
__________________
Made2Own

Please login or register to view this content. Registration is FREE

Last edited by Brian07002; 02-04-2012 at 09:40 AM..
Brian07002 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Category Display...

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.49076 seconds with 12 queries