ยินดีต้อนรับคุณ, บุคคลทั่วไป กรุณา เข้าสู่ระบบ หรือ ลงทะเบียน

เข้าสู่ระบบด้วยชื่อผู้ใช้ รหัสผ่าน และระยะเวลาในเซสชั่น

ThaiSEOBoard.comพัฒนาเว็บไซต์Programmingscript อ่าน txt เป็นแถวได้ แต่จะทำอย่างไรให้ลบ แต่ละเเถวได้?
หน้า: [1]   ลงล่าง
พิมพ์
ผู้เขียน หัวข้อ: script อ่าน txt เป็นแถวได้ แต่จะทำอย่างไรให้ลบ แต่ละเเถวได้?  (อ่าน 2505 ครั้ง)
0 สมาชิก และ 1 บุคคลทั่วไป กำลังดูหัวข้อนี้
Fallen
หัวหน้าแก๊งเสียว
*

พลังน้ำใจ: 162
ออฟไลน์ ออฟไลน์

กระทู้: 2,751



ดูรายละเอียด เว็บไซต์
« เมื่อ: 11 ตุลาคม 2019, 10:18:19 »

ผมมี code อ่าน ไฟล์ data.txt ได้ โดยมันจะอ่านเรียงเป็นเเถว

ต้องการให้ติ๊กเลือก แถวใดๆ แล้วกดลบได้ (มันจะไปลบใน data.txt แล้วก็ขยับเลื่อนบรรทัดขึ้นมา ไม่ให้เป็นบรรทัดว่าง)
ตอนนี้ทำได้แค่ติ๊ก แต่ไม่รู้จะใส่คำสั่งให้มันทำอย่างนั้นได้ยังไง พอจะมีวิธีไหมครับ?

 wanwan019

โค๊ด:
<?php
$MESSAGE_FILE 
"data.txt";
?>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
if (! file_exists($MESSAGE_FILE))
error("Can't find file, check '\$MESSAGE_FILE' var...");
$file_arry file($MESSAGE_FILE); //or error("Can not open \$MESSAGE_FILE");
if (($HTTP_POST_VARS['BT_Suppress'] != "") && is_array($HTTP_POST_VARS['select']))  {
while (list($HTTP_POST_VARS['key'], $HTTP_POST_VARS['val']) = each($HTTP_POST_VARS['select'])) {
if ($HTTP_POST_VARS['val'] != "") {
$file_arry[$HTTP_POST_VARS['val']] = "";
$modif 1;
}
}
if ($modif) {
$txt join(''$file_arry);
$fp fopen("$MESSAGE_FILE""w"); // error("Can not write \$MESSAGE_FILE");
flock($fp1);
fputs($fp$txt);                                                     
flock($fp3);
fclose($fp);
}
}
echo 
"<form METHOD=POST>
<table><TR><TD>ลำดับที่</TD><TH>รายละเอียด</TH></TR>"
;

$no 0;
while (list(
$key$val) = each($file_arry)) {
if ($val != "") {
$no++;
echo "<TR><TD><INPUT type=\"checkbox\" Name=\"select[$key]\" value=\"$key\"> $n</TD>
<TH>
$val</TH></TR>";
}
}
echo 
"</table><BR>";
echo 
"<INPUT TYPE=\"Submit\" Value=\"ลบข้อมูลที่เลือก\" NAME=\"$val\"></FORM>";
?>

ผลลัพธ์ code ข้างบน


data.txt


 wanwan017 wanwan017 wanwan017
บันทึกการเข้า

gilbert
สมุนแก๊งเสียว
*

พลังน้ำใจ: 587
ออฟไลน์ ออฟไลน์

กระทู้: 713



ดูรายละเอียด
« ตอบ #1 เมื่อ: 11 ตุลาคม 2019, 13:29:01 »

เอาข้อมูลที่ได้จากการติ๊กเขียนทับไฟล์เดิมครับ
บันทึกการเข้า
Fallen
หัวหน้าแก๊งเสียว
*

พลังน้ำใจ: 162
ออฟไลน์ ออฟไลน์

กระทู้: 2,751



