Skip to content
Advertisement

Hyperlinks is not working right for search result titles

I am trying to show search results from two tables. I have used union to put them together. Right now they work, when I search for something and matches, it shows up from both of the tables but I am having problem with the hyperlinks. Right now, for the search results it is always redirecting to grants individual when clicked on the title even for the results from art table. Please help, how do I make sure it goes to the right details page.

Controller

function searchResult() {
    $keyword    =   $this->input->post('keyword');
    $data['results']    =   $this->products_model->searchResult($keyword);
    $this->load->view($this->header)
    $this->load->view('searchresults.php', $data);
    $this->load->view('footer');
}

View

<br>
<table>
<?php if (!$results){ ?>

  <h1> No results found. </h1>

<?php 
}

else { ?>
 <h2> Search Results </h2>
<?php

foreach($results as $row){ ?>

<br>
  <table class="table">
 <tr>
    <td><h2><a href="<?=  base_url()?>product/grantindividual/<?=$row->id?>"><?php echo $row->title?> 
  </h2></a></td>
</tr>

<tr>
    <td><h2><a href="<?=  base_url()?>product/artcallindividual/<?=$row->id?>"><?php echo $row- 
    >title?></h2></a></td>
</tr>
</table>
<?php }

 }
?>
<br>

Advertisement

Answer

You need to have a column to differentiate the results, no need for adding an actual column in table itself, use dummy columns

Change this

$this->db->select($this->db->escape('v_grants') .  ' AS source, id, Title');

To

$this->db->select($this->db->escape('v_grants') .  " AS source, id, Title, 'grant' as type ");

Change this

$this->db->select($this->db->escape('v_art_call') . ', id, Title');

To

$this->db->select($this->db->escape('v_art_call') . ", id, Title, 'art' as type ");

Then you can easily use if-else statement to display the proper row (grant/art)

<table class="table">

    foreach($results as $row){ ?>
        if($row->type == 'grant'){ ?>

                <tr>
                 <td><h2><a href="<?=base_url()?>product/grantindividual/<?=$row->id?>"><?php echo $row->title?> 
                    </h2></a>
                 </td>
                </tr>

        <?php }elseif($row->type == 'grant') { ?>

                <tr>
                  <td><h2><a href="<?=base_url()?>product/artcallindividual/<?=$row->id?>"><?php echo $row->title?></h2></a>
                  </td>
                </tr>

        <?php } ?>

    <?php } ?>

</table>
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement