SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Mar 2002
    Location
    Michigan
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Okay I need help with multiple things.

    This just isn't working as I planned. Allow me to explain. I have a list of game titles displayed ten at a time. Each row has a checkbox next to it so you can select any title you want. Once you select the titles, you click submit and each game title is supposed to be added to a list. Well, the problem I'm having is that the query won't execute in a foreach() loop. I have all ten values of the checkboxes pulled from an array I created. I'm positive there is a better way to do what I am doing, as by all means I'm nowhere near a professional programmer.... that's why I post here all the time.

    PHP Code:
    <?
    require_once("include/vbfunctions.php");
    require_once(
    "cp/includes/_db.php");
    require_once(
    "cp/includes/_functions.php");
    $userName $bbuserinfo['username'];
    $userID $bbuserinfo['userid'];
    $type $_REQUEST['type'];
    if(!
    $userName) {
    redirectTo("login.php");
    } else {
     
     
    $games = array(
    $_REQUEST['1'],
    $_REQUEST['2'],
    $_REQUEST['3'],
    $_REQUEST['4'],
    $_REQUEST['5'],
    $_REQUEST['6'],
    $_REQUEST['7'],
    $_REQUEST['8'],
    $_REQUEST['9'],
    $_REQUEST['10'],
    );
     
    foreach(
    $games as $key => $value) {
    if(
    $value != "0") {
     
    $sql mysql_query("
         INSERT INTO games_owned
         (
         username,
         userid,
         gameID,
         type
         )
         VALUES
         (
         
    $userName,
         
    $userID,
         
    $value,
         
    $type
         )
        "
    ,$db);
        if(
    $type == "1") {
         
    $set "collection";
        } else {
         
    $set "wishlist";
        }
        
    redirectTo("managelist.php?display=$set");
    }
     
     
     
    }
    }
    ?>
    BTW, you may want to select 1 value from the checkbox or all 10. I need this to work regardless of the selections a user makes.

  2. #2
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about renaming all checkboxes to games_list[] and then loop over them like this.
    PHP Code:
    foreach ($_POST['games_list'] as $game_id) {
        
    // ... do something


  3. #3
    SitePoint Addict
    Join Date
    Mar 2002
    Location
    Michigan
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, let me see if that'll work. I'll post the results here.

  4. #4
    SitePoint Addict
    Join Date
    Mar 2002
    Location
    Michigan
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do you know, it worked!

    Thanks a lot!

  5. #5
    SitePoint Addict
    Join Date
    Mar 2002
    Location
    Michigan
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Almost forgot, here's the code:

    Partial code for the games index (10 items)
    PHP Code:
    <input type="checkbox" name="game_list[<? echo $c++; ?>]" value="<?=$id?>">
    $c = 1; right before my while() loop as I select results.

    PHP Code:
    <?
    require_once("include/vbfunctions.php");
    require_once(
    "cp/includes/_db.php");
    require_once(
    "cp/includes/_functions.php");
    $userName  $bbuserinfo['username'];
    $userID  $bbuserinfo['userid'];
    $type  $_REQUEST['type'];
    if(!
    $userName) {
     
    redirectTo("login.php");
    } else {
     

     
    $games = array(
      
    $_REQUEST['1'],
      
    $_REQUEST['2'],
      
    $_REQUEST['3'],
      
    $_REQUEST['4'],
      
    $_REQUEST['5'],
      
    $_REQUEST['6'],
      
    $_REQUEST['7'],
      
    $_REQUEST['8'],
      
    $_REQUEST['9'],
      
    $_REQUEST['10'],
      );

      foreach(
    $_POST['game_list'] as $value) {
       if(
    $value "0") {
       
       
    $sql mysql_query("
         INSERT INTO games_owned
         (
          username,
          userid,
          gameID,
          type
         )
          VALUES
         (
          '
    $userName',
          '
    $userID',
          '
    $value',
          '
    $type'
         )
       "
    ,$db);
        if(
    $type == "1") {
         
    $set "collection";
        } else {
         
    $set "wishlist";
        }
        
    redirectTo("managelist.php?display=$set");
       }
          
     

      }
    }
    ?>
    Again, thanks for the help.

  6. #6
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Two things, when generating the checkboxes you can drop $c, you only need game_list[] and you will still get an array. The second thing is that you can now drop the $games array code.


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
  •