Welcome Guest, Not a member yet? Register   Sign In
News tutorial help needed
#1

[eluser]Atown[/eluser]
So 3 days latter, 4 attempts at the tutorial, and 2 pages of forum posting searches and I'm finally registered and posting for help. I've used Wordpress and Drupal for a long time but I'm really trying to learn more how this kind of stuff works and operates.

So here is the url: http://loadersequence.com/ci/index.php/news


routes:
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| URI ROUTING
| -------------------------------------------------------------------------
| This file lets you re-map URI requests to specific controller functions.
|
| Typically there is a one-to-one relationship between a URL string
| and its corresponding controller class/method. The segments in a
| URL normally follow this pattern:
|
| example.com/class/method/id/
|
| In some instances, however, you may want to remap this relationship
| so that a different class/function is called than the one
| corresponding to the URL.
|
| Please see the user guide for complete details:
|
| http://ellislab.com/codeigniter/user-guide/general/routing.html
|
| -------------------------------------------------------------------------
| RESERVED ROUTES
| -------------------------------------------------------------------------
|
| There area two reserved routes:
|
| $route['default_controller'] = 'welcome';
|
| This route indicates which controller class should be loaded if the
| URI contains no data. In the above example, the "welcome" class
| would be loaded.
|
| $route['404_override'] = 'errors/page_missing';
|
| This route will tell the Router what URI segments to use if those provided
| in the URL cannot be matched to a valid route.
|
*/
$route['default_controller'] = 'pages/view';
$route['404_override'] = 'errors/page_missing';

$route['news/create'] = '/news/create';
$route['news/(:any)'] = '/news/view/$1';
$route['news'] = '/news';

/*

scaffolding is just a temp term and can be named anything, its just a place holder
defining what we're doing

$route['default_controller'] = "pages";

*/

