RE: [PHP] Re: Editing files by line
-Original Message- From: Analysis Solutions Without seeing the script, it'd be hard for us to say. So, post the _relevant parts_ of the script to the list. Here's the code: ? // Saving edited data to the file // Data File : This file need to be writeable $file_name = data.txt; // check to see if submit was pressed if ($insert $linenumber) { // check existence of file $try = touch($file_name); if (!$try) { echo pI'm sorry, but I couldn't open a file!; exit; } $array = file($file_name); // this is the stuff we get from the form, we insert it into an array $input = array ($id, $name, $creator, $myreview, $prefix, $directory, $sort, $comments); // note that we add a \n (line break) to the end of the string. $output_line = implode ($input, |).\n; // Now open the file (get a file pointer) $output_stream = fopen($file_name, w+); $counter = 0; foreach($array as $key = $line) { if($counter == $linenumber) { $line = $output_line; } $out[] = chop($line); $counter++; } foreach($out as $key = $line) { $result=fputs($output_stream, $line.\n); } // close the file pointer fclose($output_stream); // give feedback if ($result) { echo pData has been sucessfully added.\n; echo a href=.$PHP_SELF.?line_num=.$linenumber.View updated record/a; } else { echo pUhoh. . . the database didn't like that.; } }else{ // Viewing data from file in a form //to read the file use: $fp2= fopen(data.txt, r); $line_cnt=0; while ($servinfo=fgets($fp2,1024)){ if ($line_cnt == $line_num){ $right_line = $servinfo; list($id, $name, $creator, $myreview, $prefix, $directory, $sort, $comments)= explode(|,$right_line); break; } $line_cnt++; } fclose($fp2); ? Form name= method=post action=?=$PHP_SELF? CENTER TABLE cellspacing=0 bgcolor=#FFCC99 TR bgcolor=#ccTDRacestyle data settings/TDTD:)/TD/TR TRTD align=rightRace ID:/TD TDinput type=text name=id size=50 maxlength=50 value=? echo $id; ?/TD/TR TRTD align=right height=28 nowrapRacestyle Name:/TD TD height=28input type=text name=name size=50 maxlength=50 value=? echo $name; ?/TD/TR TRTD align=rightCreator:/TD TDinput type=text name=creator size=50 maxlength=50 value=? echo $creator; ?/TD/TR TRTD align=right valign=topMyReview:/TD TDinput type=text name=myreview size=50 maxlength=400 value=? echo $myreview; ?/TD/TR TRTD align=rightRace Prefix/TD TDinput type=text name=prefix size=50 maxlength=50 value=? echo $prefix; ?/TD/TR TRTD align=rightRace Directory:/TD TDinput type=text name=directory size=50 maxlength=50 value=? echo $directory; ?/TD/TR TRTD align=rightSort:/TD TDselect name=sort option value=ship_names ? if ($sort==ship_names) { print( selected);} ?small case/option option value=ship_names_2L ? if ($sort==ship_names_2L) print( selected);} ?Upper Case/option /select/TD/TR TRTD align=right valign=topComments:/TD TDinput type=text name=comments size=50 maxlength=400 value=? echo $comments; ?/TD/TR /TABLE /CENTER P align=center input type=hidden name=linenumber value=? if !isset($line_num)){ $line_num=0;echo $line_num;}else {echo $line_num;} ? input type=submit value =Submit name=insert input type=reset value =Reset /P /FORM ? } ? The data.txt file has this format: 5|8|4|2|7|2|ship_names| 2|3|7|2|1|9|ship_names_2L|7 4|3|2|7|5|6|ship_names|3 The default line_num is 0. The other entries - database3.php?line_num=1, database3.php?line_num=2 etc. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Re: Editing files by line
-Original Message- From: Analysis Solutions Sent: Tuesday, July 16, 2002 4:40 PM To: PHP List Subject: Re: [PHP] Re: Editing files by line On Tue, Jul 16, 2002 at 04:25:25PM -0400, Onaje Johnston wrote: if ($insert $linenumber) { But, if $linenumber is 0, this process won't happen. And, that was the complaint you mentioned up front. So, you should do an isset($linenumber) instead. --Dan Thanks. It works using if ($insert isset($linenumber)) {. So because the value of linenumber is 0 on the first line, the if statement was evaluating to false and therefore the update wouldn't occur, correct? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Re: Editing files by line
-Original Message- From: Analysis Solutions Sent: Friday, July 12, 2002 9:57 AM To: PHP List Subject: Re: [PHP] Re: Editing files by line Unfortunately, if a file is opened for appending via fopen('name', 'a'), the manual says the data is put at the end of the file regardless of using fseek(). --Dan I've written a script that edits a specific line in a text file and saves the changes to the file for that specific line. I've noticed though that I can't edit the first line in the file, which would be zero in the array created by the file() function. When I write the edited data back to the file I am using w+ as the mode. Is this related to the array or to the file mode? -- Onaje Johnston -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: compare arrays problem
Thank you Tim and Richard. With your help I was able to figure out how to get this script working as I intended. Here's the code, in case anyone wants to do something similar in the future. :-)) $pages=mysql_query("SELECT CP.page_id, pagename FROM cluster_pagetbl as CP, pagetbl WHERE CP.cluster_id = '$id' AND CP.page_id=pagetbl.page_id order by page_id"); $pgids= array(); if (mysql_Numrows($pages)0) { $prows=mysql_NumRows($pages); $i=0; while ($i$prows){ //figure out how to get page ids into array $pid= mysql_result($pages,$i, page_id); $pagename =mysql_result($pages, $i, pagename); $pgids[$pid] = $pagename; $i++; } } //prints what is in the pgid array. foreach($pgids as $pid = $pagename){ print $pid.' = '.$pagename.'br'; } $query2=mysql_query("select page_id, pagename FROM pagetbl order by page_id"); $mpgids= array(); if (mysql_Numrows($query2)0) { $numrows=mysql_NumRows($query2); $x=0; while ($x$numrows){ $mpid=mysql_result($query2,$x, page_id); $mpagename=mysql_result($query2,$x, pagename); $mpgids[$mpid] = $mpagename; $x++; } } //prints the checkboxes, compares pgids and mgids array foreach ($mpgids as $mpid = $mpagename){ print 'input type=checkbox name=page_ids[] value="'.$mpid.'"'; if ($pgids[$mpid] == $mpgids[$mpid]){ print " checked"; } print ''.$mpagename; } //double check to see what values are in both arrays, then print it. $diff = array_intersect($mpgids,$pgids); foreach ($diff as $element){ print 'p'.$element.'br';} -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] compare arrays problem
I am hoping that someone really understands arrays reads this and can tell me why the following isn't working as I expect it to. I've been trying for days to get it to work. $pages=mysql_query("SELECT CP.page_id, pagename FROM cluster_pagetbl as CP, pagetbl WHERE CP.cluster_id = '$id' AND CP.page_id=pagetbl.page_id order by page_id"); /* SQL Result for $id=1: page_id pagename 1 breakingnews 3 weather */ if (mysql_Numrows($pages)0) { $prows=mysql_NumRows($pages); $i=0; while ($i$prows){ //figure out how to get page ids into array $pid= mysql_result($pages,$i, page_id); $pagename =mysql_result($pages, $i, pagename); $pgids= array("$pid" = "$pagename"); foreach($pgids as $pid = $pagename){ print $pid.' = '.$pagename.'br'; } /* prints: 1 = breakingnews 3 = weather At this point the correct number of values appear to be in the pgids array */ $i++; } } $query2=mysql_query("select page_id, pagename FROM pagetbl order by page_id"); /* SQL Result: page_id pagename 1 breakingnews 2 pastnews 3 weather */ if (mysql_Numrows($query2)0) { $numrows=mysql_NumRows($query2); $x=0; while ($x$numrows){ $mpid=mysql_result($query2,$x, page_id); $mpagename=mysql_result($query2,$x, pagename); $mpgids= array("$mpid" = "$mpagename"); foreach ($mpgids as $mpid = $mpagename){ print 'input type=checkbox name=page_ids[] value="'.$mpid.'"'; if ($pgids == $mpgids){ print " checked"; } print ''.$mpagename; } // prints out three checkboxes, since that's the total number of pages present //but only the third checkbox (weather) gets checked, the first one should be checked also $x++; } } //I used the array intersect function to doublecheck what is going on and it finds only weather also //What is happening to the first value? $diff = array_intersect($mpgids,$pgids); foreach ($diff as $element){ print 'p'.$element.'br';} //prints: weather -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]