ดูรายละเอียด เว็บไซต์
« ตอบ #2 เมื่อ: 14 ตุลาคม 2019, 11:05:44 »

เอาข้อมูลที่ได้จากการติ๊กเขียนทับไฟล์เดิมครับ

พอมีตัวอย่างไหมครับ  wanwan001
บันทึกการเข้า

น้ำใส
หัวหน้าแก๊งเสียว
*

พลังน้ำใจ: 266
ออฟไลน์ ออฟไลน์

กระทู้: 2,134



ดูรายละเอียด เว็บไซต์
« ตอบ #3 เมื่อ: 14 ตุลาคม 2019, 13:17:52 »

ไม่ได้สร้างData เป็น array แค่เขียนลบไฟล์ data.txt จะสั่งให้โปรแกรมไปลบบรรทัด มีวิธีการเดียวสร้างไฟล์ใหม่ไป save ทับไฟล์เดิม เหมือนความคิดเห็นท่าน
เอาข้อมูลที่ได้จากการติ๊กเขียนทับไฟล์เดิมครับ
บันทึกการเข้า

แนะนำ บทความดีๆ By น้ำ ใ ส ด อ ท ค อ ม กาละแมกะทิสด ขนมลาแผ่นนิ่ม หอม อร่อย ทำ สดใหม่ทุกวัน
aommiez
ก๊วนเสียว
*

พลังน้ำใจ: 13
ออฟไลน์ ออฟไลน์

กระทู้: 303



ดูรายละเอียด
« ตอบ #4 เมื่อ: 17 ตุลาคม 2019, 07:36:22 »

โค๊ด:
function remove_line($file, $remove) {
    $lines = file($file, FILE_IGNORE_NEW_LINES);
    foreach($lines as $key => $line) {
        if($line === $remove) unset($lines[$key]);
    }
    $data = implode(PHP_EOL, $lines);
    file_put_contents($file, $data);
}

remove_line('data.txt,1);
บันทึกการเข้า
BOS
ก๊วนเสียว
*

พลังน้ำใจ: 33
ออฟไลน์ ออฟไลน์

กระทู้: 459



ดูรายละเอียด เว็บไซต์
« ตอบ #5 เมื่อ: 18 ตุลาคม 2019, 02:52:46 »

โค๊ด:
function remove_line($file, $remove) {
    $lines = file($file, FILE_IGNORE_NEW_LINES);
    foreach($lines as $key => $line) {
        if($line === $remove) unset($lines[$key]);
    }
    $data = implode(PHP_EOL, $lines);
    file_put_contents($file, $data);
}

remove_line('data.txt,1);

ต่อยอดให้ครับ
โค๊ด:
<meta charset="utf8"/>
<a href="index.php">back</a>
<?php

$txtFile "data.txt";

if ( file_exists($txtFile) ) {
$txt file($txtFile);
//var_dump($txt);
$res '<form action="" id="myForm" method="post"><table>
<tr><th>ลำดับที่</th><th>รายละเอียด</th></tr>'
;
foreach ( $txt as $id => $row ) {
$id++;
$res.= '<tr>
<td>
<label>'
.$id.'</label>
<input type="hidden" name="line_'
.$id.'" value=""/>
<input type="checkbox" name="line_'
.$id.'" value="'.$row.'"/>
</td>
<td>'
.$row.'</td>
</tr>'
;
}
$res.= '<tr>
<td></td>
<td><input type="submit" value="DELETE" /></td>
</tr></table></form>'
;
echo $res;
}
function remove_line($file$remove) {
$lines file($fileFILE_IGNORE_NEW_LINES);
foreach ( $lines as $key => $line ) {
if( ($key+1) === $remove ) unset($lines[$key]);
}
$data implode(PHP_EOL$lines);
file_put_contents($file$data);
header"location: index.php" );
exit(0);
}
if ( isset($_POST) ) {
//echo "<pre>".print_r($_POST,true)."</pre>";
for ( $n=1$n<=count($_POST); $n++ ) {
if ( !empty($_POST["line_$n"]) ) {
remove_line$txtFile$n );
}
}
}

