B
B
Bahodir Ismatov2018-08-10 07:14:36
Yii
Bahodir Ismatov, 2018-08-10 07:14:36

Help with Yii2 Active record, how to create a controller?

Hi all! I want to make an e-schedule website.
I have a problem with convert php to yii2 :)
i am new to yii2. I wrote a form for adding lessons in pure PHP. i need to convert to yii2.
5b6d104f2c8b8358672515.png5b6d105b2573a171570744.png

php code
<?
if (isset($_POST['add'])){
$fac = $_POST['fac'];
$guruh = $_POST['guruh'] ;

  for( $j=1; $j<=6; $j++){
        if ($j==1) $day = "Dushanba";
        else
        if ($j==2) $day = "Seshanba";
        else
        if ($j==3) $day = "Chorshanba";
        else
        if ($j==4) $day = "Payshanba";
        else
        if ($j==5) $day = "Juma";
        else
        if ($j==6) $day = "Shanba";
  for ( $i=1; $i<=6; $i++) {
      if (isset($_POST[$j.'f'.$i]) && ($_POST[$j.'f'.$i]!="") ||isset($_POST[$j.'f'.$i.'t']) && ($_POST[$j.'f'.$i.'t']!="") ) {
                if ( $i == 1) {
                    $res = mysqli_query ($con,"DELETE FROM d_jadval WHERE day='$day' and group_id=(SELECT id FROM gruppa WHERE nomer=$guruh and facultety='$fac') ");
                }
                if (isset($_POST[$j.'f'.$i]) && ($_POST[$j.'f'.$i]!="") )
                    $fan = trim(mysqli_real_escape_string($con,$_POST[$j.'f'.$i]));
                    if (isset($_POST[$j.'p'.$i]) && ($_POST[$j.'p'.$i]!="")) $para = trim(mysqli_real_escape_string($con,$_POST[$j.'p'.$i]));
                    if (isset($_POST[$j.'t'.$i]) && ($_POST[$j.'t'.$i]!="")) $teach = trim(mysqli_real_escape_string($con,$_POST[$j.'t'.$i]));
                    if (isset($_POST[$j.'type'.$i]) && ($_POST[$j.'type'.$i]!="")) $type = trim(mysqli_real_escape_string($con,$_POST[$j.'type'.$i]));
                    if (isset($_POST[$j.'x'.$i]) && ($_POST[$j.'x'.$i]!="")) $xona = trim(mysqli_real_escape_string($con,$_POST[$j.'x'.$i]));


            if (isset($_POST[$j.'f'.$i.'t']) && ($_POST[$j.'f'.$i.'t']!="") ) {
            	$fan1 = trim(mysqli_real_escape_string($con,$_POST[$j.'f'.$i.'t']));
        if (isset($_POST[$j.'t'.$i.'t']) && ($_POST[$j.'t'.$i.'t']!="")) $teach1 = trim(mysqli_real_escape_string($con,$_POST[$j.'t'.$i.'t']));
        if (isset($_POST[$j.'type'.$i.'t']) && ($_POST[$j.'type'.$i.'t']!="")) $type1 = trim(mysqli_real_escape_string($con,$_POST[$j.'type'.$i.'t']));
        if (isset($_POST[$j.'x'.$i.'t']) && ($_POST[$j.'x'.$i.'t']!="")) $xona1 = trim(mysqli_real_escape_string($con,$_POST[$j.'x'.$i.'t']));

        $query = "INSERT INTO d_jadval 
          (fan,fan1,teacher,teacher1,room,room1,lesson_id,day,group_id,course_id,type,type1,group_no) 
VALUES ('$fan','$fan1','$teach','$teach1','$xona','$xona1',$para,'$day',
(SELECT id FROM gruppa WHERE nomer=$guruh and facultety=$fac) , 
(SELECT course FROM gruppa WHERE nomer=$guruh and facultety=$fac) ,$type,$type1,$guruh ) ";
        } 
        else {
    
          $query = "INSERT INTO d_jadval (fan,teacher,room,lesson_id,day,group_id,course_id,type,group_no) VALUES ('$fan','$teach','$xona',$para,'$day',(SELECT id FROM gruppa WHERE nomer=$guruh and facultety=$fac) , (SELECT course FROM gruppa WHERE nomer=$guruh and facultety=$fac) ,$type,$guruh ) " ;
        }
        
      $res_add = mysqli_query($con,$query) or die ("Insert : ".$con->error)  ;
        }
      }
    }
    if ($res_add){
         $dars="SELECT * FROM d_jadval WHERE group_id=(SELECT id FROM gruppa WHERE nomer=$guruh and facultety=$fac)" ;
             $return_arr = array();
                   $fetch = mysqli_query($con,$dars);

                    while ($row = mysqli_fetch_array($fetch, MYSQLI_ASSOC)) {
                        $row_array['id'] = $row['lesson_id'];
                        $row_array['fan'] = stripslashes($row['fan']);

                        if ($row['fan1']!="") $row_array['fan1'] = stripslashes($row['fan1']);
                        else  $row_array['fan1'] = "-";
                        $idd=$row['teacher'];
                        echo $idd;
                        $q1 = mysqli_query($con,"SELECT fish FROM teachers WHERE id=$idd" ) or die("sel.teach ".$con->error);
                        $q2 = mysqli_fetch_array($q1);
                        $q3 = stripslashes($q2['fish']);
                        $row_array['teacher'] = $q3;

                        if ($row['teacher1']!="") {

                            $q1 = mysqli_query($con,"SELECT fish FROM teachers WHERE id=".$row['teacher1']);
                            $q2 = mysqli_fetch_array($q1);
                            $q3 = stripslashes($q2['fish']);
                            $row_array['teacher1'] = $q3;
                        }
                        else $row_array['teacher1'] = "-";
                        $row_array['room'] = $row['room'];
                        if ($row['room1']!="")  $row_array['room1'] = $row['room1'];
                        else  $row_array['room1'] = "-";
                        $row_array['day'] = $row['day'];
                        $row_array['type'] = $row['type'];
                        if ($row['type1']!="") $row_array['type1'] = $row['type1'];
                        else $row_array['type1'] = "-";
            

                        array_push($return_arr,$row_array);
                    }
                            $dir ="tables/";
                            $file5 = $dir.$guruh.'_'.$fac.'.php';
                            $header = "<?php header('Access-Control-Allow-Origin: *')?>";
                            file_put_contents($file5,$header.json_encode($return_arr));

                       
         echo " <h1 class=\"page-header\" style=\"color: green; text-transform: uppercase; \">Dars jadvali kiritildi!</h1>";
         echo " <script>setTimeout(function () {window.location.href = 'tables.php';},5000); 
            </script><a href=\"addtables.php\">Ortga</a>"; exit(0);
            }

  }

?>

please help, thanks in advance!!!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question