2015-12-10 47 views
0

我想创建一个轮询系统,其中用户可以选择单选按钮,选择将在数据库中递增递增共享价值数据库

insert_view.php - 简单形式插入,用于选择所需的字段

<html> 
<body> 
<?php echo form_open('insert_ctrl'); ?> 
<h1>Insert Data Into Database Using CodeIgniter</h1><hr/> 
<?php if (isset($message)) { ?> 
    <CENTER><h3 style="color:green;">Data inserted successfully</h3></CENTER> <br> 
    <?php } ?> 
    <?php echo "which language do you prefer..?";?><br/> 
    <?php echo form_radio(array('id' => 'rad1', 'name' => 'rad1','value'=>0)); ?> 
    <?php echo form_label('JAVA'); ?> <?php echo form_error('rad1'); ?><br /> 

    <?php echo form_radio(array('id' => 'rad2', 'name' => 'rad2','value'=>0)); ?> 
    <?php echo form_label('PHP'); ?> <?php echo form_error('rad2'); ?><br /> 
    <?php echo form_radio(array('id' => 'rad3', 'name' => 'rad3','value'=>0)); ?> 
    <?php echo form_label('C\C++'); ?> <?php echo form_error('rad3'); ?><br /> 

    <?php echo form_radio(array('id' => 'rad4', 'name' => 'rad4','value'=>0)); ?> 
    <?php echo form_label('DOTNET'); ?> <?php echo form_error('rad4'); ?><br /> 

    <?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?> 

    </body> 
    </html> 

insert_ctrl.php - 用于通过控制器的帮助从视图发送请求到模型。

<?php 
    class insert_ctrl extends CI_Controller { 
    function __construct() { 
    parent::__construct(); 
    $this->load->helper('url'); 
    $this->load->model('insert_model'); 
    } 

    function index() { 
    $data = array(

    'Option1' => $this->input->post('rad1'), //for selecting the required option 
    'Option2' => $this->input->post('rad2'), //for selecting the required option 
    'Option3' => $this->input->post('rad3'), //for selecting the required option 
    'Option4' => $this->input->post('rad4') 
    ); 
    $vote = $this->input->post('Submit'); 

    if($vote=="Option1"){ 
    $value=$value+1; 
    } 
    elseif($vote=="Option2"){  
    $value=$value+1; 
    } 
    elseif($vote=="Option3"){ 
    $value=$value+1; 
    } 
    elseif($vote=="Option4"){ 
    $value=$value+1; 
    } 
    //Setting values for tabel columns 

    //Transfering data to Model 
    $this->insert_model->form_insert($data); 


    //$data['message'] = 'Data Inserted Successfully'; 
    //Loading View 
     $this->load->view('insert_view', $data); 
     } 
     } 
    ?> 

insert_model.php

 <?php 
    class insert_model extends CI_Model{ 
     function __construct() { 
     parent::__construct(); 
     } 
    function form_insert($data){ 
    // Inserting in Table(students) of Database(college) 
    $this->db->insert('students', $data); 
     } 
     } 
    ?> 
+1

这是非常好的,我们知道你想要什么..你想这真的很糟糕..现在..有什么问题?请不要重复超过10次..谢谢 –

+0

真正的问题是什么? – ciruvan

+0

很棒..我也想要一辆宝马..但是不幸的是,我不能在这里写下为什么我不能拥有它.. –

回答

0

类名应以大写字母开头,

class Insert_ctrl extends CI_Controller { 

如果要增加值在同一个表,然后将值传递给型号,

$data['value'] = $value; 
$this->insert_model->form_insert($data); 

首先什么是$value的值。

如果该值是存在于不同的表,增量这样,

Update table name set value = value + 1 where question_id = id. 

而不是那么多if else

if($vote=="Option1" || $vote=="Option2" || $vote=="Option3" || $vote=="Option4"){ 
    $value=$value+1; 
} 
+0

我创建了它只从视图中增加它..但是谢谢 –