SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot JagXJ220's Avatar
    Join Date
    Mar 2001
    Location
    Not worth the drive
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm developing a feature on my site where registered users can login and review cars. I only want the users to be able to review a car once, so I want the car name to be added to an array which will then be stored in that user's profile in my mysql database. I'm wondering how I'd go about storing an array in my database, so that I can finally launch my new user registration features.

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wouold say you would want to serialize the array before storing it. Then you can retrieve it, unserialize it, add to it, and then reserialize it for putting back into the database. This should give you a better understanding of what serialize does. Run this script on your server and watch what it does. Using that you should be able to pull it off, please ask again if this doesnot work for you.

    PHP Code:
    <?
    $cars 
    = array("Lambo","Ferrari","Lotus","Pinto");
    $cars serialize($cars);
    print 
    "This is serialized - $cars<br><br>";
    $cars unserialize($cars);
    print 
    "This is unserialized - $cars<br><br>";
    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2001
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well actually a designed database table is basically an array. This is true for any database platform including MySQL, Oracle, MS SQL Server, etc.

    For your specific problem I would probably create a 2 column table with the UserID and CarID as the 2 columns. When you go to save the array of cars that the user has viewed, simply insert a row with the persons UserID and the CarID (from the array) into the table. You could also remove the array all together and just perform a direct insert into the table when a user views a car.

    If you have your car information already in the database this is especially good, because to know what cars to display to the user you can simply join back to the "AlreadyViewed" table and display all the cars from the "Cars" table where the UserID isn't in the "AlreadyViewed" table. You SELECT statement would look something like:

    SELECT Cars.CarID, Cars.CarInfo
    FROM Cars, AlreadyViewed
    WHERE NOT AlreadyViewed.CarID = Cars.CarID
    AND AlreadyViewed.UserID = <current user>

    I hope this helps...

  4. #4
    SitePoint Zealot JagXJ220's Avatar
    Join Date
    Mar 2001
    Location
    Not worth the drive
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the info guys. I think I'll use the second method on my site.

  5. #5
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    JagXJ220, that's a good idea. Storing an array in one database field is a hackish solution. BlackCat's suggestions conform with relational database theory. Storing an array in a database field violates one of the first three (the first I think) "normal forms" which are the rules to follow to "normalise" data. Anyway, the rule I am thinking of is that each field in a database should hold only one "atomic" value. Holding an array of values in a field violates this.

    The advantage of having as well designed a data model as possible is that it should mean that you have modelled well the real world entities in your system (car, review, author, etc) and the relationships that exist between the entities. The idea of having your database design "normalised" means that the data is being held in a structure that will enable you to not only query it in the way you are thinking of now, but allow you to manipulate the data in other ways in future that are unplanned now. So it may seem irrelevent to the problem at hand whether you store an array of values in a single field but it does impact on the overall usefullness of the data.

    Anyway, the reason I'm rambling about this is that IMHO, the biggest favour web programmers can do themselves is read enough to understand data normalisation to the third (general) normal form. Once you've read and grasped the concepts they become intuitive and make designing database tables like second nature. The famous Kevin Yank article on building database driven web sites at www.webmasterbase.com has a whole section on database design that is a good read.

  6. #6
    SitePoint Enthusiast
    Join Date
    Mar 2001
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    FreakySid is correct. If you design the database well and adhere to the 3NF (3rd Normal Form) you will help yourself out immensely both now and for the future... Not to mention, you make us DBA's very happy

  7. #7
    Idea Developer
    Join Date
    Sep 2000
    Location
    Bethlehem, PA
    Posts
    521
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    storing 1 array in a field is a really really bad solution, thats why we invented databases in the first place, read an article on database normalization, phpbuilder and jhust about any mysql site have good ones.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •