Page 1 of 1

delete based on input

Posted: Sun Jan 03, 2010 7:41 pm
by angelic_devil
i have a xml sheet that has links ..i m trying to delete the line from the xml based on line generated from the values i enter in the text field . wat its doing currently is it doesnt delete and gives and error undefined offset:8

please help

Code: Select all

 <?php
$myheading="";
$mylink="";
$mybody="";
 
if (isset($_POST['submit'])) {
 
$mylink = $_POST["link"];
$myheading = $_POST["heading"];
 
$mybody = '<image heading="'.$myheading.'" links="'.$mylink.'"/>';
 
    $start = "<gallery>";
    $end = "</gallery>";
    $file = "article.xml";
    $i=$len=0;
    $arr=array();
    $handle = fopen($file, "r");
    if ($handle) {
        while (!feof($handle)) {
            $temp = fgets($handle);
            if ($temp=="$start"){
            $arr[$i]="$start";
            $i++;
            }
            if($temp=="$end"){ $arr[$i]="";
            $i++;
            }
            if ($temp !="$start" && $temp !="$end" && $temp != $mybody){
             $arr[$i]="$temp";
             $i++;
            }            
        }
        $len=$i;
        fclose($handle);
    }
    $handle = fopen($file,"w");
    if ($handle) {
        $i=0;
        while ($i<=$len) {              
            fwrite($handle,$arr[$i]);
            $i++;            
        }
        fwrite($handle,"\n$end");
        fclose($handle);
        $process="complete";
    }
    else {
     $process="incomplete";
    }
}
 
?> 
<table id="structure">
    <tr>
        <td id="page">
            <h2>delete data</h2>
            <?php if (!empty($process)) {echo "<p class=\"message\">" . $process . "</p>";} ?>
            
            <form action="delete_p.php" method="post">
            <table>
                <tr>
                    <td>heading</td>
                    <td><input type="text" name="heading" maxlength="23" value="<?php echo htmlentities($myheading); ?>" /></td>
                </tr>
                <tr>
                    <td>links</td>
                    <td><input type="text" name="link" maxlength="40" value="<?php echo htmlentities($mylink); ?>" /></td>
                </tr>
                <tr>
                    <td colspan="2"><input type="submit" name="submit" value="delete data" /></td>
                </tr>
            </table>
            </form>
        </td>
    </tr>
</table>
 
 
my xml file is as follows:-

Code: Select all

 
<gallery>
<image heading="google" links="google"/>
<image heading="rediff" links="rediff"/>
<image heading="g" links="g"/>
<image heading="jerk" links="jerk"/>
<image heading="jack" links="jack"/>
</gallery>
 
 

Re: delete based on input

Posted: Sun Jan 03, 2010 8:52 pm
by requinix
Off-by-one error.

Code: Select all

while ($i<=$len) {
You want <.

Also, fgets returns lines with the newline attached. You don't account for that.
rtrim the newlines off and remember to put them back when you write out $arr.

Re: delete based on input

Posted: Sun Jan 03, 2010 9:09 pm
by angelic_devil
the problem also is that tht line doesnt get deleted it just adds </gallery> again below the existing </gallery> in the xml file