SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    What's HTML?
    Join Date
    May 2001
    Location
    San Diego, CA
    Posts
    1,701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Rewinding an array

    I have a set of checkboxes being displayed on a page, the name of each tag is "categories", and the value depends on the Category ID. So it would look something like this:
    <input type="checkbox" name="categories" value="1">Scenic<br>
    <input type="checkbox" name="categories" value="2">3D<br>
    <input type="checkbox" name="categories" value="3">Animals<br>

    So I'm trying to add the categories into a table, along with the corresponding image ID. I use a while loop to accomplish this. The problem is it will only add the last selected element (for example, if they all were checked it would only add 3". I figured the array is probably pointing to its last element, so I do:
    $categories = reset($categories);
    and get this warning.

    Warning: Variable passed to reset() is not an array or object

    Checked php.net and someone posted that reset() can cause problems with form input data because PHP thinks you're passing in a scalar. They listed a code snippet they created, but I edited it to fit my needs:
    $c = 0;
    if(isset($categories))
    {
    reset($categories);
    while($categories[$c])
    {
    $sql = "INSERT INTO $table2_name SET wallpaperID=$wallpaperID, categoryID=$categories[$c]";
    $result = mysql_query($sql,$cnx) or die("Could not execute query. The error is on the SQL or connection line.<br>Your entered query is: $sql");
    $c++;
    }
    }

    But still, it's only adding the last selected element. Any ideas?
    Ryan Kuhle - A Proud Advisor - Got Questions? Just Ask!
    Get your website started for less than $20! Click Here

  2. #2
    What's HTML?
    Join Date
    May 2001
    Location
    San Diego, CA
    Posts
    1,701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now that I think about it, I'm pretty sure it's a problem with the input array. The values just overwrite each other, so instead of $categories having 3 values, it only has one. What can I do to the input tags to overcome this?

    Should
    <input type="checkbox" name="categories">
    Be changed to
    <input type="checkbox" name="categories[]">
    Ryan Kuhle - A Proud Advisor - Got Questions? Just Ask!
    Get your website started for less than $20! Click Here

  3. #3
    What's HTML?
    Join Date
    May 2001
    Location
    San Diego, CA
    Posts
    1,701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, it was the array that was causing a problem. Working great now!
    Ryan Kuhle - A Proud Advisor - Got Questions? Just Ask!
    Get your website started for less than $20! Click Here

  4. #4
    Victory shall be mine tubedogg's Avatar
    Join Date
    Mar 2001
    Location
    Medina, OH
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Um ...Glad we could help!
    Kevin


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
  •