# PHP Category Script



## chimwando (Feb 24, 2011)

Im trying to create a site using PHP that will containing a category page, each company that registers will be given an option to select the category they belong. they will be a category page and I want when the company selects a category when registering that company will be placed to that category so that when a user select a category he/she is given the companies that registered under that category. and will be able to view the services that company is offering..this also means creating a data base that will be able to handle such.
Am not very good with PHP but am learning.
please help if you can.
THEY SAY WHATS THE POINT OF HAVING KNOWLEDGE WHEN YOU CANT SHARE IT?


----------



## Mr.LLB (May 27, 2008)

You have to be very specific in terms of what you need help with.


----------



## ehymel (Aug 12, 2007)

chimwando,

What is your question? What you have described is a very plain jane straightforward database-driven php website. No one here will just write it for you. In this forum, you write what you can and if you run into a problem there are lots of very helpful people to lend a hand.


----------



## chimwando (Feb 24, 2011)

its like this there is a registration page that has a catigory feild,when a user selects a category, that user is linked to that category in the category table in the data base(this category table has 2 feilds"categoryID,& category" the category field will have the categories the user will be selecting from).then there is a categories page, the page that will be displaying the categories in the data base,and when you click on a category it will view the pippo registered under that category.
so am stack on the categories page how to link the categories to the categories in the DB,and when you click on the user that registered under that category it should bring you info about the user......


----------



## ehymel (Aug 12, 2007)

Still not sure what your question is


----------



## chimwando (Feb 24, 2011)

hai Ehymel

what i have is this....

1) a Data Base "cat_test" with the following tables 


  category
  users

the *category table* has the following feilds:
I)cat_id,int,auto increment
II)uid,jnt
III)category,text info in this category (automobile,Employment,.....) these are the options(categories) a user will be given to select from.

*Users Table*

I)uid,auto increment
II)username,text
III)password,text
IV)email,vchr
..........................................................................................

so there is also a registration form where users wher they select the catigory they want:

*Register*

Username: Password: Confirm Password: Email: Category: Select A Category AutomobileComputers & InternetEmployment Login Here
php code:

(register.php)


<?php
// set your infomation.
$dbhost='localhost';
$dbusername='root';
$dbuserpass='carenn';
$dbname='cat_test';
// connect to the mysql database server.
mysql_connect ($dbhost, $dbusername, $dbuserpass);
mysql_select_db($dbname) or die("Cannot select database");

//Are they just getting here or submitting their info?
if (isset($_POST["username"])) {
$username = $_POST["username"];
$password = $_POST["password"];
$cpassword = $_POST["cpassword"];
$email = $_POST["email"];
//Was a field left blank?
if($username==NULL|$password==NULL|$cpassword==NULL|$email==NULL) {
echo "A field was left blank.";
}else{
//Do the passwords match?
if($password!=$cpassword) {
echo "Passwords do not match";
}else{
//Has the username or email been used?
$checkuser = mysql_query("SELECT username FROM users WHERE username='$username'");
$username_exist = mysql_num_rows($checkuser);

$checkemail = mysql_query("SELECT email FROM users WHERE email='$email'");
$email_exist = mysql_num_rows($checkemail);

if ($email_exist) {
echo "The username or email is already in use";
}else{
//Everything seems good, lets insert.
$query = "INSERT INTO users (username, password, email) VALUES('$username','$password','$email')";
mysql_query($query) or die(mysql_error());
echo "The user $username has been successfully registered.";
}
}
}
}
?>

*Register*



Username:  
Password:  
Confirm Password:  
Email:  Category:

Select A Category
<?PHP 
$catquery3 = mysql_query("SELECT * FROM category");
while($row = mysql_fetch_array($catquery3, MYSQL_ASSOC))
{
echo "".$row['category']."";
}
?>
 
 


.......................................................................................

so as soon as a user registers and log's in they are directed to the categorylist.php page. the use of this page is to display the list of categories in the Database, and when a user clicks on the category the user will be shown or directed to a page that will have a list of name's(users) that regitered under that category. the following is the code for the categorylist.php page:

php code:

(categorylist.php)

<?PHP

$dbhost='localhost';
$dbusername='root';
$dbuserpass='carenn';
$dbname='cat_test';

// connect to the mysql database server.
mysql_connect ($dbhost, $dbusername, $dbuserpass);
mysql_select_db($dbname) or die("Cannot select database");

