0

Hello,
Another issue came up, when needed to run mass-functions.
archiveinv.php is generating html-file with result of query function. This part is working fine. And launched from form-link "archiveinv.php?offerid=$offerid" does it's job.
But, when i need to launch many of them in same time, with array result, it's harder to get working.
I manage it almost with this part of script. This generates a result of needed rows for execution. But needs still a new query. Executing only one by one.
It doesn't do the loop for execute all in same time. I use here header-function, but can i use something else with this?

$keys = array($offerid);
    for($i = 0; $i < count($offerid); $i++) {
    header('Location:archiveinv.php?offerid='."$keys[$i]".'"');
    foreach($offerid[$keys[$i]] as $key => $value) {
    $key . " : " . $value . "";
    }

echoed output shows correct results as:
header('location:archiveinv.php?offerid=160415132928);
header('location:archiveinv.php?offerid=160415135316);

5
Contributors
13
Replies
50
Views
1 Year
Discussion Span
Last Post by masimies
Featured Replies
  • 1
    cereal 1,419   1 Year Ago

    Hi, it happens because the `header('Location: url');` will be overwritten by the last loop value, which is what will be executed. You cannot start multiple redirections. Also, what is this line supposed to do? $key . " : " . $value . ""; Print? Read More

  • 1
    cereal 1,419   1 Year Ago

    In addition, for a simple approach you could use `require` to load the generator file and loop the offerid to it, for example: <?php # example array $offerid = range(1, 10); foreach($offerid as $value) require 'archiveinv.php'; Then in `archiveinv.php` instead to refer to `$_GET['offerid']` use `$value` which is set in … Read More

0

To sniff this out, add something like print count($offerid) after line 1 to see what you have there.

0

But i got already results. And it works, but only one by one, with new refresh.

0

@OP
Since I only have your code and not what's in $offerid, my advice is to print out more until you debug it.

1

Hi,

it happens because the header('Location: url'); will be overwritten by the last loop value, which is what will be executed. You cannot start multiple redirections.

Also, what is this line supposed to do?

$key . " : " . $value . "";

Print?

0

Ok, i taught so with header-function. What else could be executed for multiple actions??
I think that rows 4 and 5 are not needed here. Results are the same without them.

0

I would like to understand better what's your goal: producing HTML files for archiving? The result is needed in the following step by the user?

0

Yes, the target is to producing html-files. Create them as many as selected by first guery, like all offers included specified time-window.
So with this part of script, i can fetch this information, got those offer-numbers.
But when i try to generate them with archiveinv.php by loop, it doesn't do it for all, like in result:
header('location:archiveinv.php?offerid=160415132928);
header('location:archiveinv.php?offerid=160415135316);
Only one by one.
With PHP, can it be done as looping scripts, like with cmd-scripts. I have seen only query results done by loop, showing the data, but executing actions, can it?

0

Sorry if I insist but it's not clear:

Yes, the target is to producing html-files. Create them as many as selected by first guery, like all offers included specified time-window.

So the goal is to generate a cache system or simply to show the selected offers? If you want to show the offer why you want to generate an HTML file for each id? Could you just populate the script?

Is the submitter going to browse the new generated files right after the generation?

You could do something similar to your approach by using curl with an HEAD request, but it will open a new connection for each offerid (unless you set up for reuse) and with the current approach it will perform a new query to the database for each offerid. This will slow down the execution. You could fork but how many concurrent executions can your server support?

Only one by one.
With PHP, can it be done as looping scripts, like with cmd-scripts. I have seen only query results done by loop, showing the data, but executing actions, can it?

You can execute an action, but there are several ways to do it: if your goal is to generate something in background you could save the offerid list to a database table and then use cronjob to execute the task by batching the result set, but it will execute a batch per minute and you will have to batch otherwise the script could timeout.

You could also use queue worker server like Beanstalkd, in which case you don't have to worry about timeouts, but it requires sometime to setup the necessary daemons.

If you explain better the reason you want to generate these files, maybe we can suggest you the best approach for the task. In my opinion, with current information, your approach is not correct, but I could have missed something and think wrong.

0

I try to open more this issue.
Every html-file is like a bill or offer of items. It's in html format. Data for this file is generated by archiveinv.php, what builds up html-file with own query. Data can be fetched by offerid-number. It's a collection about numbers of rows (items), what have own id.
This loop-script generates list of offers, and have to send command for html-file generation. Currently, i can do this by button in offerid's grid. One by one.
If ever, there is like 900 offers. I do not like to click 900-times to generate files.

0

Hi,
If you like to open different windows you can try something like this using javascript:

$s1 = 'var offeridnr = ['; 
for($i = 0; $i < count($offerid); $i++)  {
...  $offeridnr = ...
    $s1.= '"'.$offeridnr.'",';
   }


$s1.= '" "] '; 
?>
<html>
...
<script>
    var s1 = "", i1 = 0;
  <?php echo $s1 ?>
  for (i1 = 0; i1 < (offeridnr.length - 2); i1++)    {
     window.open('archiveinv.php?offerid='+offeridnr[i1]);
  }
</script>
 ...

I can imagine there are better solutions when someone understand better
what you really want.

1

In addition, for a simple approach you could use require to load the generator file and loop the offerid to it, for example:

<?php

    # example array
    $offerid = range(1, 10);

    foreach($offerid as $value)
        require 'archiveinv.php';

Then in archiveinv.php instead to refer to $_GET['offerid'] use $value which is set in the loop:

<?php
    # your code here
    print $value;

Otherwise create a set of functions or a class, load the file once and then loop the methods to generate the file:

<?php

    require 'archiveinv.php';

    $offerid = range(1, 10);

    foreach($offerid as $value)
        echo printIt($value);

And in archiveinv.php:

<?php

    function printIt($var)
    {
        return $var;
    }

This is more the PHP way. In any case there are at least two issues:

  1. it is synchronous, so the user must wait for the script to complete the execution
  2. the script can timeout and break the flow

To avoid them I would use cronjob or, if I want to start the job immediately, Beanstalkd with a good client:

Edited by cereal

1

I tried that require, but no luck. It didn't do anything. So closest method is still header-function. But one by one effect.
Maybe the solution lie behind the java, but i'm bad with it, and my site have less java as possible. Just in practical cause. I have to manage it by my selve.
Well, i have to think more this. If there is totally different way to handle this. If the html file is created in same time, when offer has been made. Then it is already made, and do not need creation later.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.