Codeigniter - creating a function looping form with controller and method - php

<form method="post" action="/scheduler/run">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Controller</label>
<input type="text" class="form-control" name="form-controller" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Method</label>
<input type="text" class="form-control" name="form-method" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Seconds</label>
<input type="number" class="form-control" name="form-seconds" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Loops</label>
<input type="number" class="form-control" name="form-loops" />
</div>
</div>
</div>
</form>
I have this form data for scheduling, I wanted to run a php function which composed with controller and method, seconds is for how many seconds this works, and loops is how many times.
My attempt:
<?php
class Scheduler extends CI_Controller {
public function run()
{
$form_data = $this->input->post();
for ($i=0;$i < $form_data['form-loops'];$i++) {
$form_data['form-controller'].'/'.$form_data['form-method']; //run the function
sleep($form_data['seconds']);
}
}
}
?>

Try this:
<?php
class Scheduler extends CI_Controller {
public function run()
{
$form_data = $this->input->post();
for ($i=0;$i < $form_data['form-loops'];$i++) {
$method = $form_data['form-controller'].'/'.$form_data['form-method']; //run the function
$this->$method;
sleep($form_data['seconds']);
}
}
}
?>

Related

Call to a member function store() on null (PostsController)

I'm trying to save in database some elements and a photo with a form, I've added the "enctype="multipart/form-data" on the form and I've executed the command "php artisan storage:link", but when I click on Upload button, Laravel returns me this error: "Call to a member function store() on null" on file PostsController
Here is my file PostsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Post;
class PostsController extends Controller
{
public function submitpost(Request $req)
{
$posttitle = $req->input('posttitle');
$postauthor = $req->input('postauthor');
$postcontent = $req->input('postcontent');
$img = $req->input('img')->store('public/img');
$dati = compact('posttitle', 'postauthor', 'postcontent', 'img');
$b = new Post();
$b->posttitle = $posttitle;
$b->postauthor = $postauthor;
$b->postcontent = $postcontent;
$b->img = $img;
$b->save();
$posts = Post::all();
return view('blog', compact('posts'));
}
}
This is my view file addpost.blade.php
<main class="main-content">
<div class="container-fluid photos">
<div class="row justify-content-center">
<div class="col-md-6 pt-4" data-aos="fade-up">
<h2 class="text-white mb-4">Create a new post</h2>
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="col-md-12">
<form action="{{route('submitpost')}}" enctype="multipart/form-data" method="post">
#csrf
<div class="row form-group">
<div class="col-md-12">
<label class="text-white" for="subject">Post Title</label>
<input type="subject" name="posttitle" id="subject" placeholder="Give a title to the post" class="form-control">
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<input type="subject" readonly hidden name="postauthor" id="subject" value="{{Auth::user()->name}}" class="form-control">
</div>
</div>
<div class="row form-group mb-5">
<div class="col-md-12">
<label class="text-white" for="message">Content of post</label>
<textarea name="postcontent" id="message" cols="30" rows="7" class="form-control" placeholder="Write your post here"></textarea>
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<input type="file" name="img" value="{{old('img')}}" placeholder="Image" value="">
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<input type="submit" value="Create Post" class="btn btn-primary btn-md text-white">
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</main>
This is my Route in web.php
Route::post('/addpost/submitpost', 'PostsController#submitpost')->name('submitpost');
And this is my model Post.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $fillable = [
'posttitle',
'postauthor',
'postcontent',
'img',
];
protected $table = 'posts';
}
As you can see in the documentation, uploaded files can be accessed using the file() method.
Try updating this:
$img = $req->input('img')->store('public/img');
To this:
$path = $req->file('img')->store('public/img');
^^^^^^^^^^
Also, you can confirm if the request actually has the file in its payload checking if the file exists, to avoid exceptions:
if ($req->hasFile('img')) {
$path = $req->file('img')->store('public/img');
}
This and more useful methods, can be seen in the File Uploads section of the docs.

form validation and record insert not working in codeigniter

