Adding php variables together - php

I am new here and a new developer, hope this hasn't been asked before and I will explain as best I can.
I'm developing a website for a clan that I play a game with (so for fun and practice) and the game has items which you can buy or sell.
These 'items' have their prices live on the website, and I would like to take the prices of 2 items and add them together to leave you with a price.
So, I am using wordpress with a plugin, tweaked the code and its dumps the variables in $data and $data2. I can echo these out and I get the numbers live on my website, so that part of the code is working.
I've tried things like :-
$result = $data + $data2;
echo $result;
Within wordpress it removes the +
Coding it with a template gives 0
Here is the address i am working on http://starhelpers.x10host.com/ge-test-page/
If you need more information do ask :)
Any help would be appreciated and thanks in advance.
Hopefully this makes it clearer.
I am using a wordpress function which web scrapes content. The function is called wpws_get_content, you give it the URL and the CSS class you want to take and you can echo the result on the page.
<?php
//so this grabs the first number
$data = wpws_get_content("http://services.runescape.com/m=itemdb_rs/Vial_of_water/viewitem.ws?obj=227", "div.stats h3 span");
//this gets the secound number
$data2 = wpws_get_content("http://services.runescape.com/m=itemdb_rs/Clean_avantoe/viewitem.ws?obj=261", "div.stats h3 span");
$result = $data + $data2;
echo $result;
//to test the variables are working (which are the numbers I would like to add)
echo $data;
echo $data2;
?>
So this code right now gives :-
$result as 0
$data as 43 (on the page)
$data2 as 4,489 (on the page)
I'd like result to say 43 + 4489 = 4922
so $result would echo 4922.
Hopefully this makes it more clearer.

yes you are right.
<?php
//Define and set your vars
$data = 10;
$data2 = 20;
//Sum
$result = $data + $data2;
//Concat
$result = $data . $data2;
//Show result
echo 'The result is: ' . $result;
?>

Related

Get info from API/URL

I have the URL https://android.rediptv2.com/ch.php?usercode=5266113827&pid=1&mac=02:00:00:00:00:00&sn=&customer=GOOGLE&lang=eng&cs=amlogic&check=3177926680
which outputs statistics.
For example:
[{"id":"2972","name":"MBC 1","link":"http://46.105.112.116/?watch=TR/mbc1-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC1En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc1.png"},{"id":"1858","name":"MBC 2","link":"http://46.105.112.116/?watch=TN/mbc2-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC2En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc2.png"},{"id":"1859","name":"MBC 3","link":"http://46.105.112.116/?watch=TN/mbc3-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc3.png"}]
I want to get the value of link count.
Can anyone help?
I tried to do:
<?php
$content = file_get_contents("https://android.rediptv2.com/ch.php?usercode=5266113827&pid=1&mac=02:00:00:00:00:00&sn=&customer=GOOGLE&lang=eng&cs=amlogic&check=3177926680");
$result = json_decode($content);
print_r( $result->link );
?>
But it didn't work.
Put the JSON in an editor and you'll see that it's an array and not an object with the link attribute. This is why you cannot access it directly. You have to loop over the items and then you'll be able to access the link property of one of the items. If you need to access the link by id, as you asked 4 months later, then just create a dictionnary in an array indexed by id and containing just the interesting data you need.
PHP code:
<?php
// The result of the request:
$content = <<<END_OF_STRING
[{"id":"2972","name":"MBC 1","link":"http://46.105.112.116/?watch=TR/mbc1-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC1En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc1.png"},{"id":"1858","name":"MBC 2","link":"http://46.105.112.116/?watch=TN/mbc2-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC2En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc2.png"},{"id":"1859","name":"MBC 3","link":"http://46.105.112.116/?watch=TN/mbc3-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc3.png"}]
END_OF_STRING;
$items = json_decode($content);
echo '$items = ' . var_export($items, true) . "\n\n";
// Create a dictionnary to store each link accessible by id.
$links_by_id = [];
// Loop over all items:
foreach ($items as $i => $item) {
// Show how to access the current link.
echo "Link $i = $item->link\n";
// Fill the dictionary.
$links_by_id[(int)$item->id] = $item->link;
}
// To access the first one:
echo "\nFirst link = " . $items[0]->link . "\n";
// Example of access by id:
// The id seems to be a string. It could probably be "1895" or "zhb34" or whatever.
// (If they are only numbers, we could convert the string to an integer).
$id = "1859";
echo "\nAccess with id $id = " . $links_by_id[$id] . "\n";
Test it here: https://onlinephp.io/c/e8ab9
Another important point: You are getting a 403 Forbidden error on the URL you provided. So typically, you will not obtain the JSON you wanted.
As I explained in the comment below, I think that you will not be able to access this page without having a fresh URL with valid query parameters and/or cookies. I imagine you obtained this URL from somewhere and it is no longer valid. This is why you'll probably need to use cURL to visit the website with a session to obtain the fresh URL to the JSON API. Use Google to find some examples of PHP scraping/crawling with session handling. You'll see that depending on the website it can get rather tricky, especially if some JavaScript comes into the game.

