• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get ID from database when a specific td is clicked

#1
So here is a table where you can select a specific row. But how do I make it that you will see the ID of the row when clicked in the URL? Thanks in advancr 

echo "<table>";
        echo "<tr>";
        echo "<th>Check</th>";
        echo "<th>Klantnaam</th>";
        echo "<th>Project</th>";
        echo "<th>ProjectID</th>";
        echo "<th>Started</th>";
        echo "<th>Agenten</th>";
        echo "<th>Nog_benaderen</th>";
        echo "<th>Terugbellers</th>";
        echo "<th>BMNRUIT</th>";
        echo "<th>Parkeer</th>";
        echo "<th>Maxdatum</th>";
        echo "<th>Eff_duur</th>";
        echo "<th>InEff_duur</th>";
        echo "</tr>";
        echo "</table?";
        
        foreach ($query->result() as $row)
{
        echo "<tr>";
        echo "<td><input type='checkbox'></td>";
        echo "<td>" . $row->klantnaam . "</td>";
        echo "<td class='kiesproject'>" . $row->project . "</td>";
        echo "<td>" . $row->projectid . "</td>";
        echo "<td>" . $row->started . "</td>";
        echo "<td>" . $row->agenten . "</td>";
        echo "<td>" . $row->nog_benaderen . "</td>";
        echo "<td><a href='http://managementtool/index.php/Home/terugbel'>" . $row->terugbellen . "</td></a>";
        echo "<td>" . $row->BMNRUIT . "</td>";
        echo "<td>" . $row->parkeer . "</td>";
        echo "<td>" . $row->maxdatum . "</td>";
        echo "<td>" . $row->Eff_duur . "</td>";
        echo "<td>" . $row->InEff_duur . "</td>";
        echo "</tr>";
}
Reply

#2
Hi,
at first you MUST escape output data, e.g. html_escape($row->agenten) for string. For digits - (int)$row->projectid and use php native function rawurlencode for url's segment.

You can display row ID via bootstrap tooltip

foreach ($query->result() as $row_id => $row)
  ....
   echo "<td class=\"tootlip\" title=\"echo $row_id\"><a href='http://managementtool/index.php/Home/terugbel'>" . $row->terugbellen . "</td></a>";
  ....
}

Or may be I do not understand your question correctly?
Reply

#3
You problem is not clear to me but maybe this is a possible solution.

You can create a loop counter variable at top of the foreach loop and use this counter as row no in your code for example.


PHP Code:
$row_no 0;
foreach (
$query->result() as $row){
        
$row_no++;
 
       echo "<tr data-row-no="$row_now .">";
 
       echo "<td><input type='checkbox'></td>";
 
       echo "<td>" $row->klantnaam "</td>";
 
       echo "<td class='kiesproject'>" $row->project "</td>";
 
       echo "<td>" $row->projectid "</td>";
 
       echo "<td>" $row->started "</td>";
 
       echo "<td>" $row->agenten "</td>";
 
       echo "<td>" $row->nog_benaderen "</td>";
 
       echo "<td><a href='http://managementtool/index.php/Home/terugbel'>" $row->terugbellen "</td></a>";
 
       echo "<td>" $row->BMNRUIT "</td>";
 
       echo "<td>" $row->parkeer "</td>";
 
       echo "<td>" $row->maxdatum "</td>";
 
       echo "<td>" $row->Eff_duur "</td>";
 
       echo "<td>" $row->InEff_duur "</td>";
 
       echo "</tr>";

 then with the help of jquery you can find the parent element of the clicked TD and then get the data attribute and get row no. like this.
Reply

#4
Since your rows are displayed by a foreach loop, you can add the row's ID to the url right away:

PHP Code:
 echo "<td><a href='http://managementtool/index.php/Home/terugbel/" $row->id "'>" $row->terugbellen "</a></td>"

Personally, I prefer to avoid echoing out the complete html and php code, because it's difficult to read (and maintain), due to the numerous double and single quotes.
In your view, you can do this to make it more readable:

PHP Code:
<?php foreach($query->result() as $row) : ?>

<tr>
   <td><?= $row->klantnaam;?></td>
   <td><a href="http://managementtool/index.php/Home/terugbel/<?= $row->id;?>"><?= $row->terugbellen;?></a></td>
</tr>

<?php endforeach; ?>

<?= is shorthand for <?php echo
Reply

#5
Thanks for all the replies. But Wouter, I want to get the Id in the url by clicking the checkbox. I tried it but it isn't really functioning like how I wanted. How do I make that?
Reply

#6
(01-05-2018, 03:45 AM)FARUKB13 Wrote: Thanks for all the replies. But Wouter, I want to get the Id in the url by clicking the checkbox. I tried it but it isn't really functioning like how I wanted. How do I make that?

First, give the checkbox a css class:
Code:
<td><input type="checkbox" class="toggle_url"></td>

Then, put the url (including the id!) between <span></span> tags and also give the span tag a css class:
PHP Code:
<td><span class="terugbel_url" style="display: none;"><a href="http://managementtool/index.php/Home/terugbel/<?= $row->id;?>"><?= $row->terugbellen;?></a></span></td> 

Initally, the url isn't visible at all. I assume there's no point in clicking the url if it doesn't have the row's id, right?

Finally, write jQuery code to display the span element if the checkbox is checked, or hide it if the checkbox is unchecked.
Code:
<script>
$(document).ready(function(){
  $('.toggle_url').click(function(){
     if ( $(this).is(':checked') ) {
       $(this).parent('tr').find('.terugbel_url').show();
    }
    else {
       $(this).parent('tr').find('.terugbel_url').hide();
    }
  });
})
</script>

Now, if you check the checkbox, the corresponding url with the id will show up.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.