?>
« แก้ไขครั้งสุดท้าย: 18 ตุลาคม 2019, 02:54:18 โดย BOS » บันทึกการเข้า

BOS
ก๊วนเสียว
*

พลังน้ำใจ: 33
ออฟไลน์ ออฟไลน์

กระทู้: 459



ดูรายละเอียด เว็บไซต์
« ตอบ #6 เมื่อ: 18 ตุลาคม 2019, 13:08:23 »

แก้ใหม่ครับ โค้ดเดิมลบหลายแถวไม่ได้ ผมก็ลืมทดสอบ ถ้าปรับโค้ดเดิม ก็กลัวโค้ดจะยาว เลยเขียนใหม่ครับ ลองดูว่าได้ไหม
โค๊ด:
<?php
 
 $dataFile 
"data.txt";
 
 if ( 
file_exists($dataFile) ) {
  
$txt file($dataFile);
  
//echo "<pre>".print_r($txt,true)."</pre>";
  
$res '<form id="myForm" method="post"><table>
          <tr><th>no.</th><th>detail</th></tr>'
;
  foreach ( 
$txt as $id => $row ) {
   
$id++;
   
$res.= '<tr>
           <td>
            <label>'
.$id.'</label>
            <input type="hidden" name="line_'
.$id.'" value="'.$row.'"/>
            <input type="checkbox" name="line_'
.$id.'" value=""/>
           </td>
           <td>'
.$row.'</td>
          </tr>'
;
  }
  
$res.= '<tr>
           <td></td>
           <td><input type="submit" name="submit" value="DELETE" /></td>
          </tr></table></form>'
;
  echo 
$res;
 }
 
 if ( isset(
$_POST['submit']) ) {
  
//echo "<pre>".print_r($_POST,true)."</pre>";
  
$myPost array_values($_POST);
  
//echo "<pre>".print_r($myPost,true)."</pre>";
  
$getFile fopen$dataFile"w" ) or die("Unable to open file!");
  foreach ( 
$myPost as $i => $v ) {
   if ( 
$v!=="DELETE" ) {
    
fwrite($getFile$v);
   }
  }
  
fclose($getFile);
  
header"location: index.php" );
  exit(
0);
 }
 
?>

โค๊ด:
function remove_line($file, $remove) {
    $lines = file($file, FILE_IGNORE_NEW_LINES);
    foreach($lines as $key => $line) {
        if($line === $remove) unset($lines[$key]);
    }
    $data = implode(PHP_EOL, $lines);
    file_put_contents($file, $data);
}

remove_line('data.txt,1);

ต่อยอดให้ครับ
โค๊ด:
<meta charset="utf8"/>
<a href="index.php">back</a>
<?php

$txtFile "data.txt";

if ( file_exists($txtFile) ) {
$txt file($txtFile);
//var_dump($txt);
$res '<form action="" id="myForm" method="post"><table>
<tr><th>ลำดับที่</th><th>รายละเอียด</th></tr>'
;
foreach ( $txt as $id => $row ) {
$id++;
$res.= '<tr>
<td>
<label>'
.$id.'</label>
<input type="hidden" name="line_'
.$id.'" value=""/>
<input type="checkbox" name="line_'
.$id.'" value="'.$row.'"/>
</td>
<td>'
.$row.'</td>
</tr>'
;
}
$res.= '<tr>
<td></td>
<td><input type="submit" value="DELETE" /></td>
</tr></table></form>'
;
echo $res;
}
function remove_line($file$remove) {
$lines file($fileFILE_IGNORE_NEW_LINES);
foreach ( $lines as $key => $line ) {
if( ($key+1) === $remove ) unset($lines[$key]);
}
$data implode(PHP_EOL$lines);
file_put_contents($file$data);
header"location: index.php" );
exit(0);
}
if ( isset($_POST) ) {
//echo "<pre>".print_r($_POST,true)."</pre>";
for ( $n=1$n<=count($_POST); $n++ ) {
if ( !empty($_POST["line_$n"]) ) {
remove_line$txtFile$n );
}
}
}

?>
บันทึกการเข้า

Fallen
หัวหน้าแก๊งเสียว
*

