Why I can't get values from this web service Array? - arrays

Can't figure out why this web service don't work. Just gives me blank. I tested the url and the data it's all there.
http://onleague.stormrise.pt:8031/OnLeagueRest/resources/onleague/News/News?id_user=a7664093-502e-4d2b-bf30-25a2b26d6021&page=1&new_filter=0
my code:
session_start();
function getNews() {
$json = file_get_contents('http://onleague.stormrise.pt:8031/OnLeagueRest/resources/onleague/News/News?id_user=a7664093-502e-4d2b-bf30-25a2b26d6021&page=1&new_filter=0');
$data = json_decode($json, TRUE);
$newst = array();
foreach($data['data']['item'] as $item) {
$newst[] = $item;
}
foreach($newst as $v)
{
$_SESSION['newsid'][] = $v['id'];
$_SESSION['newstitle'][] = $v['title'];
$_SESSION['newstext'][] = $v['news'];
$_SESSION['newslink'][] = $v['link'];
$_SESSION['newsdate'][] = $v['date'];
$_SESSION['newsentityName'][] = $v['entityName'];
$_SESSION['aclikes'][] = $v['account']['likes'] . ")";
$_SESSION['acdislikes'][] = $v['account']['dislikes'] . ")";
$_SESSION['accomentes'][] = $v['account']['commentes'] . ")";
$_SESSION['acshares'][] = $v['account']['shares'] . ")";
$_SESSION['acclicks'][] = $v['account']['clicks'] . ")";
}
}
getNews();
$key = count($_SESSION['newsid']);
for ($i = 0; $i <= $key; $i++) {
echo $_SESSION['newsid'][$i] . "<br />";
}

Are you sure that this line is correct:
foreach($data['data']['item'] as $item) {
$newst[] = $item;
}
Reading this makes me think that you are overwriting the entire $newst array with a single item... for each item. Thus the array will end up being the value of the last $item (which might be empty).
I'd expect something like:
foreach($data['data']['item'] as $item) {
$newst.push( $item );
}
(note, not tested and my syntax may be dodgy... but you get the drift).

Related

Undefined array key 1 in foreach loop

i have this piece of code. On the top i download text data from URL and i get string. This string has some uninportant text so this text is in $info and important number are in $array than i did some reIndex array because for some reason i had $array[0] empty.
I have 2 array: $array and $array_try both arrays shoud have similar structure as u can see here:
For some reason im getting Undefined array key 1 in this line $array_value[] = $parts[1];
When i change in foreach loop $array for $array_try it works well.
Best
$data = file_get_contents("$url");
$data = explode('Kurz', $data);
$kurz = '|Kurz';
$info= substr_replace($data[0], $kurz, -1, );
echo "<br>";
$array = explode ("\n", $data[1]);
//foreach ($array as $value){
// echo $value . "<br>";
//}
unset($array[0]);
$array = array_values($array);
$array_try = [
"04.01.2021|26,140",
"05.01.2021|26,225",
"06.01.2021|26,145"
];
$array_date = [];
$array_value = [];
foreach($array as $value) {
$parts = explode('|', $value);
$array_date[] = $parts[0];
$array_value[] = $parts[1];
}
var_dump($array_try);
echo "<br>";
echo "<br>";
var_dump($array);

How can i get top 15 occurrences in an array and use each value to fetch data from mysql database?