$query = "SELECT * FROM settings WHERE id = '1' limit 1";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))

$catquery = mysql_query("SELECT * FROM category WHERE uid = '0' ORDER BY category ASC");
$catrows = mysql_num_rows($catquery);

$catquery2 = mysql_query("SELECT * FROM category WHERE cat_id <= '".$total."' && uid = '0' ORDER BY cat_id ASC");
echo "

";
while($row = mysql_fetch_array($catquery2, MYSQL_ASSOC))
{

echo "
".$row['category']."";
}
echo "

[/TD][TD]";

$catquery3 = mysql_query("SELECT * FROM category WHERE cat_id > '".$total."' && uid = '0' ORDER BY cat_id ASC");
echo "

";
while($row = mysql_fetch_array($catquery3, MYSQL_ASSOC))
{

echo "
".$row['category']."";
}
echo "

[/TD]";

?>
[/TD]
[TD]<?PHP echo $block3; ?>[/TD]
[/TR]

.................................................................................................................

at the moment it can only display the categories in the Database(cat_test) table (category) feild(category) but when i try to click on the link for the category it does not give me the name's registered under that category.
the following is the error i receive:

*Not Found*

The requested URL /cat_test/category-1.php was not found on this server.
..................................................................................................................

Help Pliz.....................................​


----------



## -Fabez- (Jul 28, 2008)

How are you deciding which page to goto when the user clicks a link, a set page with the category being passed as a POST or a seperate page for each category ? If the file does not exist try renaming the intended target file to the requested name or change the link target.


----------



## chimwando (Feb 24, 2011)

its a set page with the category being passed as a POST......


----------



## -Fabez- (Jul 28, 2008)

Are you sure you've called the target page the correct name then ?


----------



## ehymel (Aug 12, 2007)

chimwando:

Several comments.
1. Your register.php contains a huge security risk to your back-end database in that you are passing unfiltered $_POST data straight to your database. I know this is just the test stage so it's fine here, but don't do this on your final site. Your database could be very easily exposed or deleted in its entirety.

2. Your categorylist.php hard-codes the uid's of the user (among other data), so the query to the db will always return the same thing regardless of who logs in. Perhaps you just done this for testing purposes, but you'll have to get the actual uid of the logged-in user for this to work.

3. Fabez has an good point, at least in what you have posted here you don't show us how you are processing the $_POST request that you mentioned.

4. Your error message references a page that is not shown as a link or form action in anything you posted here. 

