Blank page after successful form submit - Codeigniter - php

I am using below function to add an item to cart (which right now is an array, that will be stored in session later). Every thing is working fine only problem is that when i click "Add+" button, it stores all the values in the array but gives a blank page after.
My Controller
if($action == 'Add+') {
if($this->session->userdata('cart')== ''){
$image=$this->input->post('hidden_image');
$name= $this->input->post('hidden_name');
$price= $this->input->post('hidden_price');
$cat= $this->input->post('hidden_cat');
$id= $this->input->post('hidden_id');
$product = array("name"=>$name, "price"=>$price, "id"=>$id, "image" => $image,"cat"=>$cat);
$item= array("0"=>$product);
$this->session->set_userdata('cart',$item);
}
else{
$count= count($this->session->userdata('cart'));
$name= $this->input->post('hidden_name');
$price= $this->input->post('hidden_price');
$id= $this->input->post('hidden_id');
$image=$this->input->post('hidden_image');
$cat= $this->input->post('hidden_cat');
$product = array("name"=>$name, "price"=>$price, "id"=>$id, "image" => $image,"cat"=>$cat);
$item= array($count=>$product);
$data = $this->session->userdata('cart');
$_SESSION["cart"][$count] = $product;
}
}
if($action == 'View') {
$image=$this->input->post('hidden_image');
$name= $this->input->post('hidden_name');
$price= $this->input->post('hidden_price');
$id= $this->input->post('hidden_id');
$cat= $this->input->post('hidden_cat');
$product = array("name"=>$name, "price"=>$price, "id"=>$id, "image" => $image,"cat"=>$cat);
$this->load->view('product_head');
$this->load->view('header',$product);
$this->load->view('product',$product);
}
My View
<?php $data= array ('id'=>'productform');
echo form_open_multipart('loader/product',$data);
$data = array(
'type' => 'hidden',
'name' => 'hidden_cat',
'value' => $row->category);
$val = set_value('hidden_cat');
echo form_input($data, $val);
$data = array(
'type' => 'hidden',
'name' => 'hidden_name',
'value' => $row->name
);
$val = set_value('hidden_name');
echo form_input($data, $val);
$data = array(
'type' => 'hidden',
'name' => 'hidden_image',
'value' => $row->image
);
$val = set_value('hidden_image');
echo form_input($data, $val);
$data = array(
'type' => 'hidden',
'name' => 'hidden_id',
'value' => $row->id
);
$val = set_value('hidden_id');
echo form_input($data, $val);
$data = array(
'type' => 'hidden',
'name' => 'hidden_price',
'value' => $row->price
);
$val = set_value('hidden_price');
echo form_input($data, $val);
$data = array(
'name' => 'action',
'style' => ' background-color: #4CAF50; border: none;',
'value' => 'View'
);
echo form_submit($data);
$data = array(
'name' => 'action',
'style' => ' background-color: #4CAF50; border: none;',
'id' => 'addbtn'
);
echo form_submit($data);
echo form_close();
?>
what I want is that after successful form submit it stay on same page at same place because there are many pages that have these hidden forms for every product.

Related

Woocommerce rest api update product image gives error

