What is the best way to have PHP execute numerous calls from an array to a script, ensuring that each function completes before proceeding?

In my script, I have an array of properties that includes a list of properties along with their corresponding .ics locations. When I run the script manually for each element in the array individually, it works perfectly. However, when I attempt to loop through the script using a while loop, it consistently fails after processing the first property and part of the second one. It is worth noting that the $this->mymodel->import() function takes between .5 - .8 seconds to execute.

This is what the property array structure looks like:

$propsArray = array (
    array('Property 1', '83736363G', 'http://blah.ics'),
    array('Property 2', '83726433H', 'http://blah.ics'),
    array('Property 3', '62727438J', 'http://blah.ics'),
    array('Property 4', '12345356Y', 'http://blah.ics'),
    array('Property 5', '28373657D', 'http://blah.ics'),
    array('Property 6', '93377656K', 'http://blah.ics'),
    array('Property 7', '11223343L', 'http://blah.ics'),
...
);

To process these properties, I use a foreach loop to iterate through the array and call the import function in my model like this:

foreach($propsArray as $property) {
      $propertyName = $property[0];
      $this->mymodel->import($property[1], $property[2]);
  }

When running $this->mymodel->import($property[1], $property[2]); manually for each individual property, everything works flawlessly. The issue arises only when I attempt to loop through the properties, resulting in a script hang without any errors. I suspect it may be related to a JS synchronization or asynchronous problem, which might require a callback or similar mechanism to ensure that the next iteration in the loop only proceeds once the previous one completes. Any suggestions on how to address this would be greatly appreciated.

Answer №1

If the task is set to run every hour and only takes 5 minutes, consider adjusting the execution limit by adding the line set_time_limit(0); at the beginning of the script to allow it to run as long as needed.

Make sure there is enough breathing room between processes to avoid overlaps that could slow down operations. Think about the sequence in which tasks need to be completed, the possibility of failures, and whether the task list will vary in size over time.

Consider exploring alternative architectures beyond a single script if scalability is a concern. A message queue/worker setup can provide more flexibility, allowing for concurrent processing based on job volume or running each task individually as needed.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

PHP: Check if an array contains values that begin with a specific value

I've created a function designed to search through an array and find values that start with the specified second parameter: public static function arrayContainsValueStartingBy($haystack, $needle) { $len = strlen($needle); foreach ($hays ...

Transferring data between JavaScript and PHP

Is there a way to transfer data from JavaScript to PHP when a button is clicked? For instance, can the getdate() function's result be sent to PHP? This transferred value will then be utilized for database manipulation. ...

How can you display a message if a variable is not found in a MySQL table?

My current code is set up to display links from a MySQL database where the movie_id matches the GET id. However, I would like to update it so that if no links are found with that movie_id, it will instead echo a message saying "No links were found." < ...

Tips for integrating reCaptcha with AJAX using the .load() function

Looking to integrate reCaptcha with the jQuery '.load' ajax function for passing information to a PHP contact form. Managed to send name, subject, and other values using this method, but unsure about passing reCaptcha info. Encountering a PHP er ...

Unable to establish the default selection for the options dropdown box

I have successfully populated a dropdown menu with data, allowing me to select from a list. However, I am struggling to set the selected option to remain displayed after making a selection. Instead of resetting to the first option in the list, I want the ...

Is it possible to bypass the initial bracket when working with arrays in PHP?

I am trying to display the [number] value from each array, but with different parent brackets [], how can I skip the first one and retrieve the desired [number]? Basically, I need to ignore the first set of brackets [] and access the second one containing ...

Using HTML as an argument in a JavaScript function

Within a variable, I have stored an entire HTML page. $body = $myhtmlpage; <a onclick="openWin('<?php echo htmlspecialchars(json_encode($body)) ?>');" href="javascript:void(0);"> Click </a> I also have this JavaScript functio ...

Who fills the Sylius Resource Metadata Registry?

Summarized Version: How does the sylius.resource_registry service in Sylius get populated with data? Detailed Version: In Sylius, a PHP ecommerce framework based on Symfony, utilizes Symfony's custom route loading system to add extra routes from spec ...

Pattern matching for replacing <a> tags

As a beginner in the world of regular expressions, I am eager to learn more about it. Currently, my goal is to extract only the inner text from an HTML tag and remove the surrounding tags. For example: Original: Lorem ipsum <a href="http://www.google.e ...

When the page initially loads, the block appears on top of the upper block and remains in place after the page is refreshed

Upon initial loading, the block appears on top of another block but remains fixed upon page refresh. The same issue occurs in the mobile version of the site and occasionally displays correctly. The website is built on WordPress and optimized using Page Spe ...

Styling Function within the CSS file

Hi there, I need some help with calling a function in my style.css file. Is it possible to do something similar to this: quick-search-input{ background:#1d1d1d url("<?php echo get_template_directory_uri(); ?>/images/icon-search.svg") no-repeat ...

Looking to create a popular page using MongoDB and PHP, seeking advice on updating documents and running queries

In order to create a realtime and fast trending page for newsletters, we are utilizing our extensive database. The aim is to showcase the most popular newsletters from various time frames such as the past 2 hours, 4 hours, 24 hours, past week, and month. ...

CSS animation - gradual disappearance on mouse exit

Hey everyone, I'm in need of some assistance with my animation. I've created a button that moves right and left in a loop, but I'm encountering an issue when the mouse moves away from the button. The transition is not smooth and it jumps bac ...

Once AJAX/GET has been utilized, further execution of jq functions such as .click may not be

I'm facing an issue with AJAX/GET code. After reloading data from a PHP file using jQuery, I have another block of code that my problem lies in the fact that after reloading data from MySQL/PHP server while the page refreshes, I can't use functio ...

Changing the boolean value in a database by invoking a function within an href link

Within the Laravel framework, I have a messages page that displays all messages associated with a particular user. Initially, these messages are marked as unread, so I implemented a button to toggle the boolean value in the database from 0 to 1 and then di ...

FreeTDS does not properly display UTF-8 symbols in its output when using UTF-8 encoding

While working on a MSSQL query using FreeTDS in my PHP application, I encountered an issue where special UTF-8 symbols were not being displayed. My odbc.ini files are configured as follows: [test] host = server.com port = xxxxx tds version = 7.4 clien ...

Choose records from a single MySql table using the id for reference

Creating a simple photo album page, where the selected image is displayed at the top and thumbnails of the remaining photos appear below. The URL structure for this project is as follows: /photos/photo.php?id=164 The database table consists of fields for ...

Using regular expressions to extract MTOM binary data

I'm currently attempting to retrieve the MTOM binary content using an extended class of SoapClient. The response I receive looks something like this: --uuid:8c73f23e-47d9-49fb-a61c-c1df7b19a306+id=2 Content-ID: <http://tempuri.org/0&g ...

Migrating historical WordPress.com website analytics to a personally hosted blog

After relocating my wordpress.com blog to a self-hosted platform with my own domain name, I have completed several steps. Migrated my previous articles and posts. Installed and activated the Jetpack plugin. Transferred my old blog followers with assista ...

Utilizing jQuery AJAX to efficiently handle branching based on the result received

I've successfully set up my first AJAX call with jQuery. The only thing left to do is to check the result from the PHP page for any database errors and display an error message if necessary. Here's the current Javascript code: <script type=" ...