I am currently working on a project (A music website) with PHP Codeigniter. I want to fetch top 15 songs by number of downloads. To my knowledge, I got all unique ID's out ranging from highest to least. But now, I want to use each of these unique ID's to fetch data from another table in the database.
I have tried using for loop; but it seems to only return one of the rows which happens to be the first and highest number of occurrence in the array. The code below echo's the ID's but how can I use each of those ID's to fetch data.
function top15(){
$this->db->select('musiccode');
$result=$this->db->get('downloads');
$query = $result->result_array();
$downloads = array();
if(!empty($query)) {
foreach($query as $row)
{
$musiccode[] = $row['musiccode'];
}
$mode = array_count_values($musiccode);
arsort($mode);
$i = 0;
foreach ($mode as $field => $number) {
$i++;
echo $field." occured ". $number." times <br>";
if ($i == 15) {
break;
}
}
}
}
for ($b=0; $b < count($field); $b++) {
$this->db->where(array('track_musiccode'=> $field));
$field = $this->db->get('tracks');
return $field->result_array();
}
The code above only produce one row for me. I expect to have more than one and according to the number of ID's in the array. Thanks in advance.
For a single column condition, you could use where_in() instead and drop the for loop :
$this->db->where_in('track_musiccode', $field);
$field = $this->db->get('tracks');
return $field->result_array();
Yes Finally i did it. Incase if anyone needs it. this works just fine
function top15(){
$this->db->where('d_month', date('m'));
$this->db->select('musiccode');
$result=$this->db->get('downloads');
$query = $result->result_array();
$downloads = array();
if(!empty($query)) {
foreach($query as $row){
$musiccode[] = $row['musiccode'];
}
$res='';
$count=array_count_values($musiccode);
arsort($count);
$keys=array_keys($count);
for ($i=0; $i < count($keys); $i++) {
$res[] .= "$keys[$i]";
}
$this->db->where_in('track_musiccode', $res);
return $this->db->get('tracks')->result_array();
}
}
And I got this too. If I wasn't going to count occurrences but just iterate the number of downloads which is far more better than counting in other not to over populate the database and make your website work slowly.
function top15(){
$tracks = '';
$this->db->group_by('counter');
$result=$this->db->get('downloads');
$query = $result->result_array();
$counter = '';
$fields = '';
if(!empty($query)) {
foreach($query as $row) {
$counter[] = $row['counter'];
}
rsort($counter);
for ($i=0; $i<=15; $i++) {
$this->db->where('downloads.counter', $counter[$i]);
$this->db->join('tracks', 'tracks.musicid = downloads.musicid');
$fields[] = $this->db->get('downloads')->row_array();
}
return $fields;
}
}
Enjoy...

Laravel - Save multiple field values in json/array in a column

I have 3 form fields name, address, profile_photo. I want to save there 3 fields in json/array format in a single column so that I can retrieve it later in view blade.
My form looks like this
I tried
$customer_details= new Customer;
{
$profile_photo = $request->file('profile_photo');
for ($i=0; $i < count(request('profile_photo')); $i++) {
$pro_fileExt = $profile_photo[$i]->getClientOriginalExtension();
$pro_fileNameToStore = time().'.'.$pro_fileExt;
$pro_pathToStore = public_path('images');
Image::make($profile_photo[$i])->save($pro_pathToStore . DIRECTORY_SEPARATOR. $pro_fileNameToStore);
$profile_ph = '/images/'.$pro_fileNameToStore; }
$cust_details=json_encode(request(['name', 'address', $profile_ph]));
$customer_details->details = $cust_details;
$customer_details->save();
}
With this profile_photo is not saved but other data are saved as:
{"name":["john","Sam"],"address":["CA","FL"]}
How can I save all there fields including profile_photo and show each details distinctly later on view blade?
Hope this will help you.
$customer_details = new Customer;
$profile_photo = $request->file('profile_photo');
$profile_ph = [];
for ($i = 0; $i < count(request('profile_photo')); $i++) {
$pro_fileExt = $profile_photo[$i]->getClientOriginalExtension();
$pro_fileNameToStore = time() . '.' . $pro_fileExt;
$pro_pathToStore = public_path('images');
Image::make($profile_photo[$i])->save($pro_pathToStore . DIRECTORY_SEPARATOR . $pro_fileNameToStore);
$profile_ph[$i] = '/images/' . $pro_fileNameToStore;
}
$data = [
'name' => $request->name,
'address' => $request->address,
'profile_ph' => $profile_ph
];
$customer_details->details = json_encode($data);
$customer_details->save();
The details column should be text if you are using mysql.
Try this:
$names = $request->input('name');
$addresses = $request->input('address');
$profile_photos = $request->file('profile_photo');
for ($i = 0; $i < count($names); $i++) {
$customer_details = new Customer;
$pro_fileExt = $profile_photos[$i]->getClientOriginalExtension();
$pro_fileNameToStore = time() . '.' . $pro_fileExt;
$pro_pathToStore = public_path('images');
Image::make($profile_photos[$i])->save($pro_pathToStore . DIRECTORY_SEPARATOR . $pro_fileNameToStore);
$profile_ph = '/images/' . $pro_fileNameToStore;
$cust_details = json_encode([
'name' => $names[$i],
'address' => $addresses[$i],
'profile_photo' => $profile_ph,
]);
$customer_details->details = $cust_details;
$customer_details->save();
}
Try this with single database query
$profile_photo = $request->file('profile_photo');
$finalData = [];
$names = [];
$addresses = [];
$images = [];
for ($i=0; $i < count(request('profile_photo')); $i++) {
$pro_fileExt = $profile_photo[$i]->getClientOriginalExtension();
$pro_fileNameToStore = time().'.'.$pro_fileExt;
$pro_pathToStore = public_path('images');
Image::make($profile_photo[$i])->save($pro_pathToStore . DIRECTORY_SEPARATOR. $pro_fileNameToStore);
$profile_ph = '/images/'.$pro_fileNameToStore; }
$names[] = $request->input('name');
$addresses[] = $request->input('address');
$images[] = $profile_ph;
}
$cust_details=json_encode(['name' => $names, 'address' => $addresses, 'images' => $images]);
$customer_details->insert($cust_details);