I want to update my product with product image but it is not updated. I am using woocommerce rest API for adding a product.
I am following these step for update product:
Create a view file for display all the product list
Create edit function in the controller for getting all product data
when uploading a new image from edit form it gives an error
Here is my code:
if($request->hasfile('filename'))
{
foreach($request->file('filename') as $image)
{
$name=$image->getClientOriginalName();
$image->move('C:/xampp/htdocs/New-flex/wp-content/uploads/backend-product-image', $name);
$productimg_path_store_db = "http://localhost/New-flex/wp-content/uploads/backend-product-image/";
$productimg_allimg[] = $productimg_path_store_db.$name;
}
}
$AddContentText = $request->get('AddContentText');
if (!empty($AddContentText)) {
/* For List All product qnumber check in array */
$newallproducts_data = $woocommerce->get('products',array('per_page' => 100));
$array = json_decode(json_encode($newallproducts_data), True);
$partssku = array();
$partid = array();
$parturl = array();
foreach ($array as $key ) {
$partssku[] = $key['sku'];
$partid[] = $key['id'];
$parturl[] = $key['permalink'];
}
/* Create associative array product id and sku for compare */
$comb = array_combine($partssku,$partid);
/* Combine content QNUMBER and QTY in array */
$chunks = array_chunk(preg_split('/(;|,)/', $AddContentText), 2);
$result = array_combine(array_column($chunks, 0), array_column($chunks, 1));
/*Product qnumber allready avilabale array*/
$diff = array_intersect_key($result,$comb);
/*NEW PRODUCT array*/
$newproduct = array_diff($result, $diff);
/* Qnumber insert in database */
$parts_name = implode(',', array_keys($result));
/* count insert in database */
$noofpartsuse = implode(',', array_values($result));
/*echo "<pre>";
echo "main data";
print_r($result);
echo "allproduct array";
print_r($comb);
echo "Product qnumber allready avilabale";
print_r($diff);
echo "NEW PRODUCT";
print_r($newproduct);
echo "</pre>";*/
// print_r($newproduct_data);
//print_r($woocommerce->post('products', $data));
}
else{
$parts_name = '';
$noofpartsuse = '';
}
$productactive = $request->get('productactive');
if(!empty($productactive))
{
$productactive = "publish";
}
else{
$productactive = "draft";
}
$ProductListOrder = $request->get('ProductListOrder');
$producttype = $request->get('producttype');
$ProductQNumber = $request->get('ProductQNumber');
$ProductName = $request->get('name');
$ProductWidthMM = $request->get('ProductWidthMM');
$ProductLengthMM = $request->get('ProductLengthMM');
$ProductWidthInch = $request->get('ProductWidthInch');
$ProductLengthInch = $request->get('ProductLengthInch');
$Productinfotext = $request->get('Productinfotext');
$Producttechnocaldesc = $request->get('Producttechnocaldesc');
$metadescription = $request->get('metadescription');
$proimgalt = $request->get('proimg-alt');
$proyoutubelink = $request->get('proyoutubelink');
$proyoutubelinkalt = $request->get('proyoutubelink-alt');
$provimeolink = $request->get('provimeolink');
$provimeolinkalt = $request->get('provimeolink-alt');
$onshapelink = $request->get('onshapelink');
$onshapelinkalt = $request->get('onshapelink-alt');
$data = [
'name' => $ProductName,
'type' => $producttype,
'status' => $productactive,
'regular_price' => '',
'description' => $Productinfotext,
'short_description' => $Producttechnocaldesc,
'sku' => $ProductQNumber,
'categories' =>array (),
'meta_data' => [
[
'key' => 'list_order',
'value' => $ProductListOrder
],
[
'key' => 'ProductWidthMM',
'value' => $ProductWidthMM
],
[
'key' => 'ProductLengthMM',
'value' => $ProductLengthMM
],
[
'key' => 'ProductWidthInch',
'value' => $ProductWidthInch
],
[
'key' => 'ProductLengthInch',
'value' => $ProductLengthInch
],
[
'key' => 'proyoutubelink',
'value' => $proyoutubelink
],
[
'key' => 'proyoutubelink-alt',
'value' => $proyoutubelinkalt
],
[
'key' => 'provimeolink',
'value' => $provimeolink
],
[
'key' => 'provimeolink-alt',
'value' => $provimeolinkalt
],
[
'key' => 'onshapelink',
'value' => $onshapelink
],
[
'key' => 'onshapelink-alt',
'value' => $onshapelinkalt
],
[
'key' => 'UseParts-link',
'value' => $parts_name
],
[
'key' => 'Noofparts-use',
'value' => $noofpartsuse
]
],
'images' => array ()
];
/* image array for store image */
if(!empty($productimg_allimg)){
$images = &$data['images'];
$n = 0;
foreach($productimg_allimg as $id)
{
$images[] = array( //this array must be created dynamic
'src' => $id,
'position' => $n++
);
}
unset($images);
}
if(!empty($_POST['categories'])){
/* Producta category array */
$categories = &$data['categories'];
foreach($_POST['categories'] as $cat)
{
$categories[] = array( //this array must be created dynamic
'id' => $cat
);
}
unset($categories);
}
$data_insert = $woocommerce->post('products', $data);
if($data_insert){
foreach ($newproduct as $key => $value) {
$newproduct_data = [
'name' => $key,
'sku' => $key
];
$woocommerce->post('products', $newproduct_data);
}
}
return redirect('products')->with('success', 'Product has been Added');
Above code written in laravel