Form validation and record insert not working in codeigniter
model
class Mymodel extends CI_Model
{
function insert($data)
{
$this->db->insert("users", $data);
}
}
controller
defined('BASEPATH') OR exit('No direct script access allowed');
class Mycontroller extends CI_controller
{
function index()
{
$this->load->view('myfolder/my_page');
}
function login()
{
$this->load->view('myfolder/login');
}
function signup()
{
$this->load->view('myfolder/signup');
}
function signupmethod()
{
$this->load->library('form_validation');
$this->form_validation->set_rules("firstname","First Name",'requird|alpha');
$this->form_validation->set_rules("lastname","Last Name",'requird|alpha');
$this->form_validation->set_rules("email","Email",'requird|alpha');
$this->form_validation->set_rules("password","Password",'requird|alpha');
$this->form_validation->set_rules("mobile","Mobile",'requird|alpha');
if ($this->form_validation->run())
{
$this->load->model("mymodel");
$data = array(
"firstname" => $this->input->post("firstname"),
"lastname" => $this->input->post("lastname"),
"email" => $this->input->post("email"),
"password" => $this->input->post("password"),
"mobile" => $this->input->post("mobile"),
"curtime" => "NOW()"
);
if($this->input->post("Signup"))
{
$this->mymodel->insert($data);
redirect(base_url() . "mycontroller/Inserted");
}
}
}
public function Inserted()
{
$this->index();
}
}
?>
view(html code)
<?php include('header.php'); ?>
<form method="post" action="<?php echo base_url()?>mycontroller/signupmethod" enctype="multipart/form-data">
<div class="container">
<div class="row">
<h3>Login</h3>
</div>
<div class="row">
<div class="col-md-6 form-group">
<label>First Name</label>
<input type="text" name="firstname" value="" class="form-control">
<span class="text-danger"><?php echo form_error("firstname"); ?></span>
</div>
<div class="col-md-6 form-group">
<label>Last Name</label>
<input type="text" name="lastname" value="" class="form-control">
<span class="text-danger"><?php echo form_error("lastname"); ?></span>
</div>
</div>
<div class="row">
<div class="col-md-6 form-group">
<label>Email</label>
<input type="text" name="email" value="" class="form-control">
<span class="text-danger"><?php echo form_error("email"); ?></span>
</div>
<div class="col-md-6 form-group">
<label>Password</label>
<input type="password" name="password" value="" class="form-control">
<span class="text-danger"><?php echo form_error("password"); ?></span>
</div>
</div>
<div class="row">
<div class="col-md-6 form-group">
<label>Mobile</label>
<input type="text" name="mobile" value="" class="form-control">
<span class="text-danger"><?php echo form_error("mobile"); ?></span>
</div>
<div class="col-md-6 form-group" style="margin-top: 23px;">
<input type="submit" name="submit" value="Signup" class="btn btn-info">
</div>
</div>
I have read every line carefully, code run but no errors showing and validation and record insert are not working. and no errors showing.
please help.
Change controller this block of code when you fetch form elements in controller always use the element name
if($this->input->post("submit"))
{
$this->mymodel->insert($data);
redirect(base_url() . "mycontroller/Inserted");
}
Correct spelling of required
$this->form_validation->set_rules("firstname","First Name",'required|alpha');
Your set_rules statements are incorrect. Use required instead of requird otherwise set_rules will fail.
When you are not sure why form_validation is failing, try getting the errors with:
If($this->form_validation->run() == false)
{
echo validation_error();
}

How to get data from database table in codeigniter?

