I have a php file that is called when a user clicks a link on a web page. It requires weekly manual editing of several variables. If I could define the variable values from an array it would be much easier to edit.
Currently the code is laid out thus:
$event1Name = "Name of first event";
$event1Date = "Date of first event";
$event1Location = "Location of first event";
...and so on for each event.
Is it possible to assign variable names and values from an array?
<?php
$num=0;
$Name="Name";
$Date="Date";
$Address="Location";
$array = [
["Poker # Bob's", mktime(0,0,0,4,17,2021), "1450 That Street"],
["Poker # The Legion", mktime(0,0,0,4,23,2021), "104 This Street"],
["Poker # Petey's House", mktime(0,0,0,4,24,2021), "1776 America's Avenue"],
["Poker # The Shriner's Club", mktime(0,0,0,5,8,2021), "33 Over There Blvd."],
];
foreach ($array as [$loc1, $time1, $address1]) {
++$num;
$event.=$num.=$Name = $loc1;
$event.=$num.=$Date = $time1;
$event.=$num.=$Location = $address1;
}
?>
SOLVED
Many thanks to El_Vanja for pointing me in the right direction. I knew nothing of variable variables.
<?php
$num=0;
$Name="Name";
$Date="Date";
$Address="Location";
$array = [
["Poker # Bob's", mktime(0,0,0,4,17,2021), "1450 That Street"],
["Poker # The Legion", mktime(0,0,0,4,23,2021), "104 This Street"],
["Poker # Petey's House", mktime(0,0,0,4,24,2021), "1776 America's Avenue"],
["Poker # The Shriner's Club", mktime(0,0,0,5,8,2021), "33 Over There Blvd."],
];
foreach ($array as [$loc1, $time1, $address1]) {
++$num;
$var = "event" . $num . $Date;
$$var = $time1;
$var = "event" . $num . $Name;
$$var = $loc1;
$var = "event" . $num . $Address;
$$var = $address1;
}
echo $event1Name . " on " . date("F d, Y",$event1Date) . " at " . $event1Location . "<br>";
echo $event2Name . " on " . date("F d, Y",$event2Date) . " at " . $event2Location . "<br>";
echo $event3Name . " on " . date("F d, Y",$event3Date) . " at " . $event3Location . "<br>";
echo $event4Name . " on " . date("F d, Y",$event4Date) . " at " . $event4Location;
?>
I'm using the Binance REST API to get previous trade information:
Endpoint: https://api.binance.com/api/v1/trades?symbol=BTCUSDT
$btc_trades = file_get_contents('https://api.binance.com/api/v1/trades?symbol=BTCUSDT');
$btc_trades = json_decode($btc_trades, true);
$five_minutes_ago = strtotime('-5 minutes');
echo "five minutes ago: " . $five_minutes_ago . "<br><br>";
foreach ($btc_trades as $btc_trade) {
$btc_trade_time = strtotime($btc_trade['time']);
$btc_trade_total = $btc_trade['qty'] * $btc_trade['price'];
if ($btc_trade_time >= $five_minutes_ago) {
$btc_trade_time = $btc_trade['time'];
echo "Time: " . $btc_trade_time . "<br>";
echo "Qty: " . $btc_trade['qty'] . "<br>";
echo "Price: $" . $btc_trade['price'] . "<br>";
echo "Total: " . $btc_trade_total . "<br><br>";
}
}
I can confirm by testing that there are trades returned that meet the condition (happened less than five minutes ago) however nothing is returned in this case.
Are the timestamps formatted differently? It seems as if I have everything correct.
From Binance API (https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md):
All time and timestamp related fields are in milliseconds.
Thanks for the help!
change to
$btc_trade_time = $btc_trade['time']/1000
I need to add "Uhr" at the end of the following Code:
<?
$doc = JFactory::getDocument();
$doc->setTitle($event->title . " | Example");
$doc->setDescription($event->title . " am " . $event->start, 'end' -> $event->end "uhr needs to be here");
?>
how can I archive this? Because if I add a . "Uhr" it doesn't work :(
$doc->setDescription($event->title . " am " . $event->start . ", " . $event->end . " Uhr");
should do it.
Formatted dates:
$doc->setDescription($event->title . " am " . date('d.m.Y', strtotime($event->start)) . ", " . date('H:i', strtotime($event->end)) . " Uhr");
I think there is something wrong with the last ->.
Maybe this works:
$doc->setDescription($event->title . " am " . $event->start, 'end' . $event->end . "uhr");
In case "am", "end" and "urh needs to be here", best you use the same quotes like:
<?
$doc = JFactory::getDocument();
$doc->setTitle($event->title . " | Tanzschule Hartung");
$doc->setDescription($event->title . " | Tanzschule Hartung" " am " . $event->start, "end" -> $event->end . "uhr needs to be here");
?>
(look at your text 'end', compared to the code above)
It looks like $event->start (and others) need to display some text which is requested.
Why you need to use a . (dot) after requesting text from another function is because PHP needs to understand when it has to display text or not.
The . is the string concatenation operator.
This might work as well:
<?
$doc = JFactory::getDocument();
// Setting titles
$title = $doc->setTitle($event->title);
$start = $event->start;
$end = $event->end;
$doc->setDescription($title . " | Tanzschule Hartung am " . $start . " , end" . $end . "uhr needs to be here");
?>
I hope this clears something up.
If you have any questions, feel free to ask.
I just made a phone number to time zone converter and it displays result as:
Requested Phone No.: +1 732 78782722
Country: United States
Expected Region: Newark, New Brunswick
Timezone: America/New_York
Date: 2015-08-05
Time: 01:51:03 am
What I want to do is place all these outputs in a single line.
Here's my output code
if(!empty($record['country_name'])) {
$this->display('<strong>Country:</strong> ' . $record['country_name']);
}
if(!empty($record['city'])) {
$this->display('<strong>Expected Region:</strong> ' . $record['city']);
}
//echo json_encode($date);
if(!empty($record['zone_name'])) {
$this->display('<strong>Timezone:</strong> ' . $record['zone_name']);
$this->display('<h2><strong>Date:</strong> ' . date('Y-m-d') . '</h2>');
$this->display('<h2><strong>Time:</strong> ' . date('H:i:s a') . '</h2>');
}
Thanks for the help.
Try this: If you want to pass that in a variable
if(!((empty($record['country_name']) && empty($record['city']) && empty($record['zone_name'])) {
$var = '<strong>Country:</strong> ' . $record['country_name'] . '<strong>Expected Region:</strong> ' . $record['city'] . '<strong>Timezone:</strong> ' . $record['zone_name']. '<h2><strong>Date:</strong> ' . date('Y-m-d') . '</h2>' . '<h2><strong>Time:</strong> ' . date('H:i:s a') . '</h2>';
}
echo $var;
Or this if you want to pass in your object:
if(!((empty($record['country_name']) && empty($record['city']) && empty($record['zone_name'])) {
$this->display('<strong>Country:</strong> ' . $record['country_name'] . '<strong>Expected Region:</strong> ' . $record['city'] . '<strong>Timezone:</strong> ' . $record['zone_name']. '<h2><strong>Date:</strong> ' . date('Y-m-d') . '</h2>' . '<h2><strong>Time:</strong> ' . date('H:i:s a') . '</h2>');
}
return $this;
You need to create a string variable and concatenate all your output to that string like below:-
$result = ''; // create an empty string
if(!empty($record['country_name']) && !empty($record['city']) && !empty($record['zone_name'])){
$result = '<strong>Country:</strong> ' . $record['country_name'].' <strong>Timezone:</strong> '. $record['city'].' <strong>Timezone:</strong> '.$record['zone_name'].' <h2><strong>Date:</strong> '. date('Y-m-d') . '</h2>'.' <h2><strong>Time:</strong> '. date('H:i:s a') . '</h2>';
}
echo $result; // print output
I'm working on json source from wunderground.com. As the example code displayed in the document. I can adjust and work out for some simple format. But I'm stuck with this one. I tried to googled every where but there's no solution.
Here's the sample codes:
<?php
$json_string = file_get_contents("http://api.wunderground.com/api/b8e924a8f008b81e/geolookup/conditions/q/IA/Cedar_Rapids.json");
$parsed_json = json_decode($json_string);
$location = $parsed_json->{'location'}->{'city'};
$temp_f = $parsed_json->{'current_observation'}->{'temp_f'};
echo "Current temperature in ${location} is: ${temp_f}\n";
?>
Well, I need some information like "Cedar Rapids" out of pws/station :
"pws": {
"station": [
{
"neighborhood":"Ellis Park Time Check",
"city":"Cedar Rapids",
"state":"IA",
"country":"US",
"id":"KIACEDAR22",
"lat":41.981174,
"lon":-91.682632,
"distance_km":2,
"distance_mi":1
}
]
}
(You can get all code by clicking this : http://api.wunderground.com/api/b8e924a8f008b81e/geolookup/conditions/q/IA/Cedar_Rapids.json )
Now the questions are:
What is this data called? (array, array in array?)
How could I pull this data out of the line?
Regards,
station is an array within the pws object.
To get the data, you can do something like this:
<?php
$json_string = file_get_contents("http://api.wunderground.com/api/b8e924a8f008b81e/geolookup/conditions/q/IA/Cedar_Rapids.json");
$parsed_json = json_decode($json_string);
$location = $parsed_json->{'location'}->{'city'};
$temp_f = $parsed_json->{'current_observation'}->{'temp_f'};
echo "Current temperature in ${location} is: ${temp_f}\n";
$stations = $parsed_json->{'location'}->{'nearby_weather_stations'}->{'pws'}->{'station'};
$count = count($stations);
for($i = 0; $i < $count; $i++)
{
$station = $stations[$i];
if (strcmp($station->{'id'}, "KIACEDAR22") == 0)
{
echo "Neighborhood: " . $station->{'neighborhood'} . "\n";
echo "City: " . $station->{'city'} . "\n";
echo "State: " . $station->{'state'} . "\n";
echo "Latitude: " . $station->{'lat'} . "\n";
echo "Longitude: " . $station->{'lon'} . "\n";
break;
}
}
?>
Output:
Current temperature in Cedar Rapids is: 38.5
Neighborhood: Ellis Park Time Check
City: Cedar Rapids
State: IA
Latitude: 41.981174
Longitude: -91.682632