2016-05-17 3 views
0

Hier habe ich Controller news.php:Einsatz und Aktualisierung in einzelner Form in CodeIgniter

<?php 

class News extends CI_Controller{ 

    public function __construct(){ 
     parent::__construct(); 
       $this->load->model('news_model'); 
       $this->load->helper('url_helper'); 
    } 

    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 = NULL) 
     { 
       $data['news_item'] = $this->news_model->get_news($slug); 

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

     $data['title'] = "News in Detail(s)"; 

     $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 22'; 

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

       $data['updateid'] = '' ; 


       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(); 
        if($update == 1){ 
        redirect('/news', 'location'); 
       } 
       } 
     } 

     public function update($id){ 

       $this->load->helper('form'); 
       $this->load->library('form_validation'); 

      $data['updateid'] = $id; 
      $data['title'] = "Update News in Detail(s)"; 
      $data['update'] = $this->news_model->edit_load_data($id); 

      $this->load->view('news/create',$data); 

     if ($this->input->post('submit')) { 

       $update = $this->news_model->update_news($id); 
       if($update == 1){ 
        redirect('/news', 'location'); 
       } 
      } 



     } 
} 

?> 

Hier meine Modelldatei news_model.php:

<?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') 
      'text' => $this->input->post('select') 
      ); 

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

    public function edit_load_data($id){ 

     $query = $this->db->get_where('news', array('id' => $id)); 


      return $query->row_array(); 

    } 

    public function update_news($id){ 

     $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') 
      'text' => $this->input->post('select') 
      ); 

       $this->db->where('id', $id); 


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




    } 

} 
?> 

Hier ist meine Form create.php Datei:

<?php echo validation_errors(); 
    echo $updateid; 
    ?> 
    <?php if($updateid == ''){ ?> 
    <?php echo form_open('news/create'); ?> 
    <?php } else { ?> 
    <?php echo form_open('news/update/'.$updateid); ?> 
    <?php } ?> 


     <label for="title">Title</label> 
     <input type="input" name="title" value="<?php echo $update['title']; ?>" /><br /> 

     <label for="text">Text</label> 
     <textarea name="text" ><?php echo set_value('text'); ?></textarea><br /> 

      <label for="text">Select</label> 
    <select name="select"> 
    <option <?php if($update['text']=='text1'){ echo "selected";} ?> value="text1">text1</option> 
     <option <?php if($update['text']=='text2'){ echo "selected";} ?> value="text2">text2</option> 
    </select> 
    <br /> 



     <input type="submit" name="submit" value="Create news item" /> 

    </form> 

hier, wenn ich Nachrichten werde/erstellen gibt in Fehler:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: update

Filename: news/create.php

Line Number: 15

Backtrace:

File: C:\xampp\htdocs\mylab\application\views\news\create.php Line: 15 Function: _error_handler

File: C:\xampp\htdocs\mylab\application\controllers\News.php Line: 54 Function: view

File: C:\xampp\htdocs\mylab\index.php Line: 315 Function: require_once " />

Was kann ich dafür tun? Ich möchte hinzufügen und bearbeiten für die gleiche Datei create.php ist es möglich, dort?

+0

Verwendung if (isset ($ update)) {} –

+0

es ist wirklich grundlegende Funktionalität, die leicht durch Lesen des Handbuchs gelöst werden könnte. – Farside

Antwort

0

Ich weiß nicht, dass dies das gewünschte Ergebnis produzieren wird, aber es wird die Verwendung von undefinierten Variablen verhindern.

<?php 
echo validation_errors(); 
$updateid = isset($updateid) ? $updateid : ''; 
echo $updateid; 

if($updateid == '') 
{ 
    echo form_open('news/create'); 
} 
else 
{ 
    echo form_open('news/update/'.$updateid); 
} 

if(!isset($update)) 
{ 
    $update['title'] = NULL; 
    $update['slug'] = NULL; 
    $update['text'] = NULL; 
} 
?> 

<label for="title">Title</label> 
<input type="input" name="title" value="<?php echo $update['title']; ?>" /><br /> 

<label for="text">Text</label> 
<textarea name="text" ><?php echo set_value('text'); ?></textarea><br /> 

<label for="text">Select</label> 
<select name="select"> 
    <option <?php 
    if($update['text'] == 'text1') 
    { 
    echo "selected"; 
    } 
    ?> value="text1">text1</option> 
    <option <?php 
    if($update['text'] == 'text2') 
    { 
    echo "selected"; 
    } 
    ?> value="text2">text2</option> 
</select> 
<br /> 

<input type="submit" name="submit" value="Create news item" /> 

</form> 

Sie sollten wahrscheinlich Ihr Modell so ändern, dass es eine Standard-Array zurückgibt, wenn/falls die Eingänge nicht vorhanden sind. Ich bin nicht in der Lage zu identifizieren, wo Sie $update an die Ansicht entweder senden.

0

Ich glaube, du bist nicht mehr als $ update [ 'title'], $ update [ 'text'] zurückkehren, etc .. Sie kehren $ title, $ text ...

Try changing $update['title'] to $title in create.php to see what happens

Verwandte Themen