พลังน้ำใจ: 162
ออฟไลน์ ออฟไลน์

กระทู้: 2,751



ดูรายละเอียด เว็บไซต์
« ตอบ #7 เมื่อ: 24 ตุลาคม 2019, 18:50:23 »

ได้แล้ววว ขอบคุณมากๆๆ ครับ  wanwan019

 wanwan017 wanwan017 wanwan017


แก้ใหม่ครับ โค้ดเดิมลบหลายแถวไม่ได้ ผมก็ลืมทดสอบ ถ้าปรับโค้ดเดิม ก็กลัวโค้ดจะยาว เลยเขียนใหม่ครับ ลองดูว่าได้ไหม
โค๊ด:
<?php
 
 $dataFile 
"data.txt";
 
 if ( 
file_exists($dataFile) ) {
  
$txt file($dataFile);
  
//echo "<pre>".print_r($txt,true)."</pre>";
  
$res '<form id="myForm" method="post"><table>
          <tr><th>no.</th><th>detail</th></tr>'
;
  foreach ( 
$txt as $id => $row ) {
   
$id++;
   
$res.= '<tr>
           <td>
            <label>'
.$id.'</label>
            <input type="hidden" name="line_'
.$id.'" value="'.$row.'"/>
            <input type="checkbox" name="line_'
.$id.'" value=""/>
           </td>
           <td>'
.$row.'</td>
          </tr>'
;
  }
  
$res.= '<tr>
           <td></td>
           <td><input type="submit" name="submit" value="DELETE" /></td>
          </tr></table></form>'
;
  echo 
$res;
 }
 
 if ( isset(
$_POST['submit']) ) {
  
//echo "<pre>".print_r($_POST,true)."</pre>";
  
$myPost array_values($_POST);
  
//echo "<pre>".print_r($myPost,true)."</pre>";
  
$getFile fopen$dataFile"w" ) or die("Unable to open file!");
  foreach ( 
$myPost as $i => $v ) {
   if ( 
$v!=="DELETE" ) {
    
fwrite($getFile$v);
   }
  }
  
fclose($getFile);
  
header"location: index.php" );
  exit(
0);
 }
 
?>

โค๊ด:
function remove_line($file, $remove) {
    $lines = file($file, FILE_IGNORE_NEW_LINES);
    foreach($lines as $key => $line) {
        if($line === $remove) unset($lines[$key]);
    }
    $data = implode(PHP_EOL, $lines);
    file_put_contents($file, $data);
}

remove_line('data.txt,1);

ต่อยอดให้ครับ
โค๊ด:
<meta charset="utf8"/>
<a href="index.php">back</a>
<?php

$txtFile "data.txt";

if ( file_exists($txtFile) ) {
$txt file($txtFile);
//var_dump($txt);
$res '<form action="" id="myForm" method="post"><table>
<tr><th>ลำดับที่</th><th>รายละเอียด</th></tr>'
;
foreach ( $txt as $id => $row ) {
$id++;
$res.= '<tr>
<td>
<label>'
.$id.'</label>
<input type="hidden" name="line_'
.$id.'" value=""/>
<input type="checkbox" name="line_'
.$id.'" value="'.$row.'"/>
</td>
<td>'
.$row.'</td>
</tr>'
;
}
$res.= '<tr>
<td></td>
<td><input type="submit" value="DELETE" /></td>
</tr></table></form>'
;
echo $res;
}
function remove_line($file$remove) {
$lines file($fileFILE_IGNORE_NEW_LINES);
foreach ( $lines as $key => $line ) {
if( ($key+1) === $remove ) unset($lines[$key]);
}
$data implode(PHP_EOL$lines);
file_put_contents($file$data);
header"location: index.php" );
exit(0);
}
if ( isset($_POST) ) {
//echo "<pre>".print_r($_POST,true)."</pre>";
for ( $n=1$n<=count($_POST); $n++ ) {
if ( !empty($_POST["line_$n"]) ) {
remove_line$txtFile$n );
}
}
}

?>
บันทึกการเข้า

หน้า: [1]   ขึ้นบน
พิมพ์