473,387 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

delete based on input

116 100+
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.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $myheading="";
  3. $mylink="";
  4. $mybody="";
  5.  
  6. if (isset($_POST['submit'])) {
  7.  
  8. $mylink = $_POST["link"];
  9. $myheading = $_POST["heading"];
  10.  
  11. $mybody = '<image heading="'.$myheading.'" links="'.$mylink.'"/>';
  12.  
  13.     $start = "<gallery>";
  14.     $end = "</gallery>";
  15.     $file = "article.xml";
  16.     $i=$len=0;
  17.     $arr=array();
  18.     $handle = fopen($file, "r");
  19.     if ($handle) {
  20.         while (!feof($handle)) {
  21.             $temp = fgets($handle);
  22.             echo $temp;
  23.             if ($temp=="$start"){
  24.             $arr[$i]="$start";
  25.             $i++;
  26.             }
  27.             if($temp=="$end"){ $arr[$i]="";
  28.             $i++;
  29.             }
  30.             if ($temp !="$start" && $temp !="$end" && $temp != $mybody){
  31.              $arr[$i]="$temp";
  32.              $i++;
  33.             }            
  34.         }
  35.         $len=$i;
  36.         fclose($handle);
  37.     }
  38.     $handle = fopen($file,"w");
  39.     if ($handle) {
  40.         $i=0;
  41.         while ($i<$len) {              
  42.             fwrite($handle,$arr[$i]);
  43.             echo $arr[$i];
  44.             $i++;            
  45.         }
  46.         fwrite($handle,"\n$end");
  47.         fclose($handle);
  48.         $process="complete";
  49.     }
  50.     else {
  51.      $process="incomplete";
  52.     }
  53.  
  54.  
  55.  
  56. }
  57.  
  58. ?> 
  59. <table id="structure">
  60.     <tr>
  61.         <td id="page">
  62.             <h2>delete data</h2>
  63.             <?php if (!empty($process)) {echo "<p class=\"message\">" . $process . "</p>";} ?>
  64.  
  65.             <form action="delete_p.php" method="post">
  66.             <table>
  67.                 <tr>
  68.                     <td>heading</td>
  69.                     <td><input type="text" name="heading" maxlength="23" value="<?php echo htmlentities($myheading); ?>" /></td>
  70.                 </tr>
  71.                 <tr>
  72.                     <td>links</td>
  73.                     <td><input type="text" name="link" maxlength="40" value="<?php echo htmlentities($mylink); ?>" /></td>
  74.                 </tr>
  75.                 <tr>
  76.                     <td colspan="2"><input type="submit" name="submit" value="delete data" /></td>
  77.                 </tr>
  78.             </table>
  79.             </form>
  80.         </td>
  81.     </tr>
  82. </table>
  83.  
  84.  

here's the xml file

Expand|Select|Wrap|Line Numbers
  1. <gallery>
  2. <image heading="google" links="google"/>
  3. <image heading="reidff" links="rediff"/>
  4. <image heading="g" links="g"/>
  5. <image heading="jerk" links="jerk"/>
  6. <image heading="jack" links="jack"/>
  7. </gallery>
  8.  
  9.  
Jan 4 '10 #1
23 1924
Dormilich
8,658 Expert Mod 8TB
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.
Jan 4 '10 #2
angelicdevil
116 100+
ok so wat shud i do?
Jan 4 '10 #3
Dormilich
8,658 Expert Mod 8TB
I’m not sure why you want to edit the XML this way.
Jan 4 '10 #4
angelicdevil
116 100+
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
Jan 4 '10 #5
Dormilich
8,658 Expert Mod 8TB
maybe there is a better way of doing this …
Jan 4 '10 #6
angelicdevil
116 100+
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?
Jan 4 '10 #7
Dormilich
8,658 Expert Mod 8TB
depending on the circumstances, you can also read the file in a string and do a string replacement.
Jan 4 '10 #8
angelicdevil
116 100+
isnt that what i m doing currently
Jan 4 '10 #9
Dormilich
8,658 Expert Mod 8TB
you’re doing it linewise and you are using an array. I don’t see string replacement involved.

