I need your advice and help
i fetched an array of data from the database and i want to process each element one by one without using foreach loop, something like
pop element a and process it, when finished
pop element b and process it, when finished
pop element b and process it
until the array become empty then the script can exit
currently i`m looping through the data using foreach loop but things are not working find.
$loaded_message = $this->lib->load_queued_messages();
if(count($loaded_message) == 0) {
die ('Nothing to do');
}
foreach($loaded_message as $tosend)
{
if($this->lib->send_sms($tosend['from'], $tosend['msg'], explode(',', $tosend['numbers']), $tosend['owner'], $tosend['qid']))
{
// Remove the message from queue
$this->lib->remove_msg_from_queued_message($tosend['qid']);
$this->lib->log('message #' . $tosend['qid']. ' sent and removed from queue', $tosend['owner']);
}else{
$this->lib->log('SENDING_ERROR: message #' . $tosend['qid']. ' not sent and remain in the queue for#', $tosend['owner']);
}
}
Inside the log table i discovered that entry was made for wrong message id and it seems like message was sent to wrong number but it does not.
hi mate you can use something like
while(sizeof($yourarray)) {
$result = array_pop(yourarray);
...yourprocessing_here(...);
}
hope this helps :)
Related
I have a script that scans every item laptop in my Snipe IT inventory, compares it against that same instance in my Jamf Pro server, and for any fields that do not match, it creates an XML and sends that XML over to Jamf. What I need is for the XML to have all of its elements "wiped" after sending the payload over. I have tried countless --- countless --- ways to make this happen, but even when I "print" the supposedly "wiped" payload at the end of the script and confirm that the element was removed, it still appears the next time around when I inspect the payload that was sent to Jamf. If I have three laptops with info that gets sent over, I get three fields. Four, I get four. And so on. I really need to figure out how to reset this XML to make this script cleaner.
Here is where I am creating the XML, if the information does not match.
// let's compare the DEVICE ASSIGNMENT across our two systems, knowing that itassets is our SOT
if ($jamf_assignment !== $itassets_assignment) {
echo "\n" . "ALERT: The device assignment is not in sync across our systems!" . "\n";
if (!isset($location)) {
$location = $jamf_xml->createElement('location');
$location->appendChild($jamf_xml->createElement('real_name',$itassets_assignment));
$process_xml = 1;
} else {
$location->appendChild($jamf_xml->createElement('real_name',$itassets_assignment));
$process_xml = 1;
}
} else {
echo "\n" . "DEVICE ASSIGNMENT CHECKS OUT! ALL GOOD HERE!" . "\n";
}
And here is where I am turning it into a payload, about to be sent.
if (!empty($location)) {
$computer->appendChild($location);
}
$jamf_xml->appendChild($computer);
$jamf_payload = $jamf_xml->saveXML();
And finally here is where I am trying to reset that XML, and start fresh with the next loop.
// and finally let's destroy the XML file
if (!empty($general)) {
$computer->removeChild($general);
}
if (!empty($location)) {
$computer->removeChild($location);
}
$jamf_xml->appendChild($computer);
$jamf_payload = $jamf_xml->saveXML();
I have a heavy processing script which can be started from a user via frontend in our intranet. Imagine something like this:
$html = file_get_contents($url);
$pattern = '/[A-Z0-9._%+-]+(#|\(at\)|\[at\])[A-Z0-9.-]+\.[A-Z]{2,4}\b/i'; //also (at) and [at]
preg_match_all($pattern,$html,$emails);
foreach ($emails[0] as $m)
{
$m[] = $m;
}
foreach($m as $n){echo $n."<br>";}
This is just an example for illustration of the question! Don't judge it on common sense.
NOW what i want is 2 things:
Stop the process on Click on a button from the user. This means: outputting the already collected array $m[].
Stop the process (or better: stop the array-collecting process and jump to the echo of the already collected array) based on time (for example max. 1 minute collecting the array, THAN jumping to echoing.
I don't want to echo live and setting max execution time will stop the script without echoing.
Thanks for your wise advise on both subquestions
You have to run script through ajax, and button click regsiter session by ajax
foreach ($emails[0] as $m)
{
if (!isset($_SESSION['STOP'])) {
$m[] = $m;
}
else { //stop code here }
}
Some basic background ...
I have a form that enters data to an xml file and another page that displays the data from teh xml depending that it meets the requirements . All of this I have managed to get done and thanks to a member on here I got it to show only the data as long as it has todays date and status is out . But I am left with the problem of trying to sort an if statement which needs to show data if it has it or show another div if not .
My Code ...
$lib = simplexml_load_file("sample.xml");
$today = date("m/d/y");
$query = $lib->xpath("//entry[.//date[contains(., '$today')]] | //entry[.//status[contains(., 'out')]]");
foreach($query as $node){
echo "<div id='one'>$node->name</div>
<div id='two'>$node->notes</div>
<div id='three'><div class='front'>$node->comments</div></div>";
}
So to reiterate if query returns matched data do the foreach else show another div
I only wish to know the right code for the if else statement if soneone could help with this I would be very grateful and will up vote any answer as soon as I have the reputation in place . I also apologise in advance if the question has been asked before or if it is too vague thanks again .
If xpath fails to resolve the path, it will return false (see here). Wrap the foreach loop in a simple check:
if( $query ) {
foreach($query as $node){
...
}
}
else {
// Echo the special div.
}
Since PHP is loose typed, if xpath happens to return an empty array, this check will also handle that case. Be aware that if the xpath call does return false, there may be a separate error at play that may require additional or alternative handling.
I have a input field and a array of email from DB table.
I am comparing the similarity of the input field to the array.
But some how, I am stuck with the loop.
Does that loop check compare each email with the input field?
It always bring me to google.com no matter what i input same or not
Here's the code from the controller:
if (isset($_POST['btn_free_download']))
{
// Get current input email from input field
$email = $this->getRequest()->getParam('email');
// Get all emails from the user
$referred_users = $this->_helper->user()->getReferredUsers()->toArray();
// Check the similarity which it with a loop
foreach ($referred_users as $referred_user)
{
similar_text($email, $referred_user['email'], $similar);
}
// If yes, Pop up message or redirect to some page
if ($similar < 97)
{
$this->_redirect('http://google.com');
}
// If not, redirect user to free download page
else
{
$this->_redirect('http://yahoo.com');
}
}
I think you need to check the manual . Foreach function is same wether you use it on zend or any other framework or raw php only.
$referred_users = $this->_helper->user()->getReferredUsers()->toArray();
$referred_users will probably hold an array of emails from the table
user, say:
$referred_users = array("one#email.com", "two#email.com", "three#email.com")
then when you use foreach loop it will iterate through each of the
emails in the array
foreach ($referred_users as $referred_user)
{
// for the first loop $referred_user = one#email.com, for second time $referred_user = two#email.com and goes on
similar_text($email, $referred_user['email'], $similar);
}
Now let us discuss your logic here:
// If yes, Pop up message or redirect to some page
if ($similar < 97)
{
$this->_redirect('http://google.com');
}
// If not, redirect user to free download page
else
{
$this->_redirect('http://yahoo.com');
}
Until and unless the last element in the array $referred_users is exactly equal to your $email
i.e. $email = "three#email.com"
you will always be given result for $similar less than 97% which means you will be redirected to google.
Which I assume you are not trying to do and probably not familiar with foreach function which is why you are not getting the expected result.
Assuming you are trying to do something like, check for the emails in the array if any of the email in the array matches (if array is from table check if the email entered from param is equal to any of the emails in the table) then redirect to somewhere or show some message else carry on. Solution below might be helpful to you.
$similarText = false;
foreach ($referred_users as $referred_user)
{
// for the first loop $referred_user = one#email.com, for second time $referred_user = two#email.com and goes on
similar_text($email, $referred_user['email'], $similar);
if ($similar > 97) {
$similarText = true;
break;
}
}
// If yes, Pop up message or redirect to some page
if ($similarText)
{
$this->_redirect('http://google.com');
}
// If not, redirect user to free download page
else
{
$this->_redirect('http://yahoo.com');
}
Hope you got the idea. But please do check the manual before posting a question in the future.
I've made up a PHP script which assigns a score to listings on a website and assigns it to the results page. I have got it to work in that it shows the score and the details but it keeps listing the same results over and over.
I can't work out what it is doing but there is a small section of code I was hoping would prevent duplicate listings. Could anyone give it a tweak and see if I am going wring somewhere?
The Code is:
$dupCatch .= $adId.",";
$dupResults = explode(',', $dupCatch);
foreach($dupResults as $dupResult){
if($dupResult == $adId){
print "";
} else {
print $showResults;
$scoreBox = 'THIS IS THE SCORE: ' . $finalScore . '';
print $scoreBox;
}
}
Thanks in advance!
Jack
The problem is that you add your current $adId to the duplicate list before you check if it is there - which it will always be, of course.
Storing a bunch of numbers in a string, explodeing it every time, is a little weird, use an array instead. You also don't need to manually loop through all the items, just use in_array()
if( !in_array($adId, $dupCatch) ){
print $showResults;
$scoreBox = 'THIS IS THE SCORE: ' . $finalScore . '';
print $scoreBox;
}
$dupCatch[] = $adId;
Needless to say: it would be a better idea to fix the part that gives you the duplicate results in the first place.
You can either try to use array_unique from php side or use unique attribute at field in mysql this way duplicates can be prevent before even inserting them.