Jump to content

Archived

This topic is now archived and is closed to further replies.

Inquiescent

Deleting XML-entries with checkboxes echoed by foreach

Recommended Posts

Hi! I have a page on which you can alter an XML file containing information as follows:

 

<productlist>

 

<product>

<id>Id</id>

<name>Name</name>

<price>Price</price>

</product>

 

</productlist>

 

Each product id, name and price are echoed through a for-each loop as text inputs which you can alter and save, thus changing the XML-file contents. For each product, there is also a checkbox with which you are supposed to use for deletion. If a product's checkbox is checked and the save button is pressed, I want the certain product which had it's checkbox checked to be deleted. Take a look at the attached image to see the output of the echo.

 

Here is the problem: If you check a box, no matter which one, product number 1 gets deleted. If you check two boxes (again, whichever you like), product 1 and 2 gets deleted, etc. This is the code for the checkbox in the foreach loop;

 

echo "<input type='checkbox' id='checkbox' name='deleteProduct[]'  />Delete";

 

There doesn't seem to be any difference namewise between the checkboxes for some reason. If I alter the checkbox name to <b>name='deleteProduct[2]'</b>, the second entry gets deleted when you press the save button, no matter which box you check.

 

I know that I'm not revealing much of the code here, but that's because the majority of it doesn't really have anything to do with the problem. If anyone would take time to help me, I'd gladly show you all of the code in a PM.

 

Thanks in advance!

post-130164-13482403721516_thumb.jpg

Share this post


Link to post
Share on other sites

Is there any reason you're using an XML file for this?

 

You'd be much better off using something like SQLite if you need a flat-file database.

Share this post


Link to post
Share on other sites

It sounds like your code already has a way to identify which checkbox is which, but it isn't working properly.  You really need to post that part of the code.

Share this post


Link to post
Share on other sites

Hey guys,

 

I solved it by doing the following:

 

$allProducts = $DOM->getElementsByTagName('products');

$count = $allProducts->getElementsByTagName('product')->length;

 

for ($i=0; $i<=($count); $i++)

{

echo "<br><input type='checkbox' id='checkbox' name='deleteProduct[".$i."]' />Delete entry ".$i."</br>";

}

 

The deletion code was so confusing that solving it this way was quite relieving.

 

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.