Welcome Guest, Not a member yet? Register   Sign In
New to Codeigniter need help
#1
Rainbow 
(This post was last modified: 02-09-2016, 06:55 AM by kaaai3.)

Well I've had some experience with php. but my problem is probably in the routing part i have followed the tutorial for the news pages but now i am trying to make the edit page. I do all this in a panel where you need to be logged in to be able to create or edit a news item.

First i load all the posts in a list:

PHP Code:
Code:
   public function newslist() {
        // If not logged in kick out
        if($this->session->userdata('logged_in')) {
           
            // Get session username
            $session_data = $this->session->userdata('logged_in');
            $data['username'] = $session_data['username'];

            // Get data from model
            $data['news'] = $this->news_model->list_news();

            // Load views
            $this->load->view('templates/header-side', $data);
            $this->load->view('panel/newslist', $data);
        }
        else {
            //If no session, redirect to login page
            redirect('login', 'refresh');
        }
    } 


I display this in a foreach where i show all the posts in me newslist.php in the view folder/panel/newslist.php

Code:
Code:
<?php foreach ($news as $news_item): ?>
    <div class="news-item-hld">
        <a href="<?php echo site_url('panel/viewitem/'. $news_item['id']); ?>">
            <div class="news-item">
                <div class="news-item-title">
                    <h3><?php echo $news_item['title']; ?></h3>
                </div>

            </div>
        </a>
    </div>
<?php endforeach;



Now if u click on an item in this list you go to the viewitem.php where u load he post by id

PHP Code:
Code:
   public function viewitem($id) {
        // If not logged in kick out
        if($this->session->userdata('logged_in')) {

            // Get session username
            $session_data = $this->session->userdata('logged_in');
            $data['username'] = $session_data['username'];      

            // Get data from model
            $data['news_item'] = $this->news_model->list_news($id);

            if (empty($data['news_item'])) {
                show_404();
            }
            // Load views
            $this->load->view('templates/header-side', $data);
            $this->load->view('panel/viewitem', $data);
        }
        else {
            //If no session, redirect to login page
            redirect('login', 'refresh');
        }
    } 


Now you get the screen where u can edit the item 

Code:
Code:
    <div class="create-news">
        <div class="create-news-inner">
            <?php echo validation_errors(); ?>
            <?php echo form_open(site_url('panel/update/'. $news_item['id'])) ?>
                <input class="create-news-input" placeholder="Title" type="input" name="title" value="<?php echo $news_item['title'];?>" />
                <input class="create-news-input" placeholder="Img url" type="input" name="url" value="<?php echo $news_item['url'];?>" />
                
                <textarea class="create-news-textarea" placeholder="Message" name="text"><?php echo $news_item['text'];?></textarea>    

                <input class="create-news-btn" type="submit" value="Post"/>
            <?php echo form_close(); ?>                 
        </div>
    </div>


if you click the submut button it uses the function update but this is where it goes wrong and i do not know why

PHP Code:
Code:
   public function update($id) {
        if($this->session->userdata('logged_in')) {

            // Set form validations
            $this->form_validation->set_rules('title','Title','required');
            $this->form_validation->set_rules('url','url','required');
            $this->form_validation->set_rules('text','text','required');
            
            // If form validation is valid
            if($this->form_validation->run())
            {
                $data['success'] = $this->news_model->update_news($id);
            }
            
            // Get data from model
            $data['news_item'] = $this->news_model->get_news($id);

            if(empty($data['news_item']))
            {
                show_404();
            }
            
            // Redirect
            redirect('panel/newslist', 'refresh');
        }
        else {
            //If no session, redirect to login page
            redirect('login', 'refresh');
        }
    } 


And these are my routes

PHP Code:
Code:
$route['news/create'] = 'news/create';
$route['contact'] = 'contact';

$route['login'] = 'login';
$route['panel'] = 'panel';
$route['panel/update'] = 'panel/update';
$route['panel/update/(:any)'] = 'panel/update/$1';

$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
But when i click update it shows me the 404 page.
Reply
#2

(02-08-2016, 09:50 AM)kaaai3 Wrote: Is anyone willing to skype while i explain my question and screen sharing?

Oh, I don't think anybody would be. Frankly, that's a lot to ask!  Smile

Why don't you describe your problem, and also tell us how familiar you are with PHP itself. That will help people tailor their responses.
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply
#3

Besides for screen sharing most of us use TeamViewer
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

I fixed it from the update function i had to delete

// Get data from model
$data['news_item'] = $this->news_model->get_news($id);

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




Theme © iAndrew 2016 - Forum software by © MyBB