使用codeigniter将多个数据保存到数据库中

时间:2014-03-22 11:29:47

标签: jquery ajax codeigniter

您好我在使用codeigniter保存数据库中的所有多个数据时遇到此问题。我在传递数据时使用ajax,我想在其中保存多个数据,但我的问题是我不知道该怎么做。有人可以帮助我吗? 这是我的控制器

class News_and_events extends CI_Controller {
  public function __construct(){
      parent::__construct();
      $this->load->library('form_validation');
      $this->load->model('admin_model', 'am');
       $this->load->library('session');
  }

  public function add_another(){
    $added_datas = $this->input->post('added_datas');
    $added_arr = explode("|", $added_datas);
    foreach($added_arr as $value){
      if($value){
        $pass_arr = explode("*", $value);
        $items = array();
        $items[] = array(
                  'Date' => $this->input->post($items[0]),
                  'Event' => $this->input->post($items[1]),
                  'Description' => $this->input->post($items[2])              
                );
        $this->am->saveData($items[]);

      }
    }

   }

}

我的模特

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

Class Admin_model extends CI_Model{
    public function editData($id){
      return $this->db->get_where('news_and_updates',array('id' => $id))->row_object();
    }

    public function saveData($array){
      $this->db->insert('news_and_updates', $array);
    }



    public function getAllData(){  
      return $this->db->order_by("Date", "desc")
                      ->get('news_and_updates')
                      ->result_object();
    }

    public function delete($id){
      $this->db->where('id', $id)->delete('news_and_updates');
    }


}
?>

和我的观点

<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script type="text/javascript">
  $(document).ready(function(){
    $("#back").click(function(){
      window.location.replace('<?php echo base_url().'news-and-events'?>');
    });


    $('tbody').on('click', '.remove', function(e) {
        e.preventDefault();
        $(this).parent().remove();
        $("#date").remove();
        $("#event").remove();
        $("#description").remove();
    });

    var id = $("#datepicker"),
        event = $("#event"),
        description = $("#description"),
        allFields = $( [] ).add( id ).add( event ).add( description ),
        tips = $( ".validateTips" );
    function checkLength( o, n, min, max ) {
        if ( o.val().length > max || o.val().length < min ) {
          o.addClass( "ui-state-error" );
          updateTips( "Length of " + n + " must be between " +
          min + " and " + max + "." );
          return false;
        } else {
          return true;
        }
   }

    function updateTips( t ) {
      tips
      .text( t )
      .addClass( "ui-state-highlight" );
      setTimeout(function() {
      tips.removeClass( "ui-state-highlight", 1500 );
      }, 500 );
    } 

    $("#dialog-form").dialog({
       autoOpen: false,
        height: 300,
        width: 380,
        modal: true,
         buttons: {
          "Add news and events": function() {
            $("#added-events").val( $("#added-events").val() + id.val() + '*' + event.val() + '*' + description.val() + '|');
             var bValid = true;
             allFields.removeClass( "ui-state-error" );

             bValid = bValid && checkLength( id, "Date",6, 50);
             bValid = bValid && checkLength( event, "Event", 6, 50 );
             bValid = bValid && checkLength( description, "Description", 6, 50 );
             if(bValid){
                $("#add tbody").append(
                                   "<tr>" +
                                    "<td id='date' name='date'>" + id.val() + "</td>" +
                                    "<td id='event' name='event'>" + event.val() + "</td>" +
                                    "<td id='description' name='description'>" + description.val() + "</td>" +
                                    "<td>" + "<a href=''>" + "Edit" + "</a>" + " | " +
                                    "<a href='javascript:;' class='remove' >" + "Delete" + "</a>" + "</td>" +
                                    "</tr>"
                                  );
              $( this ).dialog( "close" );
             }
          },
           Cancel: function() {
              $( this ).dialog( "close" );
           }
        },
           close: function() {
              allFields.val( "" ).removeClass( "ui-state-error" );
            }

      });

      $("#add-another")
        .button()
        .click(function(){
          $("#dialog-form").dialog("open");
        });

      $("#submit").click(function(){
        var  added_datas = $("#added-events").val();
        alert(added_datas);
          $.post('<?php echo base_url().'news-and-events/add_another'?>', { added_datas:added_datas }, function(){
          //alert(data);
            window.location.replace('<?php echo base_url().'news-and-events'?>');
      });

  });  
</script>
 <div class="container" >
    <br />
    <br />
    <br />
    <ul id="nav">
     <li><a href="<?php echo base_url().'homepage'?>" title="Home"><h4>Home</h4></a></li>
     <li><a href="<?php echo base_url().'news-and-events'?>" title="News and Events"><h4>News and Events</h4></a></li>
     <li><a href="" title="Activities"><h4>Activities</h4></a></li>
    </ul>
    <div class="starter-template">
      <h1 style="float:left;">Add News And Events</h1>
      <div class="clear"></div>
      <br />

        <div id="dialog-form" title="Add Another">    
          <p class="validateTips">All form fields are required.</p>
          <form>
            <table>
              <tr>
                <td><input type="text" name="date" id="datepicker" placeholder="Date" class="text ui-widget-content ui-corner-all"/></td>
              </tr>
              <tr>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td><input type="text" name="event" id="event" placeholder="Event" class="text ui-widget-content ui-corner-all"/></td>
              </tr>
               <tr>
                <td>&nbsp;</td>
              </tr>
               <tr>
                <td><input type="text" name="description" id="description" placeholder="Description" class="text ui-widget-content ui-corner-all" /></td>
              </tr>
            </table>
          </form>
        </div>

         <div id="add_another" style="float:left;">
            <input id="add-another" type="button" value="Add Another" class="btn btn-primary" />
         </div>
         <div class="clear"></div> 
          <br />
          <form >
            <table id="add" width="500%" class="table-data table ui-widget ui-widget-content">
              <thead>
                <tr class="ui-widget-header ">
                  <td width="10%;">Date</td>
                  <td>Event</td>
                  <td>Description</td>
                  <td width="10%;">Options</td>
                </tr>
              </thead>
               <tbody>
                </tbody>
            </table>
            <input type="hidden" id="added-events" name="added-events" />
            <div style="float:left; margin-top:20px;">
              <input type="button" id="submit" class="btn btn-success" value='Add' />
            </div>
          </form>
      <br />
      <br />
      <div class="clear"></div>
      <br />
      <a href="javascript:;" id="back" title="Back" style="float:left;">Back </a>
    </div>

  </div><!-- /.container -->
<script> 
      var date = new Date();
      var currentMonth = date.getMonth();
      var currentDate = date.getDate();
      var currentYear = date.getFullYear();

      $('#datepicker').datepicker({
        minDate: new Date(currentYear, currentMonth, currentDate),
        dateFormat: "yy-mm-dd"
      });

</script>

在我的控制器中,我使用foreach($ added_arr作为$ value)从我的视图传递数据任何帮助都非常感谢 谢谢!

0 个答案:

没有答案