CodeIgniter variable data/ value not passing to the view from the Controller - php

I need to echo out a variable value in the CodeIgniter view file. But when I run the site with...
<?php echo $highcharts; ?>
...on line 108, in the view, it gives an error. Please help me to echo out the value, so I can use it in HighCharts.
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: highcharts
Filename: reports/bqt_daily_income_view.php
Line Number: 108
This is my Controller:
function bqt_daily_income() {
$json_data = array();
$query = $this->db->query("SELECT time_stamp, SUM(paid) AS income FROM payments WHERE type = 'Banquet Reservation' GROUP BY time_stamp ORDER BY time_stamp ASC LIMIT 30");
if ($query->num_rows() > 0) {
foreach($query->result_array() as $row) {
$json_data['data'][] = (int) $row['income'];
}
}
$highcharts = json_encode($json_data);
//return $json_data;
//print_r($json_data); // Array ( [data] => Array ( [0] => 1700 [1] => 5000 ) )
//print_r($highcharts); // {"data":[1700,5000]}
$this->load->view('reports/bqt_daily_income_view', $highcharts);

Try like:
$this->load->view('reports/bqt_daily_income_view', array('highcharts' => $highcharts));
In view file:
echo $highcharts;
Alternative post method:
$this->data['highcharts'] = json_encode($json_data);
$this->data['other'] = "bla bla bla";
$this->load->view('reports/bqt_daily_income_view', $this->data);
In view file:
echo $highcharts;
echo $other;

I think you need:
$data['highcharts'] = json_encode($json_data);
$this->load->view('reports/bqt_daily_income_view', $data);

You are supposed to pass the array of data to the view and the keys of array will become the the variables accessible in views
function bqt_daily_income() {
$json_data = array();
$query = $this->db->query("SELECT time_stamp, SUM(paid) AS income FROM payments WHERE type = 'Banquet Reservation' GROUP BY time_stamp ORDER BY time_stamp ASC LIMIT 30");
if ($query->num_rows() > 0) {
foreach($query->result_array() as $row) {
$json_data['data'][] = (int) $row['income'];
}
}
$highcharts['highcharts'] = json_encode($json_data);
//return $json_data;
//print_r($json_data); // Array ( [data] => Array ( [0] => 1700 [1] => 5000 ) )
//print_r($highcharts); // {"data":[1700,5000]}
$this->load->view('reports/bqt_daily_income_view', $highcharts);
}
in view
<?php echo print_r(json_decode($highcharts)); ?>

Related

php get the all array values in a single array with key name

I write these function for fetch records from database in php.But in this array i get all the values in a single array.I want to get
this type of result.What to do for get this type of array.
Example:
Array
(
[city] => Array
(
[0] => ABINGTON
[1] => CHELTENHAM
[2] => PHILADELPHIA
)
[state] => Array
(
[0] => Florida
[1] => Other
)
)
public function selectLocations($POST)
{
$f1=0;
$location = array();
$SELECT_CITY = "SELECT DISTINCT city FROM listings WHERE Matrix_Unique_ID > 0 LIMIT 0,5";
$cityresult = $this->conn->query($SELECT_CITY);
while($row = $cityresult->fetch_assoc())
{
$location[$f1] = $row['city'];
$f1++;
}
$SELECT_STATE = "SELECT DISTINCT state_or_province FROM listings WHERE Matrix_Unique_ID > 0";
$stateresult = $this->conn->query($SELECT_STATE);
while($row1 = $stateresult->fetch_assoc())
{
$location[$f1] = $row1['state_or_province'];
$f1++;
}
return $location;
}
Try the following , it should work for you .
public function selectLocations($POST)
{
$location = array();
$SELECT_CITY = "SELECT DISTINCT city FROM listings WHERE Matrix_Unique_ID > 0 LIMIT 0,5";
$cityresult = $this->conn->query($SELECT_CITY);
while($row = $cityresult->fetch_assoc())
{
$location['city'][] = $row['city'];
}
$SELECT_STATE = "SELECT DISTINCT state_or_province FROM listings WHERE Matrix_Unique_ID > 0";
$stateresult = $this->conn->query($SELECT_STATE);
while($row1 = $stateresult->fetch_assoc())
{
$location['state'][] = $row1['state_or_province'];
}
return $location;
}

push array data in every index of an array using codeigniter php

I have two tables sport_tbl, match_tbl. In sport_tbl, i defined sport_name such as cricket. In match_tbl, I have match_name,match_date,sport_id.
I want to show match_date of every sport_name (ex. i am showing match_date list for cricket sport and i want to show every date has match_name list).
I want to show one distinct match_date.
Image
my controller code:-
$url = 'cricket' // for example first sport_name
$data['getSportMatch'] = $this->user_model->getSportMatch($url);
my model code:-
public function getSportMatch($sport)
{
$query = $this->db->get_where('match_tbl',array('sport_name' => $sport));
if($query->num_rows > 0)
{
foreach($query->result() as $item){
$data[] = $item;
}
return $data;
}
}
my code in view:-
<div><?php foreach($getSport as $item): ?><h4><?= $item->sport_name; ?></h4><div><?= foreach($getSportMatch as $item): ?>
match_date)) ?>here i want to show list match_name of every match_date
My table structure images
1) sport_tbl
2) match_tbl
3) another match_tbl
you can solve this in model easily. if i did not understand wrong . you need 2 function in model.
1. will get sport names
2. will get matches of given sport name
//model functions
function get_sports(){
$data = array();
$sports = $this->db->select('sport_name')->from('sport_tbl')->get()->result();
if($sports)
{
foreach($sports as $sport){
$data[$sport->sport_name] = $this->get_matches($sport->sport_name);
}
return $data;
}
}
function get_matches($sport_name){
$matches = $this->db->select('*')->from('match_tbl')->where('sport_name',$sport_name)->get()->result();
return $matches;
}
so in view data will be something like this
$data => array(
'cricket'=> array(0 => array(
'match_id' => 11,
'sport_id' = 2 .....
)))
Try this coding ...
public function getSportMatch($sport)
{
$query = $this->db->query("SELECT * FROM sport_tbl as st INNER JOIN match_tbl as mt ON st.sport_id = mt.sport_id WHERE st.sport_name ='".$sport."'");
if($query->num_rows > 0)
{
$query_result = $query->result_array();
$final_result = array();
foreach($query_result as $result ) {
$date = $result['match_date'];
$final_result[$date][] = $result;
}
return $final_result;
}
}
View Coding :-
if(isset($final_result)) {
foreach($final_result as $result) {
echo $result['match_date']; // First display match date
if(isset($result['match_date'])) {
foreach($result['match_date'] as $match) {
echo $match['match_name']; // Second listout the match name
}
}
}
}

