Welcome Guest, Not a member yet? Register   Sign In
News Section Tutorial - 500 Internal Server Error
#1

[eluser]imwithsam[/eluser]
I'm brand new to CodeIgniter and am trying to work my way through the tutorials on my localhost. I made it through the Static Pages tutorial just fine, but am stuck on the News Section tutorial. I keep receiving a 500 Internal Server error. I've looked over my code multiple times, and am missing what I'm doing wrong. Does anyone have any suggestions? FYI, I'm running my application and system directories outside my root. Thanks in advance!

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

$route['news/(:any)'] = "news/view/$1";
$route['news'] = "news";
$route['default_controller'] = "pages/view";
$route['(:any)'] = "pages/view/$1";
$route['404_override'] = '';

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

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'mcn';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

news_model.php model
Code:
<?php
class News_model extends CI_Model {
  public function __construct()
  {
    $this->load->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();
  }
}

news.php controller
Code:
<?php
class News extends CI_Controller {
  public function __construct()
  {
    parent::__construct();
    $this->load->model('news_model');
  }

  public function index()
  {
    $data['news'] = $this->news_model->get_news();
    $data['title'] = 'News archive';

    $this->load->view('templates/header', $data);
    $this->load->view('news/index', $data);
    $this->load->view('templates/footer', $data);
  }

  public function view($slug)
  {
    $data['news_item'] = $this->news_model->get_news($slug);

    if (empty($data['news_item']))
    {
      show_404();
    }

    $data['title'] = $data['news_item']['title'];

    $this->load->view('templates/header'), $data);
    $this->load->view('news/view', $data);
    $this->load->view('templates/footer', $data);
  }
}

index.php view
Code:
<?php foreach ($news as $news_item): ?>

  <h2>&lt;?php echo $news_item['title'] ?&gt;</h2>
  <div class="main">
    &lt;?php echo $news_item['text'] ?&gt;
  </div>
  <p><a href="news/&lt;?php echo $news_item['slug'] ?&gt;">View article</a></p>

</?php endforeach ?&gt;

view.php view
Code:
&lt;?php
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];
#2

[eluser]InsiteFX[/eluser]
If your system and application directories are out side of your root then you need to edit the index.php

Code:
$system_path = '../system';

$application_folder = '../application';

That should give you an idea, mine are setup like this:

Code:
system
application
public_html
-- assets
-- .htaccess
-- index.php
#3

[eluser]imwithsam[/eluser]
Thanks for the quick reply, InsiteFX! I forgot to include my index.php. My application and system folders are in a "ci_200_mcn" folder on the same level as my htdocs. (index.php is in an "mcn" folder in my htdocs.) It ran fine on the Static Pages tutorial.

index.php
Code:
$system_path = '../../ci_220_mcn/system';
$application_folder = '../../ci_220_mcn/application';
#4

[eluser]InsiteFX[/eluser]
The ../ means up one directory, I do not think that you need the ci_220_mcn directory name in it

Take a look at mine above, where index.php is in my public_html folder.
#5

[eluser]imwithsam[/eluser]
I think I need the ../../ci_200_mcn/ in my path because of the way I set up my directory structure. I do not think the Static Pages tutorial would have worked without this being set up correctly. Hopefully this will shed some light on how I'm set up (for better or for worse):

Code:
ci_200_mcn
-- system
-- application
htdocs
-- mcn
  -- .htaccess
  -- index.php

But just to be thorough, I reset everything to be like your directory structure and I still have the same issue so I do not think the problem has to do with my application and system paths. Is there anything else you can think of?
#6

[eluser]InsiteFX[/eluser]
The 500 is a server error which means it is not finding your server which will happen is the index.php file is not setup correct.

Show me you directory layout and I should be able to figure it out for you.

Or you can download TeamViewer and Skype and we can fix it online
#7

[eluser]imwithsam[/eluser]
Wow, thanks for all the help! I sent you a request over Skype.

So, like I mentioned, I changed my directory structure to be more like yours. It now looks like this:
Code:
application
system
htdocs
-- index.php

and my index.php is now:
Code:
$system_path = '../system';
$application_folder = '../application';
#8

[eluser]InsiteFX[/eluser]
If it is still not working then it may because you are outside your web root htdocs.

mine is setup like this:

Code:
htdocs <- this is your web root
ci22 <- sub-folder
-- application
-- system
-- public_html
---- .htaccess
---- index.php
#9

[eluser]imwithsam[/eluser]
Ok, I moved my application and system folders into htdocs, and changed the paths in index.php. Still getting the same result.
#10

[eluser]CroNiX[/eluser]
I'd get it working by using the default settings (like how the structure is when you unzip it). Test it in the browser and make sure you see the default welcome screen. Then start altering it. I'd also use full paths for /system and /application if you move them.
Windows: "C:\path\to\system"
Linux: /path/to/system

But before you do anything, I'd check your php and apache error logs and see what it says about the 500 error since you already had it working and then it stopped after you were developing. 500 errors can also be caused by a bad .htaccess or apache config.




Theme © iAndrew 2016 - Forum software by © MyBB