PHP JSON Parsing Issue

I have this JSON output from a Government API, I need to display it using PHP. The problem is I can't use foreach more then once in a row or it doesn't work. I can't load all the criteria into the first foreach because say the first piece of data ACASS returns 3 results, all the fields after it will be displayed 3 times. Each field could return 1-10 results so there needs to be a system that accounts for variables.
I'm thinking the solution is to put all of the JSON items I need displayed into the first foreach but set them to only display if they're populated. That or use the current coding system I have but account for variable numbers somehow.
Any potential solutions are greatly appreciated.
This is the JSON output... https://api.data.gov/sam/v4/registrations/9606040070000?api_key=WI7nHENlp6QDMnWsb0Nnmzsv1slPDTjNM0XBoKvY
Here's the PHP I'm using...
echo "ACASS ID:".$decoded_results['sam_data']['registration']['qualifications']['acass']['id']."</br>";
foreach($decoded_results['sam_data']['registration']['qualifications']['acass']['answers'] as $acass)
{
echo 'Answer Text:'.$acass['answerText'].'</br>';
echo 'ACASS Section:'.$acass['section'].'</br>';
}
$formerfirm = $decoded_results['sam_data']['registration']['qualifications']['acass']['answers'][2]['FormerFirm'];
echo 'Former Firm ID:'.$formerfirm['id'].'</br>';
echo 'Former Firm Year Established:'.$formerfirm['yearEstablished'].'</br>';
echo 'Former Firm Name:'.$formerfirm['name'].'</br>';
echo 'Former Firm DUNS'.$formerfirm['duns'].'</br>';
I did my best to keep this short and simple question / code wise. In summary the issue is if you look at the JSON the data hierarchy makes a lot of the information display under ACASS/Answers and then the next category. I never know how many responses there will be and I'm not sure how to account for those variables.
I would like to thank everyone on these boards who has guided me as a new member and helped me post cleaner, more concise questions. Also thank you to everyone who has taken their own personal time to help me learn to become a better programmer.
use a tool like http://jsonviewer.stack.hu/ for visualizing your json structure. It helps a lot.
<?php
$url = "https://api.data.gov/sam/v4/registrations/9606040070000?api_key=WI7nHENlp6QDMnWsb0Nnmzsv1slPDTjNM0XBoKvY";
$contents = json_decode(file_get_contents($url));
// echo var_dump($contents);
$sam_data = $contents->sam_data;
// echo var_dump($sam_data);
$registration = $sam_data->registration;
//echo var_dump($registration);
$acass = $contents->sam_data->registration->qualifications->acass;
$id = $acass->id;
echo "id: ". $id . "<br />";
//echo var_dump($acass->answers);
foreach($acass->answers as $answer) {
if(isset($answer->FormerFirm)) {
$formerFirm = $answer->FormerFirm;
echo var_dump($formerFirm);
}
}

Issue Scraping with PHP

I am trying to attain a value, but step over other values that change dynamically.
The table section looks as follows:
Total 1.18 3.33 $20,000 16.2%
The code I am using to find the third value in preg_match is:
<?php
function get_total(){
$file_string = file_get_contents('url');
preg_match('#Total</td><td>\d\.\d+</td><td>d\.\d+</td><td>$(\d+)</td><td>d+\.\d\%\</td></tr></table><br><span id="ExStockDetailTableF1F2"#',$file_string, $data);
$loss = $data[1];
return $loss;
}
?>
Wouldn't it be easier just search for \$[\d\,]+ since represent third group?

String from an array from an array from the database

