Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Deleting Item from xml

  1. #1
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default Deleting Item from xml

    I loop through an xml and give my user a checkbox to click to delete...the form is sent back to itself for processing.


    // list all entries to delete in main body of document
    $file = '../sidebar.xml';
    $xml = simplexml_load_file($file);

    foreach($xml->children() as $myquote)
    {
    echo"<hr>
    <input type=\"hidden\" name=\"dcount\" id=\"dcount\" value=".$myquote->id." />
    Quote: ".$myquote->thequote."<br />
    Reference: ".$myquote->reference."<br />
    <input type=\"checkbox\" name=".$myquote->id." id=".$myquote->id." /> Delete Item";
    }

    //script for when the page loads...to delet item
    //I'm testing and am just wanting to print the name (dcount) of items checked
    $dcount = $_POST["dcount"];
    $dfile = '../sidebar.xml';
    $dxml = simplexml_load_file($dfile);

    foreach($dxml->children() as $dmyquote)
    {
    if($dcount=='checked')
    {
    echo $dcount."<br />";
    }
    }

    The page works. I can clice one or many checkboxes but when I submit, I don't get any echo. What am I doing wrong?

    I'm starting to feel like a complete idoit when php is concerned...but I'm just learning.

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by I am Abby View Post
    ...
    if($dcount=='checked')
    {
    echo $dcount."<br />";
    }
    ...
    $dcount will never equal 'checked'. You set their values as the entry IDs.

    Any "dcount" checkboxes that were not checked won't show up a POST variables. They're gone, so don't worry about them.

    If you just want to echo which were checked, try something like this:
    PHP Code:
    <?php

    foreach($dcount as $d){
       echo 
    $d;
    }

    ?>
    To actually find the corresponding entries, try something like:
    PHP Code:
    <?php

    $dcount 
    $_POST["dcount"];
    $dfile '../sidebar.xml';
    $dxml simplexml_load_file($dfile);

    foreach(
    $dxml->children() as $dkid){
      if(
    in_array($dkid->id$dcount)){
         echo 
    $dkid->name.' was checked<br>';
      }
    }
    //  "$dkid->name" is just an example, 
    //  e.g., if you have an xml node called "name."; 
    //  use whatever node name you have
    ?>

  3. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Traq beat me and answer sounds more thorough.
    Corrections to my coding/thoughts welcome.

  4. #4
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    And I thought mine was so good...well except for the fact that it didn't work.

    I'm going to have to study this for a while...to figure out what's happening here. Thanks traq and almost bluewalrus.

  5. #5
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    Quote Originally Posted by traq View Post
    PHP Code:
    $dcount $_POST["dcount"];
    $dfile '../sidebar.xml';
    $dxml simplexml_load_file($dfile);

    foreach(
    $dxml->children() as $dkid){
      if(
    in_array($dkid->id$dcount)){
         echo 
    $dkid->name.' was checked<br>';
      }
    }
    ?> 
    when I used the above code substituting "name" with "thequote" (one of my fields)
    "echo $dkid->thequote.' was checked<br>';

    I get Warning in_array() expects parameter 2 to be array, string given in (then points to line with) "if(in_array($dkid->id, $dcount))"

    This line is just looking into the xml for the item (in the field) "id" that matches varible sent by the form?

  6. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    um... try this:
    PHP Code:
    $dcount = array();

    foreach(
    $_POST['dcount'] as $post){
      
    $dcount[] = $post;

    If you're testing this by only checking one box, for example, then $dcount won't be an array. this is kinda repetitive if $_POST['dcount'] is already an array, but it does force the "array" type even if there's only one value.

    Edit:

    Just noticed: your checkboxes need to have name="dcount[]" , (not just "dcount"!!) or all you'll get is the value of the last checkbox selected.

    Last edited by traq; 04-27-2010 at 07:57 PM.

  7. #7
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    I think the problem is with my logic. I keep overwriting my dcount instead of making an array out of it.

  8. #8
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by I am Abby View Post
    I think the problem is with my logic. I keep overwriting my dcount instead of making an array out of it.
    yeah, I think so. See my [ edit ] above.

    I *think* that adding [] will make dcount an array even if there's only one selection - in which case, my original suggestion will work fine. Test it out, and let us know.

  9. #9
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    Quote Originally Posted by I am Abby View Post

    // list all entries to delete in main body of document
    $file = '../sidebar.xml';
    $xml = simplexml_load_file($file);

    foreach($xml->children() as $myquote)
    {
    echo"<hr>
    <input type=\"hidden\" name=\"dcount\" id=\"dcount\" value=".$myquote->id." />
    Quote: ".$myquote->thequote."<br />
    Reference: ".$myquote->reference."<br />
    <input type=\"checkbox\" name=".$myquote->id." id=".$myquote->id." /> Delete Item";
    }

    //script for when the page loads...to delet item
    //I'm testing and am just wanting to print the name (dcount) of items checked
    $dcount = $_POST["dcount"];
    $dfile = '../sidebar.xml';
    $dxml = simplexml_load_file($dfile);

    foreach($dxml->children() as $dmyquote)
    {
    if($dcount=='checked')
    {
    echo $dcount."<br />";
    }
    }
    Problem with my logic is...the line <input type=\"hidden\" name=\"dcount\" id=\"dcount\" value=".$myquote->id." />
    This line should be taken out.

    I need to find out if what checkboxes have been checked if any...and echo them.

    btw: can someone tell me how I can add my picture by my name like other people have?
    Last edited by I am Abby; 04-28-2010 at 03:10 PM.

  10. #10
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Oh! I didn't even catch that. Put the dcount value in the checkbox instead.
    PHP Code:
    '<input type="checkbox" name="dcount[]" value="'.$myquote->id.'" /> Delete Item'
    As for the user pictures, I think you have to be a "senior coder" before you can do that.

  11. The Following User Says Thank You to traq For This Useful Post:

    I am Abby (04-28-2010)

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
  •