0

This will be my last question.

I am getting no rows as an result and i dont know what im doing wrong, plus i want to return multiple rows,

like in mysql where i can just use the argument like so i can return multiple rows and it would be a helpful query for searches as most searches are not accurately written or many. for example, i want to search a book from author john but john has 5 books and if i use first_row it would only return the first book and there might be a chance that the one im looking for would be at the 2nd or 3rd or 4th or 5th row.

Im new to ci, ive read a guide wherein i can use num_rows() but im not sure how to use it yet as ive not seen an example. Ive searched but to my disappointment, i found nothing that satisfies my needs.

Here is my html:

<?php echo form_open('bookstore/booksearch'); ?>
                                <table>
                                    <tr>
                                        <td>
                                    <label for="searchid">Search:</label>
                                        </td>
                                        <td>
                                    <input type="text" size="15" name="searchval" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                    <label for="searchtype">Type:</label>
                                        </td>
                                        <td>
                                    <select name="searchtype">
                                        <option value="book_id">Id</option>
                                        <option value="book_author">Author</option>
                                        <option value="book_name">Title</option>
                                    </select>
                                        </td>
                                    </tr>
                                    <tr>
                                    <input type="submit" name="submit" value="Search" />
                                    </tr>
                                </table>
                                <?php echo form_close(); ?>

                            </ul>
                        </div>
                        <div>

<table cellpadding='5'>
<th>Book ID</th>
<th>Title</th>
<th>Author</th>
<th>Released Year</th>
<th>ISBN</th>

<?php
if(isset($books)) : foreach($books as $book) :  
?>      
        <tr>
        <td><?php echo $book['book_id'] ?></td>
        <td><?php echo $book['book_name'] ?></td>
        <td><?php echo $book['book_author'] ?></td>
        <td><?php echo $book['book_year'] ?></td>
        <td><?php echo $book['book_isbn'] ?></td>
        </tr>
<?php
    endforeach;
?>

<?php
else :
?>
<h5>No records</h5>
<?php
endif;
?>
</table>

Controller:

public function booksearch()
    {
    if($_POST['submit'])
        {
        $col= $this->input->post('searchtype');
        $val= $this->input->post('searchval');

        $data['book'] = $this->books_model->showbook($col,$val);

        }
        $this->load->view('showbooks',$data);
    }

and my model:

public function showbook($col, $searchid)
{
        $this->db->select()->from('books')->where(array($col=>$searchid));
        $query=$this->db->get();
        return $query->first_row('array');
}

I have posted a question earlier but with similar codes, I resolved it since i only missed the name attribute, small things you dont expect to be the problems can really waste your time. I Hope i didnt miss anything this time or it would be a shame on me if it happened twice. Thanks for your patience!

2

1 Answer 1

1

Replace:

public function showbook($col, $searchid)
{
    $this->db->select()->from('books')->where(array($col=>$searchid));
    $query=$this->db->get();
    return $query->first_row('array');
}  

To:

public function showbook($col, $searchid)
{
    $this->db->select()->from('books')->where(array($col=>$searchid));
    $query=$this->db->get();
    return $query->result_array();
}
Sign up to request clarification or add additional context in comments.

1 Comment

@user1891608 yes its returns all the results as an array which met with where condition.

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.