phpcodeigniter

How can I display specific row data in Codeigniter?


I have a bootstrap card with Read More Option in my CodeIgniter view. On clicking the button, url is displaying the selected card ID, but only the first row from the data base is fetching. Not sure, where I am doing it wrong.

Code for Controller starts here

public function getDetails()
{
        $query = $this->db->query("SELECT swo_brief_intro, swo_image_heading FROM services_offered");

        $row = $query->row();

        if (isset($row))
        {
                echo $row->swo_image_heading;
                echo $row->swo_brief_intro;
        }

        
}

Code for View starts here

<div class="row clearfix">

<?php 
    $query = $this->db->query("SELECT * FROM services_offered LIMIT 15");
    foreach ($query->result() as $row) {
        echo "<div class='col-lg-4 bottommargin-sm'>";
        echo "<div class='feature-box media-box fbox-bg'>";
        echo "<div class='fbox-media'>";
        echo "<a href='#'><img src='$row->swo_images' alt='Featured Box Image' style='height:250px; width:450px;'></a></div>";
        echo "<div class='fbox-content fbox-content-lg'>";
        $string = $row->swo_brief_intro;
        $string = word_limiter($string, 15);
        echo "<h3 class='nott ls0 font-weight-semibold'>$row->swo_image_heading<span class='subtitle font-secondary font-weight-light ls0'>$string</span></h3>";
        echo "<a href='Fetch/getDetails/{$row->id}' class='button-link border-0 color btn-edit'>Read More</a>";
        echo "</div>";
        echo "</div>";
        echo "</div>";


    }
?>

Please assist. Thanks


Solution

  • a) You forgot to add Id parameter in your function.

    b) As well as forgot to change query based on that.

    public function getDetails($id)
    {
    
        $row = $this 
                -> db
                -> select('swo_brief_intro, swo_image_heading')
                -> where('<put here id column name>', $id)
                -> limit(1)
                -> get('services_offered')
                -> row();
        
        if (isset($row))
        {
            echo $row->swo_image_heading;
            echo $row->swo_brief_intro;
        }
            
    }