SanjanaMathew
SanjanaMathew

Reputation: 1

delete a particular row from session array

I am a beginner in php. I want to know how a particular row in a php session array can be deleted(not from the database, but from the page only). I have a table namely, issues having columns issue_id & issue_descrp. The page is displayed with a table in which each row contains issue and its corresponding id. Each row contain a delete button too. What I want is to delete the corresponding row from the page when I click the button. This is my php code:

    <?php

    foreach($_SESSION['meeting_issues'] as $meeting_issues)
        {
            $query="select issue_id,issue from issues where issue_id='$meeting_issues'";
            $result=$_SESSION['connection']->query($query) or die (mysql_error());
            while($row = $result->fetch_assoc())
            {?>
                <?php $issue_id=$row['issue_id']; ?>
                <tr><td><?php echo $row['issue_id']; ?></td><td><?php echo $row['issue']; ?></td><td><input type="button" name="<?php echo $row['issue_id']; ?>" id="button" value="Remove"/></td>
                </tr>
                <?php
            }       
        }
        ?>

Hope my question is clear. Please help me. Thanks in advance.

Upvotes: 0

Views: 1256

Answers (2)

Deep Frozen
Deep Frozen

Reputation: 2075

To remove a row on the page itself, you will need Javascript or jQuery. jQuery is advised because of all the possibilities it gives and it is easier to use than normal Javascript.

jQuery:

$("#button").parents("tr:closest").remove();

Javascript:

document.getElementById('button').parentNode.parentNode.parentNode.removeChild(document.getElementById('button').parentNode.parentNode);

As you can see, jQuery is alot faster and more easy to type.

You are using an ID for the buttons, but the ID is always the same. I recommend using classes for this, because an ID should be unique on a page.

jQuery website

Upvotes: 1

Hurricane Hamilton
Hurricane Hamilton

Reputation: 574

use unset to delete array elements such as those in $_SESSION

http://php.net/manual/en/function.unset.php

do not delete the whole session this way, use this instead

http://php.net/manual/en/function.session-unset.php

Upvotes: 2

Related Questions