why my public variable cannot read by other function?
my public variable :
public $dataku = array();
first i set data to my public variable from model:
function cari() {
$penerima = $this->input->post('penerima');
$tanggal = $this->input->post('tanggal');
$tanggal2 = $this->input->post('tanggal2');
$id_komoditas = $this->input->post('id_komoditas');
$this->dataku['judul'] = $this->judul;
$this->dataku['pencarian'] = $this->mdl->get_data_antara_tanggal($penerima, $tanggal, $tanggal2, $id_komoditas);
$this->dataku['url'] = $this->url;
$title = "Data ".$this->judul;
$subtitle = $this->url;
$content = $this->load->view('cari.php', $this->dataku, true);
$this->load_content($title, $subtitle, $content);
}
but, when i want to read the public variable data again, it hasnt a data :
function check_data(){
$data = isset($this->dataku['pencarian']) ? $this->dataku['pencarian'] : 'no data';
print_r($data);
}
please, i need your help.
you need to access the class properties with $this. Change the following lines:
$this->dataku['judul'] = $this->judul;
$this->dataku['pencarian'] = $this->mdl->get_data_antara_tanggal($penerima, $tanggal, $tanggal2, $id_komoditas);
$this->dataku['url'] = $this->url;
Also edit the following (you have an extra $):
function check_data(){
$data = isset($this->dataku['pencarian']) ? $this->dataku['pencarian'] : 'no data';
print_r($data);
}
Related
I have a controller code like this, but I don't know how to pass the variable in $date="dataInputan ['date']" into the ViewSewing function of the code section: $ date = ........;
i try using return,,
but the this code can't get true condition if
full code
public function index2()
{
if (isset($_POST['dataInputan'])) {
$dataInputan = $_POST['dataInputan'];
$date = $dataInputan['date'];
$rst = $this->dataLineSewingModel->getSewingLine($date);
echo json_encode($rst);
//$this->session->set_flashdata('dateset', $date);
return $date;
} else {
return date('Y-m-d');
}
}
public function viewSewing($GroupID)
{
//$date = $this->session->flashdata('dataset');
$date = $this->index2();
print_r($date);
$data['getGroup'] = $this->dataLineSewingModel->getDetailLineSewing($GroupID, $date);
$data['getTitle'] = $this->dataLineSewingModel->getTitleSewing($GroupID);
$data['GroupID'] = $GroupID;
$this->load->view('total_emp/lineSewingView', $data);
}
please help me
I assume that you want to call controller viewSewing from page viewSewing but you want the data from index2.
I think you can do something like this.
public function index2($dataIn)
{
if (isset($dataIn)) {
$dataInputan = $_POST['dataInputan'];
$date = $dataInputan['date'];
$this->dateAll = $dataInputan['date'];
$rst = $this->dataLineSewingModel->getSewingLine($this->dateAll);
echo json_encode($rst);
}
}
public function ViewSewing($GroupID)
{
$dataInput = $this->input->post('dataInputan');
$date = $dataInput['date'];
$this->index2($dataInput);
$data['getGroup'] = $this->dataLineSewingModel->getDetailLineSewing($GroupID, $date);
$data['getTitle'] = $this->dataLineSewingModel->getTitleSewing($GroupID);
$data['GroupID'] = $GroupID;
$this->load->view('total_emp/lineSewingView', $data);
}
public function ViewSewing($GroupID)
{
if (isset($_POST['dataInputan'])) {
//$_POST['dataInputan'] -> is array
$dataInputan = $_POST['dataInputan'];
$dateAll = $dataInputan['date'];
$rst = $this->dataLineSewingModel->getSewingLine($dateAll);
$json_data = json_encode($rst);
}
if($json_data!=''){
$data['json'] = $json_data;
}
$data['getGroup'] = $this->dataLineSewingModel->getDetailLineSewing($GroupID, $date);
$data['getTitle'] = $this->dataLineSewingModel->getTitleSewing($GroupID);
$data['GroupID'] = $GroupID;
$this->load->view('total_emp/lineSewingView', $data);
}
//this extract the json_data and show in view page
$result = json_decode($json, true));
I am not able to check if there is any data in a variable in laravel 6 with below function.
public function view($slug){
$userdata = Constant_model::getDataOneColumn('users',"username",$slug);
if (!empty($userdata)) {
$data = array(
'title'=>"User Profile - ",
'description'=>"profile",
'seo_keywords'=>'',
'users'=>$userdata
);
return view('view_user_profile',$data);
}else{
$data = array(
'title'=>"Not found ",
'description'=>"profile",
'seo_keywords'=>'',
'users'=>$userdata
);
return view('view_user_profile',$data);
}
}
getDataOneColumn function in Constant_model
public static function getDataOneColumn($table, $col1_name, $col1_value){
$data = DB::table("$table")->where("$col1_name",'=',$col1_value)->get();
return $data;
}
Getting if data even $userdata has no value. please help
Instead of checking if (!empty($userdata)) {
You can check laravel collection like if ($userdata->isNotEmpty()) {
public function view($slug){
$userdata = Constant_model::getDataOneColumn('users',"username",$slug);
if ($userdata->isNotEmpty()) { // Change this line
$data = array(
'title'=>"User Profile - ",
'description'=>"profile",
'seo_keywords'=>'',
'users'=>$userdata
);
return view('view_user_profile',$data);
}else{
$data = array(
'title'=>"Not found ",
'description'=>"profile",
'seo_keywords'=>'',
'users'=>$userdata
);
return view('view_user_profile',$data);
}
}
Use count:
if ($userdata->count() > 0) {
I try to pass the parameters from controller to url.
I try to achieve something like this: http://localhost/management_system/Job/search_job?keyword=word&location=location
for now i only get http://localhost/management_system/Job/search_job
Dose somebody know how i can achieve that?
Below you can see my controller:
public function search_job(){
if ($this->input->post('submit')) {
$location = $this->input->post('location', TRUE);
$keyword = $this->input->post('keyword', TRUE);
$data['all_jobs'] = $this->Job_model->search_job($keyword, $location);
if ($data['all_jobs'] > 0 ) {
$data['categoryes'] = $this->Job_model->get_category();
$this->autoload('job_list', 'eJobs', $data);
}
}else{
redirect('Job/jobs');
}
}
In your action you are waiting for a POST request but this: http://localhost/management_system/Job/search_job?keyword=word&location=location is a GET request.
If your request is this: http://localhost/management_system/Job/search_job?keyword=word&location=location
and you want to get location and keyword you must write code like this:
public function search_job() {
if ($this->input->post('submit')) {
$location = $this->input->get('location', TRUE);
$keyword = $this->input->get('keyword', TRUE);
$data['all_jobs'] = $this->Job_model->search_job($keyword, $location);
if ($data['all_jobs'] > 0 ) {
$data['categoryes'] = $this->Job_model->get_category();
$this->autoload('job_list', 'eJobs', $data);
}
} else {
redirect('Job/jobs');
}
}
You need to change
$this->input->post()
to
$this->input->get()
I have a json api I'm trying to feed to several functions. I'm not entirely sure how to make the file_get_contents global so that it can be accessed by multiple functions at once.
Here is my current PHP code:
function getVideoTitle($getVideoID) {
$json_output = file_get_contents("https://www.googleapis.com/youtube/v3/videos?id=".$getVideoID."&key={MY_KEY}&fields=items(id,snippet(title,description),statistics)&part=snippet,statistics");
$json = json_decode($json_output, true);
$video_title = $json['items'][0]['snippet']['title']; // Video Title
return $video_title;
}
function getVideoDesc($getVideoID) {
$json_output = file_get_contents("https://www.googleapis.com/youtube/v3/videos?id=".$getVideoID."&key={MY_KEY}&fields=items(id,snippet(title,description),statistics)&part=snippet,statistics");
$json = json_decode($json_output, true);
$video_description = $json['items'][0]['snippet']['description']; //Description
return $video_description;
}
echo getVideoTitle($getVideoID);
echo getVideoDesc($getVideoID);
If I remove the two $json variables and place them outside of the function I get an error that it couldn't find the variable $json.
Also, would making it a global variable make it run faster? Retrieving the API is currently running pretty slowly. Would it be wiser to switch to Javascript..? Thanks.
If the $json is supposed to be constant, then I believe you're better to create a class, since you need to provide the $getVideoID for the ID of the video, and additionally you have the ability to change the video conveniently :)
<?php
class MyVideoClass {
private $json = null;
public function __construct($videoID) {
$this->changeVideo($videoID);
}
public function changeVideo($videoID) {
$json_output = file_get_contents("https://www.googleapis.com/youtube/v3/videos?id=".$videoID."&key={MY_KEY}&fields=items(id,snippet(title,description),statistics)&part=snippet,statistics");
$this->json = json_decode($json_output, true);
}
function getVideoTitle() {
$video_title = $this->json['items'][0]['snippet']['title']; // Video Title
return $video_title;
}
function getVideoDesc() {
$video_description = $this->json['items'][0]['snippet']['description']; //Description
return $video_description;
}
}
// somewhere:
$myVideo = new MyVideoClass($yourVideoID);
echo $myVideo->getVideoTitle();
echo $myVideo->getVideoDesc();
// sometime else
$myVideo->changeVideo($anotherVideoID);
echo $myVideo->getVideoTitle();
echo $myVideo->getVideoDesc();
here what i have wrote for you
<?php
class Video{
private $data;
public function __construct($videoId, $key){
$json_output = file_get_contents("https://www.googleapis.com/youtube/v3/videos?id=".$videoId."&key=".$key."&fields=items(id,snippet(title,description),statistics)&part=snippet,statistics");
isset($json_output) ? $this->data = $json_output : $this->data = false;
}
public function getTitle(){
if($this->data){
$video_title = $this->data['items'][0]['snippet']['title']; // Video Description
return $video_title;
}else{
return false;
}
}
public function getDesc(){
if($this->data){
$video_desc = $this->data['items'][0]['snippet']['description']; // Video Title
return $video_desc;
}else{
return false;
}
}
public function change($videoId, $key){
$this->__construct($videoId, $key);
}
}
Usage Example:
include('video.php'); // if you going to save the class in separate file
$video = new Video('Video-Id-Here', 'your-key');
echo $video->getTitle().'<br/>';
echo $video->getDesc();
//new video
$video->change('new-video-id', 'new-key');
echo $video->getTitle().'<br/>';
echo $video->getDesc();
and you can add more functions to the class
I want to send two data arrays from my controller to view how can I do it ?
Following is my controller code
class Home extends CI_Controller {
public function box() {
$url = $this->pageURL();
$id_from_url = explode('/', $url);
$id = $id_from_url[6];
$query = $this->db->get_where('mc_boxes', array('idmc_boxes' => $id));
$row = $query->row();
$rowcount = $query->num_rows();
if ($rowcount <= 0) {
echo 'ID not found';
} else {
$box_id = $row->idmc_boxes;
$customer_id = $row->customers_idcustomers;
$language_id = $row->languages_idlanguages;
$template_id = $this->getTemplateID($box_id);
$template_data = $this->getTemplateData($template_id);
$variables_data = $this->getVariables($customer_id, $language_id);
$title = $variables_data[0]['value'];
$this->load->view('template', $template_data);
}
}
}
In my template view when I echo $title it says it is undefined
how can I send the whole $variables_data array with $template_data array
Thanks :)
Instead of using each array ,all do set to one
Like that,giving important sections only
...................
$data['template_data'] = $this->getTemplateData($template_id);
$data['variables_data'] = $this->getVariables($customer_id, $language_id);
$data['title'] = $variables_data[0]['value'];
$this->load->view('template', $data);
you can take $template_data and $variables_data in view files
Generally you pass in data as:
$data['template_data'] = $template_data;
$data['title'] = $$title;
....
$this->load->view('template', $data);