I need to get the data that has been stored in my database to print in my website.I want to input the user entered word and then store it in a table after that i want it to show on a page.I have already saved created a form that takes the user entered data and stores it in a table but have not been able to display the data.I want the data entered by the user be available in any file
Here is my View
<form class="form-horizontal"
action="<?php echo base_url() ?>index.php/submit/new_form_submit" method="post">
<fieldset>
<br>
<div class="form-group">
<label class="col-md-4 control-label" for="Title">Title</label>
<div class="col-md-4">
<input id="Title" name="Title" type="text" placeholder="" class="form-control input-md" required="">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="Price">Price</label>
<div class="col-md-4">
<input id="Price" name="Price" type="number" placeholder="" class="form-control input-md" required="">
</div>
</div>
<!-- Textarea -->
<div class="form-group">
<label class="col-md-4 control-label" for="textarea">Describe your product</label>
<div class="col-md-4">
<textarea class="form-control" id="textarea" name="textarea"></textarea>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="Link">Link to Live preview</label>
<div class="col-md-4">
<input id="Link" name="Link" type="url" placeholder="e.g http://www.example.com" class="form-control input-md" required="">
</div>
</div>
<!-- File Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="filebutton">Screenshot of your theme</label>
<div class="col-md-4">
<label for="file-input">
<!-- <div class="thumbnail">
<img src="<?/*= base_url() */?>Images/placeholder.jpg"/>
</div>-->
</label>
<input id="file-input" type="file"/>
</div>
</div>
<br>
<!-- Button (Double) -->
<div class="form-group">
<label class="col-md-4 control-label" for="button1id"></label>
<div class="col-md-8">
<button type="submit" class="btn btn-success">Save</button>
<a id="cancel" name="cancel" class="btn btn-danger" href="<?php echo base_url(); ?>index.php/home">
Cancel</a>
</div>
</fieldset>
Here is my Controller
<?php
session_start();
class Submit extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('security');
$this->load->helper('url');
$this->load->helper('form');
$this->load->model('Submit_Database');
$this->load->library('form_validation');
}
public function index()
{
$this->load->view('templates/header');
$this->load->view('submitf/submit');
$this->load->view('templates/footer');
}
public function new_form_submit()
{
$this->load->helper('url');
$this->form_validation->set_rules('Title', 'Title', 'trim|required|xss_clean');
if ($this->form_validation->run() == FALSE) {
$this->load->view('templates/header');
$this->load->view('pages/home');
$this->load->view('templates/footer');
} else {
$data = array(
'Title' => $this->input->post('Title'),
'Price' => $this->input->post('Price'),
'textarea' => $this->input->post('textarea'),
'Link' => $this->input->post('Link')
);
$result = $this->Submit_Database->submit_insert($data);
if ($result == TRUE) {
$this->load->view('templates/header');
$this->load->view('pages/about');
$this->load->view('templates/footer');
} else {
$this->load->view('templates/header');
$this->load->view('submitf/submit');
$this->load->view('templates/footer');
}
}
redirect('');
}
}
Here is my model
<?php
Class Submit_Database extends CI_Model
{
function __construct()
{
parent::__construct(); // construct the Model class
$this->load->database();
}
// Insert registration data in database
public function submit_insert($data)
{
// Query to insert data in database
$this->db->insert('submit', $data);
}
}
Any help would be much appreciated!
The logic is this:
You need a function in model to select from the database.
A function in controller to call the model function and load the view with the data.
And a view file to actually display the data.
You can use sessions in order to store the data in a session variable and access it in any file. You have to load the session library.

After clicking hyperlink in view, function in controller file is not responding - CodeIgniter

I am trying to echo by calling register method in Home.php controller page (in fact I am calling another page in register method) after clicking "Click here to register" hyperlink in View. However, I am getting a Page not found error. Not sure whats happening.
I checked few solutions in stackoverflow but could not be able to figure out the issue. Thanks in advance. Really Appreciated!
This is my Controller file called Home.php:
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Home extends CI_Controller
{
public function __construct()
{
parent::__construct();
//$this->load->database();
}
public function index()
{
//$this->load->helper('url');
$this->load->view('index_login');
//echo "hjkaj";
}
public function register()
{
// $this->load->view('Register');
echo "welcome to ksj";
}
}
?>
This is my View file called index_login.php:
<?php
$this->load->helper('url');
?>
<form class="form-horizontal container well" action="search.php" method="post">
<div class="form-group">
<label for="inputEmail3" class="col-md-2 control-label">UserName</label>
<div class="col-md-5">
<input type="text" class="form-control" id="username" name="username" placeholder="User Name">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-md-2 control-label">Password</label>
<div class="col-md-5">
<input type="password" class="form-control" id="password" name="password" placeholder="Password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-md-2">
<button type="submit" class="btn btn-default">Sign in</button>
</div>
</div>
<b>New User?</b></br> <a href="<?php
echo "index.php/home/register";
?>"> Click here to register</a>
</form>
add base_url() funtion with your link.
<b>New User?</b></br> <a href="<?php
echo base_url()."index.php/home/register";
?>"> Click here to register</a>

