Route Controller function to welcome.blade file - php

I have created a function to generate the chart in a TestChart.php controller.
now i need to call that function in my welcome.blade file. but i don't know how to do that.All i get is that the genChart function is undefined(welcome.blade file)
The genChart function is what is want to call in the welcome.blade file
Controller(where the chart is created):
public function index()
{
$columns = array();
$charts = array();
foreach ($columns as $column) {
$charts[$column] = $this->genChart("ami_demo2018", $column);
}
return view('welcome')->with('charts', $charts);
}
function genChart($table, $column)
{
$labels = Array();
$data = Array();
$distinct = DB::select("select distinct `{$column}` FROM `{$table}`");
$sql = "SELECT ";
foreach ($distinct as $key => $value) {
foreach ($value as $key1 => $value1) {
$sql .= "SUM(case when `{$column}` = '{$value1}' THEN 1 END ) AS `{$value1}`,";
if (empty($value1)){
$value1 = 'Niet ingevuld';
}
array_push($labels, $value1);
}
}
$sql = rtrim($sql, ',');
$sql .= " From {$table}";
$result = DB::select($sql);
foreach ($result as $row) {
foreach ($row as $column => $value) {
array_push($data, (int)$value);
}
}
$chart = new chart();
$chart->labels($labels);
$chart->dataset('First Dataset', "bar", $data);
return $chart;
}
the welcome.blade file(where i want to call the genChart function):
<?php
if(isset($_GET['submit'])){
$selected_val = $_GET['select'];
genChart($table, $selected_val);
}
?>
</div>
<div id="container">
#foreach($charts as $chart)
{!! $chart->container() !!}
{!! $chart->script() !!}
#endforeach
</div>

You are using $this->getChart() and defining it as a normal function. You will need to define it as a class function.
Change:
function genChart($table, $column)
To:
public function getChart($table, $column)
This will define it as a class function/method.