PHP how to get value from multidimensional array?

I have an array in a class and want to obtain the 'Apple' key value ('iPhone').
I assume a for each loop needs to be used.
How would I go about doing this?
UPDATE: I also need to specify the customerType and productType key values.
class Products {
public function products_list() {
$customerType = 'national';
$productType = 'phones';
$phoneType = 'Apple';
$productsArr = array();
$productsArr[] = array(
'customerType' => 'national',
'productType' => array(
'phones' => array(
'Apple' => 'iPhone',
'Sony' => 'Xperia',
'Samsung' => 'Galaxy'
),
'cases' => array(
'leather' => 'black',
'plastic' => 'red',
),
),
'international' => array(
'phones' => array(
'BlackBerry' => 'One',
'Google' => 'Pixel',
'Samsung' => 'Note'
),
'cases' => array(
'leather' => 'blue',
'plastic' => 'green'
),
)
);
}
}
I have created a function that you can more or less give it any product and it will return the key and value from the array
<?php
class Products
{
public function products_list()
{
$customerType = 'national';
$productType = 'phones';
$phoneType = 'Apple';
$productsArr[] = array(
'customerType' => 'national', 'productType' => array(
'phones' => array(
'Apple' => 'iPhone',
'Sony' => 'Xperia',
'Samsung' => 'Galaxy'
),
'cases' => array(
'leather' => 'black',
'plastic' => 'red',
),
),
'international' => array(
'phones' => array(
'BlackBerry' => 'One',
'Google' => 'Pixel',
'Samsung' => 'Note'
),
'cases' => array(
'leather' => 'blue',
'plastic' => 'green'
),
)
);
echo $this->get_value($phoneType, $productsArr) .'<br>';
echo $this->get_value($customerType, $productsArr) .'<br>';
echo $this->get_value($productType, $productsArr) .'<br>';
}
function get_value($product, array $products)
{
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($products), RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iterator as $key => $value) {
if (is_string($value) && ($key == $product)) {
return 'key ->' . $key .' value ->'.$value;
}
}
return "";
}
}
$phone_products = new Products();
$phone_products->products_list();
To use it within the class just call
$this->get_value($phoneType, $productsArr);
from without the class call
$phone_products = new Products();
echo ($phone_products->get_value($phoneType, $productsArr));
//output: key ->Apple value ->iPhone
NB: $phoneType, $productsArr will either be defined the methods they are being used in or passed from other methods or define global variables within the class.
If you want single entry:
echo $productsArr[0]['productType']['phones']['Apple']."<br />";
If there are multiple data, you can use foreach:
foreach ($productsArr as $prod){
echo $prod['productType']['phones']['Apple']."<br />";
}
just try
foreach($productsArr[0]['productType']['phones'] as $phones){
echo $phones[$phoneType]; }
foreach($productsArr as $key1 => $data1 ){
if(is_array($data1))
foreach($data1 as $key2 => $data2 ){
if(is_array($data2))
foreach($data2 as $key3 => $data3 ){
if(array_key_exists('Apple', $data3)) {
echo $data3['Apple'] ."\n";
}
}
}
}

foreach within array doesnt work, how to add values to to an array?

