How to echo checkbox group with multidimentional array - php

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;
}
}
}

Related

Blank page after successful form submit - Codeigniter

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.

Output array information with PHP

I have the following array:
$features = array(
array(
'name' => 'Communication',
'plans' => array(
'standard' => 'yes',
'advanced' => 'yes'
)
),
array(
'name' => 'French',
'plans' => array(
'standard' => 'no',
'advanced' => 'yes'
)
)
);
How can I output this:
- Communication : standard > yes
- Communication : advanced > yes
- French : standard > no
- French : advanced > yes
What I tried:
foreach ($features as $feature => $info) {
echo $feature['name'].' : ' ['plans']['standard'].' > '.$feature['name']['plans']['standard'].'<br />';
echo $feature['name'].' : ' ['plans']['standard'].' > '.$feature['name']['plans']['advanced'].'<br />';
}
But it doesn't work because nothing is outputted.
Could you please help me ?
Thanks.
You should use a nested for loop as plans itself is an array as follows
foreach ($features as $feature) {
foreach($feature['plans'] as $key=>$val){
echo $feature['name'].' : '. $key.' > '.$val.'<br />';
}
}
I got the following output
Communication : standard > yes
Communication : advanced > yes
French : standard > no
French : advanced > yes
Try this:
foreach ($features as $feature => $info) {
echo $info['name'].' : standard > ' . $info['plans']['standard'].'<br />';
echo $info['name'].' : advanced > ' . $info['plans']['advanced'].'<br />';
}
You are referring to the wrong variable in your loop
This should do the trick. You should used $info to access the data inside because everytime you do a foreach loop, you get inside the parent array.
$features = array(
array(
'name' => 'Communication',
'plans' => array(
'standard' => 'yes',
'advanced' => 'yes'
)
),
array(
'name' => 'French',
'plans' => array(
'standard' => 'no',
'advanced' => 'yes'
)
)
);
foreach ($features as $feature => $info) {
echo $info['name']. ' : standard > '. $info['plans']['standard'] . '<br />';
echo $info['name']. ' : advanced > '. $info['plans']['advanced'] . '<br />';
}

How to edit the code so that the results are as we expected?

