I'm stuck on next:
I get this error: Notice: Trying to access array offset on the value of type null in C:\xampp\htdocs\admin_area\view_rel.php on line 64. How to solve this error and what kind of error is?
My PHP code:
<?php
$i = 0;
$get_rel = "select * from bundle_product_relation";
$run_rel = mysqli_query($con,$get_rel);
while($row_rel = mysqli_fetch_array($run_rel)){
$rel_id = $row_rel['rel_id'];
$rel_title = $row_rel['rel_title'];
$bundle_id = $row_rel['bundle_id'];
$product_id = $row_rel['product_id'];
$get_p = "select * from products where product_id='$product_id'";
$run_p = mysqli_query($con,$get_p);
$row_p = mysqli_fetch_array($run_p);
$p_title = $row_p['product_title'];
$get_b = "select * from products where product_id='$bundle_id'";
$run_b = mysqli_query($con,$get_b);
$row_b = mysqli_fetch_array($run_b);
$b_title = $row_b['product_title'];
$i++;
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $rel_title; ?></td>
<td><?php echo $p_title; ?></td>
<td><?php echo $b_title; ?></td>
<td>Delete</td>
<td>Edit</td>
</tr>
<?php } ?>
Error is on line 64 and that line of code is $p_title = $row_p['product_title'];.
Database is for products:
1. product_id
2.p_cat_id
3.cat_id
4.manufacturer_id
5.date
6.product_title
7.product_url
8.product_img1
9.product_img2
10.product_img3
11.product_price
12.product_psp_price
13.product_desc
14.product_features
15.product_video
16.product_keywords
17.product_label
18.status
Database for bundle_product_relation:
1.rel_id
2.rel_title
3.product_id
4.bundle_id
Where im wrong? Thanks all
Answer on this question is:
I haved error on line 64 and that line of code is $p_title = $row_p['product_title'];.
Solution for me is:
From: $p_title = $row_p['product_title']; to:
$p_title = isset($row_p['product_title']);
Related
I want to display the results of the $ distance variable to the table in the view file.
But I get an error in my script, can anyone help solve this error?
I also want to sort the results of the $ distance variable from the smallest.
Error appears in line 38, which is in the foreach function in my view file.
This is the script from my controller file :
function index()
{
$center_lat = -7.2574719;
$center_lng = 112.7520883;
$data= $this->m_metode->tampil_data_fotografer();
{
foreach($data->result_array() as $row)
{
$lat=(float)$row['latitude'];
$lng=(float)$row['longitude'];
// $distance= $lat * $center_lat;
$distance['tb_fotografer'] =( 6371 * acos((cos(deg2rad($center_lat)) ) * (cos(deg2rad($lat))) *
(cos(deg2rad($lng) - deg2rad($center_lng)) )+ ((sin(deg2rad($center_lat))) *
(sin(deg2rad($lat))))));
// var_dump($distance);
}
}
$this->load->view('public/pencari/v_tampil_rekomendasi',$distance);
}
My view :
<?php
$no = 1;
foreach($tb_fotografer->result_array() as $fg){
?>
<tr>
<td><?php echo $no++ ?></td>
<td><?php echo $fg->nama ?></td>
<td><?php echo $fg->alamat ?></td>
<td><?php echo $fg->kamera ?></td>
<td><?php echo $fg->spesifikasi_foto ?></td>
<td><?php echo $fg->keahlian_foto ?></td>
<td><?php echo $fg->latitude ?></td>
<td><?php echo $fg->longitude ?></td>
</tr>
<?php } ?>
This variable "$tb_fotografer" store a float value not an array, and you can not access with result_array(). you must store as an array like this:
$distance['tb_fotografer'][] = ( 6371 * acos((cos(deg2rad($center_lat)) ) * (cos(deg2rad($lat))) *
(cos(deg2rad($lng) - deg2rad($center_lng)) )+ ((sin(deg2rad($center_lat))) *
(sin(deg2rad($lat))))));
base on your view field you needed to send $data to your view file.
$this->load->view('public/pencari/v_tampil_rekomendasi',['tb_fotografer' => $data->result_array()]);
and in your view file:
foreach($tb_fotografer as $fg){
for sort you can store data in another variable like $output and put distance on it, after that you can sort with this method: usort($array, "cmp");
after all the steps you can send to your view.
final code:
function index()
{
$center_lat = -7.2574719;
$center_lng = 112.7520883;
$data= $this->m_metode->tampil_data_fotografer();
$out = []
{
foreach($data->result_array() as $row)
{
$lat=(float)$row['latitude'];
$lng=(float)$row['longitude'];
// $distance= $lat * $center_lat;
$distance =( 6371 * acos((cos(deg2rad($center_lat)) ) * (cos(deg2rad($lat))) *
(cos(deg2rad($lng) - deg2rad($center_lng)) )+ ((sin(deg2rad($center_lat))) *
(sin(deg2rad($lat))))));
$out[] = array_merge($row, ['distance' => $distance]);
}
}
// #todo use usort for sort base on distance
$this->load->view('public/pencari/v_tampil_rekomendasi',['tb_fotografer' => $out]);
}
You've overwritten the $distance['tb_fotografer'] variable on each iteration and also you've accessed object on array data.
Try to modify the controller like this :
function index()
{
$center_lat = -7.2574719;
$center_lng = 112.7520883;
$data= $this->m_metode->tampil_data_fotografer();
{
foreach($data->result_array() as $key => $row)
{
$lat=(float)$row['latitude'];
$lng=(float)$row['longitude'];
// $distance= $lat * $center_lat;
$distance['tb_fotografer'][$key][] = $row; // get all of the preserved result_array data
$distance['tb_fotografer'][$key][]['distance'] = ( 6371 * acos((cos(deg2rad($center_lat)) ) * (cos(deg2rad($lat))) *
(cos(deg2rad($lng) - deg2rad($center_lng)) )+ ((sin(deg2rad($center_lat))) *
(sin(deg2rad($lat))))));
// var_dump($distance);
}
}
$this->load->view('public/pencari/v_tampil_rekomendasi',$distance);
}
And the v_tampil_rekomendasi view like this :
<?php
$no = 1;
foreach($tb_fotografer as $fg){
?>
<tr>
<td><?php echo $no++ ?></td>
<td><?php echo $fg['nama'] ?></td>
<td><?php echo $fg['alamat'] ?></td>
<td><?php echo $fg['kamera'] ?></td>
<td><?php echo $fg['spesifikasi_foto'] ?></td>
<td><?php echo $fg['keahlian_foto'] ?></td>
<td><?php echo $fg['latitude'] ?></td>
<td><?php echo $fg['longitude'] ?></td>
<td><?php echo $fg['distance'] ?></td>
</tr>
<?php } ?>
hi all i am trying to display array of data in a table .but the main task is i need to display all users that are under one supervisor, they may/may not have attempted the test .i am using if else condition in controller but am getting error like
Message: Undefined index: status and Message: Undefined index:
submittimestamp
below is my code
controller:
if ($supervisor) {
$users_query="SELECT u.* ,v.value FROM usr_data u, udf_text v WHERE u.usr_id != 6 AND u.usr_id = v.usr_id " . $supervisor_condition . " GROUP BY u.usr_id";
// echo $users_query;
$user_records = $this->base_model->executeSelectQuery($users_query);
$final_dataa = [];
foreach ($user_records as $user) {
$user=(object)$user;
$user_test=" SELECT u.*,o.title,ta.*,tpr.workingtime, tpr.pass, tpr.tstamp, tpr.points, tpr.maxpoints, tm.minimum_level, tcr.mark_official,(tcr.reached_points/tcr.max_points)*100 as result,v.value,ta.submittimestamp,tcr.mark_official FROM usr_data u, object_data o,tst_tests tt,tst_active ta,tst_pass_result tpr, tst_result_cache tcr,udf_text v, tst_mark tm WHERE u.usr_id != 6 AND u.usr_id=ta.user_fi AND tt.obj_fi=o.obj_id AND v.usr_id=u.usr_id AND ta.test_fi=tt.test_id AND tt.test_id = tm.test_fi AND tcr.active_fi=ta.active_id AND tm.passed = 1 AND ta.active_id=tpr.active_fi AND tcr.active_fi=ta.active_id AND v.field_id='2' AND ta.user_fi = $user->usr_id GROUP by ta.submittimestamp";
// echo $user_test;
$tst_records=$this->base_model->executeSelectQuery($user_test);
if (count($tst_records)) {
foreach ($tst_records as $tst) {
$tst=(object)$tst;
$dta['usr_id'] = $user->usr_id;
$dta['firstname'] = $user->firstname;
$dta['matriculation'] = $user->matriculation;
$dta['approve_date'] = $this->udfTextData(1, $user->usr_id);
$dta['department'] = $user->department;
$dta['job_title'] = $this->udfTextData(6, $user->usr_id);
$dta['submittimestamp'] = $tst->submittimestamp;
$dta['test_title'] = $tst->title;
$dta['division'] = $tst->value;
$mark_official = $tst->mark_official;
if ($mark_official == "passed" || $mark_official == "Passed" || $mark_official == "PASSED") {
$result_status = '<span class="label label-primary"> Completed </span>';
$completed = TRUE;
} else {
$result_status = '<span class="label label-danger"> Failed </span>';
$failed = TRUE;
}
$dta['status'] = $result_status;
$final_dataa[] = $dta;
}
}
else{
$dta['usr_id'] = $user->usr_id;
$dta['firstname'] = $user->firstname;
$dta['matriculation'] = $user->matriculation;
$dta['approve_date'] = $this->udfTextData(1, $user->usr_id);
$dta['department'] = $user->department;
$dta['job_title'] = $this->udfTextData(6, $user->usr_id);
$dta['test_title'] = $user->title;
$dta['division'] = $user->value;
$final_dataa[] = $dta;
}
}
}
$dataa['recordss'] = $final_dataa;
$this->load->view('supervisor', $dataa);
I am using if else condition if users have test else not like that .below is my view code
view:
<h3> Skill Matrix Report Process Based Training Record in LMS </h3>
<hr>
<?php $uniq_rec = array_unique($recordss,SORT_REGULAR);
// var_dump($uniq_rec);
$uniq_name = array_unique(array_column($recordss, 'firstname'));
$uniq_test = array_unique(array_column($recordss, 'test_title'));
?>
<table class="table" id="myTable">
<thead>
<tr>
<th>Employe NO</th>
<th>Employe Name</th>
<th>Date Joined</th>
<th>division</th>
<th>department</th>
<th>jobtitle</th>
<?php
foreach ($uniq_test as $row) {
?>
<th><?php echo $row?></th>
<?php
}
?>
</tr>
</thead>
<tbody>
<?php
foreach ($uniq_rec as $row) {
$row = (object)$row;
// var_dump($row);
$date_joined_y = substr($row->approve_date,0,4);
$date_joined_m = substr($row->approve_date,4,2);
$date_joined_d = substr($row->approve_date,6,2);
$date_joined = $date_joined_d."-".$date_joined_m."-".$date_joined_y;
?>
<tr>
<td><?php echo ucfirst($row->matriculation); ?></td>
<td><?php echo $row->firstname?></td>
<td> <?php echo ucfirst($date_joined); ?></td>
<td><?php echo ucfirst($row->division); ?></td>
<td><?php echo ucfirst($row->department); ?></td>
<td><?php echo ucfirst($row->job_title); ?></td>
<?php
foreach ($uniq_test as $uniq) {;
$status = "";
foreach ($recordss as $rec) {
if($uniq == $rec['test_title'] && $rec['firstname'] == $row->firstname){
echo "<td>".$rec['status'].$rec['submittimestamp']."</td>";
$status = "true";
}
}
if($status != "true"){
echo "<td></td>";
}
$status = "";
}
?>
but the main task is i need to display all users who are attempted test or not but those users are under one Supervisor
submittimestampbut the main task is i need to display all users who are attempted test or not but those users are under one Supervisor.i am using if else condition in controller but am getting error like
Message: Undefined index: status and Message: Undefined index:
submittimestamp
I need help, have managed to get here with searching Google.. but stuck and cannot find a decent sample to complete my script.
A simple stock value system for intranet.
All i need is to calculate the Stock Value Row.
See red block on Image below
My Code - How do I calculate the Value with my script.. or any other example's I can look (links here or on the web) - MANY THANKS....
<?php try {
$conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb);
$conn->exec("SET CHARACTER SET utf8");// Sets encoding UTF-8
$sql = "select * from stock_dry where stock_cat_id = 14 order by stock_id" ;
$result = $conn->query($sql);
if($result !== false) {
$cols = $result->columnCount();
foreach($result as $row) {
?>
<tr>
<td class="text-left"><?php echo $row['stock_id'];?></td>
<td class="text-left"><?php echo $row['stock_name'];?></td>
<td class="text-left"><?php echo $row['stock_count'];?></td>
<td class="text-left"><?php echo $row['stock_price'];?></td>
<td class="text-left">
<?php
$sum_total = $row['stock_count'] * $row['stock_price'];
echo $sum_total;
?>
</td>
<td class="text-left"><?php echo $row['stock_cat'];?></td>
</tr>
<?php
} } $conn = null; }
catch(PDOException $e) {
echo $e->getMessage();}
?>
You can simply add a variable before the loop like:
$tot = 0;
Then after the sum_total calc you add:
$tot += $sum_total;
I also would do a little change to sum_total (if you work with integers):
$sum_total = intval( $row['stock_count'] ) * intval( $row['stock_price'] );
or (if you work with floats):
$sum_total = floatval( $row['stock_count'] ) * floatval( $row['stock_price'] );
And with:
echo number_format( $sum_total, 2 );
You can print the float with 2 decimals.
Here is my model - I want to assign the rows and echo out on the view page e.g
My Model:
public function dsduan(){
$data = array();
$q = $this->db->query("SELECT duan.id, duan.tenduan, duan.diachi, quan.quan, duan.tdt, duan.mdxd, duan.tt, duan.cdt, duan.dvql, duan.dvtk, duan.dvtc, duan.dvgs, duan.loai, duan.tienich, duan.noidung, duan.hinhanh FROM duan LEFT JOIN quan ON duan.quan=quan.id ORDER BY duan.tenduan ASC");
if ($q->num_rows == 1)
{
foreach ($q->result() as $row)
{
$data[]['id'] = $row->id;
$data[]['tenduan'] = $row->tenduan;
$data[]['diachi'] = $row->diachi;
$data[]['quan'] = $row->quan;
}
}
return $data;
}
My Controller:
public function dsduan() {
$this->load->model('madmin');
$duan = $this->madmin->dsduan();
$this->load->view('danhsachduan', $duan);
}
My View:
<td><?php echo $id; ?></td>
<td><?php echo $tenduan; ?></td>
<td><?php echo $diachi; ?></td>
<td><?php echo $quan; ?></td>
How do I properly pass my model array to my controller so that I can echo out on my view?
My view Error:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: id
Filename: views/danhsachduan.php
Line Number: 44
In your model you use two dimensional array:
$data[]['id'] = $row->id;
then in your controller you pass this array to view.
so if you want to access two dimensional array into your view, you have to use foreach loop.
such as change your array in model like following:
$data['id'][] = $row->id;
and in your view access array using:
foreach($id as $res)
{
echo $res;
}
update your view like this
<?php foreach($duan as $d) { ?>
<td><?php echo $d['id']; ?></td>
<td><?php echo $d['tenduan']; ?></td>
<td><?php echo $d['diachi']; ?></td>
<td><?php echo $d['quan']; ?></td>
<?php } ?>
public function dsduan(){
$data = array();
$q = $this->db->query("SELECT duan.id, duan.tenduan, duan.diachi, quan.quan, duan.tdt, duan.mdxd, duan.tt, duan.cdt, duan.dvql, duan.dvtk, duan.dvtc, duan.dvgs, duan.loai, duan.tienich, duan.noidung, duan.hinhanh FROM duan LEFT JOIN quan ON duan.quan=quan.id ORDER BY duan.tenduan ASC");
if ($q->num_rows == 1)
{
foreach ($q->result() as $row)
{
$data['id'] = $row->id;
$data['tenduan'] = $row->tenduan;
$data['diachi'] = $row->diachi;
$data['quan'] = $row->quan;
}
}
return $data;
}
In codeigniter, it use extract() method when binding variable from controller to view. In this case, you can do like this.
In Model
public function dsduan()
{
$data = array();
$q = $this->db->query("SELECT duan.id, duan.tenduan, duan.diachi, quan.quan, duan.tdt, duan.mdxd, duan.tt, duan.cdt, duan.dvql, duan.dvtk, duan.dvtc, duan.dvgs, duan.loai, duan.tienich, duan.noidung, duan.hinhanh FROM duan LEFT JOIN quan ON duan.quan=quan.id ORDER BY duan.tenduan ASC");
if ($q->num_rows)
{
$data = $q->result_array();
}
return $data;
}
In Controller
public function dsduan()
{
$this->load->model('madmin');
$data['duan'] = $this->madmin->dsduan();
$this->load->view('danhsachduan', $data);
}
In View, if your array is multi-dimensional, you can use like this,
<?php foreach($duan as $d) { ?>
<td><?php echo $d['id']; ?></td>
<td><?php echo $d['tenduan']; ?></td>
<td><?php echo $d['diachi']; ?></td>
<td><?php echo $d['quan']; ?></td>
<?php } ?>
if your array is one-dimensional, you can use like this,
<td><?php echo $duan['id']; ?></td>
<td><?php echo $duan['tenduan']; ?></td>
<td><?php echo $duan['diachi']; ?></td>
<td><?php echo $duan['quan']; ?></td>
Have modified your code. Kindly replace below with your code.
Model:
public function dsduan()
{
$q = $this->db->query("SELECT duan.id, duan.tenduan, duan.diachi, quan.quan, duan.tdt, duan.mdxd, duan.tt, duan.cdt, duan.dvql, duan.dvtk, duan.dvtc, duan.dvgs, duan.loai, duan.tienich, duan.noidung, duan.hinhanh FROM duan LEFT JOIN quan ON duan.quan=quan.id ORDER BY duan.tenduan ASC");
if($q->num_rows() > 0)
return $q->result();
return false;
}
Controller:
public function dsduan()
{
$this->load->model('madmin');
$data['duan'] = $this->madmin->dsduan();
$this->load->view('danhsachduan', $data);
}
View:
<?php foreach($duan as $d) { ?>
<td><?php echo $d->id; ?></td>
<td><?php echo $d->tenduan; ?></td>
<td><?php echo $d->diachi; ?></td>
<td><?php echo $d->quan; ?></td>
<?php } ?>
The code shown below is used to manually fill an array.
<?php
include_once 'include/DatabaseConnector.php';
$data = array(
array(0,array("111",' EE112',' AA','FT445'),"2004-03-01 10:00","2004-03-01 14:00"),
array(1,array("111",' BC124',' RYA','FE675'),"2004-03-01 16:00","2004-03-01 18:00"),
array(2,array("11",' BE225',' FA','AE667'),"2004-03-01 09:00","2004-03-01 10:00"),
array(3,array("11",' TC828',' BA','FF745'),"2004-03-01 06:00","2004-03-01 08:00")
);
?>
Now I want to fill this array from the database:
$query1="SELECT * FROM MyData;";
$result1=DatabaseConnector::ExecuteQueryArray($query1);
<?php foreach ($result1 as $row):?>
<tr>
<td><?php echo $row['resReg']; ?></td>
<td><?php echo $row['resTitle']; ?></td>
<td><?php echo $row['resAvailability'] ? 'Yes' : 'No';?></td>
</tr>
<?php endforeach;?>
How to assign the columns of result1 to the columns of the array?
Am i right assuming you want to do something like this:
<?php
$query1 = "SELECT name,color1,color2 FROM MyPets;";
$result1 = DatabaseConnector::ExecuteQueryArray($query1);
$petArray = array();
foreach($result1 as $pet):
$petArray[] = array('name' => $pet['name'],
'colors' => array(
$pet['color1'],
$pet['color2'])
);
?>