I have this complicated array.
<?php
require_once('core/connect.php');
require_once('core/database.class.php');
require_once('core/controller.class.php');
require_once('core/settings.class.php');
$database = new Database($db);
$controller = new Controller($db);
$settings = new Settings($db);
$database->selectAll('SELECT * FROM bookings_calendar');
$result = $database->fetchAll();
$count = 0;
$arr = array();
foreach($result as $row)
{
$arr['booking_date'][$count] = $row['booking_date'];
$arr['new'][$count] = $row['new'];
$arr['completed'][$count] = $row['completed'];
$arr['accepted'][$count] = $row['accepted'];
$arr['cancelled'][$count] = $row['cancelled'];
$count++;
}
header("content-type: application/json");
$year = date('Y');
$month = date('m');
echo json_encode(array(
array(
'id' => 111,
'title' => $arr['new'][0] . ' new',
'start' => $arr['booking_date'][0],
'url' => "bookings/ordered-by-date/" . str_replace('-','', $arr['booking_date'][0]),
'color' => '#F7F8E0',
'textColor' => 'black'
),
array(
'id' => 111,
'title' => $arr['new'][1] . ' new',
'start' => $arr['booking_date'][1],
'url' => "bookings/ordered-by-date/" . str_replace('-','', $arr['booking_date'][1]),
'color' => '#F7F8E0',
'textColor' => 'black'
),
array(
'id' => 111,
'title' => $arr['new'][2] . ' new',
'start' => $arr['booking_date'][2],
'url' => "bookings/ordered-by-date/" . str_replace('-','', $arr['booking_date'][2]),
'color' => '#F7F8E0',
'textColor' => 'black'
),
));
?>
As you can see i can only put values manually by changing index, however i'd like to put all elements into that array automatically, but unfortunately i cannot use a foreach loop within an array. And my php skills are not that good, so im searching for some help.
Any help really appreciated. Thanks!
Get rid of the foreach and use a for loop.
$arr = array();
for($i=0; $i < count($result); $i++) {
$arr[$i]['title'] = $result[$i]['new'] . ' new';
$arr[$i]['whatever'] = $result[$i]['whatever'];
}
return json_encode($arr);

How to echo checkbox group with multidimentional array

