following along with :CodeIgniter from Scratch: The Calendar Library
The code from the earlier version:
class Mycal extends CI_Controller
{
function display($month, $year) {
$conf = array(
'start_day' => 'Monday',
'show_next_prev' => true,
'next_prev_url' => base_url() . 'mycal/display'
);
$this->load->library('calendar', $conf);
print $this->calendar->generate($month, $year);
}
}
generates these errors:
A PHP Error was encountered
Severity: Warning
Message: Missing argument 1 for Mycal::display()
Filename: controllers/mycal.php
Line Number: 4 A PHP Error was encountered
Severity: Warning
Message: Missing argument 2 for Mycal::display()
Filename: controllers/mycal.php
Line Number: 4 A PHP Error was encountered
Severity: Notice
Message: Undefined variable: year
Filename: controllers/mycal.php
Line Number: 16 A PHP Error was encountered
Severity: Notice
Message: Undefined variable: month
Filename: controllers/mycal.php
Line Number: 16
I updated to the following:
class Mycal extends CI_Controller {
function display($year = null, $month = null) {
$year = $this->uri->segment(3);
$month = $this->uri->segment(4);
$conf = array(
'start_day' => 'Monday',
'show_next_prev' => true,
'next_prev_url' => base_url() . 'mycal/display/'
);
$this->load->library('calendar', $conf);
print $this->calendar->generate($year, $month);
}
}
It works as desired, but is it following best practices for CI?
When the display function is called, $year and $month will be set to the URI segments (or null) for you. The $this->uri->segment call is not needed here.
Also echo is slightly faster than print (according to this).
function display($year = null, $month = null) {
$conf = array(
'start_day' => 'Monday',
'show_next_prev' => true,
'next_prev_url' => base_url() . 'mycal/display/'
);
$this->load->library('calendar', $conf);
echo $this->calendar->generate($year, $month);
}
Building off of Rocket's answer, you need to set the year and month if they're not passed in the url.
By doing $year = null, $month = null as the parameters, it means that if no parameters are passed, $year will equal null and $month will equal null, but if parameters are passed in the url, $year will equal the first parameter and $month will equal the next parameter.
function display($year = null, $month = null) {
$year = ($year == null) ? date('Y') : $year;
$month = ($month == null) ? date('n') : $month;
$conf = array(
'start_day' => 'Monday',
'show_next_prev' => true,
'next_prev_url' => base_url() . 'mycal/display/'
);
$this->load->library('calendar', $conf);
echo $this->calendar->generate($year, $month);
}
Related
I'm writing a laravel application to send sms. However the postfields part is throwing an error. How do I resolve it?
private function sendMessage($message, $recipients) {
$encodeMessage=urlencode($message);
$authkey = 'XYZ';
$senderid = '';
$route = ;
$country = ;
$data = array(
'authkey' => $authkey,
'recipients' => $recipients,
'message' => $encodeMessage,
'sender' => $senderid,
'route' => $route,
'country' => $country,
);
//dd($recipients)
$url = " ";
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURL_POSTFIELDS => $data
));
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);$output=curl_exec($ch);
if(curl_errno($ch)) {
echo 'error '.curl_error($ch);
}
curl_close($ch);
return back()->with('success','Messages sent successfully');
}
This is the error I'm getting:
"Use of undefined constant CURL_POSTFIELDS - assumed 'CURL_POSTFIELDS'
(this will throw an Error in a future version of PHP)"
Your error says that you use a constant that is undefined, which means it has never been declared. Indeed, as it has already been said in the comments, the correct constant is CURLOPT_POSTFIELDS.
This error happens because your php version dont't have the curl extension, try to change this version if you miss, or install the curl extension. To do this, run
sudo apt install php-version-curl
$fileName = storage_path('app') . '/tmp.xlsx';
$file_put_contents($fileName, file_get_contents($path));
$fields['file_name'] = $this->makeCurlFile($fileName);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
protected function makeCurlFile($file)
{
$mime = mime_content_type($file);
$info = pathinfo($file);
$name = $info['basename'];
$output = new \CURLFile($file, $mime, $name);
return $output;
}
I have a URL used in blade template as:
href="{{ route('download', ['year' => $year, 'month' => $month, 'file' => $file_path]) }}"
when I am running my code then it is giving me an error as:
Undefined variable: year (View: C:\wamp64\www\Blog\employee-portal\resources\views\finance\invoice\edit.blade.php)
How can i define this $year variable in my controller?
In my controller the function is written as:
public function download($year, $month, $file, $inline = true)
{
$headers = [
'content-type' => 'application/pdf',
];
$file_path = FileHelper::getFilePath($year, $month, $file);
if (!$file_path) {
return false;
}
if ($inline) {
return Response::make(Storage::get($file_path), 200, $headers);
}
return Storage::download($file_path);
}
}
Edit function is written as:
public function edit(Invoice $invoice)
{
$projectStageBillings = $invoice->projectStageBillings;
$projectStageBilling = $projectStageBillings->first();
$client = $projectStageBilling->projectStage->project->client;
$client->load('projects', 'projects.stages', 'projects.stages.billings');
$billings = [];
foreach ($projectStageBillings as $key => $billing) {
$billing->load('projectStage', 'projectStage.project');
$billings[] = $billing;
}
return view('finance.invoice.edit')->with([
'invoice' => $invoice,
'clients' => Client::select('id', 'name')->get(),
'invoice_client' => $client,
'invoice_billings' => $billings,
]);
}
This error states that the view finance\invoice\edit.blade.php is missing the variable $year. And it is true, take a look at the return of your edit function:
return view('finance.invoice.edit')->with([
'invoice' => $invoice,
'clients' => Client::select('id', 'name')->get(),
'invoice_client' => $client,
'invoice_billings' => $billings,
]);
You are not sending any $year variable to the view here (the variables sent to the view are invoice,clients,invoice_client and invoice_billings.
To solve your problem, just send a $year variable to the view and you'll be ok :)
I've issue with my CMS whenever I tried to Add new page with the following line of code
<?php echo form_open_multipart('admin/page/edit/'. $page->id); ?>
it gives me error
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$id
Filename: page/edit.php
Line Number: 5
my edit function is this which perform add & update functionality
public function edit($id = NULL) {
//Fetch a page or set new one
if ($id) {
$this->data['page'] = $this->page_m->get($id);
count($this->data['page']) || $this->data['errors'][] = 'Page Could not be found';
} else {
$this->data['page'] = $this->page_m->get_new();
}
$id == NULL || $this->data['page'] = $this->page_m->get($id);
//Pages for dropdown
$this->data['pages_no_parents'] = $this->page_m->get_no_parents();
//dump($this->data['pages_no_parents']);
//Setup form
$rules = $this->page_m->rules;
$this->form_validation->set_rules($rules);
//Process the form
if ($this->form_validation->run() == TRUE) {
$data = $this->page_m->array_from_post(array(
'title',
'slug',
'order',
'body',
'template',
'parent_id',
'filename'
));
/* * ***********WORKING FOR IMAGE UPLOAD AND SAVE PATH TO DATABASE*************** */
if (!empty($_FILES['filename'])) {
$fdata = $this->do_upload('filename'); /// you are passing the parameter here
$data['filename'] = base_url() . 'uploads/' . $fdata;
}
$this->page_m->save($data, $id);
// echo '<pre>' . $this->db->last_query() . '</pre>';
redirect('admin/page');
}
//Load the view
$this->data['subview'] = 'admin/page/edit';
$this->load->view('admin/_layout_main', $this->data);
}
public function do_upload($field_name) { // but not retriveing here do this
$field_name = 'filename';
$config = array(
'allowed_types' => '*',
'max_size' => '1024',
'max_width' => '1024',
'max_height' => '768',
'upload_path' => './uploads/'
);
$this->load->library('upload');
$this->upload->initialize($config);
if (!$this->upload->do_upload($field_name)) {
echo $this->upload->display_errors();
die();
$this->data['error'] = array('error' => $this->upload->display_errors());
//$this->data['subview'] = 'admin/page/edit';
//$this->load->view('admin/_layout_main', $this->data);
} else {
$fInfo = $this->upload->data();
//return $fInfo['file_path'].$fInfo['file_name'];
// $this->filename = $fInfo;
return $fInfo['file_name'];
}
}
<?php echo form_open_multipart('admin/page/edit/'. ((isset($page->id)) ? $page->id : '')); ?>
As I mentioned in my comment, if you are creating a new record (I assume:) your page object will not have an id yet, so you just have to do a quick check to make sure it exists and if not output an empty string.
I'm trying to display tweets on a website built on codeigniter but can't seem to pull in the user tweets. To do this, I'm pulling the following script into my header as an include and then printing the tweet in the appropriate section with the code below. I've already set up the access tokens and consumer keys as well. Any ideas on why this is working?
Include file in header
<?php
function buildBaseString($baseURI, $method, $params) {
$r = array();
ksort($params);
foreach($params as $key=>$value){
$r[] = "$key=" . rawurlencode($value);
}
return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
}
function buildAuthorizationHeader($oauth) {
$r = 'Authorization: OAuth ';
$values = array();
foreach($oauth as $key=>$value)
$values[] = "$key=\"" . rawurlencode($value) . "\"";
$r .= implode(', ', $values);
return $r;
}
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
$oauth_access_token = "ACCESS TOKEN HERE";
$oauth_access_token_secret = "ACCESS TOKEN SECRET HERE";
$consumer_key = "CONSUMER KEY HERE";
$consumer_secret = "CONSUMER KEY SECRET HERE";
$oauth = array( 'oauth_consumer_key' => $consumer_key,
'oauth_nonce' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_token' => $oauth_access_token,
'oauth_timestamp' => time(),
'oauth_version' => '1.0'
);
$base_info = buildBaseString($url, 'GET', $oauth);
$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
$oauth['oauth_signature'] = $oauth_signature;
// Make Requests
$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
//CURLOPT_POSTFIELDS => $postfields,
CURLOPT_HEADER => false,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
$twitter_data = json_decode($json, false);
$latest_tweet = $twitter_data[0];
?>
Print tweet
<span class="tweet">"<?php if(!empty($latest_tweet)){echo $latest_tweet->text;} else{echo "Welcome to Time Equities!";} ?>"</span>
I am trying to integrate the following code into my project. it is held in a library
function do_std_login($email, $password) {
$CI =& get_instance();
$login = $CI->users_model->login($email, md5($password));
if($login){
$session_array = array(
'user_id' => $login->user_id,
'name' => $login->name,
'type' => 'Standard'
);
$CI->session->set_userdata($session_array);
// Update last login time
$CI->users_model->update_user(array('last_login' => date('Y-m-d H:i:s', time())), $login->user_id);
return true;
} else {
$this->errors[] = 'Wrong email address/password combination';
return false;
}
}
I am calling it this way:
$login = $this->jaclogin->do_std_login($this->input->post('email'),$this->input->post('password'));
but when I run it I get the following error
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Login::$users_model
Filename: libraries/jaclogin.php
Line Number: 45
I have check I am do load the correct library in the codeigniter autoload file.
Any Ideas?
Thanks
Jamie Norman
Using your CI instance, load your model explicitly in the library like so..
function do_std_login($email, $password) {
$CI =& get_instance();
//--------------
$CI->load->model('users_model'); //<-------Load the Model first
//--------------
$login = $CI->users_model->login($email, md5($password));
if($login){
$session_array = array(
'user_id' => $login->user_id,
'name' => $login->name,
'type' => 'Standard'
);
$CI->session->set_userdata($session_array);
// Update last login time
$CI->users_model->update_user(array('last_login' => date('Y-m-d H:i:s', time())), $login->user_id);
return true;
} else {
$this->errors[] = 'Wrong email address/password combination';
return false;
}
}