SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Thread: PHP Problem...

  1. #1
    SitePoint Wizard
    Join Date
    Apr 2000
    Posts
    1,483
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Being a relative PHP newbie, I'm having trouble doing something which I consider pretty complex.
    Here is the problem:
    I have a whole load of checkboxes, all with the same name but different values. (ie <input type="checkbox" value="this is different on each" name="this is the same on each">).
    What I want to do is for each one that is checked, write a record to a mySQL table. One of those fields should contain the "value" attribute of the checkbox, and the other should contain something which is the same for all records written in that session.
    I'm thinking I need to loop through an array of the checkbox results and write a record each loop but I'm not sure how to do it.
    Sorry if I haven't explained this well enough...if you don't understand I'll try to explain it again.

    Thanks in advance! :)

  2. #2
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    A very similar example is treated in Part Six of my article series. When adding a joke to the sample database presented there, a user must be able to specify which categories that joke belongs to. In the example, I present the user with a form containing a set of checkboxes, one for each catgory. The code to process that form then loops through the checkboxes checking each one and adding an entry to the database if it is checked.

    That portion of the example is here: http://www.webmasterbase.com/article...id=228&pid=691

    The only difference between that example and yours is that the checkboxes don't all have the same NAME. Read through my example and decide for yourself if you really need to stick with identically-named checkboxes. If you do, post back and I'll explain how.
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  3. #3
    SitePoint Wizard
    Join Date
    Apr 2000
    Posts
    1,483
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help Kevin...I'll go and part 6 of your article right now!

  4. #4
    SitePoint Wizard
    Join Date
    Apr 2000
    Posts
    1,483
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've read your tutorial and now I do have some idea of how to do what I want to do.
    However, my category names are not coming out of a database and they don't really need to because they won't change.
    For that reason I called the checkboxes the same thing so that there would be something to loop through (ie an array).
    I would rather keep the categories out of the database if possible.
    But other than that I want to do almost exactly the same as your article is showing.

    Thanks in advance!

  5. #5
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It makes a lot more sense to keep all of your site's data in one place (i.e. the database). Just because something won't change doesn't mean that it shouldn't be databased. Databases are actually much faster at reading things than changing things, and keeping all your data in the database will speed up things like listing all articles in a given category significantly!

    Ask yourself this: why aren't you putting your categories in the database? I can't think of a single advantage to doing things your way, but I can see several disadvantages.
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  6. #6
    SitePoint Wizard
    Join Date
    Apr 2000
    Posts
    1,483
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help Kevin...I changed it all over to the database and it works fine now!
    I'm having a few problems with the display page but I'll try to solve them myself before posting them over here.

  7. #7
    SitePoint Wizard
    Join Date
    Apr 2000
    Posts
    1,483
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK well I got a bit stuck with the page that displays the news items...
    Hope someone can help:
    The SQL statement I am using has been basically taken direct out of the tutorial, but it seems to return all news items, whether or not they match the siteid that is specified in the querystring.
    Here is the sql:
    Code:
    SELECT DISTINCT ID, news_text FROM news_items, news_display WHERE ID > 0 AND SID=$siteid AND ID=NID
    So as not to confuse people, I should tell you that this is for a News script...the reason i need the lookup table etc that it is for a network of 21 sites and I need the user to be able to choose which sites the news item will appear on.

    Hope you can help!
    Thanks in advance!

  8. #8
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Can you help us out by either pasting your two table definitions (type DESC tblName in mysql), or by prefixing all your column names (ID, NID, SID, etc.) with their table names (tblName.ID, tblName.NID, tblName.SID)? I'm having a hard time figuring out what you're trying to accomplish.
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  9. #9
    SitePoint Wizard
    Join Date
    Apr 2000
    Posts
    1,483
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK I'll add the table names:
    Code:
    SELECT DISTINCT * FROM news_items, news_display WHERE news_items.ID > 0 AND news_display.SID=$siteid AND news_items.ID=news_display.NID
    Basically I have table "news_display" as the Lookup table,and "news_items" as the main table.
    Hope that is explained a bit better...

    Thanks in advance

  10. #10
    SitePoint Wizard
    Join Date
    Apr 2000
    Posts
    1,483
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK I managed to fix it! I just played around with the sql statement - not even exactly sure what I changed
    But it works, and thats the main thing..


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
  •