How to get selected value in dropdownlist by using cakephp?

My code is something like below :
public function makeEntryTime($first = '') {
$j = 0;
if (empty($first)) {
$j = 1;
} else {
$entry[$j] = $first;
$j++;
}
for ($i = $j; $i <= 12; $i++) {
$entry[$i . ' am'] = $i . ' am';
}
for ($i = $j; $i <= 12; $i++) {
$entry[$i . ' pm'] = $i . ' pm';
}
return $entry;
}
And here is the drop down list code :
$this->Form->select('ClubOpenDay.0.open_time', $this->makeEntryTime(), array("empty" => false, 'class' => 'input-medium'));
My problem is I am getting the value like 11 am, 12 am.But I want to make it selected when I will get value 11 am or 12 am from database.Any idea how can I do this?
Please try this:
<?php echo $this->Form->select('ClubOpenDay.0.open_time',$this->makeEntryTime(),array("empty" => false,'value' => $value_selected,'class' => 'input-medium')); ?>
"$value_selected" is any value with in the input range.
i.e what $this->makeEntryTime() returns.
Thanks
I assume your method is in view which you shouldn't do this. remove public from your function and remove $this to call your function.
try to keep your method inside a controller at least. Here is if you want to keep your method in view.
function makeEntryTime($first = '') {
$j = 0;
if (empty($first)) {
$j = 1;
} else {
$entry[$j] = $first;
$j++;
}
for ($i = $j; $i <= 12; $i++) {
$entry[$i . ' am'] = $i . ' am';
}
for ($i = $j; $i <= 12; $i++) {
$entry[$i . ' pm'] = $i . ' pm';
}
return $entry;
}
echo $this->Form->select('selete_id', makeEntryTime(), array("empty" => false, 'class' => 'input-medium'));
but if you want to make it MVC.
//controller
function makeEntryTime($first = '') {
$j = 0;
if (empty($first)) {
$j = 1;
} else {
$entry[$j] = $first;
$j++;
}
for ($i = $j; $i <= 12; $i++) {
$entry[$i . ' am'] = $i . ' am';
}
for ($i = $j; $i <= 12; $i++) {
$entry[$i . ' pm'] = $i . ' pm';
}
return $entry;
}
//page function - for examlpe add() method
public function add(){
$this->set('times', $this->makeEntryTime());
}
//view of add method
echo $this->Form->select('time_id', $times, array("empty" => false, 'class' => 'input-medium'));

Wordpress use array key and value to set data attribute

I have setup an array of product values. I need to set each array key to the data-attribute and the value to the data-attribute value
<li data-name="product1" data-price="49.99" data-rating="4.5"></li>
So in Wordpress I have this filter hook
function product_data_sorting( $product_attrs, $product, $terms) {
global $post;
// Product Attributes
$product_attrs['name'] = $post->post_name;
$product_attrs['price'] = $product->price;
$product_attrs['rating'] = $product->get_average_rating();
$product_attrs['newness'] = $post->post_modified;
// Product Categories
foreach ($terms as $term) {
$categories[] = $term->slug;
}
$product_attrs['categories'] = $categories;
foreach ($product_attrs as $attribute) {
if ( is_array($attribute) ) {
//This is a category
}
else {
$results[] = 'data-' . $key . '="' . '$attribute' . '"';
}
}
return $results
}
add_filter( 'add_sorting', 'product_data_sorting', 10, 3 );
I am getting all the correct data pushed in to the $product_attrs array that I need, but not sure how to work with the array key and values so that in the $results array it would be formatted like this:
$results = (data-name="product1", data-price="49.99", data-rating="4.5");
Hope that makes some sense
thanks
You would need key=>value pair within foreach.
$ignored = array ('newness'); // array to maintain what attributes you want to ignore
foreach ($product_attrs as $key => $attribute) {
if ( is_array($attribute) ) {
//This is a category
}
else {
if(!inarray($key, $ignored)
$results[] = 'data-' . $key . '="' . $attribute . '"';
}
}
Also note that $attribute isn't enlcosed in single quotes as you have in the question. Variables inside single quote can not be parsed

Resources