/* End of file routes.php */
/* Location: ./application/config/routes.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();
}


public function set_news()
{
$this->load->helper('url');

$slug = url_title($this->input->post('title'), 'dash', TRUE);

$data = array(
  'title' => $this->input->post('title'),
  'slug' => $slug,
  'text' => $this->input->post('text')
);

return $this->db->insert('news', $data);
}

Code:
<?php


class News extends CI_Controller {

public function view($page = 'index')
{

if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
  show_404();
}



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');

}









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');
}


public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');

$data['title'] = 'Create a news item';

$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'text', 'required');

if ($this->form_validation->run() === FALSE)
{
  $this->load->view('templates/header', $data);
  $this->load->view('news/create');
  $this->load->view('templates/footer');
  
}
else
{
  $this->news_model->set_news();
  $this->load->view('news/success');
}
}





}

View
Code:
<?php
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];


I'm extremely grateful for the help and really just kind of frustrated because I know I'm not that ignorant and everyone keeps saying these tutorials and userguide are awesome but I've been struggling with CI for a couple weeks and made it through most things but just this basic blog thing has gotten me stumped.
#2

[eluser]CroNiX[/eluser]
You posted everything except for what the actual problem is. What specifically isn't working?

Your routes shouldn't start with a /. It should just be "controller/method/params"
Code:
$route['news/(:any)'] = '/news/view/$1';  //'news/view/$1'
Probably not the problem but thought I'd mention it.

Also, if your /application dir isn't in its default location (same level as index.php), this line will need to be changed:
Code:
if ( ! file_exists('application/views/pages/'.$page.'.php'))
to something more dynamic like
Code:
if ( ! file_exists(APPPATH . 'views/pages/'.$page.'.php'))
#3

[eluser]Atown[/eluser]
[quote author="CroNiX" date="1349891690"]You posted everything except for what the actual problem is. What specifically isn't working?[/quote]

Thats what the link was for Sad

its error 500 and nothing operates outside of the index.php page
Quote:Your routes shouldn't start with a /. It should just be "controller/method/params"
Code:
$route['news/(:any)'] = '/news/view/$1';  //'news/view/$1'
Probably not the problem but thought I'd mention it.

fixed Smile
Quote:Also, if your /application dir isn't in its default location (same level as index.php), this line will need to be changed:
Code:
if ( ! file_exists('application/views/pages/'.$page.'.php'))
to something more dynamic like
Code:
if ( ! file_exists(APPPATH . 'views/pages/'.$page.'.php'))

could you elaborate why that works? not that i dotn believe that it solves stuff but I'm really trying to learn this stuff and honestly curious

still returning error 500
#4

[eluser]CroNiX[/eluser]
Sorry, I don't usually follow links. If it's not all in one easy location I generally skip it.

Look at the bottom of index.php where it defines CI's paths. APPPATH is dynamically generated and it won't matter where the /application folder is located (it's best to keep /application and /system out of root for security). If you hardcode the application path (and it's a relative path - like it is in the tutorial) and move your /application dir outside of root, it will break unless you use the predefined constants. It's just best to use the constants so you don't have anything to update (except index.php) if your application moves locations.

Based on that 404 page (I don't see 500 error) and the message it supplies, and the fact you get it if you also go to the home controller: http://loadersequence.com/ci/index.php and also the root of your site: http://loadersequence.com

I'd say it's your hosting doing this and nothing to do with CI. Are you sure your host is properly set up? Does it work from localhost?
#5

[eluser]Atown[/eluser]
[quote author="CroNiX" date="1349892673"]Sorry, I don't usually follow links. If it's not all in one easy location I generally skip it.
[/quote]

Yeah... I hear ya.

[quote author="CroNiX" date="1349892673"]
Look at the bottom of index.php where it defines CI's paths. APPPATH is dynamically generated and it won't matter where the /application folder is located (it's best to keep /application and /system out of root for security). If you hardcode the application path (and it's a relative path - like it is in the tutorial) and move your /application dir outside of root, it will break unless you use the predefined constants. It's just best to use the constants so you don't have anything to update (except index.php) if your application moves locations.[/quote]
makes sense.

[quote author="CroNiX" date="1349892673"]

Based on that 404 page (I don't see 500 error) and the message it supplies, and the fact you get it if you also go to the home controller: http://loadersequence.com/ci/index.php
[/quote] mmmm weird im not sure we're looking at the same thing. me clicking that link works just fine it shows:

Quote:CodeIgniter 2 Tutorial

Pages is currently acting as a category view like wordpress does. The controller for pages is what calls most of it, then the view for individual pages can be set as well. but the controller already calls a lot of things for it so there is no reason to be redundant. © 2011

Chrome with for the news section returns :
Quote:Server error
The website encountered an error while retrieving http://loadersequence.com/ci/index.php/news. It may be down for maintenance or configured incorrectly.
Here are some suggestions:
Reload this webpage later.
HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.

While firefox turns up a blank page.

Quote:I'd say it's your hosting doing this and nothing to do with CI. Are you sure your host is properly set up? Does it work from localhost?

my host is a small hosting co. and so I dont enough have cpanel. Its basically just phpmyadmin and ftp access. its $2.50 a mo though so i dont mind too much.

Localhost i dont have a test xamp server setup, just because my laptop is on its last 2gb.... I'm trying to hold out for black friday for more storage Sad



Quote:Based on that 404 page (I don’t see 500 error) and the message it supplies, and the fact you get it if you also go to the home controller: http://loadersequence.com/ci/index.php and also the root of your site: http://loadersequence.com

I was trying to make the /ci/ the root that I was working. I use loadersequence as a testing ground for a lot of different projects.
#6

[eluser]CroNiX[/eluser]
I get a 404 from 1&1 hosting on each of your links when I click on them. When going to your homepage, it shows a generic 1&1 landing page saying your domain was just set up and "click here" to see how to set your site up. It is indeed coming from 1&1 hosting. If you've recently set up the domain, it's possible it hasn't propagated via all of the nameservers yet.
#7

[eluser]Atown[/eluser]
hmmm then i'm just waiting on some propogation. Had to flush dns on my end so I could actually see those >_<
#8

[eluser]Atown[/eluser]
good morning Smile ! still morning on east coast i think too.....

Anywho, stuff has propogated and no difference on my end but hopefully you'll be able to see stuff now.

#9

[eluser]Aken[/eluser]
You need to make sure to call the parent construct in your models.

Code:
class News_model extends CI_Model {

public function __construct()
{
  parent::__construct();
  $this->load->database();
}

// The rest of your model code...
}

Your controller also looks malformed (missing curly braces to close methods). You are also defining the view() method more than once.

Also, if you have an .htaccess file to remove index.php, I suggest you take it out for the time being, as it is often the cause of 500 server errors. Otherwise it could be the PHP errors causing problems.
#10

[eluser]Atown[/eluser]
[quote author="Aken" date="1350013972"]You need to make sure to call the parent construct in your models.

Code:
class News_model extends CI_Model {

public function __construct()
{
  parent::__construct();
  $this->load->database();
}

// The rest of your model code...
}

Your controller also looks malformed (missing curly braces to close methods). You are also defining the view() method more than once.

Also, if you have an .htaccess file to remove index.php, I suggest you take it out for the time being, as it is often the cause of 500 server errors. Otherwise it could be the PHP errors causing problems.[/quote]

Awesome so it was a missing curly bracket and the fact that I called two views, even though the view was ($slug)... and I followed the tutorial.... how come the tutorial had me call that view in addition to the other?

I then got a invalid db error
"You have specified an invalid database connection group."

which I troubleshooted into the config/database.php and changing the
Quote:$active_group = 'default';
to default (it was set to "test" for whatever reason. I think it was from another issue on stackoverflow I was trying to use to help figure out my other issues.)

Thank you for your help everyone, I really appreciate getting back on track with this




Theme © iAndrew 2016 - Forum software by © MyBB