i use kartik-v/yii2-slider in my project .
with this code i added one slider A range select :
echo '<b class="badge">$10</b> ' . Slider::widget([
'name'=>'rating_3',
'value'=>'250,650',
'sliderColor'=>Slider::TYPE_GREY,
'pluginOptions'=>[
'min'=>10,
'max'=>1000,
'step'=>5,
'range'=>true
],
]) . ' <b class="badge">$1,000</b>';
i have 2 column for min value and max value in (set_money) table :
min_money
max_money
how i can save thats variable in my database !
I do not know how to get that variable in controller
in view file, in form
<?php
echo '<b class="badge">$10</b> ' . Slider::widget([
'name'=>'min_money',
'value'=>'250,650',
'sliderColor'=>Slider::TYPE_GREY,
'pluginOptions'=>[
'min'=>10,
'max'=>1000,
'step'=>5,
'range'=>true
],
]) . ' <b class="badge">$1,000</b>';
in controller
public function actionCreate()
{
$model = new Model; // give your actual model name instead of Model
if($model->load(Yii::$app->request->post()))
{
list($model->min_money, $model->max_money) = explode(',', $model->min_money);
// now both $model->min_money and $model->max_money are set and contains value submitted in form by Kartik Slider Widget
if($model->save(true))
{
// success -> redirect
}
else
{
// error render to form again
}
}
}
Related
I want to have the same name of image in database and in the image folder one I move. In folder is work but in database don't have ideas, can someone help me! Thanks
public function postRegister(Request $request) {
request()->validate([
'email' => 'required',
'password' => 'required|min:8',
'fullname'=>'required',
'birthday'=>'required',
'country'=>'required',
'address'=>'required|min:10',
'image' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048',
]);
$image = $request -> file('image');
$destination = base_path().'/public/img';
$file_name = rand(100,1).date('h-i-s');
$image->move($destination, $file_name.".".$image->getClientOriginalExtension());
$data = $request->all();
// dd($image);
$check = $this->create($data);
return Redirect::to("login")->withSuccess('Great! You have Successfully loggedin');
}
If you want to set your image name to your db record.
Pass it to your $data before calling create method.
public function postRegister(Request $request) {
....
$data = $request->all();
$data['imageName'] = $file_name.".".$image->getClientOriginalExtension();
$check = $this->create($data);
return Redirect::to("login")->withSuccess('Great! You have Successfully loggedin');
}
If you want use mass assignment for save data you can use YourModelName::create(data).but you should pay attention to 2 thing .
first one define all fillable fields that you want allow to assign mass in your model class.so you should add your imageName field in fillable array .
second you should append yourImageName to your data that assign mass.like :data['yourImageField']=setvalue.
But if you want to assign row by row(its more safe and better i think) you should do like this: modelObject->Imagefield="imageName" and at end modelObject->save().
I am trying to return a mysql field that has had the mysql REPLACE function applied to it before it is returned to Cake. I am actually trying to remove text from a field if it matches my variable.
Theoretically inside the find, I want:
'fields' => array('REPLACE(E.title, "'.$data['Make']['name'].'", "") AS E.title', ...)
How do I do this?
Thanks
try to use Virtual Fields in your model
class ModelName extends AppModel {
public $virtualFields = array(
"my_virtual_field_title" => "REPLACE(yourfield,'OLD STR','NEW STR')"
);
}
using :
$your_query= $this->ModelName->find('first');
echo $your_query['ModelName']['my_virtual_field_title'];
or you can set directly the virtualfields :
$this->ModelName->virtualFields['my_virtual_field_title'] = "
CASE
WHEN title = 'custom text' THEN 'custom result'
.
.
.
ELSE ''
END
";
more information :
http://book.cakephp.org/2.0/en/models/virtual-fields.html
Controller :
class gallery extends CI_Controller {
function index()
{
$this->load->model('gallery_model');
if($this->input->post('upload'))
{
$this->gallery_model->do_upload();
}
$data['images'] = $this->gallery_model->get_images();
$this->load->view('gallery',$data);
}
}
Model :
<?php
class gallery_model extends CI_Model {
var $gallery_path;
var $gallery_path_url;
function gallery_model()
{
$this->gallery_path = realpath(APPPATH . '../images');
$this->gallery_path_url = base_url().'images/';
}
function do_upload()
{
$config = array(
'allowed_types' => 'jpeg|jpg|gif|png',
'upload_path' => $this->gallery_path
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->gallery_path. '/thumbs',
'maintain_ration' => TRUE,
'width' => 150,
'height' =>150
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
}
View:
<!DOCTYPE html>
<html>
<head>
<title>My gallery</title>
<meta charset="UTF-8"/>
</head>
<body>
<div id="upload">
<?php
echo form_open_multipart('gallery');
echo form_upload('userfile');
echo form_submit('upload','Upload');
echo form_close();
?>
</div>
</body>
</html>
I want to do something like an photo album. In the upload form i upload photos in images folder and the thumb into thumb folder. What i want to do is to store the titles of pictures into the database and retrieve from there and i don't know how to store the pictures titles.
I would add a text field to your form for Image Title, pass it to your do_upload method from the gallery model, or separately into a new image database model (to connect and query your database).
I'm writing this on the fly and is untested. Some advice to the process, make sure you are cleaning the inputs. Codeigniter does a great job at this, but it's good coding to share that advice. Also, you should like an image with its title. I would maybe also pass to the database, the dir path of the image and/or its thumb path. It also makes calling the image from gallery easier as you are already querying the Database for meta data. Something like a foreach($img in$result){ echo $img['title']." - ".echo $img['path'];} to produce "My title - http://example.com/images/thumb/1234.jpg"
View (input)
..
echo form_input('imgTitle', 'Image Title');
..
Controller
..
if($this->input->post('upload'))
{
$data['imgTitle'] = $this->input->post('imgTitle'); //Assigns the post value to the data array so you can pass it via $data to your views
$this->gallery_model->do_upload($data['imgTitle']);
}
..
Model
..
function do_upload($imgTitle=null) //passes a variable as NULL unless specified
{
..
$DB1 = $this->load->database(); //specify db connection/name
$data = array(
'title' => $imgTitle, //passed from controller
'name' => 'My Name');
$this->db->insert('mytable', $data);
..
}
Im having some trouble getting on with my first codeigniter project, and i have a feeling the answer is really near...
Im populating a form of inputfields from a database, and when the user submits the form, it should run through the rows of the database, and update the content based on the 'id' of each row.
My form (in the view 'admin.php') looks something like this:
<?php echo form_open('admin/update'); ?>
<?php foreach($content as $row) : ?>
<?php echo form_input('id['. $row->id .'][id]', $row->id); ?>
<?php echo form_input('id['. $row->id .'][order]', $row->order); ?>
<?php echo form_input('id['. $row->id .'][title_text]', $row->title_text); ?>
<?php echo form_textarea('id['. $row->id .'][body_text]', $row->body_text); ?>
<?php
if ($row->visibility == 'visible') {
echo form_checkbox('id['. $row->id .'][visibility]', 'visibility', TRUE);
} else {
echo form_checkbox('id['. $row->id .'][visibility]', 'visibility', FALSE);
}
?>
<?php endforeach;?>
<?php echo form_submit('Save Changes', 'Save Changes'); ?>
<?php echo form_close(); ?>
Now, most of this is based on a mix of tutorials and help documents. Here's the code from the controller 'admin', that i call when people click 'Save changes':
function update()
{
$this->load->model('admin_model');
$this->admin_model->updateRecords($_POST['id']);
$this->index();
}
The data is then passed on to the model, wherein the function looks like this:
function updateRecords($data)
{
$this->db->insert_batch('content', $data);
}
This is the only way i have gotten it to somehow work. It inserts the data fine, but adds it in new rows instead of updating the ones already there, according to their unique 'id's.
For good orders sake, here's the columns i have in my mySQL database:
id (PRIMARY and AUTOINCREMENT)
order
title_text
body_text
visibility
origin_date
Thanks alot in advance :)
Here's how I might do it, I added in some basic error handling; Hope this explains itself:
Model
function updateRecords($records)
{
// Method 1
// If you want to proceed if there is an error
$errors = array();
// Method 2
// If you want to rollback if there is an error
$this->db->trans_start();
foreach ($records as $id => $values):
// Your form fields conveniently match the column names
// No need to create the data array, we already have it
// If you want to update `origin_date` just do this:
// $values['origin_date'] = time();
// See if checkbox values were sent
// Assumes visibility is integer
if (empty($values['visibility']))
{
$values['visibility'] = 0;
}
else
{
$values['visibility'] = 1;
}
// Using method 1, store the error id in an array
$this->db
->where('id', $id)
->update('content', $values) OR $errors[] = $id;
// If using method 2, stop the loop
$this->db
->where('id', $id)
->update('content', $values) OR break;
endforeach;
// Method 1
// Return the array of failed updates
// Controller will check if the array is empty
// You can tell the user which updates failed or just how many
return $errors;
// Method 2
// This will return TRUE or FALSE
$this->db->trans_complete();
return $this->db->trans_status();
}
Controller
function update()
{
$records = $this->input->post('id');
$this->load->model('admin_model');
// Make sure to return errors here if they occur
// Using method 1
$errors = $this->admin_model->updateRecords($records);
$num_errors = count($errors);
if ($num_errors > 0)
{
echo "There were {$num_errors} errors!";
// You can optionally print which records had errors
echo "There were errors updating these rows: ".implode(', ', $errors);
}
// Using method 2
$success = $this->admin_model->updateRecords($records);
if ( ! $success)
{
echo "Error performing update, no records were saved.";
}
$this->index();
}
Don't just echo the errors of course, use some kind of message library, session data, or send variables to the view. Some references:
http://codeigniter.com/user_guide/database/transactions.html
http://codeigniter.com/user_guide/database/active_record.html#update
I'd like to create the title of my custom type posts based on the custom fields the user enters. For example i have fields 'first name' and 'last name' and want the title created from whatever the user enters and the permalink to create the slug from it too.
I've tried this at the end of my declaring the custom type in functions.php:
function save_member(){
global $post;
update_post_meta($post->ID, "fname", $_POST["fname"]);
update_post_meta($post->ID, "lname", $_POST["lname"]);
$this_post = array();
$this_post['ID'] = $post->ID;
$this_post['post_title'] = $_POST["fname"].' '.$_POST["lname"];
wp_update_post($this_post);
}
It just hangs if i add the last 4 lines of the code above.
Chances are that you are initiating a loop when wp_update_post is called by firing some do_action() call within there (or related function calls).
Do this:
public static function repair_title( $title ){
global $post;
// only do this for a specific type, may want to
// expand this as needed
if( "your_type" != $post->post_type ) return $title;
$firstname = $_POST["firstname"];
$lastname = $_POST["firstname"];
$title = $firstname . " " . $lastname;
return $title;
}
public static function repair_name( $name ){
global $post;
if( "your_type" != $post->post_type ) return $name;
$firstname = $_POST["firstname"];
$lastname = $_POST["firstname"];
$name = wp_unique_post_slug(
$firstname . " " . $lastname,
$post->ID,
$post->post_type,
0
);
return $name;
}
Edit: changed this upon noting a bit of a bug in my own code (I just happened to be working on this at the moment). I was using get_post_custom() and this is fired before the post object is changed, so you need to grab your values form the $_POST posted form variables.
Cheers!