user3116846
user3116846

Reputation: 107

How to dynamically create a link from SQL results

I am trying to dynamically create a link from my SQL query output, but I am not sure how to uniquely get the values of the result that has been clicked. I used an anchor to call a method in my controller, but I don't know how to recognise what has been clicked.

Heres my view code where I send data from the controller and output it:

 <?php
        if (isset($q)) {
            foreach ($q as $row) {
                ?><a href="/CI/index.php/mainController/title"><?php echo $row->title; ?> </a><?php
                echo "<br>";
            }
        }
        ?>

Heres the method I call, and how I try to get the data using CIs built in methods but as it is not a form it doesn't work and I am not sure what else I can use to get the value:

public function title(){
        $id = $this->input->get('title');
        print_r($id . "hello");
    }

So I would like to know what is the best way to get a specific value from output of an SQL statement without having to use a form?

Upvotes: 0

Views: 153

Answers (3)

sjagr
sjagr

Reputation: 16502

You can pass values to your controller with the URI

public function title($id){
    // $id = $this->input->get('title');
    print_r($id . "hello");
}

and to link to the controller

echo '<a href="'.site_url('mainController/title/'.$row->title).'">';

Note you should use the URL helper to use the site_url() function.

I suggest you read the manual to learn about more of the tools that are made available to you from CodeIgniter's core, or else you're not really taking advantage of a framework.

Upvotes: 1

Allen King
Allen King

Reputation: 2516

You can assign an id to the A tag and then read the value of the A tag in jQuery/javascript when the page loads.

Upvotes: 0

paquino
paquino

Reputation: 464

You could append some sort of identifier in the url

i.e.

<a href="/path/to/controller.php?something=else">Click!</a>

Then the controller.php, you can retrieve the variable

$something = $_REQUEST['something']; //$something = 'else'

Upvotes: 2

Related Questions