It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
this is my controller main.php this is uploading the file in directory what i want to store the same file name in database and retrieve the image by name. you can tell be the solution code or edit my code really need the code
<?php
class Main extends CI_controller{ // mian controller
function index(){
$this->load->view('main_view.php', array('error'=>''));
}
function upload(){ // upload function for image
$config['upload_path'] = './images/'; //directory
$config['allowed_types'] = 'jpg|jpeg|gif|png';//type allow
$this->load->library('upload',$config);
if(! $this->upload->do_upload()){
$error = array('error'=>$this->upload->display_errors());
$this->load->view('main_view',$error);
}
else
{
//
$file_data = $this->upload->data();
$data['img'] = base_url().'/images/'. $file_data['file_name'] ;
$this->load->view('success_msg',$data);
}
}
}
?>
this is my view main_view.php
<html>
<body>
<? echo $error;?>
<? echo form_open_multipart('main/upload'); ?>
<input type="file" name="userfile" />
<input type="submit" name="submit" value="Upload" />
<?php echo form_close();?>
</body>
</html>
i want to upload file name in database and can easily retrieve the image
You can retrieve the image name wth: $this->upload->file_name
EDIT:
there are some errors in your code.
1) this $this->load->view('main_view.php', array('error'=>''));should be $this->load->view('main_view', array('error'=>''));without the .php extension.
2) CI_controller should be CI_Controller
You can use this simple lines of code to upload a file in a folder and grab the file name and store it in database.
$image_path = '../../test_images/'.$_FILES['question_image']['name'];
if(is_uploaded_file($_FILES['question_image']['tmp_name']))
{
move_uploaded_file($_FILES['question_image']['tmp_name'], $image_path) ;
}
$image_url = "test_images/".$_FILES['question_image']['name'];
$image_path is the url where you want to upload your file and $image_url will contain the url that your are going to store into database
Now you can retrieve the image by
<img src="<?base_url($image_url)?>" />
Related
I know this question is probably going to get downvoted and I will probably get into trouble but I am hoping someone may be able to help me with my situation.
On my site I use json to download data from an external source, and then I style it beautifully.
Within the json data is an individual ID for each data set.
What I want to accomplish is to have a database where I can insert the ID and a url link.
I have created the table within the wordpress database via phpMyAdmin, but I want to create a page within the admin section where I can simply add the data in.
For displaying the json data I use a php insert addon, within that php clip i want to do a piece of code that checks the database for the id within my custom database and displays the link.
I will be honest I don't know where to start on this, even if its just a link to a source that shows me how to create an admin page and submit data to the database within wordpress dashboard.
I really appreciate any help given and like I say I know I should try harder, but when ever I do a search all I get is 100's of references to add an admin to the database manually.
Thanks,
Adam
Edit I just realized I never put any table information in my question.
The table name within wordpress is called: wp_home_tickets
within that are 3 fields: id (auto increasement), gameid (numeric) and ticketlink (text)
thanks.
For adding a custom settings page in your admin, use the Settings API https://codex.wordpress.org/Settings_API
Here is a nice tutorial using it https://deliciousbrains.com/create-wordpress-plugin-settings-page/#wp-settings-api
To fetch data from your custom table, use the wpdb class https://developer.wordpress.org/reference/classes/wpdb/. More specifically, you can use wpdb::get_results if you will have multiple rows sharing the same id https://developer.wordpress.org/reference/classes/wpdb/get_results/
Or wpdb::get_row if you will ever only have one https://developer.wordpress.org/reference/classes/wpdb/get_row/
Hope this helps you out!
For anyone wishing to see how it was done, here is how I did it.
I created a file in my theme called db_admin_menu.php and added the following to it:
<?php
function ticket_admin_menu() {
global $team_page;
add_menu_page( __( 'Tickets', 'sports-bench' ), __( 'Tickets', 'sports-bench' ), 'edit_posts', 'add_data', 'ticket_page_handler', 'dashicons-groups', 6 ) ;
}
add_action( 'admin_menu', 'ticket_admin_menu' );
function ticket_page_handler() {
$table_name = 'wp_home_tickets';
global $wpdb;
echo '<form method="POST" action="?page=add_data">
<label>Team ID: </label><input type="text" name="gameid" /><br />
<label>Ticket Link: </label><input type="text" name="ticketlink" /><br />
<input type="submit" value="submit" />
</form>';
$default = array(
'gameid' => '',
'ticketlink' => '',
);
$item = shortcode_atts( $default, $_REQUEST );
$gameid = $item['gameid'];
if ($wpdb->get_var("SELECT * FROM wp_home_tickets WHERE gameid = $gameid ")) { echo 'Ticket already exists for this game.'; goto skip; }
if (!empty($_POST)) { $wpdb->insert( $table_name, $item ); }
skip:
}
?>
I then put this code in my script that fetches and displays the json:
$matchid = $match['id'];
$ticket_url = $wpdb->get_var("SELECT ticketlink FROM wp_home_tickets WHERE gameid = '$matchid' ");
if ($ticket_url) { echo 'Get Tickets'; }
I hope someone does find it of use, i did have to use a wordpress plugin called `Insert PHP Code Snippet' by xyzscripts to be able to snippet the php to a shortcode, but that is not the purpose of this post.
Thanks again for your help.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
my controller
public function add() {
$this->helpers = array('TinyMCE.TinyMCE');
$this->layout = 'adminpanel';
if ($this->request->is('post')) {
$this->Tour->create();
if ($this->Tour->save($this->request->data)) {
$this->Session->setFlash(__('Your possstt has been saved.'));
return $this->redirect(array('controller'=>'admin','action' => 'tour'));
}
$this->Session->setFlash(__('Unable to add your schedule.'));
}
my view file
echo $this->Form->create('Tour');
echo $this->Form->input('vartitle',array('class' => 'form-control','label' => 'Tour Title'));
// etc.................
echo $this->Form->input('varbigimg', array('type' => 'file'));
echo $this->Form->end('Save Post',array('class' => 'form-control'));
I already check model because image name are stored in database but I want to use that image. I want to save that image in any folder so help me. I am using new version thanks
So tell me how to store image in webroot folder and just save name in database field varbigimg field. I also want to display it on page too. So please solve my problem thanks
I am new in cakephp thanks
Use following
echo $this->Form->create('Tour',array('autocomplete' => 'off','enctype'=>'multipart/form-data'));
Allows image/video data to be processed.
Inside controller
if ($this->request->is('post')) {
if(is_uploaded_file($this->request->data['Tour']['varbigimg']['tmp_name']))
{
$fileNameFull = $this->request->data['Tour']['varbigimg']['name'];
$oldFile = '../../app/webroot/img/'.$fileNameFull;
move_uploaded_file(
$this->request->data['Tour']['varbigimg']['tmp_name'],
$oldFile
);
$this->Tour->create();
$this->request->data['Tour']['varbigimg'] = $fileNameFull;
//HERE you need to specify same for thum
$this->request->data['Tour']['varthumimg'] = $fileNameFull;
////HERE you need to specify same for thumbfield
if ($this->Tour->save($this->request->data)) {
$this->Session->setFlash(__('Your possstt has been saved.'));
return $this->redirect(array('controller'=>'admin','action' => 'tour'));
}
}
}
$this->Session->setFlash(__('Unable to add your schedule.'));
//Update
//Specify input type in your code explicitely here.
echo $this->Form->input('vartitle',array('type'=>'text','class' => 'form-control','label' => 'Tour Title'));
Man , i get to you the needs to make it simply.
First you need access the file in your controller, using $this->data['Tour']['varbigimg'];
ok , it's your file.
Ok, now you need send this file to webroot, you can use File Class of the cakephp
http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html
You can move and copy files to webroot using WWW_ROOT constraint, this constant get to you the correct path to webroot folder, if u need send to webroot img use IMAGES constant.
Using, Folder/File and cakephp constants it's very easy to make your needs.
If you need a more information about this, plz ask me and i create a real example to you.
Tnx
You need to move your uploaded file to a folder. Get the file info of your $this->request->data and move to a folder in webroot.
There are many plugins for that. Like: Upload Plugin 2.0
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
In yii i am creating forget password functionality. after clicking on forgetpassword button, server will provide one php paga with blank text field for entering primary email id. Now how to retrive this email id in controller's method and how to check wheather this email id exist in databse. Please help me...
This will help you get started, you would put a method similar to this in your controller and create a view with a password field on it.
public function actionForgotPassword(){
if(isset($_POST['email']{
$record=User::model()->find(array(
'select'=>'email',
'condition'=>'email=:email',
'params'=>array(':email'=>$_POST['email']))
);
if($record===null) {
$error = 'Email invalid';
} else {
$newpassword = 'newrandomgeneratedpassword';
$record->password = md5($newpassword );
$record->save(); //you might have some issues with the user model when the password is protected for security
//Email new password to user
}
}else{
$this->render('forgetPassword'); //show the view with the password field
}
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am using cake php 2.0 version, I need to send a PDF attached email once clicked on survey button in my application?
Any help greatly appreciated.
Try this:
First of all, you should ensure the class is loaded using App::uses():
<?php
App::uses('CakeEmail', 'Network/Email');
in your function/action:
$email = new CakeEmail();
$email->attachments('/full/file/path/file.pdf')
$email->to = 'em...#email.co.uk';
$email->subject = 'Something';
$email->replyTo = $client['Client']['email'];
$email->from = $client['Client']['email'];
$email->emailFormat = 'html';
if($email->send()){
die('Email Sent!');
}else{
die('Failed to send email');
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I Want to separate the database functionality and logic part by having the database operations in the model and the logic part in the controller. Earlier I had all the code in the action part of the controller itself. I have tried something, but it doesn't work.
This is what I had earlier:
/* Controller */
function insertFormName(){
$formname=$_POST['formname'];
$ret = $this->Form->query("Select id from forms order by id DESC LIMIT 1");
$newid=$ret[0]['forms']['id'];
$this->Form->updateAll(array('Form.name' => "'$formname'"),array('Form.id' => $newid));
}
And now I changed it a bit, which doesn't work:
/* Controller */
function insertformname()
{
$this->data['Form']['formname']=$this->params['form']['formname'];
$this->Form->save($this->data);
$this->Form->updateAll(array('Form.name' => "'$formname'"),array('Form.id' => $newid));
}
/* Model */
function save($data)
{
$ret = $this->Form->query("Select id from forms order by id DESC LIMIT 1");
$newid=$ret[0]['forms']['id'];
$this->Form->updateAll(array('Form.name' => "'$formname'"),array('Form.id' => $newid));
return $newid;
}
EDIT:
I have tried it another way.. Have the entire functionality in the model and just call that function from the controller.Is this method correct?
/* Model */
function saveFormName($data)
{
$this->formname=$data[$this->name]['formname'];
$ret = $this->Form->query("Select id from forms order by id DESC LIMIT 1");
$newid=$ret[0]['forms']['id'];
$this->Form->updateAll(array('Form.name' => "'$formname'"),array('Form.id' => $newid));
}
/* controller */
function insertformname()
{
$this->data['Form']['formname']=$this->params['form']['formname'];
$this->Form->saveFormName($this->data);
}
It looks like you should probably revisit the Cake book (book.cakephp.org) and redo the lessons. If you setup your form correctly, you shouldn't have to manually assign $_POST['formname'] to $this->data. Try setting the field names in your form (in the HTML) to data[ModelName][FieldName].
Next:
$this->Form->updateAll(array('Form.name' => "'$formname'"),array('Form.id' => $newid));
Why are you updating data right after you saved it? Where do the $newid and $formname variables come from? You have them defined in the Model::save, but not in the controller.
This seems like you're trying to fight with the Cake automagic stuff too much. Perhaps you should repost your question, but please spell out your high level description rather than just a "why doesn't this work?" It looks to me like this could be simplified a ton, but, again, I'm not quite sure what your objectives are.
Respectfully,
Travis