delete based on input

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • angelicdevil
    New Member
    • Apr 2009
    • 116

    delete based on input

    wat i m trying to do is delete a line from the xml based on the line generated i.e $mybody dynamically from the values entered by the user.

    in the code i provide...it simply adds a line </gallery> belowe the existing line of </gallery> and the line echoing $temp and $arr[i] do not display it shows a icon similar broken page. just run the code n u will understand wat i m saying.

    Code:
    <?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);
                echo $temp;
                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]);
                echo $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>

    here's the xml file

    Code:
    <gallery>
    <image heading="google" links="google"/>
    <image heading="reidff" links="rediff"/>
    <image heading="g" links="g"/>
    <image heading="jerk" links="jerk"/>
    <image heading="jack" links="jack"/>
    </gallery>
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    the line echoing $temp and $arr[i] do not display it shows a icon similar broken page.
    neither of them produces valid HTML. and if I read fgets() right, the line break is included and thus neither of your conditions match.

    Comment

    • angelicdevil
      New Member
      • Apr 2009
      • 116

      #3
      ok so wat shud i do?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        I’m not sure why you want to edit the XML this way.

        Comment

        • angelicdevil
          New Member
          • Apr 2009
          • 116

          #5
          just testing various ways. so what do i need to do now to get this working as i dont think the values r being put into the arr[i] and re written

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            maybe there is a better way of doing this …

            Comment

            • angelicdevil
              New Member
              • Apr 2009
              • 116

              #7
              of wat? deleting from xml? how ... currently it creates a array from the data i want after comparing it with $start $end and $mybody...and then writes this array to the xml....

              wat other way?

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                depending on the circumstances, you can also read the file in a string and do a string replacement.

                Comment

                • angelicdevil
                  New Member
                  • Apr 2009
                  • 116

                  #9
                  isnt that what i m doing currently

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    you’re doing it linewise and you are using an array. I don’t see string replacement involved.

                    see also
                    str_replace()
                    file_get_conten ts()
                    file_put_conten ts()

                    Comment

                    • angelicdevil
                      New Member
                      • Apr 2009
                      • 116

                      #11
                      in the line 31 doesnt it add the string in the $temp to the array?

                      doesnt $temp hold the line from the xml?

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        in the line 31 doesnt it add the string in the $temp to the array?
                        only if the condition is met.

                        PS. easy appending an element to an array:
                        Code:
                        $arr[] = "new value";

                        Comment

                        • angelicdevil
                          New Member
                          • Apr 2009
                          • 116

                          #13
                          ok so when the condition is met it shud add right...but in the script it does show the existing contents of the file but add an additional </gallery> again below the one already existing...y is tht so? and it writes the content too when the condition is not met.

                          Comment

                          • angelicdevil
                            New Member
                            • Apr 2009
                            • 116

                            #14
                            can u plz test the code on localhost u will understand wat i say.

                            Comment

                            • angelicdevil
                              New Member
                              • Apr 2009
                              • 116

                              #15
                              in the line 21 doesnt $temp hold the string line from the xml file?

                              Comment

                              Working...