First write a route in web.php
Route::post('/getChart/{table}/{selected_val}',ControllerName#genChart)->name('genChart');
After that in blade file on submit write the route
route('genChart',['table'=>$table,'selected_val'=>$selected_val])
Make your function genChart public to access...
i.e public function genChart()

Related

How looks good structure of construct for creating multidimensial array

I am trying to create constructor which creates an multidimensional array. My result should be like this:-
Checkout my array $result_array
For now I have error: Illegal offset type. Note that I have als use __toString() becose I work on xml data.
class Property {
public $xmlClass;
public $elemClass = '';
public $first_array = array();
public $result_array = array();
public $data = '';
public $data2 = '';
public function __construct($xml, $elem) {
$this->xmlClass = $xml;
$this->elemClass = $elem;
foreach ($xml->xpath('//*[#baza]') as $val) {
$this->first_array[] = $val;
foreach ($val->ksiazka as $value) {
$data = $value->$elem->__toString();
$this->result_array[$this->first_array][] = $data;
}
}
}
public function getResult() {
return $this->result_array;
}
}
$result_autor = new Property($xml, 'autor');
$autor = $result_autor->getResult();
You need to change your two foreach() like below:-
foreach($xml->xpath('//*[#baza]') as $val) {
//$this->first_array[] = $val; not needed
foreach($val->ksiazka as $key=> $value){ //check $key here
$data = $value->$elem->__toString();
$this->result_array[$key][] = $data; // add $key hear
}
}
If the above not worked then check this too:-
foreach($xml->xpath('//*[#baza]') as $key=> $val) { //check $key here
//$this->first_array[] = $val; not needed
foreach($val->ksiazka as $value){
$data = $value->$elem->__toString();
$this->result_array[$key][] = $data; // add $key hear
}
}

How to get six character of a string in SQL code in laravel5?

I create a table with 5 columns and 'benefits' is one of the column.I can get whole data but I need to get the first 6 letters from the 'benefits' field. The benefits column have more than 300.
my controller code
public function List(Request $request)
{
if($request->ajax()){
$data = Package::get();
$list = array();
$i=1;
foreach ($data as $key => $Package) {
$dd = array();
if($Package->free_benifits != ""){
array_push($dd, '<center>'.$Package->free_benifits.'</center>');
}else {
array_push($dd, '<center>-</center>');
}
}
You can use substr function to cut a string with start and length
public function List(Request $request)
{
if($request->ajax()){
$data = Package::get();
$list = array();
$i=1;
foreach ($data as $key => $Package) {
$dd = array();
if($Package->free_benifits != ""){
$benifits=substr($Package->free_benifits,0,5);
array_push($dd, '<center>'.$benifits.'</center>');
}else {
array_push($dd, '<center>-</center>');
}
}

insert foreach controllers on CodeIgniter

I want to try to add foreach on controllers , because before I show you foreach on views
This my Controllers
$data['report_data'] = $this->hasil_m->get_data($nomor);
//create foreach here, from data $data['report_data']
$data=array('title' =>'KOPKAR - Pelanggan',
'isi' =>'admin/hasil'
);
$this->load->view('dashboard/wrapper',$data);
This my Models
$this->db->select('*');
$this->db->from('tb_produk as pro');
$this->db->join('tb_reseller as res', 'pro.reseller_produk = res.nomor_reseller');
$this->db->join('tb_pelanggan as pel', 'pro.id_barcode = pel.id_barcode');
$this->db->select_sum('jumlah');
$this->db->group_by('res.nomor_reseller');
$ambil = $this->db->get('');
if ($ambil->num_rows() > 0) {
foreach ($ambil->result() as $data) {
$hasil[] = $data;
}
return $hasil;
}
This my Views
Instead of doing :
foreach ($ambil->result() as $data) {
$hasil[] = $data;
}
return $hasil;
just
return $ambil->result_array();

Array printed correctly in controller,but not printed in the view page

print_r($listb) is working correctly in controller. but when we are passing this array to view page, its not working in the view page. Controller and view page and Model function is given below:
Controller
public function index() {
$this->load->helper('url');
$this->load->view('user/templates/header');
$data['banner'] = $this->banner_model->viewbanner();
$this->load->view('user/templates/sidebar', $data);
$data1['list1'] = $this->featured_model->viewfeaturedlist(1);
$listb = array();
foreach ($data1['list1'] as $list1) {
$list = explode(',', $list1->fet_list);
$type = $list1->fet_type;
foreach ($list as $pid) {
if ($type == 1) {
$listb['hhh'] = $this->featured_model->viewb2bproduct($pid);
//print_r($listb);
}
}
}
$this->load->view('user/templates/featured', $listb);
exit;
$this->load->view('user/templates/footer');
}
}`enter code here`
View
<?php
print_r($hhh);
?>
Model
public function viewb2bproduct($id) {
$this->db->select('*');
$this->db->from('jil_products');
$this->db->where('prd_id', $id);
$query = $this->db->get();
return $query->result();
}
In your controller Change this Line :
$listb['hhh'] = $this->featured_model->viewb2bproduct($pid);
to
$listb['hhh'][] = $this->featured_model->viewb2bproduct($pid);
In view :
<?php
foreach ($fff as $product){
echo $products[0]['prd_id']."<br>";
echo $products[0]['prd_name']."<br>"; //and so on
}
Your $listb['hhh'] is in foreach loop and you are overriding it each time for every element of $list. and in controller you are printing it inside the foreach loop thats why it is giving output but not in your view.
In model
public function viewb2bproduct($id) {
$this->db->select('*');
$this->db->from('jil_products');
$this->db->where('prd_id', $id);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
In controller
public function index()
{
$this->load->helper('url');
$data['banner'] = $this->banner_model->viewbanner();
$data1['list1'] = $this->featured_model->viewfeaturedlist(1);
$this->load->view('user/templates/sidebar', $data);
$this->load->view('user/templates/header');
$this->load->view('user/templates/featured', $listb);
$this->load->view('user/templates/footer');
}
}
and in view
<?php
foreach ($list1 as $new_list1)
{
echo "<p>".$new_list1['table_field']."</p>"
}
I have no idea on this part
$listb = array();
foreach ($data1['list1'] as $list1)
{
$list = explode(',', $list1->fet_list);
$type = $list1->fet_type;
foreach ($list as $pid) {
if ($type == 1) {
$listb['hhh'] = $this->featured_model->viewb2bproduct($pid);
//print_r($listb);
}
}
}
$hhh=array();
foreach ($data1['list1'] as $list1) {
$list = explode(',', $list1->fet_list);
$type = $list1->fet_type;
foreach ($list as $pid) {
if ($type == 1) {
$hhh[] = $this->featured_model->viewb2bproduct($pid);
//print_r($listb);
}
}
}
$listb['hhh']=$hhh;
$this->load->view('user/templates/featured', $listb);

Using foreach in codeigniter view to generate select options

I ma using codeigniter to generate some html options,but i only get one result,first result from the table.
This is my controller
public function edit_event($id = 0){
$id = $this->uri->segment(3);
$current_id = $this->ion_auth->get_user_id();
$data['data'] = $this->as->the_event_edit($id);
$data['groups'] = $this->as->the_groups_list($current_id);
$this->load->view('editevent',$data);
}
This is my model
public function the_groups_list($current_id){
$query = $this->db->get_where('all_groups', array('group_owner' => $current_id));
foreach ($query->result() as $row)
{
$data = array(
'group_title' => $row->group_title,
'group_name' => $row->group_name,
'group_owner' => $row->group_owner
);
return $data;
}
}
This is the other model
public function as_event_edit($id){
$query = $this->db->get_where('all_scheduled_messages', array('id' => $id));
foreach ($query->result() as $row)
{
$data = array(
'as_title' => $row->as_title,
'as_event_name' => $row->as_event_name,
'as_owner' => $row->as_owner,
'as_type' => $row->as_type,
'as_target_dataset' => $row->as_target_dataset,
'as_timestamp' => $row->as_timestamp,
'as_time' => $row->as_time,
'as_day' => $row->as_day
);
return $data;
}
}
I am then using $groups['group_title'] in view and only the first group title gets displayed even though i have like 4 group titles from the other rows.
How can i return and pass an array that i can then to the view so as to use foreach to iterate and display the group titles?.
In your model you're not creating a multi-dimensional array. You need to add keys either using a counter:
$data = array();
$i=0;
foreach ($query->result() as $row){
$data[$i]['as_title'] = $row->as_title;
$data[$i]['as_event_name'] = $row->as_event_name;
$data[$i]['as_owner'] = $row->as_owner;
$data[$i]['as_type'] = $row->as_type;
$data[$i]['as_target_dataset'] = $row->as_target_dataset;
$data[$i]['as_timestamp'] = $row->as_timestamp;
$data[$i]['as_time'] = $row->as_time;
$data[$i]['as_day'] = $row->as_day;
$i++;
);
return $data;
or use the key of the incoming array
$data = array();
foreach ($query->result() as $id => $row){
$data[$id]['as_title'] = $row->as_title;
$data[$id]['as_event_name'] = $row->as_event_name;
$data[$id]['as_owner'] = $row->as_owner;
$data[$id]['as_type'] = $row->as_type;
$data[$id]['as_target_dataset'] = $row->as_target_dataset;
$data[$id]['as_timestamp'] = $row->as_timestamp;
$data[$id]['as_time'] = $row->as_time;
$data[$id]['as_day'] = $row->as_day;
);
return $data;
Change the model from $data = to $data[] = to insert each row and not only one.
In your view loop over the groups, like so:
foreach ($data as $group) {
echo "Title" . $groups['group_title'];
}
RTM: Adding Dynamic Data to the View
In the controller: rename your $data['data'] to $data['event']
$data['event'] = $this->as->the_event_edit($id);
$data['groups'] = $this->as->the_groups_list($current_id);
$this->load->view('editevent',$data);
In your view:
foreach ($event as $items) {
var_dump($items);
}
foreach ($groups as $group) {
var_dump($group);
}

Categories