my english may be confusing, i'll try to be specific. It's about PHP, here in stackoverflow i found a piece of code that is so near to give me what i want (an answer of ValkerK), i have two arrays to iterate into one and i found an answer to a question that was exactly what i was lookin for. I just need to create a variable and send the prints to an e-mail. It may be simpliest but i'm not exactly an expert, here is the code if you can help me, thanks for reading this.
$want = new ArrayIterator($_POST['product']);
$amount = new ArrayIterator($_POST['howmany']);
$it = new MultipleIterator;
$it->attachIterator($want);
$it->attachIterator($amount);
foreach($it as $e) {
echo $e[0], ' : ', $e[1], ", ";
}
Then i have this prints
Product1:10, Product2:12, Product3:7.... etc
I need a variable to send that to an e-mail, but i still can't make it it work... thankes for your help.
Use something like this:
$contents = '';
foreach($it as $e) {
$contents .= $e[0] . ' : ' . $e[1] . ", ";
}
Now you can email $contents, which will contain the exact same output. You can also use output buffering, but for such a simple use-case, I wouldn't bother with it.
$var = implode("\n",array_map(function($v){
return $v[0]. ':'. $v[1].",";
}, $it));
Related
I need to be able to store and echo regular expressions. In my case the user enters the regex into a form and that exact sequence of characters needs to be echo-ed to the screen sometime later. The problem is that the echo changes the characters.
So for instance I have tried this
$regex = '(?<=amount\">\$)(.*?)(?=</strong>)';
but when I echo it..
echo $regex;
I get...
((((amount\">\$)(.*?)(?=)
If I do this
$regex = htmlentities($regex);
I get this which helped with the missing part of the regex but not the multiple ((((
((((amount\">\$)(.*?)(?=</strong>
htmlspecialchars did not help either.
How do I get it to echo the variable exactly as it is written? And what would I need to do to store them in MySQL and retrieve them exactly as written?
EDIT - in response to some observations below, I add a bit more detail. This new example was done on a PHP 7.1 server in the cloud, Centos 7 rendered using Chrome.
$regex = '(?<=amount\">\$)(.*?)(?=</strong>)';
$page_elements_regex[1][0] = $regex;
$page_elements_regex[1][1] = addslashes($regex);
$page_elements_regex[1][2] = htmlspecialchars($regex);
$page_elements_regex[1][3] = htmlentities($regex);
echo "regex " . $page_elements_regex[1][0] . "<BR>";
echo "addslashes " . $page_elements_regex[1][1] . "<BR>";
echo "htmlspecialcharacters " . $page_elements_regex[1][2] . "<BR>";
echo "htmlentities " . $page_elements_regex[1][3] . "<BR>";
Results
regex ((((amount\">\$)(.*?)(?=)
addslashes ((((amount\\\">\\$)(.*?)(?=)
htmlspecialcharacters ((((amount\">\$)(.*?)(?=</strong>)
htmlentities ((((amount\">\$)(.*?)(?=</strong>)
It is also a big clue that if you take off the first ( like this
$regex = '?<=amount\">\$)(.*?)(?=</strong>)';
The result removes the first a of amount!! Is it interpreting the regex instead of echoing it?
?(((mount\">\$)(.*?)(?=)
I have solved it, and I feel a bit foolish about the answer. My bad.
Somewhere else in my code I had this
$regex[1] = '(?<=amount\">\$)(.*?)(?=</strong>)';
$regex[2] = '(?<=amount\">\$)(.*?)(?=</strong>)';
$regex[3] = '(?<=amount\">\$)(.*?)(?=</strong>)';
I have no idea why this gave the result it did, rather than a straight up error, but once removed it all is fine. The bottom line is that both htmlspecialcharacters and htmlentities give the right answer, Lesson learnt. Check all the code, my mistake was in the use of arrays, defining $regex as an array and a variable, not as I first thought here.
I've read the Q&A regarding this topic, but it unfortunately doesn't answer my question, because I'm a Beginner in PHP.
I'm using a function to display a polygon on a google Map. That works all fine. The coords are stored in the following variable:
$polygon = array(
"43.231297 -79.813721",
"43.238438 -79.810768",
"43.230335 -79.809395",
"43.230312 -79.809296",
"43.240208 -79.808983",
"43.230225 -79.808884",
"43.240116 -79.808617",
"43.229823 -79.807388",
"43.231235 -79.802649",
"43.237137 -79.800774",
"43.231297 -79.813721"
);
I now want to get the latitude and longitude dynamically out of a MySQL database. My code below runs great and returns the desired coordinates:
<?
foreach ($BusinessAreaMunich as $item) {
echo "new google.maps.LatLng(" .$item['AreaCoordLatitude'] . "," .$item['AreaCoordLongitude'] . "), \n";
}
?>
However, I've tried to do the following:
$polygon = array(
foreach ($BusinessAreaMunich as $item) {
echo $item['AreaCoordLatitude'], $item['AreaCoordLongitude'];
}
);
Now I know that doesn't work, but I don't know how to solve my issue. Could you please give me an idea how to solve this?
Proper code is:
$polygon = array(); // define `$polygon` as array
foreach ($BusinessAreaMunich as $item) {
// create `string` value as a result of concatenating coords and a space
$coords = $item['AreaCoordLatitude'] . ' ' . $item['AreaCoordLongitude'];
// append a `string` to `$polygon`
$polygon[] = $coords;
// or simply:
// $polygon[] = $item['AreaCoordLatitude'] . ' ' . $item['AreaCoordLongitude'];
}
// output to see what you have
print_r($polygon);
I'm using a basic LAMP server for my website and looking to rewrite a script I have that's linked with Twitch's API. The issue that I'm having is trying to find, for lack of a better word, the opposite of the foreach.
For example I have an array of names that are sent with a URL to the Twitch servers, and if one of the names are currently streaming "streams" will have lots of values, and if they aren't currently streaming "streams" will return null.
This works wonderful for checking people and showing they are online, but I also need it to display the ones who are offline and I can't figure out how to do that. Something like a foreachelse or something like that. This is the code I have below. Thanks.
<?php
$username = array("zeromi", "aerosgw", "krylen","tshirt_aion","snooky_aion","vashiro","papa456","vinley_aion","hanfkrokette","wtfast_siel","neckofthewood","paraproc","aionnae","uhiwi","mufflermankr","valorium","knighterrantry","soulune","relizex3","vinlockz","trevyn201","tiger529","xkegi","logsnsticks","meowform","uzuk3","kalzard01","squall_m","suyji","headpcgamer","sariett_siel");
$callAPI = implode(",",$username);
$data = json_decode(#file_get_contents('https://api.twitch.tv/kraken/streams?channel=' . $callAPI), true);
foreach ($data['streams'] as $streams){
$name = $streams['channel']['name'];
echo $name.'<br>';
}
?>
I don't see the problem, if you just use array_diff, you can filter out the usernames.
$online = array();
foreach ($data['streams'] as $streams)
$online[] = $streams['channel']['name'];
$offline = array_diff($username, $online);
echo 'Online users: ' . implode(', ', $online) . "\n<br>";
echo 'Offline users: '. implode(', ', $offline);
Output (at time of writing):
Online users: sariett_siel, mufflermankr
Offline users: zeromi, aerosgw, krylen, tshirt_aion, snooky_aion, vashiro, papa456, vinley_aion, hanfkrokette, wtfast_siel, neckofthewood, paraproc, aionnae, uhiwi, valorium, knighterrantry, soulune, relizex3, vinlockz, trevyn201, tiger529, xkegi, logsnsticks, meowform, uzuk3, kalzard01, squall_m, suyji, headpcgamer
I have a problem that's born of being a dyed in the wool procedural programmer who's here forced into using some OOP constructs in order to make use of a library I need to use. I am stuck unable to access variables- other than print them out. Let me illustrate:
foreach($html->find('span[class="given-name"]') as $e)
echo $e->innertext . '<br>';
foreach($html->find('span[class="family-name"]') as $e)
echo $e->innertext . '<br>';
The above will print out a long list of first names, followed by a long list of surnames. However I want to be able to access them together. For example I want to be able to say something like $myarray["firstname"] = (*whatever is in $e->innertext*) and also $myarray["surnamename"] = (*whatever is in the next $e->innertext*)
When I try the seemingly obvious:
$x = $e->innertext;
it crashes. I assume that's because I am passing a pointer to $x instead of a value, but how on earth do I tell it I want the value - in this case a part of a person's name, to be assigned to $x, or my array, or whatever the variable might be?
I am almost a complete neophyte when it comes to OOP concepts and constructs, so please bear that in mind. Thank you for your assistance!
If your document is well structured and in order, this should do the trick:
$name=array();
$surname=array();
foreach($html->find('span[class="given-name"]') as $e){
$name[]=$e->innertext;
}
foreach($html->find('span[class="family-name"]') as $e){
$surname[]=$e->innertext;
}
foreach($name as $key=>$value){
echo $name[$key] . " " . $surname[$key] . "<br>";
}
I've been scouring around for information through the google Calendar API, the Zend docs, and here, and just about everything I find seems to make assumptions on what I already know about PHP, so I'm just getting more lost. I do have a good deal of programming experience... with... um... a FORTH variant. Anyway! I'm trying to pass the output of a PHP script that can be used to get all of the important data from a calendar event into said FORTH variant. What's driving me up the wall is that I can't figure out how to grab something as simple as the UID of a message. Here's what I'm working with:
function outputCalendar($client)
{
$gdataCal = new Zend_Gdata_Calendar($client);
$eventFeed = $gdataCal->getCalendarEventFeed();
foreach ($eventFeed as $event) {
echo $event->title->text . " (" . $event->id->text . ")\n";
foreach ($event->when as $when) {
echo $when->startTime . "\n";
}
}
}
This is based off the example code they gave me, and instead of formatting with with XML tags like in the example, just puts each on its own new line (which is easier for me to pull into the other language.)
Now, I tried to add echo $when->startTime . "\n"; to the loop, but it just tells me:
Fatal error: Uncaught exception 'Zend_Gdata_App_InvalidArgumentException' with message 'Property uid does not exist' in /var/mucktools/Zend/Gdata/App/Base.php:484
So, obviously, I'm going about grabbing the UID the wrong way. Now, here's the thing. These two lines:
echo $event->title->text . " (" . $event->id->text . ")\n";
echo $when->startTime . "\n";
...are pulling data from the event. However, 'title'. 'text', 'startTime' all look like things pulled out of one's posterior. I know, cognitively, that can't be true. There is a library and an API here. But I want to know where I can find a listing of all the crap I can pull out of $event and what the syntax is to do so. Can anyone help me with this?
And before you ask, yes, I have a very good reason to be grabbing the output of a PHP script and stuffing it into an obscure FORTH variant. And no, there's not another way that won't be more complicated than this one. I've done my homework here.
I'm sure you've read the documentation for Zend Gdata. It tells you that you need to provide getCalendarEventFeed() with a Zend_Gdata_Query object, otherwise I think you just get public data back.
Your code then should look something like this:-
$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
$user = "user#gmail.com";
$pass = "userpassword";
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$gDataCal = new Zend_Gdata_Calendar($client);
$query = $gDataCal->newEventQuery();
$query->setVisibility('private');
$query->setProjection('full');
$query->setOrderby('starttime');
$query->setFutureevents('true');
$eventFeed = $gDataCal->getCalendarEventFeed($query);
foreach ($eventFeed as $event) {
echo $event->title . " (" . $event->id . ")\n";
foreach($event->when as $when){
echo $when->startTime . "\n";
}
}
Hopefully that should get you started in the right direction.
Also the php_openSSL module has to be enabled in PHP for this to work, but I assume you have that already enabled to get as far as you did.