CodeIgniter Forums
Simple question/issue...new to CodeIgniter - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Simple question/issue...new to CodeIgniter (/showthread.php?tid=51995)

Pages: 1 2


Simple question/issue...new to CodeIgniter - El Forum - 05-25-2012

[eluser]codelogic[/eluser]
Hi all,

I am very new to CodeIgniter, but I would consider myself pretty skilled with PHP.

Since I am new, I thought a good place to start would be the User Guide tutorial for creating a News Page. http://ellislab.com/codeigniter/user-guide/tutorial/news_section.html

However, I am having a very annoying issue that I cannot get fixed. When I fill out my news form (with a title and text) and hit the "Create news item" button on the form, it redirects me to http://[my-site-url]/CI/index.php/news/create/[my-site-url]/CI/index/news/create/ with a 404 error. It seems to be copying and pasting my URL into the address bar without deleting the previous address.

Could it be that I am not connecting to the database correctly? I am almost positive that I am connecting correctly as I have quadruple checked my login info to make sure it was right.

Here is my code it's fairly simple and right from the tutorial... all I have is a 1 model, 4 views, and 1 controller.

If anyone can figure out why this weird mistake is happening with my URL I would be very happy. Thanks!!


Edit: Fixed!! Had to add http:// to my base_url.

Edit 2: Running into a second problem where I want to view a single article, however it redirects me to index.php/news/news/(my article) instead of index.php/news/(my article).

index.php creates the "view article" link.



Model: news_model.php

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


Views: index.php, create.php, view.php, success.php


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

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

&lt;?php endforeach ?&gt;

create.php
Code:
<h2>Create a news item</h2>

&lt;?php echo validation_errors(); ?&gt;

&lt;?php echo form_open('news/create') ?&gt;

<label for="title">Title</label>
&lt;input type="input" name="title" /&gt;&lt;br />

<label for="text">Text</label>
&lt;textarea name="text"&gt;&lt;/textarea><br />

&lt;input type="submit" name="submit" value="Create news item" /&gt;

&lt;/form&gt;


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

success.php
Code:
Success!


Controller: news.php

Code:
&lt;?php
class News extends CI_Controller {

public function __construct()
{
  parent::__construct();
  //load our model
  $this->load->model('news_model');
}
//The code below gets all news records from the model and assigns it to a variable.
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');
}
//individually view news items.
public function view($slug)
{
  $data['news'] = $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');
}
}



Simple question/issue...new to CodeIgniter - El Forum - 05-25-2012

[eluser]Ayeyermaw[/eluser]
Can you also show the section of your view that shows the &lt;form&gt; open tag.


Simple question/issue...new to CodeIgniter - El Forum - 05-25-2012

[eluser]codelogic[/eluser]
[quote author="Ayeyermaw" date="1337981233"]Can you also show the section of your view that shows the &lt;form&gt; open tag.[/quote]

Yeah, it's under create.php in my post Smile.


Simple question/issue...new to CodeIgniter - El Forum - 05-25-2012

[eluser]Ayeyermaw[/eluser]
lol sorry - didnt see it. may have had a wee bit too much sun today Wink


Simple question/issue...new to CodeIgniter - El Forum - 05-25-2012

[eluser]codelogic[/eluser]
[quote author="Ayeyermaw" date="1337981527"]lol sorry - didnt see it. may have had a wee bit too much sun today Wink[/quote]

No worries Smile. I was thinking it may be some kind of error with form_open(). Also... after checking my database, it is EMPTY. That means the form is not submitting data to my database either, as well as displaying a messed up copied URL as above.


Simple question/issue...new to CodeIgniter - El Forum - 05-25-2012

[eluser]Ayeyermaw[/eluser]
I'll have a think but nothing jumps out at me right now.
If you haven't already then edit your application/config/config.php and set:
Code:
$config['log_threshold'] = 4;
This will generate full logging of errors so you then review what it says in the logfile. That's usually your biggest clue


Simple question/issue...new to CodeIgniter - El Forum - 05-25-2012

[eluser]codelogic[/eluser]
[quote author="Ayeyermaw" date="1337981955"]I'll have a think but nothing jumps out at me right now.
If you haven't already then edit your application/config/config.php and set:
Code:
$config['log_threshold'] = 4;
This will generate full logging of errors so you then review what it says in the logfile. That's usually your biggest clue[/quote]

I have changed that to 4, but now when I look at application/logs, all it shows is index.html, with a "Directory access is forbidden" message.


Simple question/issue...new to CodeIgniter - El Forum - 05-25-2012

[eluser]InsiteFX[/eluser]
Sounds like your base url or routes are messed up someplace.

Show your base url and routes file.



Simple question/issue...new to CodeIgniter - El Forum - 05-25-2012

[eluser]Ayeyermaw[/eluser]
oh!

Maybe you are overriding the log path?
In the same config file check
Code:
$config['log_path'] = '';
If it's an empty string then the log file is in application/logs otherise it's the value set in that variable.

if you still cant see the log files then there's a problem with your codeigniter setup


Simple question/issue...new to CodeIgniter - El Forum - 05-25-2012

[eluser]codelogic[/eluser]
[quote author="InsiteFX" date="1337982393"]Sounds like your base url or routes are messed up someplace.

Show your base url and routes file.
[/quote]

Code:
$config['base_url'] = 'mysiteurl.com/CI/';

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


[quote author="Ayeyermaw" date="1337982477"]oh!

Maybe you are overriding the log path?
In the same config file check
Code:
$config['log_path'] = '';
If it's an empty string then the log file is in application/logs otherise it's the value set in that variable.

if you still cant see the log files then there's a problem with your codeigniter setup[/quote]

I did not override it, I left it blank! And I believe my settup is fine... I don't see what is wrong with my setup.

Damn I want this problem to be fixed so I can move along with learning more CodeIgniter.



Edit: I fixed the issue by adding http://. I guess it was that simple of a fix Smile.

But... I have ran into another minor problem. When I click 'view article' on my index.php/news page... it directs me to index.php/news/news/[article] with a 404.