Log in

View Full Version : Deleting Item from xml



I am Abby
04-27-2010, 03:04 PM
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.

traq
04-27-2010, 03:32 PM
...
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

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

?>
To actually find the corresponding entries, try something like:

<?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
?>

bluewalrus
04-27-2010, 03:34 PM
Traq beat me and answer sounds more thorough.

I am Abby
04-27-2010, 03:46 PM
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.

I am Abby
04-27-2010, 04:13 PM
$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?

traq
04-27-2010, 07:47 PM
um... try this:


$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.



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.

I am Abby
04-27-2010, 07:48 PM
I think the problem is with my logic. I keep overwriting my dcount instead of making an array out of it.

traq
04-27-2010, 07:58 PM
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.

I am Abby
04-28-2010, 02:32 PM
// 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?

traq
04-28-2010, 03:21 PM
Oh! I didn't even catch that. Put the dcount value in the checkbox instead.

'<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.

I am Abby
04-28-2010, 03:25 PM
Thanks traq for the help.

they should let everyone put there pictures up...it will take me years to become a senior.