1

I'm storing an SQL query result in a $data variable and passing it to a search_view like this from my model:

        $this->db->like('LOWER(title)', strtolower($query));
        $q = $this->db->get('questions');
        $data = $q->row_array();
        $this->load->view('search_view', array('data' => $data));

And I am trying to display all results in my view using following structure:

<?php if ($data['search_type'] == 'question') { 
        foreach ($data as $question) {
?>


<div class="question-result">
    <h2><?php echo $question['title']; ?></h2>
    <p>Posted by: <a href="#"><?php echo $question['username']; ?>username</a></p>
    <p>Date Posted: <?php echo $question['date']; ?></p>
</div>


<?php } } ?>

Where $data['search_type'] is something I need to check to see what type of search was performed, this bit is tested and works fine, no need to worry about it, however

I get an illegal string offset error for all variables like username, title and date that I use.

eddit* example dump:

array(7) { ["question_id"]=> string(1) "3" ["title"]=> string(19) "First Question Ever" ["content"]=> string(13) "Hello Content" ["date"]=> string(19) "2015-01-03 15:08:30" ["user_id"]=> string(2) "14" ["username"]=> string(4) "Ilya" ["search_type"]=> string(8) "question" } 

for some reason this is not returning more than one question, although there should be more for a search performed.

1
  • Preface your snippet with var_dump($data); to see what you actually got. Commented Jan 3, 2015 at 15:26

1 Answer 1

1

You don't need to use another foreach since its not in another dimension anymore. Once you've used ->row_array() only one row is returned.

<?php if ($data['search_type'] == 'question') { ?>

<div class="question-result">
    <h2><?php echo $data['title']; ?></h2>
    <p>Posted by: <a href="#"><?php echo $data['username']; ?>username</a></p>
    <p>Date Posted: <?php echo $data['date']; ?></p>
</div>

<?php } ?>

If you expect to have more results then use ->result_array() instead:

$data = $q->result_array();

Then on view:

<?php foreach ($data as $question): ?>
    <?php if ($question['search_type'] == 'question') { ?>
        <div class="question-result">
            <h2><?php echo $question['title']; ?></h2>
            <p>Posted by: <a href="#"><?php echo $question['username']; ?>username</a></p>
            <p>Date Posted: <?php echo $question['date']; ?></p>
        </div>
    <?php endif; ?>
<?php endforeach; ?>

Or might as well just add $this->db->where('search_type', 'question') so you wouldn't have to put an if inside the foreach in above loop.

Sign up to request clarification or add additional context in comments.

2 Comments

Fixed the issue where I was only getting one result now I can see both and formated correctly however after that I still get the errors :/
@Ilya could you elaborate those errors, which are working and which are not? however after that I still get the errors :/ its hard to guess on my side :)

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.