I am currently messing around with iTunes Api and have ran into a problem with the returning process of albums track list returning the first result as the actual album data instead of the tracklist.
$loop['artist_name'] = $counted[$x]->artistName;
$loop['album_id'] = $counted[$x]->collectionId;
$loop['album_name'] = $counted[$x]->collectionName;
$loop['track_number'] = $counted[$x]->trackCount;
$loop['artwork_url'] = $counted[$x]->artworkUrl100;
$loop['copyright'] = $counted[$x]->copyright;
$loop['genre'] = $counted[$x]->primaryGenreName;
$loop['release_date'] = $counted[$x]->releaseDate;
$data_b = file_get_contents('https://itunes.apple.com/lookup?id='.$loop['album_id'].'&entity=song');
$response_ab = json_decode($data_b);
print '<pre>';
print_r($response_ab);
print '</pre>';
The above portion for print_r returns the following data.
[results] => Array
(
[0] => stdClass Object
(
[wrapperType] => collection
)
[1] => stdClass Object
(
[wrapperType] => track
)
Then continues onward down the track list, how can I remove the first [0] option from my loop? It appears this only returns the album name anyway when I am trying to get the tracklist.
I didn't test the below code however it should work.
$data_b = file_get_contents('https://itunes.apple.com/lookup?id='.$loop['album_id'].'&entity=song');
$response_ab = json_decode($data_b);
$count = $response_ab->results;
$arr = count($count);
for($a = 1; $a < $arr; $a++) {
// do some cool stuff here
}
I'm trying to compare a string with an array key and if they match, I want to call a function to replace the value of the matched key.
my array:
Array ( [id] => 1 [username] => Ahmed [name] => Ahmed Jalal [avatar] => no_avatar.png )
my function:
resizer($avatar,'50');
and I'm using this cod to do the job
$query = $db->prepare($mysql);
$db->stmt_assoc($query, $table);
$query->fetch();
if ($table['avatar']) {
$data[] = call_user_func('resizer', $table['avatar'],'50');
$table['avatar']=$data;
}
if($type == 'json'){
return json_encode($data);
}else{
return $table;
}
Ok sorry for my misunderstanding so here is some code based off the array you gave. It will turn each key into the name of a variable and store the value of that key in the named variable.
$g['id'] = 1;
$g['username'] = 'Ahmed';
$g['name'] = 'Ahmed Jalal';
$g['avatar'] = 'no_avatar.png';
foreach($g as $k => $v)
{
$$k = $v;
}
echo $avatar; // displays no_avatar.png
Yes you can use resize if you include the path to the image in avatar or in a new array key while using gd or imagemagick or another gfx addition to php.
Hi I was wondering if it is possible to grab:
Here is my current code:
<?php
include('php-riot-api.php');
$region = 'euw';
$grab_id = 19631093;
$instance = new riotapi($region);
$grab_dataB = $instance->getStats($grab_id);
$decode_dataB = json_decode($grab_dataB);
$grab_tier = $decode_data->{'aggregatedStats[7].Unranked[wins]'};
print_r($decode_dataB);
?>
This is my result:
http://2v2.lolnode.com/testing.php (http://pastebin.com/DrJDnuaC)
I would like to be able to get the numbered result for Unranked[wins] (which is [aggregatedStats] => stdClass Object ( ) ) [7])
You can get directly:
$grab_tier = $decode_dataB->playerStatSummaries[7]->wins;
But you won't know for sure that the 8'th key is the one you want, or ...
You can loop in your results:
$unranked = '';
foreach($decode_dataB->playerStatSummaries as $summary){
if($summary->playerStatSummaryType == 'Unranked'){
$unranked = $summary;
break;
}
}
print_r($unranked);
and get values, eg $unranked->wins
you can json_decode with 2'nd parameter true (object is transformed into array) and loop like a normal array.
$decode_dataB = json_decode($grab_dataB, true);
I was hoping someone might be able to help me out or point me in the right direction. I've spent a week trying to figure out how to access and update the weight field in WP ecommerce and Ive had no success at accessing it and updating new values.
The script I'm using access a website to scrape comic book info. It then parses the data from the xml file and assigns variables for the second is for values that are specific to WP Ecommerce.
The whole script works beautifully and creates product pages for each comic book and all data is added into each product. Except for one! and I've been stuck for a week trying to figure out how to access the meta and update it. That's the _wpsc_product_metadata array for weight and weight_unit and other meta values that are stored within.
A comic weighs roughly 3 ounces and I've been trying to assign this value to each card as it's created "3" and "ounce" - I've been unsuccessful.
I've tried probably over so many different ways from digging into the ecommerce code to looking on the net, reading about multidimensional arrays, serialized arrays and it isn't clicking in my noggin'. Keep in mind I've only been programming in PHP for the past month, and prior to a month ago I knew nothing about code other than when I dabbled in C and C++ 15 years ago. Back then, it was mainly copying existing code and changing the outputs in MUDS (multi-user dungeons) - back when online gaming was text-based, lol.
I assumed I could access WP Ecommerce meta by using:
get_post_meta($post_id, '_wpsc_product_metadata', true); That did not work.
Other methods of accomplishing this I tried:
get_post_meta(get_the_id($post_id), '_wpsc_product_metadata', true); // no success
I tried specific product ids using it like so:
get_the_id(300), or just '300', i've used single quotes double quotes no qoutes, "{}", etc etc etc etc
Now when I've used:
get_post_meta($post_id, ''); //while in the loop
The values for SKU and price are outputted but not what's within _wpsc_product_metadata
Then when i use this:
$product_data['meta'] = array();
$product_data['meta'] = maybe_unserialize(get_post_meta('$post_id', '' ));
This is what I get when I print_r
Array ( [_wpsc_price] => Array ( [0] => Array ( [0] => 19.99 ) )
[_wpsc_sku] => Array ( [0] => Array ( [0] => 978-0-7851-5209-5 ) ) )
So I tried one more thing I went into a specific product and i manually set weight to 5 and I changed the code as follows:
$product_data['meta'] = array();
$product_data['meta'] = maybe_unserialize(get_post_meta( '44317', '' ));
This is the output:
Array ( [_wpsc_stock] => Array ( [0] => Array ( [0] => ) )
[_wpsc_product_metadata] => Array ( [0] => Array ( [0] =>
a:19:{s:25:"wpec_taxes_taxable_amount";s:0:"";s:13:"external_link";s:0:
"";s:18:"external_link_text";s:0:"";s:20:"external_link_target";s:0:"";s:6:
"weight";s:3:"0.3";s:11:"weight_unit";s:5:"pound";s:10:"dimensions";a:6:
{s:6:"height";s:1:"0";s:11:"height_unit";s:2:"in";s:5:"width";s:2:"0
";s:10:"width_unit";s:2:"in";s:6:"length";s:1:"0";s:11:"length_unit";
s:2:"in";}s:8:"shipping";a:{s:5:"local";s:1:"0";s:13:"international";s:1:"0";}
s:14:"merchant_notes";s:0:"";s:8:"engraved";s:1:"0";s:23:
"can_have_uploaded_image";s:1:"0";s:15:"enable_comments";s:0:"";
s:24:"unpublish_when_none_left";s:1:"0";s:11:"no_shipping";s:1:"0";s:16:
"quantity_limited";s:1:"0";s:7:"special";s:1:"0";s:17:"display_weight_as";s:5:
"pound";s:16:"table_rate_price";a:2:{s:8:"quantity";a:0:{}s:11:"table_price";a:0:
{}}s:17:"google_prohibited";s:1:"0";} ) ) [_wpsc_special_price] => Array ( [0] =>
Array ( [0] => 0 ) ) [_edit_last] => Array ( [0] => Array ( [0] => 1 ) )
[_edit_lock] => Array ( [0] => Array ( [0] => 1333358836:1 ) ) [_wpsc_sku] =>
Array ( [0] => Array ( [0] => 978-0-7851-6421-0 ) ) [_wpsc_price] => Array
( [0] => Array ( [0] => 24.99 ) ) [_wpsc_is_donation] => Array ( [0] => Array (
[0] => 0 ) ) [_wpsc_currency] => Array ( [0] => Array ( [0] => a:0:{} ) ) )
I was hoping, no I'm begging that someone might be able to enlighten me and show me how to access and update this data that is stored in _wpsc_product_metadata specifically weight and weight_unit and it would be even better if you do know how, you could show me an example of updating a newly created posts weight to a value and changing the default weight_unit from pounds to ounces.
Here is the code for reference:
<?php
function scraping_comic()
{
$html = file_get_html('http://site-to-strip.com/');
$matches = str_replace (' ', ' ', $article);
foreach($html->find('li.browse_result') as $article)
{
// get comic title
$item['title'] = trim($article->find('h4', 0)->find('span',0)->outertext);
// get comic title url
$item['title_url'] = trim($article->find('h4', 0)->find('a.grid-hidden',0)->href);
// get comic image
$item['image_url'] = trim($article->find('img.main_thumb',0)->src);
// get comic excerpt
$item['excerpt'] = trim($article->find('p.browse_result_description_release', 0)->plaintext);
// get comic sales info
$item['on_sale'] = trim($article->find('.browse_comics_release_dates', 0)->plaintext);
// strip numbers and punctuations
$item['title2'] = trim(preg_replace("/[^A-Za-z-\t\n\s]/","",$article->find('h4',0)->find('span',0)->plaintext));
$item['title3'] = trim(preg_replace("/[^A-Za-z]/","",$article->find('h4',0)->find('span',0)->plaintext));
$ret[] = $item;
}
$html->clear();
unset($html);
return $ret;
}
$ret = scraping_comic();
if ( ! empty($ret))
{
foreach($ret as $v)
{
//download the image
$url = $v['image_url'];
$title = $v['title3'];
$now = time();
$num = date("w");
if ($num == 0)
{ $sub = 6; }
else { $sub = ($num-1); }
$WeekMon = mktime(0, 0, 0, date("m", $now) , date("d", $now)-$sub, date("Y", $now));
$todayh = getdate($WeekMon);
$d = $todayh[mday];
$m = $todayh[mon];
$y = $todayh[year];
$date_stamp = $d.$m.$y;
//scrape inside pages
$scrape = 'http://domain.com';
$comic_details = $v['title_url'];
$comic_details_url = $scrape.$comic_details;
$url2 = file_get_html($comic_details_url);
foreach($url2->find('.comics_detail_lead_left_panel_content') as $the_details);
$matches = str_replace (' ', ' ', $the_details);
{
$item2['image2_url'] = trim($the_details->find('img.frame-img',0)->src);
$item2['full_desc'] = trim($the_details->find('p',0)->plaintext);
$item2['data'] = trim($the_details->find('dl',0)->plaintext);
}
$url2->clear();
unset($url2);
//download medium-sized image
$root2 = ('/home/****/public_html/wp-content/blogs.dir/14/files/comics/' .$title.$date_stamp. '-medium.jpg');
$image2 = $item2['image2_url'];
copy($image2, $root2);
unset($root2);
unset($image2);
//match specific data and assign variables
$string = $item2['data'];
$number = preg_match("/(Comic|Hardcover|Paperback)[^A-Za-z]+/", $string, $fields);
switch ($fields[1])
{
case ('Comic'):
$cat = array(51, 52);
break;
case ('Hardcover'):
$cat = array(85, 52);
break;
case ('Paperback'):
$cat = array(95, 52);
break;
default: "";
}
$number = preg_match("/((January)|(February)|(March)|(April)|(May)|(June)|(July)|(August)|(September)|(October)|(November)|(December))[^A-Za-z0-9,]+[A-Za-z0-9,\s]+/", $string, $fields);
$date = $fields[0];
$number = preg_match("/((UPC)|(ISBN))[^0-9-]+([0-9-]+)/", $string, $fields);
$upc = $fields[4];
$number = preg_match("/((Price))[^0-9.]+([0-9.\s]+)/", $string, $fields);
$price = $fields[3];
$full_desc = $item2['full_desc'];
$maintitle = $v['title'];
$excerpt = $v['excerpt'];
$comic_post = array();
$comic_post['post_title'] = wp_strip_all_tags($maintitle);
$comic_post['post_content'] = wp_strip_all_tags($full_desc);
$comic_post['post_status'] = 'publish';
$comic_post['post_author'] = 1;
$comic_post['post_type'] = 'wpsc-product';
$comic_post['post_category'] = $cat;
$comic_post['comment_status'] = 'closed';
$comic_post['ping_status'] = 'closed';
$comic_post['post_excerpt'] = wp_strip_all_tags($excerpt);
// create comic book
$post_id = wp_insert_post( $comic_post );
// category insertion does not work fixed this by calling wp_set_post_terms
wp_set_post_terms($post_id, $cat, 'wpsc_product_category' );
$wpsc_custom = update_post_meta;
$wpsc_custom($post_id, '_wpsc_price', $price);
$wpsc_custom($post_id, '_wpsc_sku', $upc);
//Gain access to WP Ecommerce meta data and assign new values to weight ***IN PROGRESS***
$product_data['meta'] = array();
$product_data['meta'] = maybe_unserialize(get_post_meta( $post_id, '' ));
//***TESTING***//
echo '<br /> the data <br />';
print_r($product_data['meta']);
//Set featured image for product
$filename = ('comics/' .$title .$date_stamp. '-medium.jpg');
update_post_meta( $post_ID, 'image_thumbnail', $imgloc );
set_featured_image($post_id, $filename);
}
}
else { echo 'Could not scrape site!'; }
?>
With help from a WP E-commerce contributor here's the missing puzzle piece to update weight.
if(!isset($product_data['meta']) || !is_array($product_data['meta'])) {
$product_data['meta'] = array();
}
if(!isset($product_data['meta']['_wpsc_product_metadata'])) {
$product_data['meta']['_wpsc_product_metadata'] =
maybe_unserialize(get_post_meta($post_id, '_wpsc_product_metadata', true));
}
if(!is_array($product_data['meta']['_wpsc_product_metadata'])) {
$product_data['meta']['_wpsc_product_metadata'] = array();
}
$product_data['meta']['_wpsc_product_metadata']['weight_unit'] = 'ounce';
$product_data['meta']['_wpsc_product_metadata']['weight'] = 0.19;
update_post_meta($post_id, '_wpsc_product_metadata', $product_data['meta']['_wpsc_product_metadata']);
for someone that has not programmed for so long and has only recently looked into PHP you do seem to be doing quite well.
Regarding the array. _wpsc_product_metadata appears to be JSON. You should look towards the PHP manual particularly the section related to JSON as linked below.
http://php.net/manual/en/book.json.php
If you use json_decode you will be able to retrieve an object that has the attributes that you require as properties for the object.
I have a label on a form select and I get that value with $ _POST like this:
$gallery = array($_POST['gallery']);
and that value will put it here:
$image = $sitemap->gallery[$gallery]->addChild('image');
the problem is giving me error is as follows:
Fatal error: Call to a member function addChild() on a non-object in
I do not understand is that if I put a value directly asin me do it like so:
$gallery = 0;
$Image = $sitemap->gallery[$gallery]->addChild('image');
I do well, what happens is that I want the user to choose,
Kind of strange as it may fix.
Understanding Arrays:
$gallery = array($_POST['gallery']);
echo "Gallery Array: <pre>".print_r($gallery,true)."</pre><br />";
Output:
Array
(
[0] => 'value in array'
)
How to get the value out of an array:
echo "Get Array Value: ".$gallery[0]."<br />"; // You should be displaying the array index 0
Adding a custom index
$gallery = array('gallery' => $_POST['gallery']);
echo "Gallery Array: <pre>".print_r($gallery,true)."</pre><br />";
Output:
Array
(
[gallery] => 'value in array'
)
Getting the value from the customer index array
echo "Get Array Value: ".$gallery['gallery']."<br />"; // You should be displaying the array index gallery
use this:
$gallery = $_POST['gallery'];
instead of this:
$gallery = array($_POST['gallery']);
You passing an array that you did not index properly
Or you can try it this way:
$gallery = array('gallery' => $_POST ['gallery']);
or
$image = $sitemap->gallery[$gallery[0]]->addChild('image');
either way should fix the problem
thank you very much, but I tested this and is giving me the same error
$gallery=array('gallery'=>$_POST['galeria']);
$image = $sitemap->gallery[$gallery]->addChild('image');
I do not understand the select form that I have is as follows:
<select id="textfield" name="galeria">
<option id="textfield" value="">Escoger de la Lista</option>
<?php
$source = 'content.xml';
// load as string
$xmlstr = file_get_contents($source);
$sitemap = new SimpleXMLElement($xmlstr);
// load as file
$sitemap = new SimpleXMLElement($source,null,true);
//$bar_count = $sitemap->gallery->count();
//for($i=0;$i<$bar_count;$i++){
$contador="0";
foreach($sitemap->gallery as $content) {
$atributo = $content->attributes();
echo "<option id='textfield' value='".$contador."'>".$atributo['Name']. "</option>";
//}
$contador++;
}
?>
</select>
anyway I've tried has done an echo and I get the result, do not really understand