Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder
    Join Date
    Jul 2005
    Location
    Oxfordshire, UK
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Multiple updates

    i have this check box on my page...

    <input type="checkbox" name="id[]" value="' . $row['id'] . '">

    and when submitted i am running this code...

    PHP Code:

    if (isset($_POST['submit'])) {
     
    $id $_POST["id"];

    $query "UPDATE updates set status = 'test' where id in ($id)";
    $result = @mysql_query ($query); //run the query

    echo 'complete';
    echo 
    $id;

    i have echoed out $id to see what it shows and it just outputs the word array and the table is not updating... where am i going wrong?
    Last edited by robojob; 12-20-2006 at 11:17 PM.

  • #2
    Regular Coder Tyree's Avatar
    Join Date
    Sep 2003
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    By setting the checkbox name as id[], you are saying you want it to be an array. To echo it back out, you need to tell php which key from the array you want to display. $id[0], for example.

    If you DON'T need it to be an array, then drop the brackets...name the checkbox 'id'
    Matt Tyree
    TyreeOnline
    If I didn't just "make it worse," show me some love! Hit me with the rep points! :) (The white scales icon on the left)

  • #3
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Array is what you want, you just have to loop through the array and build up your id's string separated by commas:
    PHP Code:
    $in '';
    foreach(
    $id as $ids)
    {
        
    $in .= $ids ',';
    }
    $in substr($in0strlen($in)-1);

    // now use $in...where id in($in) 
    Good luck;

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,538
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    This might not really answer your question, but
    $id will be null (non-existent) if the checkbox
    is NOT checked. PHP only assigns a value if
    the box is checked.

    So, $id[0] will be the value of $row[id], only if it's checked,
    otherwise, $id is null.

    ... and because you specified "id[]", it's expecting an array,
    so try echoing $id[0] and see if it shows up.

    Maybe the array thing is where your problem is?

  • #5
    Regular Coder Tyree's Avatar
    Join Date
    Sep 2003
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, it really all depends on what you're trying to accomplish. We don't know if you're outputting several rows of data, each having a checkbox...in that case you WOULD want to set them to an array.

    As for Brandoe's suggestion on what to do with the $id array once you have it. Brandoe probably knows better. But, I prefer to use the join() function rather than what he did.
    Once you have populated the $id array:
    PHP Code:
    if (is_array($id)){
    $in join(','$id);
    }
    // now use $in...where id in($in) 
    Matt Tyree
    TyreeOnline
    If I didn't just "make it worse," show me some love! Hit me with the rep points! :) (The white scales icon on the left)

  • #6
    Regular Coder
    Join Date
    Jul 2005
    Location
    Oxfordshire, UK
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Brandoe85 View Post
    Array is what you want, you just have to loop through the array and build up your id's string separated by commas:
    PHP Code:
    $in '';
    foreach(
    $id as $ids)
    {
        
    $in .= $ids ',';
    }
    $in substr($in0strlen($in)-1);

    // now use $in...where id in($in) 
    Good luck;

    Thanks, this did it perfectly!

  • #7
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Quote Originally Posted by Tyree View Post
    Yes, it really all depends on what you're trying to accomplish. We don't know if you're outputting several rows of data, each having a checkbox...in that case you WOULD want to set them to an array.

    As for Brandoe's suggestion on what to do with the $id array once you have it. Brandoe probably knows better. But, I prefer to use the join() function rather than what he did.
    Once you have populated the $id array:
    PHP Code:
    if (is_array($id)){
    $in join(','$id);
    }
    // now use $in...where id in($in) 
    Yeah, I wanted to use implode() but couldn't remember the name while I was typing the post, heh

    http://us3.php.net/implode

  • #8
    Regular Coder Tyree's Avatar
    Join Date
    Sep 2003
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hmmm, I don't know why I never realized join() and implode() were interchangable! Never had to look them up on php.net, I guess!
    Matt Tyree
    TyreeOnline
    If I didn't just "make it worse," show me some love! Hit me with the rep points! :) (The white scales icon on the left)

  • #9
    Regular Coder
    Join Date
    Jul 2005
    Location
    Oxfordshire, UK
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i have this code now...

    PHP Code:
        if (isset($_POST['submit'])) {
     
    $id $_POST["id"];
    $status $_POST['status'];

    $in '';
    foreach(
    $id as $ids)
    {
        
    $in .= $ids '<br/>';
    }
    $in substr($in0strlen($in)-1);

    echo 
    $in;

    and i have a form that has several rows with a checkbox for each row with its id number.

    for updating multiple records this now work, what i want to do now is add a new feature that....

    when the check boxes are checked it submits the page and inputs each id from the array in to a text field... i have tried echo $in[0]; etc and it only echos a single number not the full id, for example if the first id in the array is 44, and i echo $in[0] all i get is a 4 not 44, any ideas?

  • #10
    Regular Coder Tyree's Avatar
    Join Date
    Sep 2003
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You'll need to do a for statement to run through the values of $id. Just printing $id[0] will print the value of the first 'member' of the array.

    PHP Code:
    for ($i=0$i <= count($id); $i++){
    print 
    $id[$i];

    As Brando and I talked about above...use this:
    PHP Code:
    if (is_array($id)){
    $in implode('<br/>'$id);
    }
    echo 
    $in
    In place of:
    PHP Code:
    $in ''
    foreach(
    $id as $ids

        
    $in .= $ids '<br/>'

    $in substr($in0strlen($in)-1); 

    echo 
    $in

    Does the EXACT same thing, but it's much cleaner.
    Last edited by Tyree; 12-21-2006 at 07:38 PM.
    Matt Tyree
    TyreeOnline
    If I didn't just "make it worse," show me some love! Hit me with the rep points! :) (The white scales icon on the left)

  • #11
    Regular Coder
    Join Date
    Jul 2005
    Location
    Oxfordshire, UK
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    if (is_array($id)){ 
    $in implode('<br/>'$id); 

    echo 
    $in[1];

    how can i get it to print a single check box. eg, check box number 3... $in[4] prints value 4 in the array not the 4th checkbox id...

  • #12
    Regular Coder
    Join Date
    Jul 2005
    Location
    Oxfordshire, UK
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ignore that last post.... had a blond moment!!!

  • #13
    Regular Coder Tyree's Avatar
    Join Date
    Sep 2003
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay, so you want a checkbox beside each entity so you can select which ones to update?

    Okay, first, I'm going to assume you know how to get a entities out of a database and put them into an array. We'll call that array $entities.

    So, you'd run a foreach on the $entitis array to print out a checkbox for each one.
    PHP Code:
    foreach($entities AS $entity){
    print 
    '<input type="checkbox" name="'.id[].'" value="1">'.$entity['name'];

    So, that prints a checkbox for each entity found in your main db table. I guessed that 'name' may be one of your field names. You can alter that as you see fit.

    So, as you check each checkbox and then submit the form the checkboxes are housed in...you'd then use the code we've given you to process the $id array.
    Matt Tyree
    TyreeOnline
    If I didn't just "make it worse," show me some love! Hit me with the rep points! :) (The white scales icon on the left)


  •  

    Posting Permissions

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