How to delete multiple images in PHP.But Single product delete is working fine.
if (isset($_REQUEST['delete_product'])) {
$ca_in_id = $_REQUEST['delete_product'];
$where = array("ca_in_id" => $ca_in_id);
$data = $this->select_where('catalog_inventory', $where);
//$res = $data->fetch_object();
$result = $this->delete_where('catalog_inventory', $where);
if ($result) {
// (file_exists($pic1)) {
// print_r($pic1);
// exit();
while ($row = mysqli_fetch_array($data)) {
$image = $row["upload_file"];
$pic1 = ("$image");
unlink('product_images/' . $pic1);
print_r($pic1);
}
exit();
//}
?>
<script>
alert("Delete Sucess");
window.location = "list-product";
</script>
<?php
} else {
?>
<script>
alert("Delete Fails");
window.location = "list-product";
</script>
<?php
}
}
And multiple Image delete show this error :-
How to solve this issues?
THANKS.
I have Solve issues First get all values in database and using explode function use the count database columns values and use finally for loop.
This My Code:-
This code means is get values in database.
$data = $this->select_where('catalog_inventory', $where);
$res = $data->fetch_object();
$image = $res->upload_file;
$allimages = explode(",", $image);
$countallimages = count($allimages);
After get multiple values and values run the looping
for ($i = 0; $i < $countallimages; $i++) {
if (file_exists("product_images/" . $allimages[$i]) == false) {
echo "error ";
exit();
}
}
for ($i = 0; $i < $countallimages; $i++) {
$path = "product_images/" . $allimages[$i];
if (empty($path)) {
} else {
unlink($path);
}
}
Thanks ...
Related
I have created a table which I am trying to populate with data from a CSV file exported from AdWords. Following is my table -
The table is only displayed when ajax call is successful because the table HTML is returned by the page I make ajax request to. As you can see I'm getting the table columns and the table is being populated properly but I'm not getting the values for some reason.
GENERATING TABLE WITH VALUES FROM FUNCTION
$campaigns_data = getCampaignsCSVData($account_name);
$campaigns_columns = $campaigns_data['columns'];
$campaigns_values = $campaigns_data['values'];
if(!empty($campaigns_columns) && !empty($campaigns_values)){
echo '<h4>Campaign Totals</h4>'.PHP_EOL;
getCampaignsTable($campaigns_columns,$campaigns_values,$campaigns_included_cols,'Clicks','DESC');
}
FUNCTION TO GENERATE CAMPAIGNS TABLE
function getCampaignsTable($columns,$values,$included_cols,$sort_by='',$sort_dir='ASC'){
$values_by_column_key = array();
foreach($values as $key=>$row){
$processed_cols = array();
foreach($row as $key=>$col){
if(is_numeric(str_replace(',','',$col))){
$col = str_replace(',','',$col);
}
if($col[0] == '$'){
$processed_cols[$columns[$key]] = str_replace('$','',$col);
}else{
$processed_cols[$columns[$key]] = $col;
}
}
$values_by_column_key[] = $processed_cols;
}
if(!empty($sort_by)){
usort($values_by_column_key, function($a,$b)use($sort_by,$sort_dir){
if($sort_dir == 'ASC'){
return $a[$sort_by] > $b[$sort_by];
}elseif($sort_dir == 'DESC'){
return $a[$sort_by] < $b[$sort_by];
}
});
}
echo '<table class="data-table">'.PHP_EOL;
echo '<thead>'.PHP_EOL;
echo '<tr>'.PHP_EOL;
if(!empty($included_cols)){
foreach($included_cols as $col){
echo '<th>'.$col['label'].'</th>'.PHP_EOL;
}
}
echo '</tr>'.PHP_EOL;
echo '</thead>'.PHP_EOL;
echo '<tbody>'.PHP_EOL;
$row_count = 0;
foreach($values_by_column_key as $key1=>$row){
$row_count++;
if($row_count <= RESULT_LIMIT){
echo '<tr>'.PHP_EOL;
foreach($included_cols as $key2=>$inc_cols){
if($inc_cols['format'] == 'currency'){
echo '<td>'.$inc_cols['currency_symbol'].$row[$key2].'</td>'.PHP_EOL;
}else{
echo '<td>'.$row[$key2].'</td>'.PHP_EOL;
}
}
echo '</tr>'.PHP_EOL;
}
}
echo '</tbody>'.PHP_EOL;
echo '</table>'.PHP_EOL;
}
FUNCTION TO READ AND PARSE CSV FILE
function getCampaignsCSVData($account){
$data = array();
$dir = ADWORDS_REPORTS_DIR.$account.'/';
$filename = ADWORDS_CAMPAIGN_FILE;
$filepath = $dir.$filename;
$row_count = 0;
if(file_exists($filepath)){
$csvfile = #fopen($filepath,'r');
if($csvfile){
while(!feof($csvfile)){
$row = fgets($csvfile);
$row = preg_replace("/[^a-zA-Z ]/", "", $row);
$row_count++;
$row_data = '';
if(stristr($row,CAMPAIGNS_REPORT_IN_COLUMNS_WORD)){
$row_data = str_replace('Impr.','Impressions',$row);
$row_data = str_replace('Interactions','Clicks',$row_data);
$data['columns'] = str_getcsv($row_data);
$columns_row_index = $row_count;
} elseif($row_count > $columns_row_index && !empty($columns_row_index)){
$row_data = $row;
if(!stristr($row,'Total') && !empty($row)){
$data['values'][] = str_getcsv($row_data);
}
}
}
}
fclose($csvfile);
}else{
$data = false;
}
return $data;
}
This code has been working for a long time but it suddenly stopped working recently without changing anything. I can't seem to get my head around it because I can't find any error. Please help. Thanks!
I'm currently have a recordset created with dreamweaver and have encode the results in json format which work fine.
Recordset
$maxRows_rs_feeds = 3;
$pageNum_rs_feeds = 0;
if (isset($_GET['pageNum_rs_feeds'])) {
$pageNum_rs_feeds = $_GET['pageNum_rs_feeds'];
}
$startRow_rs_feeds = $pageNum_rs_feeds * $maxRows_rs_feeds;
mysql_select_db($database_vivalooks, $vivalooks);
$query_rs_feeds = "SELECT fname,lname FROM profile";
$query_limit_rs_feeds = sprintf("%s LIMIT %d, %d", $query_rs_feeds, $startRow_rs_feeds, $maxRows_rs_feeds);
$rs_feeds = mysql_query($query_limit_rs_feeds, $vivalooks) or die(mysql_error());
$row_rs_feeds= mysql_fetch_assoc($rs_feeds);
if (isset($_GET['totalRows_rs_feeds'])) {
$totalRows_rs_feeds = $_GET['totalRows_rs_feeds'];
} else {
$all_rs_feeds = mysql_query($query_rs_feeds);
$totalRows_rs_feeds = mysql_num_rows($all_rs_feeds);
}
$totalPages_rs_feeds = ceil($totalRows_rs_feeds/$maxRows_rs_feeds)-1;
do {
echo json_encode($row_rs_feeds);
} while ($row_rs_feeds = mysql_fetch_assoc($rs_feeds));
mysql_free_result($rs_feeds);
Recordset Results
{"fname":"Benjamin","lname":"Blay"}{"fname":"Alfread","lname":"Mark"}{"fname":"yaa","lname":"tiwaa"}
But i want the results to be encode like this rather
[{"fname":"Benjamin","lname":"Blay"},{"fname":"Alfred","lname":"Mark"},{"fname":"yaa","lname":"tiwaa"}]
Try
do {
$json[]=json_encode($row_rs_feeds);
} while ($row_rs_feeds = mysql_fetch_assoc($rs_feeds));
echo "[".implode(", ", $json)."]";
OR
do {
$rows[] = $row_rs_feeds;
} while ($row_rs_feeds = mysql_fetch_assoc($rs_feeds));
echo json_encode($rows);
You can change your loop to look like this:
echo "[";
$count = 0;
do {
if($count !== 0)
echo ",";
$count++;
echo json_encode($row_rs_feeds);
} while ($row_rs_feeds = mysql_fetch_assoc($rs_feeds));
echo "]";
$count is there just to skip first "," so you don't get [,{...},{...}] or you can put it like false/true... I just can't remember right now what is better in case you have 10k steps loop...
Try:
do {
$rows[] = $row_rs_feeds;
} while ($row_rs_feeds = mysql_fetch_assoc($rs_feeds));
echo json_encode($rows);
I am receiving the following error message "Header may not contain more than a single header, new line detected" I know it says that a new line has been detected, but I cannot figure where this line is coming from. I have tried to trim the variables..I have re-written the header line in different ways, without any result. I added the getallheaders function to see what was being passed, but I see no new line or any extra characters in the output $headers. Even using ob_start() does not help.
<?php
ob_start();
include "catalog.obj";
session_start();
$catalogObj = $_SESSION['catalogObj'];
if (isset($_POST['st']))
$st = $_POST['st'];
else
$st = '0';
if (isset($_POST['num']))
$num = $_POST['num'];
else
$num = '0';
if (isset($_POST['type']))
$type = $_POST['type'];
else
$type = '0';
if (isset($_POST['rec']))
$rec = $_POST['rec'];
else
$rec = '0';
if (isset($_POST['option']))
$option = $_POST['option'];
else
$option = '0';
if(strcmp($_POST['submit'],"Reset Form") == 0)
{
header("location: search_catalog.php?type=$type&firstTime=1");
exit;
}
elseif(strcmp($_POST['submit'],"Catalog Administration") == 0)
{
Header("Location: administration.php");
exit;
}
else
{
$inventory_id_num = $_POST['inventory_id_num'];
$inventory_desc = $_POST['inventory_desc'];
$inventory_revision = $_POST['inventory_revision'];
$quantity = $_POST['quantity'];
$catalog_status_id = $_POST['catalog_status_id'];
$order_form_type_id = $_POST['order_form_type_id'];
$catalogObj->inventory_id_num = $inventory_id_num;
$catalogObj->inventory_desc = $inventory_desc;
$catalogObj->inventory_revision = $inventory_revision;
$catalogObj->quantity = $quantity;
$catalogObj->catalog_status_id = $catalog_status_id;
//$catalogObj->order_form_type_id = array();
$catalogObj->order_form_type_id = $order_form_type_id;
$count=count($order_form_type_id);
for ($i=0; $i<$count; $i++)
{
//print "order_form_type_id: $order_form_type_id[$i]<br>";
if(strlen($order_form_type_id[$i]) > 0)
{
$catalogObj->order_form_type_id[$i] = $order_form_type_id[$i];
}
}
if(strcmp($_POST['submit'],"Back to Order Form") == 0)
{
Header("Location: order_form.php?num=$num");
exit;
}
else
{
//$url = "type=".$type."option=".$option."rec=".$rec."st=".$st."num=".$num;
Header("location: search_catalog_handler.php?type=$type&option=$option&rec=$rec&st=$st&num=$num");
//Header("location: search_catalog_handler.php?" . rawurlencode($url));
if (function_exists('getallheaders'))
{
$headers = getallheaders();
print_r( $headers);
}
exit;
}
}
function getallheaders()
{
$headers = '';
foreach ($_SERVER as $name => $value)
{
if (substr($name, 0, 5) == 'HTTP_')
{
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
return $headers;
}
?>
First, thanks for the pointers! The problem in the above code was with the $st variable. I am not very experienced with headers and rewriting them but I had add the following conditinal statement:
if (!empty($_POST['st']))
{
$st = $_POST['st'];
$num = $_POST['num'];
$type = $_POST['type'];
$rec = $_POST['rec'];
$option = $_POST['option'];
}
To the beginning of my code, so it the complete code is:
<?php
ob_start();
/*************************************
altered complete 12/20/2013
rjm
*************************************/
include "catalog.obj";
session_start();
$catalogObj = $_SESSION['catalogObj'];
if (!empty($_POST['st']))
{
$st = $_POST['st'];
$num = $_POST['num'];
$type = $_POST['type'];
$rec = $_POST['rec'];
$option = $_POST['option'];
}
if(strcmp($_POST['submit'],"Reset Form") == 0)
{
header("location: search_catalog.php?type=$type&firstTime=1");
exit;
}
elseif(strcmp($_POST['submit'],"Catalog Administration") == 0)
{
Header("Location: administration.php");
exit;
}
else
{
echo "<pre>";
print_r($_POST);
echo "</pre>";
//exit;
$inventory_id_num = $_POST['inventory_id_num'];
$inventory_desc = $_POST['inventory_desc'];
$inventory_revision = $_POST['inventory_revision'];
$quantity = $_POST['quantity'];
$catalog_status_id = $_POST['catalog_status_id'];
$order_form_type_id = $_POST['order_form_type_id'];
$catalogObj->inventory_id_num = $inventory_id_num;
$catalogObj->inventory_desc = $inventory_desc;
$catalogObj->inventory_revision = $inventory_revision;
$catalogObj->quantity = $quantity;
$catalogObj->catalog_status_id = $catalog_status_id;
$catalogObj->order_form_type_id = $order_form_type_id;
$count=count($order_form_type_id);
for ($i=0; $i<$count; $i++)
{
if(strlen($order_form_type_id[$i]) > 0)
{
$catalogObj->order_form_type_id[$i] = $order_form_type_id[$i];
}
}
if(strcmp($_POST['submit'],"Back to Order Form") == 0)
{
Header("Location: order_form.php?num=$num");
exit;
}
else
{
Header("location: search_catalog_handler.php?type=$type&option=$option&rec=$rec&st=$st&num=$num");
exit;
}
}
?>
This allows for a specific type search (with parameters) and a general type search (no parameters) from the sending page.
Assuming that catalog.obj does not output any information to the browser (which would result in an error as well), your $type variable looks like the culprit since it's the only wildcard.
Note that you'll need to do the following for all POSTed variables in your script that you want to use in a URI:
Sine it's possible that $type could be anything (it's using the POSTed variable sometimes), you should clean it up before spitting it back out in your header:
$type = urlencode($type); // Prepares the variable to be inserted in the URI
header("Location: search_catalog.php?type=$type&firstTime=1");
I'm using this script to list a few Twitch.tv streams and their status (offline or online).
If there are no online streams found, I want it to display a text saying that all are offline.
Code that checks if the added streams are online:
//get's member names from stream url's and checks for online members
$channels = array();
for ($i = 0; $i < count($members); $i++) {
if (isset($json_array[$i])){
$title = $json_array[$i]['channel']['channel_url'];
$array = explode('/', $title);
$member = end($array);
$viewer = $json_array[$i] ['stream_count'];
onlinecheck($member, $viewer);
$checkedOnline[] = signin($member);
}
}
unset($value);
unset($i);
//checks if player streams are online
function onlinecheck($online, $viewers)
{
//If the variable online is not equal to null, there is a good change this person is currently streaming
if ($online != null)
{
echo ' <strong>'.$online.'</strong>';
echo '  <img src="/images/online.png"><strong> Status:</strong> Online! </br>';
echo '<img src="/images/viewers.png"><strong>Viewers:</strong>  ' .$viewers.'</br>';
}
}
Full code:
<html>
<head>
<title>Streamlist</title>
</head>
<body>
<?php
$members = array("ncl_tv");
$userGrab = "http://api.justin.tv/api/stream/list.json?channel=";
$checkedOnline = array ();
foreach($members as $i =>$value){
$userGrab .= ",";
$userGrab .= $value;
}
unset($value);
$json_file = file_get_contents($userGrab, 0, null, null);
$json_array = json_decode($json_file, true);
$channels = array();
for ($i = 0; $i < count($members); $i++) {
if (isset($json_array[$i])){
$title = $json_array[$i]['channel']['channel_url'];
$array = explode('/', $title);
$member = end($array);
$viewer = $json_array[$i] ['stream_count'];
onlinecheck($member, $viewer);
$checkedOnline[] = signin($member);
}
}
unset($value);
unset($i);
function onlinecheck($online, $viewers) {
if ($online != null) {
echo ' <strong>'.$online.'</strong>';
echo '  <img src="/images/online.png"><strong> Status:</strong> Online! </br>';
echo '<img src="/images/viewers.png"><strong>Viewers:</strong>  ' .$viewers.'</br>';
}
}
$alloffline = "All female user streams are currently offline.";
function signin($person){
if($person != null){
return $person;
}
?>
</body>
</html>
............................................................................................................................................................................
Is it because your $userGrab URL contains usernames twice? This is the URL whose contents you're retrieving:
http://api.justin.tv/api/stream/list.json?channel=painuser,ZombieGrub,Nathanias,Youbetterknowme,ncl_tv,painuser,ZombieGrub,Nathanias,Youbetterknowme,ncl_tv
Having looked at the response, it doesn't look like it's causing the problem. The strange URL is a result of you appending to the $userGrab string in the first foreach loop, after you've already added them with the implode() function call before. I think twitch.tv is rightly ignoring duplicate channels.
If all the values in $checkedOnline are null, everyone is offline. Put this at the end of your first code sample:
$personOnline = false;
foreach($checkedOnline as $person) {
if($person !== null) {
$personOnline = true;
break;
}
}
if(!$personOnline) {
echo 'No one is online';
}
else {
//there is at least someone online
}
I wrote a upload script on my site where users can upload a predefined number of items (12 in my case). A user can edit his project (of course...).
So I also wrote an update-script. Project are read from the database and a user can reupload or delete certain files. But what I have write know is, when a users doesn't upload a new file, the original file is overwritten by an empty value (makes sense, since the <input> is empty.
But I want to prevent that. How do I do this?
Form:
$activities = Project::getProjectById($_DB, $projectnumber);
if(!empty($activities)) {
foreach($activities as $k => $v) {
echo "<input type='text' value='".$v['name']."' name = 'newproject_name' />";
$i = 1;
while ($i <= 12) {
if(!empty($v["photo$i"])) {
echo "<img src='../files/".$v["photo$i"]."' width='200' height='200' />";
echo "<input type='file' name='images[]' />";
}
$i++;
}
}
echo "<input type=submit value='Update' name='submitted'>";
echo "</form>";
}
PHP:
$project = new Project();
$project->name = $_POST['newproject_name'];
$project->photo1 = $_FILES['images']['name'][0];
$project->photo2 = $_FILES['images']['name'][1];
$project->photo3 = $_FILES['images']['name'][2];
$project->photo4 = $_FILES['images']['name'][3];
$project->photo5 = $_FILES['images']['name'][4];
$project->photo6 = $_FILES['images']['name'][5];
$project->photo7 = $_FILES['images']['name'][6];
$project->photo8 = $_FILES['images']['name'][7];
$project->photo9 = $_FILES['images']['name'][8];
$project->photo10 = $_FILES['images']['name'][9];
$project->photo11 = $_FILES['images']['name'][10];
$project->photo12 = $_FILES['images']['name'][11];
if($project->updateProject($_DB, $projectnummer)) {
$feedback = "<div class='feedback good'>ok!!</div>";
} else {
$feedback = "<div class='feedback bad'>NOT ok!</div>";
}
You don't have to worry about the getProjectById and updateProject-function, they work just fine. But as you can see, the script will upload all the empty values and overwrite them in the database...
I would like to prevent that! Any advice?
EDIT:
if (!empty($_FILES['images']['name'][0])) {
$project->photo1 = $_FILES['images']['name'][0];
}
if (!empty($_FILES['images']['name'][1])) {
$project->photo2 = $_FILES['images']['name'][1];
}
if (!empty($_FILES['images']['name'][2])) {
$project->photo3 = $_FILES['images']['name'][2];
}
if (!empty($_FILES['images']['name'][3])) {
$project->photo4 = $_FILES['images']['name'][3];
}
if (!empty($_FILES['images']['name'][4])) {
$project->photo5 = $_FILES['images']['name'][4];
}
if (!empty($_FILES['images']['name'][5])) {
$project->photo6 = $_FILES['images']['name'][5];
}
if (!empty($_FILES['images']['name'][6])) {
$project->photo7 = $_FILES['images']['name'][6];
}
if (!empty($_FILES['images']['name'][7])) {
$project->photo8 = $_FILES['images']['name'][7];
}
if (!empty($_FILES['images']['name'][8])) {
$project->photo9 = $_FILES['images']['name'][8];
}
if (!empty($_FILES['images']['name'][9])) {
$project->photo10 = $_FILES['images']['name'][9];
}
if (!empty($_FILES['images']['name'][10])) {
$project->photo11 = $_FILES['images']['name'][10];
}
if (!empty($_FILES['images']['name'][11])) {
$project->photo12 = $_FILES['images']['name'][11];
}
$project->photo1 = $_FILES['images']['name'][0];
$project->photo2 = $_FILES['images']['name'][1];
$project->photo3 = $_FILES['images']['name'][2];
$project->photo4 = $_FILES['images']['name'][3];
$project->photo5 = $_FILES['images']['name'][4];
$project->photo6 = $_FILES['images']['name'][5];
$project->photo7 = $_FILES['images']['name'][6];
$project->photo8 = $_FILES['images']['name'][7];
$project->photo9 = $_FILES['images']['name'][8];
$project->photo10 = $_FILES['images']['name'][9];
$project->photo11 = $_FILES['images']['name'][10];
$project->photo12 = $_FILES['images']['name'][11];
can be rewriten as
for ($i = 0; $i <=11; $i++) {
$photo = 'photo' . ($i + 1);
$project->$photo = $_FILES['images']['name'][$i];
}
And you can resume all your edit with:
for ($i = 0; $i <=11; $i++) {
$photo = 'photo' . ($i + 1);
if ( ! empty($_FILES['images']['name'][$i])) {
$project->$photo = $_FILES['images']['name'][$1];
}
}
Maybe You should use is_uploaded_file() function (LINK)?
EDIT:
for($i = 0; $i <= 11; $i++) {
if(!is_uploaded_file($_FILES['images']['name'][$i]))
$project->photo[$i] = $_FILES['images']['name'][$i];
}