Need Help with PHP

Discussion in 'PHP' started by Pixel T., May 8, 2009.

  1. #1
    Ok, so I started coding this script. Its going to be a learning experience for me but so far its not looking too good.

    I have created a membership script already so that part is done. But now I am stuck.

    How should I set up the rows in the database so that the user will be able to add a domain and then be able to display them in his profile?

    The problem I am having is if I create a row for domain 1, domain 2 ect, and someone has 50 domains, then it will not work.

    Right now I have 1 table (users). Should I create another one for domains?
     
    Pixel T., May 8, 2009 IP
  2. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Maybe serialize the data so you have 1 single colum for them.

    Example

    
    $domains = array("My Site" => "domain1.com", "His Site" => "domain2.com", "Her Site" => "domain3.com");
    
    $serialized_domains = serialize($domains);
    
    echo $serialized_domains;
    
    Code (markup):
    Would output
    a:3:{s:7:"My Site";s:11:"domain1.com";s:8:"His Site";s:11:"domain2.com";s:8:"Her Site";s:11:"domain3.com";}

    which can be inserted into a database, then upon retreival you could do
    $domains = unserialize($serializeddata); to turn it back into an array.

    Other for easier editing purposes you create a domains table , and each domain has a "uid" which points to the user's id

    So if you wanted to pull all their domains
    "SELECT * FROM domains WHERE uid = 3" to grab all the domains for user 3 (the 3 matching the ID from the main user's table.
     
    kblessinggr, May 8, 2009 IP
  3. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi,

    kblessinggr gave you two good examples, although if you're wanting to shorten the queries down -

    SELECT * FROM users LEFT JOIN domains ON domains.uid = users.id WHERE users.id = 3
    Code (markup):
    for example will fetch the the users information and the domains assigned to that user. (query may not be 100% accurate, it's late here and i've been drinking ;))

    Regards,

    Steve
     
    Steve136, May 8, 2009 IP
  4. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Steve... baby steps...

    but yes you're right :p
     
    kblessinggr, May 8, 2009 IP
  5. Pixel T.

    Pixel T. Well-Known Member

    Messages:
    1,205
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    170
    #5
    Still kind of confused. Can someone take me through step by step of what Im suppose to do? I can give you something in exchange if needed.

    Thanks,
     
    Pixel T., May 8, 2009 IP
  6. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #6

    The simple answer is : yes, create a separate table for domains, just make sure to provide a uid field in the domain table so that there's some point or reference to who owns the domain.

    Steve's query is an example of how to get the results back as a single query.
     
    kblessinggr, May 8, 2009 IP
  7. Aaron Sustar

    Aaron Sustar Peon

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I remember how it was for me when I was in your shoes, writing my first bits and pieces of code ... It's been probably 10 years now, but still ... I know you really need a clear explanation, because everything seems so damn confusing, right?

    I'll try my best to explain what you need to do.

    First you need a table for users, let's call it, hummm, "users". In our little example this table has got 3 fields - "uid", "username", "password". When you are setting those fields up, pay special attention to the field "uid". Under "Extra" choose "auto_increment" and select the radio button that says "Primary".

    This means your "uid" field is the primary key for table "users". Every user will have a different "uid" value and because of "auto_increment" setting this value will automatically get an appropriate value when you add a new user.

    If you run this query "INSERT INTO users (username, password) VALUES ('{$username}', '{$hashed_password}');", you will create a user with "uid" = 1. If you run this query again, you will get another user with "uid" = 2. And so on.

    Now you need to create a table "websites" with fields "wid", "user", "url". Again, set up "wid" as the primary key for the table "websites".

    Now, whenever a user adds a new website, you take that user's unique "uid" and add a website with a query like this "INSERT INTO websites (user, url) VALUES ($id_of_this_user, '$website_url');".

    This way every website in the table "websites" tells you exactly what user it belongs to, because the value in field "user" is actually the "uid" of the corresponding user.

    I hope this was clear enough ... If you have any questions, let us know, there are a lot of guys who are really willing to help here at DigitalPoint Forums. And if you complete all this without any problems and need to get data back from the database, let us know as well and we will go from there.

    Good luck! ;)
     
    Aaron Sustar, May 8, 2009 IP
  8. vaibhavcoder

    vaibhavcoder Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hi,

    I think making a separate table is good idea as you can then manage the data seprately.
    what you have to do is to just make a single table with following fields
    id
    uid
    domain_name
    any other if you need so

    and then at the time when you want to insert the record into the database just insurt uid also which you can get from user table.

    Noddy
     
    vaibhavcoder, May 8, 2009 IP
  9. Pixel T.

    Pixel T. Well-Known Member

    Messages:
    1,205
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    170
    #9
    Wow thanks Aaron for that VERY clear explanation. I think that i know how this is suppose to be set up. I'll keep you guys updated and again thanks
     
    Pixel T., May 9, 2009 IP
  10. vaibhavcoder

    vaibhavcoder Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I agree aron,

    I think this is the simplest way to give explanations about any thing.
    I am new here but I will try my level best to help some one. :)

    Noddy
     
    vaibhavcoder, May 12, 2009 IP
  11. Aaron Sustar

    Aaron Sustar Peon

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    No problem, Pixel T., this is what we are here for, right? ;)
     
    Aaron Sustar, May 12, 2009 IP
  12. Pixel T.

    Pixel T. Well-Known Member

    Messages:
    1,205
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    170
    #12
    Yup. I have most of the script working now, should have a beta script out soon. Will be available to public soon. :)
     
    Pixel T., May 13, 2009 IP
  13. vaibhavcoder

    vaibhavcoder Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Hi Pixel T

    I will be waiting for your beta version dude.

    Regards
    Noddy
     
    vaibhavcoder, May 14, 2009 IP