I need help to get the data from my checkbox-group with multidimensional array options to reflect in my post page(single.php code). Radio type is working well but the checkbox-group type is not. I added on the bottom the sample code found in my single.php for the radio type which query the data to my post page for your reference.
Here's the array from my metabox.php code:
<?php
// array
$prefix = 'wtf_';
$meta_box = array( 'id' => 'site',
'title' => 'Program Details',
'page' => 'post',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array('name' => 'Principal Return',
'desc' => 'Principal Return After Expiry or Not',
'id' => $prefix . 'principal',
'type' => 'radio',
'options' => array(
array('name' => ' Yes ', 'value' => 'Yes-after expiry'),
array('name' => ' No ', 'value' => 'No-included on the interest')
)
),
array(
'name' => 'Compounding',
'desc' => 'Choose if compounding is allowed or not',
'id' => $prefix . 'compounding',
'type' => 'radio',
'options' => array(
array('name' => ' Yes ', 'value' => 'Allowed'),
array('name' => ' No ', 'value' => 'Not Allowed'),
array('name' => ' Re-purchase', 'value' => 'Yes thru re-purchase')
)
),
array ('name' => 'Payment Processors',
'desc' => 'Payment Processsor Accepted',
'id' => $prefix.'processors',
'type' => 'checkbox_group',
'options' => array(
array('label' => ' Liberty Reserve ', 'value' =>'LR'),
array('label' => ' SolidTrustPay ', 'value' =>'STP'),
array('label' => ' EgoPay ', 'value' =>'EgoPay'),
array('label' => ' Perfect Money ', 'value' =>'PM'),
array('label' => ' Payza ', 'value' =>'Payza'),
array('label' => ' PayPal ', 'value' =>'PayPal'),
array('label' => ' Bankwire ', 'value' =>'Bankwire')
))))
// Callback function to show fields in meta box
function mytheme_show_box() {
global $meta_box, $post;
// Use nonce for verification
echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<table class="form-table">';
foreach ($meta_box['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<tr>',
'<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
'<td>';
switch ($field['type']) {
case 'text':
echo $statetemt;
break;
case 'textarea':
echo $statetemt;
break;
case 'select':
echo $statetemt;
break;
case 'radio':
foreach ($field['options'] as $option) {
echo $statetemt; }
break;
case 'checkbox':
foreach ($field['options'] as $option) {
echo $statetemt;}
break;
case 'checkbox_group':
foreach ($field['options'] as $option) {
echo '<input type="checkbox" value="'.$option['value'].'" name="'.$field['id'].'[]" id="'.$option['value'].'"',$meta && in_array($option['value'], $meta) ? ' checked="checked"' : '',' />',$option['label']; }
echo '<br /><span class="description">'.$field['desc'].'</span>';
break;
}
//From my single.php code <<<<=================
<div class="sdinfo"><strong>Principal Return</strong>:<span><?php $principal = get_post_meta(get_the_ID(), 'wtf_principal', true);
if (isset($principal[0])) {
echo $principal ;
} else if (isset($principal[1])) {
$principal = get_post_meta(get_the_ID(), 'wtf_principal', true);
echo $principal;
} else {_e('Not Available');} ?></span></div>
<div class="sdinfo"><strong>Program Started</strong>:<span> <?php $started = get_post_meta(get_the_ID(), 'wtf_started', true); if (isset($started[0])) { echo $started;
} else {_e('Not Available');} ?></span></div>
<div class="sdinfo"><strong>Compounding</strong>:<span>
<?php $compounding = get_post_meta(get_the_ID(), 'wtf_compounding', true);
if (isset($compounding[0])) {
echo $compounding ;
} else if (isset($compounding[1])) {
$compounding = get_post_meta(get_the_ID(), 'wtf_compounding', true);
echo $compounding;
} else if (isset($compounding[2])) {
$compounding = get_post_meta(get_the_ID(), 'wtf_compounding', true);
echo $compounding;
} else {_e('Not Available');} ?></span></div>
?>
This give me an output from post meta like this:
admin screenshot
This is the output from my post page. :
post page screenshot
Please help!.. I am not a programmer hope you can share me an answer in much details.Thank you in advance!
All what you need is point an array using var_dump($array) to check where and what data you need from arrays fields.
Second you need to get foreach() function. For example you want grab an array of data:
'priority' => 'high', should be as example:
echo $meta_box["priority"] which will result as value of array: high
If you are not sure and not familiar with array use as simple:
foreach ($meta_box as $arr)
{
var_dump($arr);
#then play and manipulate how to grab a fields a data:
foreach ($arr["fields"][""] as $fa)
{
var_dump($fa["name"]);
var_dump($fa["desc"]);
var_dump($fa["desc"]);
}
#...
}
Learn how to grab via var_dump() if you unsure with array of data.
Payment Processors LR STP EgoP
echo $meta_box["fields"][""]["name"];
Than you need grab a data of an array:
foreach ($meta_box["fields"][""]["options"] as $options)
{
foreach ($options as $options_key)
{
foreach ($options_key as $opt_val)
{
$result .= ' '.$opt_val;
}
}
}

Can you compare an array of strings in PHP and if there are two that are the same it will only return it once?

Here is the script I am running and I would like if there are 2 strings that the same to only display one string and not both. I dont know where to add the array_unique() I have added it to my script but it doesnt seem to work properlly, instead it is taking out all the strings with the same value Here is the script I am running and I would like if there are 2 strings that the same to only display one string and not both
//Get slider data from theme options
$company1 = $data['md_media_company_img1'];
$company2 = $data['md_media_company_img2'];
$company3 = $data['md_media_company_img3'];
$company4 = $data['md_media_company_img4'];
$company5 = $data['md_media_company_img5'];
$company6 = $data['md_media_company_img6'];
$company7 = $data['md_media_company_img7'];
$company8 = $data['md_media_company_img8'];
$company9 = $data['md_media_company_img9'];
$company10 = $data['md_media_company_img10'];
$company11 = $data['md_media_company_img11'];
$company12 = $data['md_media_company_img12'];
/*Slides Array*/
$company_name = array(
'company1' => array(
'name' => $company1,
),
'company2' => array(
'name' => $company2,
),
'company3' => array(
'name' => $company3,
),
'company4' => array(
'name' => $company4,
),
'company5' => array(
'name' => $company5,
),
'company6' => array(
'name' => $company6,
),
'company7' => array(
'name' => $company7,
),
'company8' => array(
'name' => $company8,
),
'company9' => array(
'name' => $company9,
),
'company10' => array(
'name' => $company10,
),
'company11' => array(
'name' => $company11,
),
'company12' => array(
'name' => $company12,
)
);
/*check if exist slide*/
$check_exist_company = 0;
$result = array_unique($company_name);
foreach($result as $company => $value) {
if (!empty ($value['name'])){
$check_exist_company = 1;
}
}
?>
<?php if($check_exist_company == 1) {// check if any slide image added in theme option, return custom slide?>
<?php $i = 1; ?>
<?php foreach($company_name as $company => $value) {
if (!empty ($value['name'])) {?>
<li><a class="nivoLink4" rel="<?php echo $i;?>" href="#"><?php echo $value['name'];?></a></li>
<?php ++$i ?>
<?php } ?>
<?php }?>
<?php } ?>
<!--/slider-->
You could just run array_unique() on the source array and just iterate over the result.

Categories