1

I have a table named "news" containing the following details: id, title, slug, text

As such, I used the following code to retrieve the whole "news" table from my database.

public function get_news($slug = FALSE)
        {
            if ($slug === FALSE)
            {
                $query = $this->db->get('news');
                return $query->result_array();
            }

            $query = $this->db->get_where('news', array('slug' => $slug));
            return $query->row_array();
        }

Displaying the result

<?php foreach ($news as $news_item): ?>

                <h3><?php echo $news_item['title']; ?></h3>
                <div class="main">
                        <?php echo $news_item['text']; ?>
                </div>
                <p><a href="<?php echo site_url('news/'.$news_item['slug']); ?>">View article</a></p>

        <?php endforeach; ?>

How should I go about to change the code in order to get the query result of "select title, text from news;"?

1 Answer 1

2

Try this

$query = $this->db->select('title, text,')->from('news')->get();

and with where:

 $query = $this->db->select('title, text,')->from('news')->where('slug', $slug)->get();
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help. May I know if I want to perform this query "select title from news where text='me'" does it correspond to the query statement below? $query = $this->db->select('title')->from('news')->where('text', 1)->get();
Yes, but at where('text', 1) why 1? Should be where('text', 'me'). Also please mark my answer as valid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.