My code is like this :
<?php
function romanic_number($integer, $upcase = true)
{
$table = array('M'=>1000, 'CM'=>900, 'D'=>500, 'CD'=>400, 'C'=>100, 'XC'=>90, 'L'=>50, 'XL'=>40, 'X'=>10, 'IX'=>9, 'V'=>5, 'IV'=>4, 'I'=>1);
$return = '';
while($integer > 0)
{
foreach($table as $rom=>$arb)
{
if($integer >= $arb)
{
$integer -= $arb;
$return .= $rom;
break;
}
}
}
return $return;
}
$testArray = array(
array(
'display' => '1', // strlen = 1 => it's Continent
'uraian' => 'European Continent',
),
array(
'display' => '114', // strlen = 3 => it's Country
'uraian' => 'England Country',
),
array(
'display' => '1141444', // strlen = 7 => it's City
'uraian' => 'London City',
),
array(
'display' => '1141445',
'uraian' => 'Manchester City',
),
array(
'display' => '1141452',
'uraian' => 'Liverpool City',
),
array(
'display' => '1141454',
'uraian' => 'Oxford City',
),
array(
'display' => '115',
'uraian' => 'Spain Country',
),
array(
'display' => '1151464',
'uraian' => 'Madrid City',
),
array(
'display' => '1151465',
'uraian' => 'Barcelona City',
),
array(
'display' => '2',
'uraian' => 'Asian Continent',
),
array(
'display' => '215',
'uraian' => 'Japan Country',
),
array(
'display' => '2151474',
'uraian' => 'Tokyo City',
),
array(
'display' => '2151475',
'uraian' => 'Osaka City',
)
);
echo '<table border=1>
<tr>
<th>No</th>
<th>Description</th>
</tr>';
$i = 1;
$j = 'A';
foreach($testArray as $key)
{
echo '<tr>
<td>';
if(strlen($key['display'])==3)
{
echo romanic_number($i);
++$i;
}
if(strlen($key['display'])==7)
{
echo $j;
++$j;
}
echo '</td>
<td>';
echo $key['uraian'];
echo '</td>
</tr>';
}
echo '</table>';
?>
The result is like this : http://postimg.org/image/9jmn7mlr1/
I want the result not like that, but like this:
https://postimg.org/image/579p02c1p/
I had try to edit the code, but i'm still can't do it.
Is there any people who can help me?
You have to do two things here:
Reset both $i and $j variables once your foreach completes the traversal of one continent.
Reset variable $j once the loop completes the traversal of every city of a country.
So your foreach loop should be like this:
foreach($testArray as $key){
if(strlen($key['display'])==1){
$i = 1; $j = 'A';
}
echo '<tr><td>';
if(strlen($key['display'])==3){
echo romanic_number($i);
++$i;
$j = 'A';
}
if(strlen($key['display'])==7){
echo $j;
++$j;
}
echo '</td><td>';
echo $key['uraian'];
echo '</td></tr>';
}
If your array is always sorted properly by Continent/Country/City before you build your html code, then you can just reset the $i/$j counters when you come to a Continent
if(strlen($key['display'])==1) {$i = 1;$j = 'A';}
just place at the start of your loop
...
foreach($testArray as $key)
{
if(strlen($key['display'])==1) {$i = 1;$j = 'A';}
...

how to get values using foreach

I am trying to create menu dynamically according to the user input. I have an array like this, I want to get each value by their indices or I want to use the values to create menu by their order:
$menu['main_menu']= array(
'menu_name' =>'main_menu',
'menu_item1'=>array('home' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'menu_id'=>'new_menu' )),
'menu_item2'=>array('development' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'sub_menu'=>'0' )),
'menu_item3'=>array('php' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'sub_menu'=>'development' )),
);
Try the following code:
foreach ($menu['main_menu'] as $item => $menu_item) {
if (is_array($menu_item)) {
foreach ($menu_item as $menu_name => $menu_attr) {
echo "menu name: " . $menu_name;
echo '<br/>';
foreach ($menu_attr as $attr => $val) {
echo $attr . "->" . $val;
echo '<br/>';
}
}
}
else{
echo $item.": ".$menu_item;
echo "<br />";
}
}
$menu['main_menu']= array(
'menu_name' =>'main_menu',
'menu_item1'=>array('home' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'menu_id'=>'new_menu' )),
'menu_item2'=>array('development' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'sub_menu'=>'0' )),
'menu_item3'=>array('php' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'sub_menu'=>'development' )),
'menu_item4'=>array('php2' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'sub_menu'=>'development' )),
'menu_item5'=>array('development2' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'sub_menu'=>'0' )),
'menu_item6'=>array('php2' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'sub_menu'=>'development2' )),
);
$submenus = [];
$mainMenu = [];
$menuStart;
$menuEnd;
//prapring data
foreach($menu['main_menu'] as $item){
if(is_array($item)){
foreach($item as $link){
if(isset($link['sub_menu']) & $link['sub_menu'] != '0'){
$submenus[] = [
'name' => key($item),
'sub_menu' => $link["sub_menu"],
'body' => "<li class='{$link["Class name"]}'><a href='{$link["URL"]}' class='{$link["Class name"]}' id='{$link["menu_id"]}'>" . key($item) . "</a>",
'end' => "</li>"
];
} else {
$mainMenu[] = [
'name' => key($item),
'body' => "<li class='{$link["Class name"]}'><a href='{$link["URL"]}' class='{$link["Class name"]}' id='{$link["menu_id"]}'>" . key($item) . "</a>",
'end' => "</li>"
];
}
}
} else {
$menuStart = "<ul class='{$item}'>";
}
}
/// menu generatig
$menuEnd = '</ul>';
echo $menuStart;
foreach($mainMenu as $menu){
echo $menu['body'];
foreach($submenus as $submenu){
if($submenu["sub_menu"] == $menu['name']){
echo "<ul>";
echo $submenu['body'];
echo "</ul>";
}
}
echo $menu['end'];
}
echo $menuEnd;
Following code may help you.
$menu['main_menu']= array(
'menu_name' =>'main_menu',
'menu_item1'=>array('home' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'menu_id'=>'new_menu' )),
'menu_item2'=>array('development' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'sub_menu'=>'0' )),
'menu_item3'=>array('php' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'sub_menu'=>'development' )),
);
foreach($menu['main_menu'] as $menu_name=>$menu_value){
echo "<br><br>menu_name ". $menu_name;
if(is_array ( $menu_value )){
foreach($menu_value as $k=>$v){
if(is_array ( $v )){
foreach($v as $key=>$value)
echo "<br>key=".$key." value=".$value;
}
}
}
}
truy this
foreach($menu['main_menu'] as $menu_name=>$menu_value)
{
if(is_array($menu_value))
{
foreach($menu_value as $value)
{
if(is_array ( $value ))
{
echo "<a href='{$value["URL"]}' class='{$value["Classname"]}'>" . key($menu_value) . "</a><br />"; //
}
}
}
}

