我想在MVC codeigniter中转换我的代码

时间:2014-11-04 09:07:53

标签: php

我对客户有非常关键的要求。他想上传多项选择题。要上传的问题可以是文本内容,也可以是图像。所以有一个输入字段用于问题,可以是文本或图像,与问题i的其他4个选项相同。即文字或图像。意味着他可以输入问题,也可以只是同时上传图像文件;同一问题的答案是可以是文本或图像的选项。这是一个非常关键的问题,多文件上传文本数据。我在核心php中编程相同,它工作正常。我使用base64编码实现了这一点。但我想要CodeIgniter格式的相同代码。那是MVC架构格式。 这是我在核心php中的代码。

<?php
$message = "Enter question ";

$this->load->database();
if (isset($_POST['no']) ){


$target_path = "./uploadIMG/";

$target_path = $target_path . basename( $_FILES['que']['name']); 

if(move_uploaded_file($_FILES['que']['tmp_name'], $target_path)) {


   // echo "The file ".  basename( $_FILES['que']['name']). 
   // $img_file = $target_path.$_FILES["que"]["name"];
$imgData = base64_encode(file_get_contents($target_path));
//$src = 'data: '.mime_content_type($img_file).';base64,'.$imgData;
$que = $imgData;    

$a = "0";

} else{
$que = $_POST['que'];
$a = "1";

} 
$target_path = $target_path . basename( $_FILES['opt1']['name']); 

if(move_uploaded_file($_FILES['opt1']['tmp_name'], $target_path)) {

  //  echo "The file ".  basename( $_FILES['opt1']['name']). 
    $img_file1 = $_FILES["opt1"]["name"];
$imgData1 = base64_encode(file_get_contents($target_path));
//$src1 = 'data: '.mime_content_type($img_file1).';base64,'.$imgData;
$opt1 = $imgData1;  
$b = $a . "|"."0";

} else{
$opt1 = $_POST['opt1'];
$b = $a . "|"."1";
} 

$target_path = $target_path . basename( $_FILES['opt2']['name']); 
if(move_uploaded_file($_FILES['opt2']['tmp_name'], $target_path)) {

    //echo "The file ".  basename( $_FILES['opt2']['name']). 
    $img_file2 = $_FILES["opt2"]["name"];
$imgData2 = base64_encode(file_get_contents($target_path));
//$src2 = 'data: '.mime_content_type($img_file2).';base64,'.$imgData;
$opt2 = $imgData2;  
$c = $b . "|"."0";

} else{
$opt2 = $_POST['opt2'];
$c = $b . "|"."1";
} 
$target_path = $target_path . basename( $_FILES['opt3']['name']); 
if(move_uploaded_file($_FILES['opt3']['tmp_name'], $target_path)) {

    //echo "The file ".  basename( $_FILES['opt3']['name']). 
    $img_file3 = $_FILES["opt3"]["name"];
$imgData3 = base64_encode(file_get_contents($target_path));
//$src3 = 'data: '.mime_content_type($img_file3).';base64,'.$imgData;
$opt3 = $imgData3;  
$d = $c . "|"."0";
} else{
$opt3 = $_POST['opt3'];
 $d = $c . "|"."1";
} 


$target_path = $target_path . basename( $_FILES['opt4']['name']); 

if(move_uploaded_file($_FILES['opt4']['tmp_name'], $target_path)) {

    //echo "The file ".  basename( $_FILES['opt4']['name']). 
    $img_file4 = $_FILES["opt4"]["name"];
$imgData4 = base64_encode(file_get_contents($target_path));
//$src4 = 'data: '.mime_content_type($img_file4).';base64,'.$imgData;
$opt4 = $imgData4;  
$e = $d . "|"."0";
} else{
$opt4 = $_POST['opt4'];
$e = $d . "|"."1";

} 


$no = $_POST['no'];
$ans= $_POST['ans'];
$expl=$_POST['expl'];
//echo " QUESTION DATA      " .$que. "<br>";//getcwd()

//echo " Answer DATA        " .$opt1. "<br>";//getcwd()

//echo " Answer Format        " .$e. "<br>";//getcwd()


$target_path = "/uploads/";

//$target_path = $target_path . basename( $_FILES['file']['name']); 



 $q_code=$this->session->userdata('question_code');

 //echo "No1 " . $no . "";
        $query = "INSERT INTO question (question_no, question, option1, option2, option3, option4, answer, explaination, layout,question_code) VALUES ('$no', '$que', '$opt1','$opt2','$opt3','$opt4','$ans','$expl','$e','$q_code')";
        $result = mysql_query($query);
        //echo "No1 " . $query . "";
        //echo "No2  " . $$result . "";
         //$msg = "question created Successfully.";
        if($result){

            //echo "No1 " . $$result . "";
            $message = "question created Successfully.";
            echo " Question created Successfully.  " . $result . "";
        }
        else
        {
            $message = "question not created please enter valid question no. ";
            echo " Question not created please enter valid question no.  " . $result . "";
        }

    }

 }?>

0 个答案:

没有答案