Jump to content
php.lv forumi
Sign in to follow this  
reGative

Kā ievadīt datus datubāzē /Izmantoju Kohana Framework/

Recommended Posts

Kontrolieris saņem datus (POST), padod modelim, modelis saglabā db.

Kontrolieris saņem datus no modeļa, padod tos skatam un skats tos attēlo.

 

 

function action_trolol(){
 $view = ..
 $user = new Model_Users($user_id);
 $data = $user->getData();
 $view->data = $data;
 $this->request->response->body($view);
}

Edited by briedis

Share this post


Link to post
Share on other sites

Tātad, kods šāds. Izmantoju tādu metodi ,kā briedis aprakstīja

<form action="?post=name" method="post">
<input type="text" name="name" />
</form>
<?php
if(isset($_GET['post']) and $_GET['post'] == 'name'){
if(!empty($_POST['name'])){
$result;
}
else{
echo 'Sistēmas kļūda!';
}
}
?>

skats

public function insert_names($name){
   	$query = DB::query(DATABASE::INSERT, 'INSERT INTO names (name) VALUES (:name)')->parameters(array(':name' => $name));
}

modelis

$this->template->content = View::factory('about/about')->set('result', Model::factory('index')->insert_names($_POST['name']));

kontrolieris.

Met erroru, ka nav nodefinēts kontrolierī $_POST['name'].

Share this post


Link to post
Share on other sites

Tas nav tas ko es aprakstīju.

 

Mana interpretācija:

//Kontrolieris

action_user(){
 $view = new View(..);
 $view->data = Model_Users::getUser($_POST['user_id']);
 $this->response->body($view);
}

//Modelis

public static function getUser($user_id){
  $data = ... SELECT * FROM users WHERE id = $user_id
  return $data;
}

//Skats
<html>
<body>
<form action="<?=Route::get('kontrolieris/user');?>">
	Lietotāja id: <input type="text" name="user_id"/>
	<input type="submit"/>
</form>

Lietotājvārds: <strong><?=$data->username;?></strong>
E-pasts: <strong><?=$data->mail;?></strong>
</body>
</html>

Esi palasījis kaut kādus materiālus par to, kas vispār ir MVC?

Edited by briedis

Share this post


Link to post
Share on other sites

Briedi, kas tas par Route::get()? Ja vajag linku, tad URL::site(). Ja to pašu lapu, tad tukšs params.

 

P.S. Ja mēs par Kohana... vai arī tas skaitās pseido-kods? :)

Share this post


Link to post
Share on other sites

Briedi, kas tas par Route::get()? Ja vajag linku, tad URL::site(). Ja to pašu lapu, tad tukšs params.

 

P.S. Ja mēs par Kohana... vai arī tas skaitās pseido-kods? :)

 

Nu ok, es uz sitiena neatceros kā bija kohanā, bet doma tieši tāda pati. Jā, vairāk jau laikam pseido-kods, nevis copy-paste strādājošs piemērs.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...