public function actionCreate1() { $model = new DatasetAuthor(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); //this is fake information $model->dataset_id = 1; //update if (!isset($_SESSION['authors'])) { $_SESSION['authors'] = array(); } $authors = $_SESSION['authors']; if (isset($_POST['DatasetAuthor'])) { $ranks = trim($_POST['DatasetAuthor']['rank']); $names = trim($_POST['DatasetAuthor']['author_name']); if (substr($names, -1) == ";") { $names = substr($names, 0, -1); } $valid = true; $namearray = array(); if ($names != "") { $namearray = explode(";", $names); } // var_dump($namearray); if ($ranks == "") { $rankarray = array(); } else { $rankarray = explode(";", $ranks); } // var_dump($rankarray); // if (count($namearray) != count($rankarray)) { // $model->addError("error", "the number of name and rank are different!"); // $valid = false; // } //test names if ($valid) { foreach ($namearray as $name) { if ($name == "") { $model->addError("author_id", "Name can't be blank!"); $valid = false; break; } } } //test ranks if ($valid) { foreach ($rankarray as $rank) { // if ($rank == "") { // $model->addError("error", "rank can't be blank!"); // $valid = false; // break; // } if (!is_numeric($rank)) { $model->addError("rank", "rank should be an integer!"); $valid = false; break; } } } // var_dump(count($rankarray)." test"); if ($valid) { foreach ($namearray as $index => $name) { if ($index < count($rankarray)) { $rank = $rankarray[$index]; } else { $rank = 1; while (true) { $found = true; //find the maximum one in the authors array foreach ($authors as $author) { if ($author['rank'] == $rank) { $found = false; break; } } if ($found) { break; } $rank++; } } $valid = true; //check if there is duplicate input foreach ($authors as $key => $author) { if ($author['rank'] == $rank && $author['name'] == $name) { $model->addError("author_id", "Duplicate input"); $valid = false; break; } } if ($valid) { //store author dataset $model->rank = $rank; $model->author_name = $name; $id = 0; if ($this->storeAuthor($model, $id)) { $newItem = array('rank' => $rank, 'id' => $id, 'name' => $name); array_push($authors, $newItem); $_SESSION['authors'] = $authors; //$vars = array('authors'); ////Dataset::storeSession($vars); } else { $model->addError("error", "database operation failure, please log out first and log in again."); } } } } } // $model = new DatasetAuthor; $author_model = new CArrayDataProvider($authors); // $model = new DatasetAuthor; $this->render('create1', array('model' => $model, 'author_model' => $author_model)); }