0

everyone! I have a table!Table

Every button text and it's id in header Change staatus to should depends from which Staatus is right now. Like 1 row or if Staatus - "mitteakttivne" there should be Aktiivne and Kustutatud buttons and so on. Furthermore, if I click to one of those buttons, Staatus should change to clicked button status and buttons should be changed as in first part question.

Table code:

 <table>
            <tr>
                <th>Paketi nimi</th>
                <th>Hind</th>
                <th>Kirjeldus</th>
                <th>Staatus</th>
                <th>Change staatus to</th>
            </tr>
            <?php
                $paketi_list = get_paketi_list();
            ?>
            <?php foreach ($paketi_list as $pakett): ?>
                <tr>
                    <td><?= $pakett['nimi']?></td>
                    <td><?= $pakett['hetke_hind']?></td>
                    <td><?= $pakett['kirjeldus']?></td>
                    <td><?= $pakett['seisundi_liik']?></td>
                    <td><button>Mitteaktiivne</button><button>Kustutatud</button></td>
                    <td><a href="changePakett.php?pakett_id=<?=$pakett['pakett_id']?> ">change</a></td>
                </tr>
            <?php endforeach; ?>

get_pakei_list() function:

 function get_paketi_list(){
    db_connect();
    $query = "SELECT pakett.nimi, pakett.kirjeldus, pakett.hetke_hind, pakett.pakett_id, pakett.paketi_seisundi_liik_id AS seisundi_id, paketi_seisundi_liik.nimetus AS seisundi_liik FROM pakett INNER JOIN paketi_seisundi_liik ON pakett.paketi_seisundi_liik_id = paketi_seisundi_liik.paketi_seisundi_liik_id";
    $result = mysql_query($query);
    $res_array = array();

    $count = 0;

    while ($row = mysql_fetch_assoc($result)) {
        $res_array[$count] = $row;
        $count++;
    }
    return $res_array;
}

Can anybody give advices, some websites, decisions how to make what I need? I will be very thankfull if you help me

1 Answer 1

1

I hope I understood your question correctly. From what I see you have packets and you need to be able to change their statuses.

What I did:

  • I used your example, but kept only the necessary info: current status, possible statuses to be modified
  • I eliminated the change link on each line and moved it once, at the end
  • I introduced a notion of packet ID, which is bassicaly the key of the array $paketi_list

The code below is pretty self explanatory, but to sumarize:

  • I assume you have 3 statuses
  • I use jQuery to switch the status, whenever a person clicks on a button + I update a hidden field in a form so that the changed can be saved later
  • the form processing is done on the same page
  • I tested this using a dummy array with WAMP and it works just fine

If you want to test , just copy the next 3 sections in the same file - I had to split them in 3 because the site would not let me paste everything in one code region.

You can see how it works here: http://screencast-o-matic.com/watch/c2hqVcnazP

By the way, I think you got voted down because you didn't provide evidence that you tried something on your end, so next time try something and if it is not working, provide the code you tried out and you'll get comments on that.

PHP - variable initialization + form treatment

<?php

    //modify next line with all your statuses
    $arrayAllStatuses = array ('Status 1', 'Status 2', 'Status 3');
    /* used for example purposes - to replace with your own code */
    $paketi_list = array (
        array('seisundi_liik' => 'Status 2'), 
        array('seisundi_liik' => 'Status 3'), 
        array('seisundi_liik' => 'Status 1')
    );
    /* ========== */

    //this next part is the code responsible for treating the form submission
    //you can move this elsewhere (as long as you update the action attribute of the form tag)

    if (isset($_POST['change_status'])) {
        $numberOfLines = $_POST['numberOfLines'];
        foreach ($paketi_list as $currentId => $pakett) {
            if (isset($_POST['current_status_row_' . $currentId]) && $_POST['current_status_row_' . $currentId] != '') {
                echo 'Packet with ID = ' . $currentId . ' has a new status = ' . $_POST['current_status_row_' . $currentId];
                //I imagine you want to save this to a database - sanitize it first using mysqli_real_escape_string
                echo "<br />";
            }
        }
    }
?>

HTML part - the table and the form containing the modified statuses

<table border="1">
    <tr>
        <th>Status</th>
        <th>Change status to</th>
    </tr>
    <?php foreach ($paketi_list as $currentId => $pakett): ?>
    <tr>
        <td id="status_row_<?php echo $currentId; ?>"><?php echo $pakett['seisundi_liik']; ?></td>
        <?php 
            $remainingUnusedStatuses = array_diff($arrayAllStatuses, array($pakett['seisundi_liik'])); 
            //next line rebases array keys - so you don't end up with an array that has a key of 0 & 2 or 1 & 2
            $remainingUnusedStatuses = array_values($remainingUnusedStatuses);      
        ?>
        <td>
            <button id="tochange-first_row_<?php echo $currentId; ?>"><?php echo $remainingUnusedStatuses[0]; ?></button>
            <button id="tochange-second_row_<?php echo $currentId; ?>"><?php echo $remainingUnusedStatuses[1]; ?></button>
        </td>
    </tr>
    <?php endforeach; ?>
</table>

<form action="" method="post">
    <input type="hidden" name="numberOfLines" value="<?php echo sizeof($paketi_list); ?>">
    <?php foreach ($paketi_list as $currentId => $pakett): ?>
        <input type="hidden" id="current_status_row_<?php echo $currentId; ?>" name="current_status_row_<?php echo $currentId; ?>" value="">
    <?php endforeach; ?>
    <input type="submit" value="Save changes" name="change_status">
</form>

The JS - you need jQuery and some functions

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
    $(function() {
        $("button[id^='tochange']").click(function() {
            var currentRow = getCurrentRow($(this));
            var currentButtonId = $(this).attr('id');

            //get current status
            var currentStatus = $("td#status_row_" + currentRow).html();
            var futureStatus = $("button#" + currentButtonId).html();

            //switch the two statuses (visually)
            $("button#" + currentButtonId).html(currentStatus);
            $("td#status_row_" + currentRow).html(futureStatus);
            //save the change in the hidden form field
            $("input#current_status_row_" + currentRow).val(futureStatus);


            //$("#btnAddProfile").html('Save');

        });
    });

    function getCurrentRow(currentObject)
    {
        //do not use any numerical values in the id, except at the end, otherwise this won't work anymore
        var currentRow = ($(currentObject).attr('id').match(/\d+/));
        return currentRow;
    }
</script>

Hope this helps - it kept me busy for a while, but I needed a change from my current project.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much.I didn't expect that somebody will help me so much(you did all). Really, big big THANKS!!! Спасибо=)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.