CodeIgniter Forums
creating HTML and PHP hyperlink text - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: creating HTML and PHP hyperlink text (/showthread.php?tid=56317)



creating HTML and PHP hyperlink text - El Forum - 12-10-2012

[eluser]Volkof[/eluser]
Hi there,

I was trying to create a hyperlink from a bunch of text that was retrieved from database. What's suppose to happen was a few lines of text will be retrieved and display and when user click on text line 1, it will return me the ID of that text (let's just say line 1 ID = 1, Line 2 ID =2)


So I did this
Code:
<div id="container">
   <h1>Select a Faculty</h1><br>
    <a href=".&lt;?php $row-&gt;facultyID ?&gt;.">
    &lt;?php
    foreach($faculties as $row){
     echo '<b>'.$row->facultyName.'</b><br/>';
     echo '<br/>';    
    }
    ?&gt;
    </a>
  </div>

So it display rows of Faculty Names, and when user click on one of them, it will return the Faculty ID.
I'm not sure what is the correct way to do this, so any advise?

Thanks


creating HTML and PHP hyperlink text - El Forum - 12-10-2012

[eluser]pickupman[/eluser]
Its you may have one of the lines of code out of order.
Code:
<div id="container">
   <h1>Select a Faculty</h1><br>    
    &lt;?php
    foreach($faculties as $row){
     echo '<a class="faculty_'.$row-&gt;facultyID.'" href="'. $row-&gt;facultyID.'">';
     echo '<b>'.$row->facultyName.'</b><br/>';
     echo '<br/>';    
    }
    ?&gt;
    </a>
  </div>

If you are wanting to return an id on click, but not change the page, you will need to use javascript. Likely jQuery to attach to the click event. Then use a regular expression to get the id from the class string.

Code:
$("a[class^='faculty_']").click(function(event){
   var mystr = $(this).attr('class');
   var id = mystr.replace(/mystr/ig, 'faculty_');
   alert(id);
    event.preventDefault();
});

Didn't test javascript, but should be close enough.



creating HTML and PHP hyperlink text - El Forum - 12-10-2012

[eluser]Volkof[/eluser]
Thanks, Im not familiar with merging php and html.

It is close to what I wanted. The texts are now hyperlinked. Now when User click on it, it should return the facultyID to the Controller class.

How do I retrieve data from View to Controller?


creating HTML and PHP hyperlink text - El Forum - 12-10-2012

[eluser]pickupman[/eluser]
Quote:How do I retrieve data from View to Controller?
Usually through the url or use ajax.


creating HTML and PHP hyperlink text - El Forum - 12-10-2012

[eluser]Volkof[/eluser]
How do I do it through URL?


creating HTML and PHP hyperlink text - El Forum - 12-11-2012

[eluser]pickupman[/eluser]
Don't be afraid to checkout the [url="http://ellislab.com/codeigniter/user-guide/general/urls.html"]user guide[/url] to better understand the [url="http://ellislab.com/codeigniter/user-guide/libraries/uri.html"]URI class[/url].