Multidimensional Array structure to PHP HTML output?

I have repeated sections in my web site.
I'm trying to decrease them,
My approach is one time i will create a html schema (predefined as array), via this schema my function undestranding that what kind of output user needs and outputs results according to my variables..
My problem is I could not find a way to complete situation..
Input variables might be (infinitive)
$vals = array('Country Name' => 'USA', 'Country Name' => 'Canada', 'Country Name' => 'Mexico');
$vals = array('Country Name' => 'USA', 'Country Name' => 'Canada');
$vals = array('Country Name' => 'USA');
Expected outputs are in order
<div class="lines">
<div><span>Country Name</span>USA</div>
<div><span>Country Name</span>Canada</div>
<div><span>Country Name</span>Mexico</div>
</div>
<div class="lines">
<div><span>Country Name</span>USA</div>
<div><span>Country Name</span>Canada</div>
</div>
<div class="lines">
<div><span>Country Name</span>USA</div>
</div>
Schema is the variables that is totally suitable with html
$schema = array(
array(
'tag' => 'div',
'class' => 'lines',
array(
'tag' => 'div',
array(
'tag' => 'span',
'key' => '$key'
),
'key' => '$key'
),
)
);
My function creates outputs from this schema. (Schema might be expand accordings to user need.)
function get_output($schema, $vals, $t = -1){
$t++;
$tag = "";
$atts = array();
$keys = array();
$code = array();
foreach($schema as $k => $v){
if(is_array($v)){
$keys[] = get_output($v, $vals, $t);
} else {
switch($k){
case "tag": $tag = $v; break;
case "key": break;
case "type": break;
default: $atts[$k] = $v; break;
}
}
}
if($tag){
$code[] = "<$tag";
foreach($atts as $k=>$v)
{
$code[] = ' '.$k.'="'.urlencode($v).'"';
}
$code[] = ">";
$code = array(implode('', $code));
}
foreach($keys as $k)
{
$code[] = $k;
}
if($tag){
$code[] = '</'.$tag.'>';
}
return implode("", $code);
}
After all this process I will just call my function with the variables., Schema predefined once.
$vals = array('Country Name' => 'USA', 'Country Name' => 'Canada', 'Country Name' => 'Mexico');
echo get_output($schema, $vals, -1);
UPDATE1
My current output is the following html code, I could not find a way to push variables. Which are Country Name / Country
<div class="lines">
<div><span></span></div>
<div><span></span></div>
</div>
UPDATE3 Different Approach to Schema
$schema = array(
'div' => array(
'class' => 'lines',
'div' => array(
'span' => array(
'key' => '$k[0]'
),
'key' => '$k[1]'
)
);

Categories