How To Price included in the Osclass RSS Feed Title? - php

I would like to ask on how to include the price in the RSS Feed Title in website running with Osclass.
Like This [Price / Title (Contact Number)]
public function dumpXML() {
echo '<?xml version="1.0" encoding="UTF-8"?>', PHP_EOL;
echo '<rss version="2.0">', PHP_EOL;
echo '<channel>', PHP_EOL;
echo '<title>', $this->title, '</title>', PHP_EOL;
echo '<link>', $this->link, '</link>', PHP_EOL;
echo '<description>', $this->description, '</description>', PHP_EOL;
foreach ($this->items as $item) {
echo '<item>', PHP_EOL;
echo '<title><![CDATA[', $item['title'], ']]></title>', PHP_EOL;
echo '<link>', $item['link'], '</link>', PHP_EOL;
echo '<guid>', $item['link'], '</guid>', PHP_EOL;
echo '<description><![CDATA[';
echo $item['description'], ']]>';
echo '</description>', PHP_EOL;
echo '<country>', $item['country'], '</country>', PHP_EOL;
echo '<region>', $item['region'], '</region>', PHP_EOL;
echo '<city>', $item['city'], '</city>', PHP_EOL;
echo '<cityArea>', $item['city_area'], '</cityArea>', PHP_EOL;
echo '<category>', $item['category'], '</category>', PHP_EOL;
echo '</item>', PHP_EOL;
}
echo '</channel>', PHP_EOL;
echo '</rss>', PHP_EOL;
}
}
Thanks You

Normally, I would tell you not to modify the core. You have a hook available designed for this purpose : 'feed' but it seems you can't access the data. So you'll have to modify the core.
Add a line 'price' => osc_item_formated_price() to both addItem() calls in oc-includes/controllers/search.php :
while(osc_has_items()) {
if(osc_count_item_resources() > 0){
osc_has_item_resources();
$feed->addItem(array(
'title' => osc_item_title(),
'link' => htmlentities( osc_item_url(), ENT_COMPAT, "UTF-8" ),
'description' => osc_item_description(),
'country' => osc_item_country(),
'region' => osc_item_region(),
'city' => osc_item_city(),
'city_area' => osc_item_city_area(),
'category' => osc_item_category(),
'dt_pub_date' => osc_item_pub_date(),
'image' => array( 'url' => htmlentities(osc_resource_thumbnail_url(), ENT_COMPAT, "UTF-8"),
'title' => osc_item_title(),
'link' => htmlentities( osc_item_url() , ENT_COMPAT, "UTF-8") )
));
} else {
$feed->addItem(array(
'title' => osc_item_title(),
'link' => htmlentities( osc_item_url() , ENT_COMPAT, "UTF-8"),
'description' => osc_item_description(),
'country' => osc_item_country(),
'region' => osc_item_region(),
'city' => osc_item_city(),
'city_area' => osc_item_city_area(),
'category' => osc_item_category(),
'dt_pub_date' => osc_item_pub_date()
));
}
}
Then you'll be able to modify the RSSfeed::dumpXML method by adding a echo $item['price'] somewhere.
PS:
I'll try to make a commit in the Osclass Github to make the feed hook usable.

Related

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

PHP filtering - detecting filter failures

Update: It appears I had snipped out the wrong parts when whittling this down to a short/concise example.
I appears to (incorrectly!!) used if(empty(0)) to detect validation failure when using filter_var_array and FILTER_VALIDATE_INT.
How would be best to do so?
http://codepad.viper-7.com/Z8MLLj
$positiveIntegerFilter = array('filter' => FILTER_VALIDATE_INT,
'options' => array(
'min_range' => 0
)
);
$filter = array(
'apples' => $positiveIntegerFilter,
'oranges' => $positiveIntegerFilter,
'pears' => $positiveIntegerFilter,
'bananas' => $positiveIntegerFilter,
'tangerine' => $positiveIntegerFilter
);
$values = Array(
"apples" => 2,
"oranges" => 4,
"pears" => 0,
"bananas" => -2,
"grapefruit" => 1
);
//Apply filter, and return only what validates
$filteredValues = filter_var_array( $values, $filter );
echo "values: ";
print_r($values);
echo "<br/>";
echo "filteredValues: ";
print_r($filteredValues);
echo "<br/>";
echo "<br/>";
//Examine filtered array for missing parts
foreach($filter as $key => $value) {
echo "key = " . $key . " // value = " . $filteredValues[$key] . "(" . (gettype($filteredValues[$key])) .")". "<br/>" . PHP_EOL;
if(empty($filteredValues[$key])) { //should test if(!isset()) ?
throw new \InvalidArgumentException("Invalid information object on key: `" . $key . "`");
}
}

3 dimension Array foreach

