unlink image using function in php - php

Please help me how can i unlink a image when i delete a product.I dont know where is my error in my code .Thank everyone so much.I tried different way to resolve this problem.I teached myself learn to code on youtube, and then I tried to create another model.Please help me solve this problem.
Html Page
<h3>List Product</h3>
<table id="user">
<thead>
<tr>
<th>Serial</th>
<th>Product Name</th>
<th>Image</th>
<th>Price</th>
<th>Category</th>
<th>Type</th>
<th>Action</th>
</tr>
</thead>
<?php
if(!empty($_GET['msg'])){
$msg = unserialize(urldecode($_GET['msg']));
foreach ($msg as $key => $value){
echo '<span style="color:blue;font-weight:bold">'.$value.'</span>';
}
}
?>
<tbody>
<?php
$i = 0;
foreach($listproduct as $key => $listpro){
$i++;
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $listpro['productName']; ?></td>
<td><img src="<?php echo BASE_URL ?>/teamplate/image/<?php echo $listpro['image']; ?>" width="60" height="60"></td>
<td><?php echo $listpro['price']; ?></td>
<td><?php echo $listpro['catName'] ?></td>
<td>
<?php
if($listpro['type']==0){
echo 'Feathered';
}else{
echo 'Non-Feathered';
}
?>
</td>
<td>Delete
|| Edit
</td>
</tr>
<?php
}
?>
</tbody>
</table>
Function in Product page
public function delPro($id = NULL){
$tablePro = 'tbl_product';
$this->data = array();
$ProductModel = $this->load->model('ProductModel');
$result = $ProductModel->delProductById($tablePro, $id);
if($result == 1){
$this->data['productbyid'] = $ProductModel->updateProductById($tablePro, $id);
$path_unlink= BASE_URL."/teamplate/image/";
foreach($this->data['productbyid'] as $this->key => $this->value){
$image_pro = $this->value['image'];
unlink($path_unlink.$image_pro);
}
$mdata['msg'] = "Product Deleted Successfully...";
}else{
$mdata['msg'] = "Product Not Deleted...";
}
$url = BASE_URL."/Product/ListProduct?msg=".urlencode(serialize($mdata));
header("Location:$url");
}
ProductModel Page
public function updateProductById($tablePro, $id){
$query = "SELECT * from $tablePro where productId = '$id'";
return $this->db->select($query);
}

Related

How To Fix Warning: Invalid argument supplied for foreach() in Php Native MVC

