I got a movie search website but when I search something it says 404 page not found.

The search view

The site is working under a framework called code igniter.

Here is the back-end code of search engine:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Search extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

    public function index($extra=array()) {
        $q = $this->input->get('q');
        if (!empty($q)) {
            redirect('search/' . $q);
        }
        redirect();
    }

    public function do_search() {
        $this->load->model('movies');
        $this->load->model('ssearch');
        $search_string = $this->uri->segment(2);

        $data['main_menubar']['categories'] = $this->movies->get_all_categories();
        $data['search']['results'] = $this->ssearch->search_for_movies($search_string);

        echo $this->load->view('includes/header', (isset($data['header']) ? $data['header'] : ''), true);
        echo $this->load->view('includes/main_menubar', (isset($data['main_menubar']) ? $data['main_menubar'] : ''), true);
        echo $this->load->view('search', (isset($data['search']) ? $data['search'] : ''), true);
        echo $this->load->view('includes/footer', (isset($data['footer']) ? $data['footer'] : ''), true);
    }

}

And the front-end code:

<div class="content_segment recentMovies clearfix">
    <div class="segment_title clearfix">
        Search: <?php echo urldecode($this->uri->segment(2)); ?>
    </div> <!-- segment_title -->
    <div class="inner_padding clearfix">
        <div class="horisontal_movie_list clearfix">
                <?php if(empty($results)) : ?>
                    <p>No movies found.</p>
                <?php endif; ?>
            <ul>
                <?php if(!empty($results)): ?>
                <?php foreach($results as $movie) : ?>
                <?php
                $categories = explode(',', $movie['categories']);
                $cat_ids = explode(',', $movie['categories_id']);
                $movie['categories'] = "";
                for ($i=0; $i < count($categories); ++$i) {
                    $movie['categories'] .= '<a href="'.site_url('view/category/'. $cat_ids[$i]).'">'.$categories[$i].'</a>, ';
                }
                $movie['categories'] = substr($movie['categories'], 0, -2);
                ?>
                <li>
                    <a href="<?php echo site_url('view/movie/'. $movie['id']); ?>">
                    <div class="imageWrapper">
                            <img src="<?php echo base_url() . 'img/movie_covers/' . $movie['cover_image']; ?>" alt="No cover!" />
                    </div>
                    </a>
                    <div class="information">
                        <h4 class="title"> <?php if($movie['favorite'] == 1) : ?><img src="<?php echo base_url(); ?>img/other/favorite_icon_small.png" alt="" /><?php endif; ?> <a href="<?php echo site_url('view/movie/'. $movie['id']); ?>"> <?php echo $movie['movie_title']; ?> </a> </h4>
                        <p class="meta"><?php echo $movie['categories']; ?> &nbsp;|&nbsp; <?php echo $movie['release_date']; ?> &nbsp;|&nbsp; First watched <?php echo date('F j, Y', $movie['watch_date']); ?></p>
                        <p class="description"><?php echo $movie['description']; ?></p>
                    </div>
                </li>
                <?php endforeach; ?>
                <?php endif; ?>
            </ul>
        </div>
    </div> <!-- list -->
</div> <!-- content_segment -->

And the search bar code in the menu bar:

<menu class="menubar clearfix">
    <a class="logo" href="<?php echo site_url(); ?>"></a>
    <ul class="clearfix">
        <li> <a href="<?php echo site_url() ?>"> Home </a> </li>
        <li> <a href="<?php echo site_url('view/movies'); ?>"> Movies </a> </li>
        <li class="categories_link">
            <a href="<?php echo site_url('view/category') ?>"> Categories </a>
            <ul>
                <?php foreach ($categories as $cat) : ?>
                <li><a href="<?php echo site_url('view/category/' . $cat['id']); ?>"><?php echo $cat['name']; ?></a></li>
                <?php endforeach; ?>
            </ul>
        </li>
        <li> <a href="<?php echo site_url('admin') ?>"> Admin area </a> </li>
    </ul>
    <div class="search">
        <form action="<?php echo site_url('search'); ?>" method="GET">
            <input type="search" name="q" value="" autofocus placeholder="Search" />
        </form>
    </div>
</menu>

I hope somebody finds a solution, I am stuck for 2 days.

Thanks.

Recommended Answers

All 2 Replies

Can you please confirm that the above backend controller is named search.php?

<form action="<?php echo site_url('search'); ?>" method="GET">

The form attribute action should be in the condtroller's object/object's method convention.

So, If we want to process the form above, the controller responsible for it will be the search controller, ===> instance of search object ==> utilizing the do_search() method.

There is a form helper in CI that can easily handle this.

$form = form_open('search/do_search',$attributes)
$submit = form_submit( 'submit', 'Search'),

pretty much equivalent to

<form action="search/do_search" class="attributes" id="attributes">

<input type="submit" name="submit" value="Search"/>

implemetnaton on the template file or front-end

<?php echo $form;?>

<!-- put the remaining of the form here -->
<?php echo $submit;?>

</form>

Here is an example application I just wrote in CodeIgniter, Twig template engine, Youtube API V2 to demonstrate the CI's form helper, pagination library, and the youtube library that I wrote.

Another one is the CI, Smarty, Youtube API V2. The same as above, but it is using Smarty template engine.

I also removed the domain.com/index.php/. so that the url is a tru MVC routing that will support domain.com/controller_name/method_name/

The linked demo above is pretty simple, but functional. I need to show evidence of some of the answers I posted in this forum. Just in case people would ask me for the proof.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.