0

find and delete specific line from text file

cart.txt (tab delimated)

guiburi PID001  KARLSTAD    Sofa    2348    2   4696
guiburi PID002  BESTA   Cabinet 1656    32  52992
guiburi PID009  PAX Wordrobe    1794    2   3588

deleteitem.php

if(isset($_GET['pname']))
            {
                $pname = trim($_GET['pname']);  

            }

Info

pname is sent from a another file using url and it used to match with the 3rd colum of the cart file and then delete the line.write back to the same file

ive tried many ways but no luck. any help? thanks

Edited by guiburi: edit 1

2
Contributors
3
Replies
5
Views
4 Years
Discussion Span
Last Post by urtrivedi
0
<?php
        if(isset($_GET['pname']))
        {
            $pname = trim($_GET['pname']);  

            $string = file_get_contents('../data/cart.txt');
            $exploded = explode("\n", $string);

            foreach ($exploded as $key => $val)
            {
                if (stristr($val,$pname))
                {
                    $returned[$key] = $val;
                }


                $string_with_removed = implode("\n", $returned);
                file_put_contents('../data/cart.txt', $string_with_removed);
            }


            header('refresh:2;url=../cart.php');
        }

        echo "<p>Deleting Item</p>";
?>

that the code i have but its not working

0

if you say its not working, what does it mean.
yOu get error, or you get some different output or what happening, tell exactly.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.