see also
str_replace()
file_get_contents()
file_put_contents()
Jan 4 '10 #10
angelicdevil
116 100+
in the line 31 doesnt it add the string in the $temp to the array?

doesnt $temp hold the line from the xml?
Jan 4 '10 #11
Dormilich
8,658 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. $arr[] = "new value";
Jan 4 '10 #12
angelicdevil
116 100+
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.
Jan 4 '10 #13
angelicdevil
116 100+
can u plz test the code on localhost u will understand wat i say.
Jan 4 '10 #14
angelicdevil
116 100+
in the line 21 doesnt $temp hold the string line from the xml file?
Jan 4 '10 #15
Dormilich
8,658 Expert Mod 8TB
upon testing the script does what it should do. and may I remind you of what I said in post #2?
Jan 4 '10 #16
angelicdevil
116 100+
how do i get rid of linebreak?
Jan 4 '10 #17
angelicdevil
116 100+
but the linebreak is /> which is part of the script so if i use str replace wont it get rid of that too?
Jan 4 '10 #18
angelicdevil
116 100+
i have put the lines

Expand|Select|Wrap|Line Numbers
  1.  $temp =str_replace("\r", "", $temp);
  2.             $temp =str_replace("\n", "", $temp);
  3.  
below line 21 to remove line break but it still doesn't delete the line that matches the entered value but instead it all in 1 line.
Jan 4 '10 #19
Dormilich
8,658 Expert Mod 8TB
but instead it all in 1 line.
obviously, you remove every line break in the file (which doesn't matter from the XML-point-of-view).

judging from my test you don't need to worry about the line breaks. but it still seems like overkill to do it this way.
Jan 4 '10 #20
angelicdevil
116 100+
but y isnt it overwriting cthe existig file properly
Jan 5 '10 #21
angelicdevil
116 100+
ol one thing when i echo back $start or $ end they r empty they r not echoing back anything y is tht so?
Jan 5 '10 #22
angelicdevil
116 100+
how can i get php to trest < or > as string
Jan 5 '10 #23
Dormilich
8,658 Expert Mod 8TB
for a change, try the functions mentioned in post #10.
Jan 5 '10 #24

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Pelle | last post by:
Hello all, I have to admit, that the idea that occurred to me recently is weird. It was somehow inspired by the huge response on the "delete operator" thread, but goes into a somewhat different...
22
by: Cylix | last post by:
I have a 4row x 1col table, I would like to drop all the content of row three. Since Mac IE5.2 does not suppport deleteRow method, I have also try to set the innerHTML=''; but it does not work. ...
9
by: Laphan | last post by:
Hi All I'm using the below to limit the input into a text box to just letters, numbers, hyphens and full stops, but I also need to allow the backspace, delete and arrow keys to come through. ...
3
by: bluez | last post by:
I want to design a webpage where user can search the data from the database and list out the related records. Each of the record got a delete button which allow user to delete the record. ...
8
by: prosad | last post by:
Hi! Have been hitting my head on the wall for two weeks now. am a php newbie. have a form that on submitting to a MySQL DB instantly generates a HTML table on the same page. what i need now is way...
1
by: jmarcrum | last post by:
Hello all! i have a “monitor-type” program, where my program recognizes my defined "commands”, and executes the given command, or produces an error message that says “unrecognized command”. After...
0
by: Slickuser | last post by:
From my PHP page: Grab all data from the database. Go through a loop to generate the HTML. Client side: From the Color drop menu list, if a user change the value. It will grab that value &...
1
by: ahilar12 | last post by:
Hi all, I am new to php,my question is that in this following code i am retrieving many rows from the database which is working good.i want to delete a particular row(s) which is checked(checkbox)...
29
by: shivasusan | last post by:
Hi! I can add rows with inputs to my HTML table dynamically using DOM, but I cannot remove selected rows. In fact, every row contains a Delete button. So, user selects the rows to remove, clicks...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.