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'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
}
}
i need help with my forms that i create, i created 5 forms in a twig file and created a controller , how can i bind my value from the forms to database, when i bind now it displays me only one Value form the 5 times , but i need 5 times to be different value :(, please help , I stuck on this thing all day already..
my twig file:
<div class="new-test">
<h2>New test </h2>
<form action="{{ path('test.create') }}" method="post">
Test name: <input type="text" name="name"/><br>
Category 1<input type="text" name="category-new" >
<div id="customWidget">
<div id="colorSelector1"><div style="background-color: #00ff00"></div>
</div>
<div id="colorpickerHolder1"></div>
</div>
Category 2<input type="text" name="category-new" ><br>
Category 3<input type="text" name="category-new" ><br>
Category 4<input type="text" name="category-new" ><br>
Category 5<input type="text" name="category-new" ><br>
<input type="submit" value="Add">
</form>
my controller:
/**
* #Route("/add/test", requirements={"name" = "\s+"}, name="test.create")
* #Method("Post")
* #return array
*/
public function createAction()
{
$success = 0;
$name = $this->getRequest()->get('name');
if( !empty($name) )
{
$test = new Test();
$test->setName($this->getRequest()->get('name'));
$em = $this->getDoctrine()->getManager();
$em->persist($test);
$em->flush();
$success = 'Test '.$test->getName().' was created';
}
else
{
$success = 'Test name can not be empty';
}
$category = $this->getRequest()->get('category-new');
for ($i=0; $i<=5; $i++){
if( !empty($category) )
{
$categoryName = new Category();
$categoryName->setName($this->getRequest()->get('category-new'));
$em = $this->getDoctrine()->getManager();
$em->persist($categoryName);
$em->flush();
$success = ' Category '.$categoryName->getName().$i.' was created';
}
else
{
$success = 'Test name can not be empty';
}
}
return $this->redirect($this->generateUrl('test.new'));
}
Not sure if i understand correctly. You want to have inputs with name 'category-new' that have different value. If so, you have problem in form view:
Category *<input type="text" name="category-new" ><br>
name should have bracket at the end name="category-new[]" or name="category-new[1]" for category 1, name="category-new[2]" for category 2, and so on.
Template:
Category 1<input type="text" name="category-new[]" >
<div id="customWidget">
<div id="colorSelector1"><div style="background-color: #00ff00"></div>
</div>
<div id="colorpickerHolder1"></div>
</div>
Category 2<input type="text" name="category-new[]" ><br>
Category 3<input type="text" name="category-new[]" ><br>
Category 4<input type="text" name="category-new[]" ><br>
Category 5<input type="text" name="category-new[]" ><br>
It`s meen, that your controller take array with name category-new
/**
* #Route("/add/test")
*
* #return array
*/
public function createAction()
{
$success = 0;
$name = $this->getRequest()->get('name');
if (!empty($name)) {
$test = new Test();
$test->setName($name);
$em = $this->getDoctrine()->getManager();
$em->persist($test);
$em->flush();
$success = 'Test ' . $test->getName() . ' was created';
} else {
$success = 'Test name can not be empty';
}
$categoryList = array_map('trim', $this->getRequest()->get('category-new'));
foreach ($categoryList as $category) {
if (!empty($category)) {
$categoryName = new Category();
$categoryName->setName($category);
$em = $this->getDoctrine()->getManager();
$em->persist($categoryName);
$em->flush();
$success = ' Category ' . $category . ' was created';
} else {
$success = 'Test category-new may not be empty';
}
}
return $this->redirect($this->generateUrl('test_create'));
}
Some words
Route("/add/test", requirements={"name" = "\s+"}, name="test.create")
requirements not need for you
name of this action will be project_bundlename_controller_action for you {project}_{bundlename}_test_create whereis i dont know you project name and bundle name - its not a diffucult i think for you. And some code :)