PHP : Getting each value from the array and calculate

I have these values in the array=Array ( [0] => 3 [1] => 2 [2] => 10 [3] => 5 )
How do i get each value separately from the array and perform the below calculation?
$j=$q*100/$ytot;
And this is what i've tried so far, which isnt giving the correct value :
$resul=mysql_query("select * from post_bet where bet_id = '$id'")or die(mysql_error());
if(mysql_num_rows($resul)>0)
{
while($row=mysql_fetch_assoc($resul))
{
$y_uid=$row['yes_userid'];
$y_uamt=$row['yes_useramount'];
$n_uid=$row['no_userid'];
$n_uamt=$row['no_useramount'];
$ytot=$row['yes_total'];
$ntot=$row['no_total'];
$hhh=explode(',',$row['yes_useramount']);
$q = print_r($hhh);
echo $j=$q*100/$ytot;
}
}
Try this-
$resul=mysql_query("select * from post_bet where bet_id = '$id'")or die(mysql_error());
if(mysql_num_rows($resul)>0)
{
while($row=mysql_fetch_assoc($resul))
{
$y_uid=$row['yes_userid'];
$y_uamt=$row['yes_useramount'];
$n_uid=$row['no_userid'];
$n_uamt=$row['no_useramount'];
$ytot=$row['yes_total'];
$ntot=$row['no_total'];
$hhh=explode(',',$row['yes_useramount']);
}
foreach($hhh as $val)
{
echo $j=$val*100/$ytot;
}
}
A simple foreach should do the trick:
foreach (explode(',', $row['yes_useramount']) as $q) {
echo $q * 100 / $ytot;
}
You can process an array in PHP using foreach like shown below:
foreach (($hhh) as $v => $k)
{
echo $k*100/$ytot;
}
Here,
$hhh=explode(',',$row['yes_useramount']);
So $hhh will return an array of values from $row['yes_useramount'] which was imploded with ,.
try this code in your script. If there having error please tell me here.
$resul=mysql_query("select * from post_bet where bet_id = '$id'")or die(mysql_error());
if(mysql_num_rows($resul)>0)
{
while($row=mysql_fetch_assoc($resul))
{
$y_uid=$row['yes_userid'];
$y_uamt[]=$row['yes_useramount'];
$n_uid=$row['no_userid'];
$n_uamt=$row['no_useramount'];
$ytot=$row['yes_total'];
$ntot=$row['no_total'];
}
for($i = 0; $i<count($y_uamt);$i++){
$sum_uamt = $sum_uamt + int($y_uamt[$i]);
}
echo $j = $sum_uamt * 100 / $ytot;

get data from array in cakephp

I'm trying to get the data from array in my controller php on Cakephp.
I have this function:
public function updateUserStatus() {
if(isset($this->params['url']["pcs"])) {
$uus = array( "pcs" =>$this->params['url']["pcs"] );
$trans = $this->Transaction->updateUserStatus($uus);
} else {
$trans = "failed";
}
$this->set('trans', $trans);
$this->layout = 'ajax';
}
And I want to get the data from status_id who have this response:
Array (
[0] => Array
(
[status_id] => 2
)
[1] => Array
(
[rem_time] => 66
)
)
How can I do it?
My question is how can i get the data for status_id ?
public function updateUserStatus() {
if (isset($this->params['url']["pcs"])) {
$uus = array("pcs" =>$this->params['url']["pcs"]);
$trans = $this->Transaction->updateUserStatus($uus);
} else {
$trans = "failed";
}
$currentStatus = 0;
if (is_array($trans) && isset($trans['status_id'])) {
$currentStatus = $trans['status_id'];
}
$this->set('trans', $trans);
$this->layout = 'ajax';
}
public function updateUserStatus($uus){
if(isset($uus["pcs"])) {
$sql = "SELECT status_id as status_id , rem_time as rem_time FROM phones WHERE pcs = '".$uus["pcs"]."' LIMIT 1";
$query = $this->query($sql);
return $query[0]['phones'];
} else {
return "failed";
}
}
Notice, we are returning $query[0]['phones'].
Let me know if it works.
The code could also use some refactoring.
For example, why is the function called updateUserStatus if it is only returning the result of a query? It should also always return an array, for consistency.

Undefined index: in PHP codeigniter while sending variable

My controller:
public function thread($page = 'empty')
{
$data['user_id'] = $this->tank_auth->get_user_id();
$data['username'] = $this->tank_auth->get_username();
//User data variable grab
//default template load data
$data['thread'] = $this->thread_model->get_thread($page);
$data['title'] = ucfirst($page); // Capitalize the first letter
$data['replies'] = $this->thread_model->get_reply($data['thread']['thread_id']);
$this->thread_model->view_increment($data);
$this->load->view('templates/head', $data);
$this->load->view('main/wrapper-start', $data);
$this->load->view('templates/leftbar', $data);
$this->load->view('main/thread', $data);
$this->load->view('main/wrapper-end', $data);
$this->load->view('templates/footer', $data);
}
The error appears on the $data['replies'] line:
Severity: Notice
Message: Undefined index: thread_id
Filename: controllers/main.php
Line Number: 40
Here is my model code that replies refers to: $this->thread_model->get_reply
public function get_reply($thread_id)
{
$query = $this->db->query("SELECT r.reply_id FROM reply_thread rt
INNER JOIN replies r
ON rt.reply_id = r.reply_id
WHERE thread_id = ". $thread_id);
$replies = $query->result_array();
$replyarray = array();
foreach ($replies as $reply)
{
$id = $reply['reply_id'];
$query = $this->db->query("SELECT * FROM replies WHERE reply_id='$id'");
$thisRow = $query->row_array();
$replyarray[] = $thisRow;
}
return $replyarray;
}
Any ideas why this error is popping up? Other than the error, the page loads perfectly fine...all the info is in tact and available through the query.
here is my get_thread model
public function get_thread($page)
{
$slug = $page;
$query = $this->db->get_where('thread', array('slug' => $slug));
return $query->row_array();
}
using a print_r($data['thread']); I get this array:
Array ( [thread_id] => 233 [owner_id] => 8 [subject] => Repercussions of D3 addiction [body] => 1. signs of unhygienic practices 2.become irritable when forced to stop playing 3. carpal tunnel 4. loss of interest in pursuing a significant other 5. sleep deprivation 6. malnutrition There's nothing positive about this. [timestamp] => 2012-05-08 19:03:02 [replystamp] => 2012-05-09 12:38:32 [last_post_id] => 3 [slug] => 233-repercussions-of-d3-addiction [replies_num] => 2 [views] => 21 )
I think you could try this:
$threadinfo = $this->thread_model->get_thread($page);
$data['thread'] = $threadinfo;
$data['replies'] = $this->thread_model->get_reply($threadinfo->thread_id);

Categories