Dynamic lists

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
brassfid
Forum Newbie
Posts: 18
Joined: Thu Jan 13, 2011 11:09 pm

Dynamic lists

Post by brassfid »

Alright, this is basic, I do not want you to code this I just want to know if php/mysql able to do this, and possibly a tutorial that you know of.

I know have an html form that a user can put information in and it will push to my mysql databse (I am very excited about this.)

the next process is a dynamic web page that pulls from the database. I want lists to be created as users insert information and it is approved. There are two lists.

1. is parent, this will be alphabetical
2.is child, this will be alphabetical below the parent.
4. When a new parent is added it will be inserted.
3. when someone adds a new child it will be dynamically inserted into an already made parent

ex.

> Loyola
>Chicago
>New Orleans
>Los Anveles
> Tulane
>New Orleans
> Princeton
>Princeton New Jeresey

Thanks!
mehran
Forum Newbie
Posts: 17
Joined: Fri Jan 21, 2011 1:31 am

Re: Dynamic lists

Post by mehran »

first of all use the order by in ur select query to retriew data from database,which help you to sort the data automatically,
like
$result = mysql_query("SELECT * FROM example ORDER BY age") ;
write the name of the column according to which you want sorting data,

and it easiy way to retriew the parant and child both in the same table
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Dynamic lists

Post by califdon »

To establish a parent-child relationship, you must use 2 tables, one for the parent records, one for the child records. This is called a one-to-many relationship. Each table must have a unique primary key, and the child table must have a foreign key that indicates which record in the parent table it belongs to. This is standard relational database concept. If you want to organize data like this:

Code: Select all

Birds
  -Finches
  -Parrots

Cats
  -Persian
  -Siamese

Dogs
  -Hounds
  -Spaniels
  -Terriers
and allow users to add new parents and new child records for a parent, and still display them in alphabetical order, you must store them in tables like this:

Code: Select all

tblParents:
  ParentID INT Auto-increment (PK)
  Name     VARCHAR(20)

tblChildren:
  ChildID  INT Auto-increment (PK)
  ParentID INT (FK)
  Name     VARCHAR(20)
so your data might look like this:

Code: Select all

tblParents:
+---+-------+
| 1 | Birds |
+---+-------+
| 2 | Dogs  |
+---+-------+
| 3 | Cats  |
+---+-------+

tblChildren:
+---+---+----------+
| 1 | 2 | Spaniels |
+---+---+----------+
| 2 | 1 | Finches  |
+---+---+----------+
| 3 | 1 | Parrots  |
+---+---+----------+
| 4 | 3 | Siamese  |
+---+---+----------+
| 5 | 2 | Hounds   |
+---+---+----------+
| 6 | 3 | Persian  |
+---+---+----------+
| 7 | 2 | Terriers |
+---+---+----------+
Now you can write queries to do display your data any way you want.
mehran
Forum Newbie
Posts: 17
Joined: Fri Jan 21, 2011 1:31 am

Re: Dynamic lists

Post by mehran »

plz explain the forigen key concept,
i cnnt understand the 2nd table u made
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Dynamic lists

Post by califdon »

mehran wrote:plz explain the forigen key concept,
i cnnt understand the 2nd table u made
This is the foundation of relational databases. I have written a 7-page tutorial on Principles of Relational Databases that is the first sticky post at http://forums.aspfree.com/microsoft-access-help-18/. I suggest you read that.
mehran
Forum Newbie
Posts: 17
Joined: Fri Jan 21, 2011 1:31 am

Re: Dynamic lists

Post by mehran »

tnx califdon ,
i will follow that,.
once again thanks and take care
brassfid
Forum Newbie
Posts: 18
Joined: Thu Jan 13, 2011 11:09 pm

Re: Dynamic lists

Post by brassfid »

OK! This gets me on a good start. I will stumble my way through this process and probably post my coding later once I figure it out.

Thanks again everyone.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Dynamic lists

Post by califdon »

brassfid wrote:OK! This gets me on a good start. I will stumble my way through this process and probably post my coding later once I figure it out.

Thanks again everyone.
I hope that you will find your efforts interesting, as well as useful. Even before you write the very first line of code, you should design your data "schema" (that is, what tables you will need and what fields will be in each table), all of which is critical to the success of anything you build with it, and none of which is intuitive--it is determined by rather strict rules, including the "normalization" of data into appropriate tables. As you have questions, we invite you to return and post them here.
Post Reply