Okay so, first of all, I searched through the www for this question, and I found some question related to arrays but not exactly to mine.
Okay so as you may know, paypal only allows one custom variable to be $POST but I need to gather the product id AND the quantity of the item bought. So to do this I made my custom variable into something that would get the post like (25-1,12-3,13-4) it means, the user bought 3 items(separated by commas), where the first number is the product id (then the separator '-' comes in) and the second one is the quantity. (so they're all in the same row and column)
Now my problem is displaying it from the database. I need to get the product name and details that's why I need to separate the numbers from each array as a string and fetch the data from the database for the information of the product. (I'm using an older version of php anyway, 5.2, I guess.)Now the problem is:
1.) It returns the word 'Array' (literally) so it would say like ArrayArrayArray
2.) How do I explode/separate those data so I can get it because I need the product ID to fetch some other data... I tried exploding it into array, then exploding it again but doesn't work (most likely my code is wrong?)
Here is my code: (I've already connected to the database)
$data = mysql_query("SELECT * from transactions") or die(mysql_error());
/* My table tag and headers goes here */
while($info = mysql_fetch_array( $data )) {
echo "<tr>";
echo '<td>' . $info['id'] . '</td>';
echo "<td>";
$array = $info['product_id_array'];
$explode_array = explode(",", $array);
foreach($explode_array as $explode_more){
$explode_more = explode("-", $explode_array);
$prod_id = $explode_more[0];
$quantity = $explode_more[1];
print_r($prod_id); //should echo the 25 in the array (25-1), right?
print_r($quantity);
}
echo"</td>";
echo"<tr>";
}
If only paypal would allow multiple custom variables T_T Thank you guys. Forgive me if I can't express my question very well or my language is not good, as english is not my first language :), Good day!
Your variable names are mixed up. Inside the foreach-loop, you should do something like this
foreach($explode_array as $explode_more){
$explode_even_more = explode("-", $explode_more);
$prod_id = $explode_even_more[0];
$quantity = $explode_even_more[1];
print_r($prod_id); //should echo the 25 in the array (25-1), right?
print_r($quantity);
}
Note, that $explode_more is used inside the loop and $explore_array is left as is.
Separate this in multiple tables, never store non-atomic values in 1 column.
Certainly not when they have relation with another table.
Suppose you want to know the sales from a certain product in some period.

How to randomly select a video and play?

Im learning JQuery and php.
Is it possible to store multiple video links within variables and get php to echo one at random??
My goal is to control my own video ads on my site and I thought this would be a good idea but I dont have a clue where I should look online.
Here is what I was thinking
<?php
$advert1 = 'MyVIDEO1.mp4';
$advert2 = 'MyVideo2.mp4';
$advert3 = 'MyVideo3.mp4';
I want a code that would go here and say: randomly select one of these vars.
echo "At random one of the vars";
?>
I hope Im making sense. help?
You could make an array, like this:
$advert[] = array();
$advert[1] = 'MyVIDEO1.mp4';
$advert[2] = 'MyVIDEO2.mp4';
$advert[3] = 'MyVIDEO3.mp4';
$chosen_one = rand(1,count($advert));
echo $advert[$chosen_one];
You can also use array_rand() instead of shuffle():
<?php
$videos = array("MyVIDEO1.mp4", "MyVideo2.mp4", "MyVideo3.mp4");
echo $videos[array_rand($videos)];
?>
For embedding the video in the right way, have a look at http://www.w3schools.com/html/html_videos.asp
For a start, PHP itself won't exactly play a video. You could use it to echo out one of the URL's to a video. So, bear in mind there is a lot more to do than just select a video.
To answer your question in the code I'd recommend the following:
Try looking up PHP arrays. You want to keep all the videos in an array.
Next up, you'll want to shuffle that array. Then select the first element.
$videos = array("MyVIDEO1.mp4", "MyVideo2.mp4", "MyVideo3.mp4");
shuffle($videos);
echo $videos[0];
This is a simple solution
Place your adverts in an array
<?php
$adverts = array("advert1" => "MyVIDEO1.mp4",
"advert2" => "MyVIDEO2.mp4",
"advert3" => "MyVIDEO3.mp4");
$count = count($adverts);
$rand_advert = rand(1, $count);
echo $adverts['advert'.$rand_advert];
?>
Or alternatively if you don't want to have keys and just put the values in the array straight away
<?php
$adverts = array("MyVIDEO1.mp4",
"MyVIDEO2.mp4",
"MyVIDEO3.mp4");
shuffle($adverts);
echo $adverts[0];
?>
You have need all value in a array then find index randomly and show it.
<?php
$advert = array(
'MyVIDEO1.mp4',
'MyVideo2.mp4',
'MyVideo3.mp4'
);
$total_video = count($advert);
$total_video--; //array index starting from 0 so decrease 1
$random_index = rand(0, $total_video); //array index 0 to 2
$video_to_play = $advert[$random_index];
echo $video_to_play;
?>

Categories