The last 2 points (#3 and #4) tells me you've not shown us all of your relevant code, so it's impossilble to help pinpoint your problem.


----------



## chimwando (Feb 24, 2011)

thanks guys am slowly getting ther. what i have done is the i have created a page for each category and named them as (category1,category2,...............) and the user clicks on the category link it goes to the specific category page(using the cat_id), but am having a problem displaying the users that registered in that category on a specific category page?how do you sugest i get that info from the database, and making sure that,its the info in that category. i would like it to view information like the username, email, & category....
wats bothring me is that info is comming from two diffrent tables.............and am kinda new to php but am willing to learn. you can try me.


----------



## -Fabez- (Jul 28, 2008)

Have you tried using a SELECT, FROM, WHERE SQL statement ? If the data is in two tables, what is stored where and are they linked using primary & foreign keys ?


----------



## chimwando (Feb 24, 2011)

user name & email is in the "users" table while the categories are in the "category" table. the relationship is between "uid" feild in the "users" table and the "uid" feild in the "category" table but i keept on geting an error every time i run the register page(every time a user tried to register), the error is as follows: 

Cannot add or update a child row: a foreign key constraint fails (`cat_test/users`, CONSTRAINT `users_ibfk_1` FOREIGN KEY (`uid`) REFERENCES `category` (`uid`))

the following "users" table shows the relationship between the tables:

*uid* -- category->cat_id category->uid users->uid users->cat_id ON DELETE -- CASCADE SET NULL NO ACTION RESTRICT ON UPDATE -- CASCADE SET NULL NO ACTION RESTRICT *username* No index defined! *password* No index defined! *email* No index defined! *cat_id*  -- category->cat_id category->uid users->uid users->cat_id ON DELETE -- CASCADE SET NULL NO ACTION RESTRICT ON UPDATE

the following "category" table shows the relationship between the tables:

*cat_id* -- category->cat_id category->uid users->uid users->cat_id ON DELETE -- CASCADE SET NULL NO ACTION RESTRICT ON UPDATE -- CASCADE SET NULL NO ACTION RESTRICT *uid* -- category->cat_id category->uid users->uid users->cat_id ON DELETE -- CASCADE SET NULL NO ACTION RESTRICT ON UPDATE -- CASCADE SET NULL NO ACTION RESTRICT *category* No index defined! 


do you have any suggestions, or a better way of going about it? if it means changing the DB i would, trust me.


----------



## ehymel (Aug 12, 2007)

chimwando said:


> the following "users" table shows the relationship between the tables:
> 
> *uid* -- category->cat_id category->uid users->uid users->cat_id ON DELETE -- CASCADE SET NULL NO ACTION RESTRICT ON UPDATE -- CASCADE SET NULL NO ACTION RESTRICT *username* No index defined! *password* No index defined! *email* No index defined! *cat_id* -- category->cat_id category->uid users->uid users->cat_id ON DELETE -- CASCADE SET NULL NO ACTION RESTRICT ON UPDATE
> 
> ...


I'm unfamiliar with setting relationships like this in MySQL, and personally don't think they are actually needed, especially for something as simple as what you are doing. I've been using MySQL for years with many, many databases deployed across many, many websites. I've never needed to set up somehting like this, and frankly I wouldn't know how to do it.

I' *guessing* that your error has something to do with this. I suggest simplifying things by getting rid of these relationships and defining everything through your SQL commands.

You also haven't addressed any of the concerns I mentioned in an earlier post. Without some answers to those items, I won't be able to help further. Best of luck!


----------



## chimwando (Feb 24, 2011)

oh k ill try doing everything without the relationship...........i get your point tho its not that easy for me
but i know am learning. cool thanks alot man


----------



## chimwando (Feb 24, 2011)

Hai Ehymel, do you have any info about about a "join" sql command and how it works?
even a book


----------



## ehymel (Aug 12, 2007)

This tutorial is informative... http://www.tizag.com/mysqlTutorial/mysqljoins.php


----------



## -Fabez- (Jul 28, 2008)

After taking a look at the information you have provided, it appears that your tables and relationships are not in third normal form, which could be a contributing factor to your problem. If you were to recreate your tables in third normal form it would help to drive down data redundancy and make it easier to work with. Also, have you declared any constraints on your database, either explicitly through a MySQL command or implicitly through setting options such as enforcing referential integrity or cascading updates and/or deletes ?


----------



## chimwando (Feb 24, 2011)

thanks for the tutorial..Febez thanks i ill try that and will update you on the out come.and no there no constraints on my database


----------



## juliensoucy (Feb 20, 2011)

Are you looking for listing all the items in a certain category (like a search feature) OR your looking to 
add information in your sql database with the data the user filled in?

Tell me cleary what you want and i will post you the code


----------



## chimwando (Feb 24, 2011)

Hi Juliensoucy
what i whant is ok let me just tell you my idea
i whant to create a site where a user will be to register (they be two types of users "reguler" & "company administrators") !!!!!!!!! this i already have !!!!!!!!!!!!!!!
so under the company admin account "user" he/she will be given the privalages to create a company

!!!!!!!!!DO not have the following!!!!!!!!!!!

so meaning i need a table (co_info) that will hold info about the companies, the following are the fields: co_name, co_webaddress,phy_address, email,phone#, cell#, fax, ........thats about it

will also need another table containing the categories those companies belong to.

category_info (table) the following are the 
fields: cat_id, category

& last table will be the services/products (table) that will hold info about the services/products the company has.
the following are the feilds:
product_id, product.

...........................................................

so first i dont know if i have to join those table's, do i?
coz each company will have info comming from those table's.

if you have any sugestions on how they tables should be or the table's you would want you can just tell me then i can make them.

SO ALL THIS IS ON THE DATA BASE SIDE
................................................................

then we go to the web page side

on this web page all i want is that it will give me a list of categories in the category_info (table) and when you click on any of the category listed it should go to another page that contains the registerd companies under that category, & when you click on any of the companies it will view the conpany info with a link to view the services/products that you can get from that company.

.................................................................................................

i hope you get the point of this system am trying to create.

do you have a solutions for me?

i really appreciate..... thanks alot

wer i come from we dont have anything like this and i really think its a once in a life time opotunity & unftnatly am just learning php(only this part of the system is the one thats giving problem's)


----------

