Jay
Jay

Reputation: 31

edit two columns in table

I am making a site where they want to edit a table with two columns,

I have followed this tutorial (link below)

http://www.jqueryajaxphp.com/live-table-edit-using-jquery-ajax-and-php-twitter-bootstrap/

I can get one column working fine, it edits and saves back to the data base, I cannot get the second one to edit.

I have made it an include so people can see the table on the site, but given them an admin page so he can edit.

http://www.medinaswindon.co.uk/med/admin/

I need to make the what’s on column editable and saveable to the db,

the code I am using is

<?php
include("connect.php");
?>

    <link href="assets/css/bootstrap.min.css" rel="stylesheet" type="text/css">
    <link href="assets/css/custom.css" rel="stylesheet" type="text/css">


    <div class="container">
      <div class="row">
          <table class= "table table-striped table-bordered table-hover">
                        <thead>
                            <tr>


                            <th colspan="1" rowspan="1" style="width: 180px;" tabindex="0">DAY /     DATE</th>

                            <th colspan="1" rowspan="1" style="width: 220px;"          tabindex="0">WHAT’S ON</th>

                            <th colspan="1" rowspan="1" style="width: 288px;" tabindex="0">Status</th>
                        </tr>
                    </thead>

                    <tbody>
                    <?php
                    $query = mysql_query("select * from information");
                    $i=0;
                    while($fetch = mysql_fetch_array($query))
                    {
                        if($i%2==0) $class = 'even'; else $class = 'odd';

                        echo'<tr class="'.$class.'">

                            <td><span class= "xedit" id="'.$fetch['id'].'">'.$fetch['name'].'</span></td>

                            <td>'.$fetch['details'].'</td>

                            <td>'.$fetch['status'].'</td>
                        </tr>';                         
                    }
                    ?>
                    </tbody>
    </table>

any ideas?

Upvotes: 1

Views: 119

Answers (1)

Jonas D
Jonas D

Reputation: 101

Try to change this part:

<td><span class= "xedit" id="'.$fetch['id'].'">'.$fetch['name'].'</span></td>
<td>'.$fetch['details'].'</td>
<td>'.$fetch['status'].'</td>

To this:

<td><span class= "xedit" id="n'.$fetch['id'].'">'.$fetch['name'].'</span></td>
<td><span class= "xedit" id="d'.$fetch['id'].'">'.$fetch['details'].'</span></td>
<td>'.$fetch['status'].'</td>

That should make the details field editable as well, and then you can go into your PHP code and submit the data based on the id.

I have not seen the PHP code used to save the information but this should illustrate how you can sort the information based on name or details field:

if($_GET['id'] and $_GET['data']) { 
    $type = substr($_GET['id'],0,1);
    $id = substr($_GET['id'],1);
    $data = $_GET['data']; 
    if ($type == 'n')
        mysql_query("update information set name='$data' where id='$id'");
    if ($type == 'd')
        mysql_query("update information set details='$data' where id='$id'");
} ?>   

On a sidenote you should refrain from using the mysql_query function as it is deprecated, look at Mysqli or PDO

Upvotes: 1

Related Questions