insert into form doesn't insert into table ? with codeigniter framework

database details:
dbtable : guests
dbcolumns : id (A_I), guestname, guestemail
when i submit a data it's only refresh the page and take no action i'm beginner please tell me where's the problem and why
i made insert form by basic php and i passed but with codeigniter maybe i'm not fully understood yet looks like my experience about 1% or 5% :D :D
Controller/Guests.php :
<?php
class Guests extends CI_Controller {
public function index($page = 'guests')
{
if ( ! file_exists(APPPATH.'/views/main/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page);
$data['pagedesc'] = 'Guests List';
$this->load->view('include/header', $data);
$this->load->model('guests_model');
$data['records'] = $this->guests_model->getAll();
$this->load->view('main/guests', $data);
$this->load->view('include/footer', $data);
}
public function addnewguest($page = 'Add New Guest')
{
if ( ! file_exists(APPPATH.'/views/main/addnewguest.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page);
$data['pagedesc'] = 'Please Fill Informations Below';
$this->load->view('include/header', $data); // loaded the header
$this->load->model('guests_model'); // loaded the model
$this->load->view('main/addnewguest', $data); // loaded add new form
if($this->input->post('createnew')) { // if click add send information to addnewguestpost() function in the guest_model file
$this->guests_model->addnewguestfunc();
}
$this->load->view('include/footer', $data); // loaded the footer
}
}
views/main/addnewguest.php
<form class="form-horizontal" action="<?php echo site_url('guests/addnewguest'); ?>" method="post">
<div class="form-group">
<label for="guestname" class="col-sm-2 control-label">Guest Name</label>
<div class="col-sm-3">
<input type="text" name="guestname" class="form-control" id="guestname" placeholder="Guest Name ...">
</div>
</div>
<div class="form-group">
<label for="guestemail" class="col-sm-2 control-label">Guest Email</label>
<div class="col-sm-3">
<input type="text" name="guestemail" class="form-control" id="guestemail" placeholder="Guest Email ..">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default" name="createnew">Add</button>
</div>
</div>
</form>
models/guests_model.php
<?php
class Guests_model extends CI_Model {
public function getAll() {
$query = $this->db->get('guests');
if($query->num_rows() > 0){
return $query->result();
} else {
return FALSE;
}
}
public function addnewguestfunc() {
$data = array(
'guestname' => $this->input->post('guestname'),
'guestemail' => $this->input->post('guestemail')
);
$this->db->insert('guests', $data);
}
}
Solved !!
forgot change button to input because i copied it from bootstrap basic templates for practicing ..
views/main/addnewguest.php after change button to input :
<form class="form-horizontal" action="<?php echo site_url('guests/addnewguest'); ?>" method="post">
<div class="form-group">
<label for="guestname" class="col-sm-2 control-label">Guest Name</label>
<div class="col-sm-3">
<input type="text" name="guestname" class="form-control" id="guestname" placeholder="Guest Name ...">
</div>
</div>
<div class="form-group">
<label for="guestemail" class="col-sm-2 control-label">Guest Email</label>
<div class="col-sm-3">
<input type="text" name="guestemail" class="form-control" id="guestemail" placeholder="Guest Email ..">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" class="btn btn-default" name="createnew">Add</input>
</div>
</div>
</form>

Categories