Im having some problem displaying a multidimensional array...
$copyscape = array (
'query' => 'www.example.html',
'querywords' => 444,
'count' => 230,
'result' => array(
'number' => array(
'index' => 1,
'url' => 'http://www.archives.gov/exhibits/charters/declaration_transcript.html',
'title' => 'Declaration of Independence - Text Transcript',
'minwordsmatched' => 406,
'viewurl' => 'http://view.copyscape.com/compare/w4med9eso0/1'
)
)
);
Basically I want to display everything and also save it in a variable...
echo "<ul>";
foreach($copyscape as $name => $value)
{
echo "<li>" . $name . " : ". $value . "</li>";
}
echo "</ul>";
I tried inserting another set of foreach inside but it gives me an
Invalid argument supplied for foreach()
Try with this code :
$copyscape = array (
'query' => 'www.example.html',
'querywords' => 444,
'count' => 230,
'result' => array(
'number' => array(
'index' => 1,
'url' => 'http://www.archives.gov/exhibits/charters/declaration_transcript.html',
'title' => 'Declaration of Independence - Text Transcript',
'minwordsmatched' => 406,
'viewurl' => 'http://view.copyscape.com/compare/w4med9eso0/1'
)
)
);
function test_print($item, $key)
{
echo "<li>" . $key . " : ". $item . "</li>";
}
echo "<ul>";
array_walk_recursive($copyscape, 'test_print');
echo "</ul>";
use this
foreach ($copyscape as $key=>$value){
echo "<li>" . $key. " : ". $value. "</li>"; // this for main Array
if (is_array ($value))
{
foreach ($value as $childArrayKey => $childArrayValue ){
echo "<li>" . $childArrayKey . " : ". $childArrayValue . "</li>"; // this for childe array
}
}
}
or
foreach ($copyscape as $key=>$value){
echo "<li>" . $key. " : ". $value. "</li>"; // this for main Array
foreach ($value['result']['number'] as $childArrayKey => $childArrayValue ){
echo "<li>" . $childArrayKey . " : ". $childArrayValue . "</li>"; // this for childe array
}
}
In case if you want to have lists inside of lists (nested levels)..
function outputLevel($arr)
{
echo '<ul>';
foreach($arr as $name => $value)
{
echo '<li>';
if (is_array($value))
outputLevel($value);
else
echo $name . ' : ' . $value;
echo '</li>'
}
echo '</ul>';
}
outputLevel($copyscape);
Try following code:
<?php
$copyscape = array (
'query' => 'www.example.html',
'querywords' => 444,
'count' => 230,
'result' => array(
'number' => array(
'index' => 1,
'url' => 'http://www.archives.gov/exhibits/charters/declaration_transcript.html',
'title' => 'Declaration of Independence - Text Transcript',
'minwordsmatched' => 406,
'viewurl' => 'http://view.copyscape.com/compare/w4med9eso0/1'
)
)
);
MultiDimRecuArray($copyscape);
function MultiDimRecuArray($copyscape) {
echo "<ul>";
foreach ($copyscape as $key=>$val) {
if(is_array($val)){
MultiDimRecuArray($val);
}else{
echo "<li>" . $key . " : ". $val . "</li>";
}
}
echo "</ul>";
}
Try something with a recursive function, like this:
function printArray($array)
{
echo "<ul>";
foreach($array as $name=>$value)
{
if(is_array($value))
{
printArray($value);
}
else
{
echo "<li>" . $name . " : ". $value . "</li>";
}
}
echo "</ul>";
}
$copyscape = array (
'query' => 'www.example.html',
'querywords' => 444,
'count' => 230,
'result' => array(
'number' => array(
'index' => 1,
'url' => 'http://www.archives.gov/exhibits/charters/declaration_transcript.html',
'title' => 'Declaration of Independence - Text Transcript',
'minwordsmatched' => 406,
'viewurl' => 'http://view.copyscape.com/compare/w4med9eso0/1'
)
)
);
printArray($copyscape);
Use this
<?php
$copyscape = array (
'query' => 'www.example.html',
'querywords' => 444,
'count' => 230,
'result' => array(
'number' => array(
'index' => 1,
'url' => 'http://www.archives.gov/exhibits/charters/declaration_transcript.html',
'title' => 'Declaration of Independence - Text Transcript',
'minwordsmatched' => 406,
'viewurl' => 'http://view.copyscape.com/compare/w4med9eso0/1'
)
)
);
PrintMultiDimArray($copyscape);
function PrintMultiDimArray($copyscape) {
echo "<ul>";
foreach ($copyscape as $key=>$val) {
if(is_array($val)){
echo "<li>";
PrintMultiDimArray($val);
echo "</li>";
}else{
echo "<li>" . $key . " : ". $val . "</li>";
}
}
echo "</ul>";
}
?>
try this..
echo '<ul>';
displayList($copyscape);
echo '</ul>';
function displayList($arr)
{
foreach($arr as $name => $value) {
if (is_array($value)) {
displayList($value);
}
else {
echo '<li>';
echo $name . ' : ' . $value;
echo '</li>';
}
}
}
Use this:
function print_stuff($arr){
foreach($arr as $key => $val){
if(is_array($val)){
echo "$key: <ul>";
print_stuff($val);
echo "</ul>";
} else {
echo "<li>$key : $val</li>\n";
}
}
}
echo "<ul>";
print_stuff($copyscape);
echo "</ul>";
The code prints a nested list including the key name of the nested lists:
query : www.example.html
querywords : 444
count : 230
result:
number:
index : 1
url : http://www.archives.gov/exhibits/charters/declaration_transcript.html
title : Declaration of Independence - Text Transcript
minwordsmatched : 406
viewurl : http://view.copyscape.com/compare/w4med9eso0/1

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

Categories