I'm trying to add a new descendant, but having difficulties achieving it, it displays some error, Would be grateful if you could take time to review what I've done thus far.
Here's
Controller
public function index() {
$this->load->view('closure_view');
}
public function add() {
$add_new = array(
'ancestor' => $this->input->post('ancestor'),
'descendant' => $this->input->post('descendant'),
'lvl' => $this->input->post('lvl'),
'id' => $this->input->post('id')
);
$cust_id = $this->closure->add($add_new);
redirect('http://localhost/kgmerchant/index.php/welcome');
}
Model
public $table;
public $closure_table = 'closures';
public function __construct($table_name = NULL, $closure_table = NULL){
parent::__construct();
$this->table = $table_name;
if ($closure_table !== NULL) {
$this->closure_table = $closure_table;
}
}
public function add($node_id, $target_id = 0) {
$sql = 'SELECT ancestor, '.$node_id.', lvl+1
FROM '.$this->closure_table.'
WHERE descendant = '.$target_id.'
UNION
SELECT '.$node_id.','.$node_id.',0';
$query = 'INSERT INTO '.$this->closure_table.' (ancestor, descendant, lvl) ('.$sql.')';
$result = $this->db->query($query);
return $result;
}
View
<form name="home" method="post" action="<?php echo base_url().'index.php/home/add' ?>" >
<input placeholder="ancestor" type="text" name="ancestor" required/>
<input placeholder="descendant" type="text" name="descendant" required/>
<input placeholder="lvl" type="text" name="lvl" required />
<input placeholder="id" type="hidden" name="id" value="" />
<input type="submit" value="Okay" />
</form>
Thanks.
Error
Message: Array to string conversion
Your model can't be called "Closure" because that's a reserved name in PHP. Change the name to something else and it should work.
Related
I put php script in my html (I use codeigniter), but when I show up in localhost, my php script eliminate all tag.
I want to put input text box for edit/update something and to show the previous text in my text box. But when I use php script, the element (input form and button) in tag php is missing in localhost.
<form method="post" action="<?php echo base_url('dashboard/categories_update'); ?>">
<div class="box-body">
<div class="form-group">
<label>Categories Name</label>
<?php foreach ($kategori as $k) { ?>
<input
type="hidden" name="id"
value="<?php echo base_url().$k->kategori_id; ?>"
>
<input
type="text" name="categories"
value="<?php echo base_url().$k->kategori_nama; ?>" placeholder="Type here. . . "
>
<?php } ?>
</div>
</div>
<div class="box-footer">
<input type="submit" class="btn btn-success" value="update">
</div>
</form>
This my controller code :
public function categories()
{
$this->load->model('m_data');
$data['kategori'] = $this->m_data->get_data('kategori')->result();
$this->load->view('dashboard/v_header');
$this->load->view('dashboard/v_categories',$data);
$this->load->view('dashboard/v_footer');
}
public function add_categories()
{
$this->load->view('dashboard/v_header');
$this->load->view('dashboard/v_categories_add');
$this->load->view('dashboard/v_footer');
}
public function categories_action()
{
$this->form_validation->set_rules('categories','Categories','required');
if ($this->form_validation->run() !=false) {
$categories = $this->input->post('categories');
$data = array(
'kategori_nama' => $categories,
'kategori_slug' => strtolower(url_title($categories))
);
$this->load->model('m_data');
$this->m_data->insert_data($data,'kategori');
redirect(base_url().'dashboard/categories');
} else {
$this->load->view('dashboard/v_header');
$this->load->view('dashboard/v_categories_add');
$this->load->view('dashboard/v_footer');
}
}
public function categories_edit()
{
$id = $this->input->post('id');
$where = array(
'kategori_id' => $id
);
$this->load->model('m_data');
$data['kategori']= $this->m_data->edit_data($where,'kategori')->result();
$this->load->view('dashboard/v_header');
$this->load->view('dashboard/v_categories_edit',$data);
$this->load->view('dashboard/v_footer');
}
public function categories_update()
{
$this->form_validation->set_rules('categories','Categories','required');
if ($this->form_validation->run() != false) {
$id = $this->input->post('id');
$kategori = $this->input->post('categories');
$where = array(
'kategori_id' => $id
);
$data = array (
'kategori_nama' => $kategori,
'kategori_slug' => strtolower(url_title($kategori))
);
$this->load->model('m_data');
$this->m_data->update_data($where,$data,'kategori');
redirect(base_url().'dashboard/categories');
}
}
there's incorrect script in my controller,
before :
public function categories_edit() {
$id = $this->input->post('id');
it should be :
public function categories_edit($id)
and now all my script is working ! thankyou guys, especially bro #RajendraSingh !
I'm trying to add a new descendant, but having difficulties achieving it, it displays some error, Would be grateful if you could take time to review what I've done thus far.
Here's
Controller
public function index() {
$this->load->view('closure_view');
}
public function add() {
$add_new = array(
'ancestor' => $this->input->post('ancestor'),
'descendant' => $this->input->post('descendant'),
'lvl' => $this->input->post('lvl'),
'id' => $this->input->post('id')
);
$cust_id = $this->closure->add($add_new);
redirect('http://localhost/kgmerchant/index.php/welcome');
}
Model
public $table;
public $closure_table = 'closures';
public function __construct($table_name = NULL, $closure_table = NULL){
parent::__construct();
$this->table = $table_name;
if ($closure_table !== NULL) {
$this->closure_table = $closure_table;
}
}
public function add($node_id, $target_id = 0) {
$sql = 'SELECT ancestor, '.$node_id.', lvl+1
FROM '.$this->closure_table.'
WHERE descendant = '.$target_id.'
UNION
SELECT '.$node_id.','.$node_id.',0';
$query = 'INSERT INTO '.$this->closure_table.' (ancestor, descendant, lvl) ('.$sql.')';
$result = $this->db->query($query);
return $result;
}
View
<form name="home" method="post" action="<?php echo base_url().'index.php/home/add' ?>" >
<input placeholder="ancestor" type="text" name="ancestor" required/>
<input placeholder="descendant" type="text" name="descendant" required/>
<input placeholder="lvl" type="text" name="lvl" required />
<input placeholder="id" type="hidden" name="id" value="" />
<input type="submit" value="Okay" />
</form>
Thanks.
Error
Message: Array to string conversion
Your model can't be called "Closure" because that's a reserved name in PHP. Change the name to something else and it should work.
I'm trying create an edit profile function for a user. When the user submits the form, it updates the data in the database. But when I try to access the edit profile page I get this error:
Type: ArgumentCountError
Message: Too few arguments to function Profile::editProfile(), 0 passed in /Applications/MAMP/htdocs/Capstone/system/core/CodeIgniter.php on line 532 and exactly 1 expected
I'm passing $id in the function, so I'm not sure why this is happening.
Model:
public function updateUser($id)
{
$data = array(
'age' => $this->input->post('age'),
'location' => $this->input->post('location'),
'favouritebeer' => $this->input->post('favouritebeer'),
'website' => $this->input->post('website'),
'bio' => $this->input->post('bio')
);
$this->db->where('id', $id);
$this->db->update('users', $data);
return $id;
}
public function getUser($id)
{
$this->db->select('*');
$this->db->where('id', $id);
$this->db->from('users');
$query = $this->db->get();
return $query-row();
}
Controller:
public function editProfile($id)
{
$this->load->model('user_model');
$this->load->view('templates/header');
$this->load->view('pages/editprofile');
$this->load->view('templates/footer');
if(isset($_POST['updatechanges'])) {
if($this->user_model->getUser($id)) {
$this->session->set_flashdata('success', 'Profile updated!');
redirect('profile', 'refresh');
}
}
}
View:
<div class ="container">
<?php
$con=mysqli_connect("localhost","root","admin","Mydb");
$sql = mysqli_query($con, "SELECT id, username, age, location, favouritebeer, website, bio FROM users WHERE username = '" . $_SESSION['username'] . "'");
$row = mysqli_fetch_array($sql);
?>
<h2><?php echo $_SESSION['username'];?>'s Profile</h2>
Logout
</br>
Change password
</br>
Change security question
</br>
</br>
<body>
<form method ="POST">
<h4>Age:</h4>
<input type="age" class="form-control" name="age" id="ageinput" value="<?php echo $row['age']; ?>">
<h4>Location:</h4>
<input type="location" class="form-control" name="location" id="locationinput" value="<?php echo $row['location']; ?>">
<h4>Favourite beer:</h4>
<input type="beer" class="form-control" name="favouritebeer" id="beerinput" value="<?php echo $row['favouritebeer']; ?>">
<h4>Website:</h4>
<input type="website" class="form-control" name="website" id="websiteinput" value="<?php echo $row['website']; ?>">
<h3>Bio: </h3>
<input type="bio" class="form-control" name="bio" id="bioinput" value="<?php echo $row['bio']; ?>">
Save Changes
</form>
</div>
if you are calling this editProfile() function from view try the following code :
in view :
edit
and in controller :
function editProfile() {
$id = $this->uri->segment(3);
// do more
}
and if you are calling this function from another function in the same controller try this :
$this->editProfile(1);
Try this code. The below mentioned function have one errors in return place.
public function getUser($id)
{
$this->db->select('*');
$this->db->where('id', $id);
$this->db->from('users');
$query = $this->db->get();
return $query->row();
}
I have made the redirect from the question i have posted here
Now i have another form in the redirected page where i need to enter name and mobile number and if it matches in my db table helloworld it will go to one page or else to another
<p>You have successfully registered</p>
<div>
<label>Login</label>
</div>
<div>
<form action="" method="post">
<label> Username </label>
<strong>:</strong>
<input class="input-text required-entry" type="text" name="fname" maxlength="20">
<label>Mobile No</label>
<strong>:</strong>
<input class="required-entry" type="number" maxlength="10" name="mobileno">
<input type="submit" name="login" value="Login">
<input type="button" name="cancel" value="Cancel">
</form>
</div>
Can any one help me how no i can verify it with existing data in db and make this work ? Shall I start it with using index controller or is there any magento way?
Update:
this is my Indexcontroler.php after below answer update
<?php
class MyCustom_Helloworld_IndexController extends Mage_Core_Controller_Front_Action
{
/*
* this method privides default action.
*/
public function indexAction()
{
if($this->getRequest()->getParams()) {
$param = $this->getRequest()->getParams();
echo $firstname = $param['fname'];
$lastname = $param['lname'];
$address = $param['address'];
$state = $param['state'];
$city = $param['city'];
$mobile = $param['mobileno'];
$model = Mage::getModel('helloworld/helloworld');
// $model->setTitle($title);
$model->setFirstname($firstname);
$model->setLastname($lastname);
$model->setAddress($address);
$model->setState($state);
$model->setCity($city);
$model->setMobileno($mobile);
$model->save();
$this->_redirect('helloworld/index/login');
// $this->_redirectReferer();
}else {
/*
* Initialization of Mage_Core_Model_Layout model
*/
$this->loadLayout();
/*
* Building page according to layout confuration
*/
$this->renderLayout();
}
}
public function loginAction()
{
$this->loadLayout();
$this->renderLayout();
}
public function loginnAction()
{
if($this->getRequest()->getParams()) {
$param = $this->getRequest()->getParams();
$username = $param['fname'];
$mobile = $param['mobileno'];
$check = Mage::getModel('helloworld/helloworld')
->AddFieldToFilter('mobileno', array('eq' => $mobile))
->AddFieldToFilter('fname', array('eq' => $username));
if(count($check)==1) {
$this->_redirectReferer();
}else {
$this->_redirect('helloworld/index/login');
}
}
}
}
you can add check like that
public function loginAction()
{
if($this->getRequest()->getParams()) {
$param = $this->getRequest()->getParams();
$username = $param['fname'];
$mobile = $param['mobile'];
$connectionresource = Mage::getSingleton('core/resource');
$readconnection = $connectionresource->getConnection('core_read');
$table = $connectionresource->getTableName('helloworld/helloworld');
$allrecord = $readconnection->select()->from(array('helloworld'=>$table))->where('helloworld.mobileno=?', $mobileno)
->where('helloworld.fname=?', $username);
$alldata =$readconnection->fetchAll($allrecord);
if(count($alldata)==1) {
$this->_redirect('home');
}else {
$this->_redirect('customer/account');
}
}
I have been practicing with PHP and mongodb. I am developing a simple web application but using OOP.
I created a class called user which has my methods to do with user like addUser, deleteUser etc. For add user, I would like for the form to carry out some simple validation tasks, but i am not sure how. Here is the class to add a new user:
function createUser($username, $name, $email, $password){
$user = array(
'username' => $username,
'name' => $name,
'email' => $email,
'password' => $password
);
if ($this->db->count(array('username' => $username)) == 0) {
$this->db->insert($user);
return true;
} else {
echo 'username taken';
}
}
And the html:
<?php
session_start();
include_once 'user.php';
include './templates/header.php';
if (isset($_POST['register']) && ($_POST['register']) == ($_POST["register"])) {
$user = new User();
$return = $user->createUser(
$_POST['username'],
$_POST['name'],
$_POST['email'],
$_POST['password'],
$_POST['password2']);
}
if ($return == true) {
echo 'you have successfully registered';
} else {
echo '</br>' . 'sorry, try again';
}
?>
<div class="container">
<div class="jumbotron">
<form method="post" action="">
<label>Username: </label><br>
<input name="username" type="text" ><br><br>
<label>Name: </label><br>
<input name="name" type="text"><br><br>
<label>Email: </label><br>
<input name="email" type="email" ><br><br>
<label>Password: </label><br>
<input name="password" type="password" ><br><br><br>
<label>Repeat Password: </label><br>
<input name="password2" type="password" ><br><br><br>
<input name="register" class="btn btn-primary btn-lg" type="submit" value="Register"><br>
</form>
</div>
</div>
Please feel free to correct me on other mistakes you may notice. I know there is bound to be some.
i wrote a simple example for you its simple and useful
class validation {
public $currentValue;
public $values = array();
public $errors = array();
public function __construct() {
parent::__construct();
// echo "burasi model sayfasi ";
}
public function post($key){
if(isset($_POST[$key])){
$this->values[$key] = $_POST[$key];
$this->currentValue = $key;
return $this;
}else{ die("hi boo boo ! your form values are empty");}
}
public function isEmpty(){
if(empty($this->values[$this->currentValue])){
$message='the form is emppty';
$this->errors[$this->currentValue]['empty'] =''.$message.'';
}
return $this;
}
public function submit(){
if(empty($this->errors)){
return true;
}else{
return false;
}
}
}
this is an example so how can you use it ?
firstly you need yo call the class
$form = new validation ();
$form->post("all you need just write post name here ")
->isEmpty();
if($form->submit()){
//everyting is ok !
you cann add delet or update data
}else{
$data["error"] = $form->errors; // and we sett the errorr mesages to the array now you can show the user the errormesages ! well done
}
}