I have build simple application for view data on table with call rest service in java eclipse, use curl in php native mvc, I have trying to call rest service use curl but show messages error in the table, how can I fix it? I will appreciate your help :))
this is my controller in php
<?php
class App_UserManagement_Control_UserManagement extends UserControl{
public function usermanagement(){
Template::setTitle('User Management');
$result = $this->getUser();
$dataresult = json_decode($result, true);
if($dataresult === NULL) {
echo "<script language = \"javascript\">window.alert(\"\Tidak Ada Data\");";
echo "javascript:history.back();</script>";
return false;
}
$data['user'] = $dataresult;
return $data;
}
public function getUser(){
Template::setTitle('User Management');
$user_comp_code = Session::get('pyrCode');
/*API URL */
$url = "http://localhost:8585/usermaster/$user_comp_code";
/*init cURL resource */
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPGET, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec ($curl);
//echo $url;echo $result; die;
if ($result === FALSE){
die('Curl failed: ' . curl_error($curl));
}
curl_close($curl);
return $result;
}
This is my code in html
<?php
$main_controll = new App_UserManagement_Control_UserManagement();
$data_from_ctr = $main_controll->usermanagement();
?>
<table cellspacing="2" cellpadding="2" border="0" align="left" id="tablecontent">
<thead style="background-color:#eee;">
<th width="25">#</th>
<th width="80">Username</th>
<th width="117">Name</th>
<th width="117">Company Code</th>
<th width="117">Company Name</th>
<th width="80">User Access</th>
<th width="80">Login Status</th>
<th width="80">User Status</th>
<th width="200">Action</th>
</thead>
<tbody>
<?php if($dataresult == NULL): ?>
<?php $i = 1; foreach ($dataresult as $row): ?>
<tr>
<td><?php echo $i++; ?></td>
<td><?php echo $row ['_user'] ?></td>
<td><?php echo $row ['_fullName'] ?></td>
<td><?php echo $row ['_pyrCode'] ?></td>
<td><?php echo $row ['_desc'] ?></td>
<td><?php echo $row ['group_user'] ?></td>
<td><?php echo $row ['_status'] ?></td>
<td><?php echo $row ['Active'] ?></td>
<td></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</fieldset>
This is my message error
Warning: Invalid argument supplied for foreach()
if($dataresult == NULL)
Looks wrong. Shouldn't it be:
if($dataresult !== NULL)
? You can't loop through null!
That's not all though. You overwrite $row with your inner loop!
<?php $i = 1; foreach ($dataresult as $row): ?>
$No=0;
foreach($data_from_ctr['user'] as $row) {

Codeigniter and mysql - all data displayinging

I am new in codeigniter and php.I made an uploading system.After uploading the file by user, i just want to display the user uploaded file.But Following code is displaying all the data of the mysql.How can user see only his uploaded data?Can someone suggest replacement
<?php
if (!empty($list) > 0) {
foreach ($list as $row) {
?>
<table class="table table-hover">
<tr>
<th>Manuscript ID</th>
<th>Journal</th>
<th>Title</th>
<th>Date</th>
<th>Action</th>
</tr>
<?php
if (!empty($list) > 0) {
foreach ($list as $row) {
?>
<tr>
<td><?php echo $row->manu_id; ?></td>
<td><?php echo $row->sub_title; ?></td>
<td><?php echo $row->title; ?></td>
<td><?php echo $row->created_date; ?></td>
<td>
<a href="<?php
echo
site_url('user/removemanuscript/' . base64_encode($row->manu_id));
?>"
title="Soft Delete">Remove
</a>
<?php if ($row->final_status == 1) { ?>
<span class="label label-primary">Approved</span>
<?php } else if ($row->final_status == 2) { ?>
<span class="label label-
danger">Disapproved</span>
<?php } else { ?>
<span class="label label-warning">Pending</span>
<?php } ?>`enter code here`
</td>
</tr>
<?php
}
}
?>
</table>
<?php
}
}
I am adding user controller for uploaded file.Please let me know
public function FinalManuscriptSubmit()
{
if ($this->Common_model->Is_User_Logged())
{
$data=array();
$joinClause=array();
$whereClause=array();
$session_data = $this->session->userdata('user_logged_in');
$u_id = $session_data['id'];
$sql="SELECT u.*,f.manu_id,m.journal_id,m.title,j.sub_title,f.created_date,f.final_status
FROM user u
INNER JOIN final_manuscript f on f.user_id = u.user_id
INNER JOIN manuscript m on m.manu_id = f.manu_id
INNER JOIN journal j on j.sub_id = m.journal_id
";
$data['list']=$this->Common_model->get_data_by_query($sql);
//print_r($data['list']);exit;
$this->load->view('user/header');
$this->load->view('user/showmanu',$data);
$this->load->view('user/footer');
}else{
redirect('user/login');
}
}
I will be thankful for your support.

Codeigniter multiple foreach loop

I have two tables meals type and meals. I want to print each meals corresponding to its meals type.
Error is that only showing last meals type meals only
view page is like
View
<?php
foreach ($mealstype as $type) {
?>
<h2><?php echo $type->type; ?></h2>
<table class="table table-bordered">
<tr>
<th>Meals</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<?php
foreach ($meals as $meals_get) {
?>
<tr>
<td><?php echo $meals_get->item; ?></td>
<td><?php echo $meals_get->price; ?></td>
</tr>
<?php } ?>
<tr>
<td> <input type="checkbox" class="ck" value="total"> Toal</td>
<td>2050</td>
</tr>
</tbody>
</table>
<?php
}
?>
Controller
function availability() {
$this->data['mealstype'] = $this->Home_model->mealstype();
foreach ($this->data['mealstype'] as $type) {
$typeid = $type->id;
$meals[] = $this->Home_model->meals($typeid, $hotel_id);
}
$this->data['meals'] = $meals[];
$this->load->view('home2', $this->data);
}
Please check this answer
Controller:
public function availability() {
$build_array = array();
mealstype= $this->Home_model->mealstype();
foreach($mealstype as $row){
$build_array[] = array(
'parent_array' => $row,
'child_array' =>$this->Home_model->meals($row->id,$hotel_id),
'child_sum' =>$this->Home_model->meals_sum($row->id,$hotel_id)
);
}
$this->data['build_array'] = $build_array;
}
and the view page is like this:
View:
<?php foreach($build_array as $type){?>
<h2><?php echo $type['parent_array']->type;?></h2>
<table class="table table-bordered">
<tr>
<th>Meals</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<?php
foreach ($type["child_array"] as $meals_get) {?>
<tr>
<td><?php echo $meals_get->item;?></td>
<td><?php echo $meals_get->price;?></td>
</tr>
<?php }?>
<tr>
<td> Toal</td>
<td>2050</td>
</tr>
<?php } ?>
Controller, check "hotel_id"
function availability()
{
$this->data['mealstype'] = $this->Home_model->mealstype();
if( count( $this->data['mealstype'] ) > 0 )
{
foreach($this->data['mealstype'] as $type)
{
$type->meals = $this->Home_model->meals( $type->id, $hotel_id ); // hotel_id no declared???
}
}
$this->load->view('home2', $this->data);
}
View
<?php if( count( $mealstype ) > 0): foreach( $mealstype as $type ): ?>
<h2><?php echo $type->type; ?></h2>
<table class="table table-bordered">
<thead>
<tr>
<th>Meals</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<?php if( count( $type->meals ) > 0 ): foreach( $type->meals as $meals_get ): ?>
<tr>
<td><?php echo $meals_get->item; ?></td>
<td><?php echo $meals_get->price; ?></td>
</tr>
<?php endforeach; endif; ?>
<tr>
<td><input type="checkbox" class="ck" value="total"> Toal</td>
<td>2050</td>
</tr>
</tbody>
</table>
<?php endforeach; endif ?>
Try to replace into this :
function availability() {
$data = array();
$data['mealstype'] = $this->Home_model->mealstype();
foreach ($data['mealstype'] as $type) {
$typeid = $type->id;
$type->meals = $this->Home_model->meals($typeid,$hotel_id);
}
$this->load->view('home2', $data);
}
check it and print_r($data); what it comes set on the basis of that in view file..
You have $hotel_id I am not sure where you set this I can not see it any where else.
We also need to see your model
public function availability() {
$data['mealstypes'] = array();
$mealstypes = $this->home_model->mealstype();
foreach ($mealstypes as $mealstype)
{
$meals = array();
$meals_results = $this->home_model->meals($mealstype->id, $hotel_id);
foreach ($meals_results $meal_result) {
$meals[] = array(
'mealstype' => $meal_result->mealstype,
'price' => $meal_result->price
);
}
$data['mealstypes'][] = array(
'type' => $mealstype->type
'meals' => $meals
);
}
$this->load->view('someview', $data);
}
View
<?php foreach ($mealstypes as $mealstype) {?>
<h2><?php echo $mealstype['type'];?></h2>
<?php foreach ($mealstype['meals'] as $meal) {?>
<?php echo $meal['mealstype'];?>
<?php echo $meal['price'];?>
<?php }?>
<?php }?>

How Can I Pass Result of Foreach from Controller to View CodeIgniter

just like my title, how can I pass foreach result from controller to view?
So far I did this:
--Controller.php
$get_url_subkategori = $this->ws_url->GetUrl('SubKategoriRetrieve?KodeKategori='.$param);
$get_json_subkategori = json_decode(file_get_contents($get_url_subkategori), true);
foreach($get_json_subkategori['SubKategoris'] as $itemSubKategori){
$kodeSubKategori = $itemSubKategori['KodeSubKategori'];
$namaSubKategori = $itemSubKategori['Nama'];
$definisiSubKategori = $itemSubKategori['Definisi'];
$unsurAbstrakSubKategori = $itemSubKategori['UnsurAbstrak'];
$aliasSubKategori = $itemSubKategori['Alias'];
$data['kodeSubKategori']=$kodeSubKategori;
$data['namaSubKategori']=$namaSubKategori;
$data['definisiSubKategori']=$definisiSubKategori;
$data['unsurAbstrakSubKategori']=($unsurAbstrakSubKategori) ? 'true' : 'false';
$data['aliasSubKategori']=$aliasSubKategori;
}
$this->load->view('include/header');
$this->load->view('kategori_detail', $data);
$this->load->view('include/footer');
Then, in my view:
<h3>Sub Kategori</h3>
<table class="table table-responsive table-inherit">
<tr>
<th>Kode Sub Kategori</th>
<th>Nama Kategori</th>
<th>Definisi</th>
<th>Unsur Abstrak</th>
<th>Alias</th>
</tr>
<tr>
<?php
foreach((array)$kodeSubKategori as $itemKodeSubKategori)
echo '<td>'.$itemKodeSubKategori.'</td>';
?>
<?php
foreach((array)$namaSubKategori as $itemNamaSubKategori)
echo '<td>'.$itemNamaSubKategori.'</td>';
?>
<?php
foreach((array)$definisiSubKategori as $itemDefinisiSubKategori)
echo '<td>'.$itemDefinisiSubKategori.'</td>';
?>
<?php
foreach((array)$unsurAbstrakSubKategori as $itemUnsurAbstrakSubKategori)
echo '<td>'.$itemUnsurAbstrakSubKategori.'</td>';
?>
<?php
foreach((array)$aliasSubKategori as $itemAliasSubKategori)
echo '<td>'.$itemAliasSubKategori.'</td>';
?>
</tr>
</table>
It successfully show ONLY THE LAST ONE data. I want to get all data from the result from foreach. How can I do that?
I suggest you to pass all $get_json_subkategori['SubKategoris'] array in data array to your view:
$get_url_subkategori = $this->ws_url->GetUrl('SubKategoriRetrieve?KodeKategori='.$param);
$get_json_subkategori = json_decode(file_get_contents($get_url_subkategori), true);
$data['itemSubKategori'] = $get_json_subkategori['SubKategoris'];
$this->load->view('include/header');
$this->load->view('kategori_detail', $data);
$this->load->view('include/footer');
And in your view:
<h3>Sub Kategori</h3>
<table class="table table-responsive table-inherit">
<tr>
<th>Kode Sub Kategori</th>
<th>Nama Kategori</th>
<th>Definisi</th>
<th>Unsur Abstrak</th>
<th>Alias</th>
</tr>
<?php foreach($itemSubKategori as $rowSubKategori): ?>
<tr>
<td><?php echo $rowSubKategori['kodeSubKategori'] ?></td>
<td><?php echo $rowSubKategori['namaSubKategori'] ?></td>
<td><?php echo $rowSubKategori['definisiSubKategori'] ?></td>
<td><?php echo $rowSubKategori['definisiSubKategori'] ?></td>
<td><?php echo $rowSubKategori['unsurAbstrakSubKategori'] ?></td>
<td><?php echo $rowSubKategori['aliasSubKategori'] ?></td>
</tr>
<?php endforeach; ?>
</table>
Because you data array override each time that's y you get only last value every time.
You need to create your array with auto incremented value
$i=0;
foreach($get_json_subkategori['SubKategoris'] as $itemSubKategori){
$kodeSubKategori = $itemSubKategori['KodeSubKategori'];
$namaSubKategori = $itemSubKategori['Nama'];
$definisiSubKategori = $itemSubKategori['Definisi'];
$unsurAbstrakSubKategori = $itemSubKategori['UnsurAbstrak'];
$aliasSubKategori = $itemSubKategori['Alias'];
$data[$i]['kodeSubKategori']=$kodeSubKategori;
$data[$i]['namaSubKategori']=$namaSubKategori;
$data[$i]['definisiSubKategori']=$definisiSubKategori;
$data[$i]['unsurAbstrakSubKategori']=($unsurAbstrakSubKategori) ? 'true' : 'false';
$data[$i]['aliasSubKategori']=$aliasSubKategori;
$i++;
}
Pass data in one array. and you can catch all in view at one foreach llop
Use this
In Controller
$get_url_subkategori = $this->ws_url->GetUrl('SubKategoriRetrieve?KodeKategori='.$param);
$get_json_subkategori = json_decode(file_get_contents($get_url_subkategori), true);
$data['itemKodeSubKategori'] = $get_json_subkategori;
$this->load->view('include/header');
$this->load->view('kategori_detail', $data);
$this->load->view('include/footer');
In View
<tr>
<?php
foreach($kodeSubKategori as $itemKodeSubKategori)
{
echo '<td>'.$itemKodeSubKategori['KodeSubKategori'].'</td>';
echo '<td>'.$itemKodeSubKategori['Nama'].'</td>';
echo '<td>'.$itemKodeSubKategori['Definisi'].'</td>';
echo '<td>'.$itemKodeSubKategori['UnsurAbstrak'].'</td>';
echo '<td>'.$itemKodeSubKategori['Alias'].'</td>';
}
?>
</tr>

Create dynamic table with api data

How do I make my table dynamically list all the values it pulls from the api, atm it only pulls 1 record and puts it in the table, the rest of the data gets put outside it
Probably something very small I'm missing here, but can't put my finger on it
Thanks for any help, very appreciated.
<?php
$trackingId = $_POST['trackingId'];
if (isset($_POST['trackingId'])) {
$fetch = json_decode(file_get_contents(sprintf('apigoeshere', $trackingId)));
if ($fetch->status != 'OK') {
echo sprintf('Error: %s', $fetch->message);
} else {
//example how to access statistics variable
$inactiveAccounts = $fetch->statistics->inactiveAccounts;
echo sprintf('Inactive accounts: %d' . "<br /><br />", $inactiveAccounts);
?>
<table border="1">
<tr>
<th>Account name</th>
<th>Level</th>
</tr>
<?php
//example how to display all accounts username
foreach ($fetch->accounts as $account) {
?>
<tr>
<td><?php
echo $account->username;
?></td>
<td><?php
echo $account->currentLevel;
?></td>
</tr>
</table>
<?php
}
}
}
?>
Didnt close foreach in right place. use this code instead
<?php
$trackingId = $_POST['trackingId'];
if (isset($_POST['trackingId'])) {
$fetch = json_decode(file_get_contents(sprintf('apigoeshere', $trackingId)));
if ($fetch->status != 'OK') {
echo sprintf('Error: %s', $fetch->message);
} else {
//example how to access statistics variable
$inactiveAccounts = $fetch->statistics->inactiveAccounts;
echo sprintf('Inactive accounts: %d' . "<br /><br />", $inactiveAccounts);
?>
<table border="1">
<tr>
<th>Account name</th>
<th>Level</th>
</tr>
<?php
//example how to display all accounts username
foreach ($fetch->accounts as $account) {
?>
<tr>
<td><?php
echo $account->username;
?></td>
<td><?php
echo $account->currentLevel;
?></td>
</tr>
<?php
}
?>
</table>
<?php
}
}
?>

Categories