• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
capture id on click

#1
I know this is a well documented topic because I've read on it in the past, but I can't seem to remember the right words to search. I don't do databases but work has required me to figure it out so sorry for the dumb question.

I got a modal that has a list of topics, when you click one it takes you to the syllabus page then the course and then lesson list. What I am trying to remember is how to capture and pass the id of the clicked item. I don't mind doing the homework if one of you could point me in right direction.
Reply

#2
This has nothing to do with CodeIgniter. It's basic JavaScript programming. Have you search for the onClick event? You can also use the .click() function if you use jQuery in your project.
Test your translation files with Translation Tester
Reply

#3
I've got the button onclick working and the ID is hard coded in the function call. I am looking for how to capture the ID of clicked button and pass it in the function call.
Reply

#4
If you use jQuery in your project, you can use the attr() function to get the value of an attribute:

Code:
$(".btn").click(function () {
 alert($(this).attr('id'));
});
Test your translation files with Translation Tester
Reply

#5
One of the major advantages of CodeIgniter is the way it handles URL's.
In your list with items, each item contains a link to show a certain page, I guess. If you include the id of a topic, you can refer to the topic. E.g. in your view:
PHP Code:
<ul>
<?
php
Foreach ($record as $record) {
 echo 
'<li>' anchor('syllabus/show_item/' $record['id'], $record['title']) . '</li>';
}
?>
</ul> 

In this example, you have a controller named "syllabus" and inside it a method named "show_item", which accepts the id as a parameter, like this:
PHP Code:
public function show_item($id)
{
  
//do whatever you want with the id...

Reply

#6
includebeer

This captures the div id, I'm actually after the DB ID. Thank you for effort though.

Wouter60

This did what I was going for. Thank you. I keep not realizing there are codeigniter ways instead of html ways.

One follow on question:
I had <a><li>words</li></a>
Applying the anchor above the list made the whole button clickable. Your method puts the link on the list so now only the words are clickable. Is there a way using the codeigniter way to make the whole containing element clickable?
Reply

#7
(01-03-2016, 08:58 AM)ryan Wrote: This captures the div id, I'm actually after the DB ID. Thank you for effort though.

Then your question is not clear. You asked "how to capture and pass the id of the clicked item". Show us the code that's not working and what you're trying to do.


Quote:This did what I was going for. Thank you. I keep not realizing there are codeigniter ways instead of html ways.

What do you mean, CI way vs. HTML way? Huh



Quote:One follow on question:
I had <a><li>words</li></a>
Applying the anchor above the list made the whole button clickable. Your method puts the link on the list so now only the words are clickable. Is there a way using the codeigniter way to make the whole containing element clickable?

Again, CI has nothing to do with that. This is a CSS problem. Put the anchor tag (a) inside the list tag (li), make your link "display: block", and then the whole line will be clickable. Also, the way you do it, I'm pretty sure it's not valid HTML (putting the anchor outside list tag).
Test your translation files with Translation Tester
Reply

#8
I don't see how this captures the div id instead of the id in your database.
In my example, I send $data['records'] from the controller to the view.
The view recognizes the array $records.
So the id of each record is the id in the database.

Concerning your hyperlinks, there is not really a CI way, but CI provides a helper function to build hyperlinks very easy:
anchor($uri, $title [,$attributes]);

The $title is surrounded by the <a> and </a> tags. So if you include <li> and </li> in the title, the effect is the same as your code with <a><li>words</li></a>.
Reply

#9
My bad for confusion, I thought I had both your names before the response. I went back and edited it. includebeer, I will agree that I should have put db id in the original post. Keep in mind, I am learning this on the fly and I am more comfortable in photoshop than I am in code so I'm pretty confused myself. I will see about doing code snippets in the future but it is difficult because I am working in a standalone environment.

Wouter60, your first solution and second suggestion both worked great. my final function looks like this.

Code:
<?php
foreach($results as $row)
{
    echo '<a>'.anchor('controllerName/syllabus/'.$row->ID, '<li>button label</li>').'</a>';
}
?>

I had to change $row['id'] to row->id because it told me it wasn't an array.
As for <a><li></li></a> being bad semantics, is there a better way of making the entire list item clickable instead of just the text? It works great and I see it done a lot but I'm all for doing things the right way if possible. Perhaps <li><button>label</button></li> would work.
Reply

#10
Just to mention that you should really have the <a> tag inside the <li> tag and not the other way around. If that breaks your css intent then you need to alter your css.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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