• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
creating HTML and PHP hyperlink text

#1
[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

#2
[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.

#3
[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?

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

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

#6
[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].


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


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