Page 1 of 1

[SOLVED]A for loop exceeds maximum time limit

Posted: Sat Jan 10, 2009 4:57 am
by orobian
Hi everyone, this is my first technical post here in the forum. I don't touch PHP since 2005, so I'm having my bad moments.

The logic behind what I would like to achieve is: I have a table of messages dynamically generated. I want the user to be able to select one or more of those messages, then delete them. I have the checkboxes that pass an array to a method of the class "messaggi", whose object has been instantiated.
When I pass the values from the table to the method it works, and the method starts to do its job, but it exceeds the script time limit (but it does what is meant: deleting the desired messages). Here are the code snippets:

Code: Select all

public function visualizzaNuoviMessaggi($societa,$codiceId) {
                        //Dbconnection data with include omitted
                  
                        //Select users and messages
                        $queryMessaggi = "SELECT
                                            *
                                          FROM
                                            utenti_societa,utenti_messaggi
                                          WHERE
                                            utenti_messaggi.utenteDestinatario = '$codiceId'
                                          AND
                                            utenti_societa.id=utenti_messaggi.utenteMittente
                                          AND
                                            utenti_messaggi.letturaMessaggio = 1
                                          ";
                        $risultatoQueryMessaggi = mysql_query($queryMessaggi);
                        if (!$risultatoQueryMessaggi) {echo "La selezione messaggi non funziona";}
                        //Prendo i messaggi nuovi
                        if ($risultatoQueryMessaggi) { 
                             //Through form action, I pass a $_GET which I'll use to trigger an if statement in the main script ?>
                            <table><form name="cancella" action="activeLaunch.php?cancellaMessaggio=1" method="post">
                                <tr id="intestazione">
                                    <td>Selezione</td><td>Mittente</td><td>Oggetto</td>
                                </tr>
                                    <?php
                                    //Here I generate the table rows with query results
                                    while($messaggi = mysql_fetch_array($risultatoQueryMessaggi)) {
                                    ?><tr>
                                        <td><input type="checkbox" name="check[]" id="check[]" value="<?php echo $messaggi['idMessaggio']; ?>"></td>
                                        <td><?php echo $messaggi['nomeUtente']." ".$messaggi['cognomeUtente'];?></td>
                                        <td><a href="activeLaunch.php?messaggioSelezionato=<?php echo $messaggi['idMessaggio'];?>&nienteLista=1"><?php echo $messaggi['oggettoMessaggio'];?></td>
                                    </tr>
                                    <?php } ?>
                                <tr>
                                    <td><a href="activeLaunch.php?nuovoMessaggio=1&nienteLista=1">Nuovo</a> <a href="javascript&#058;document.cancella.submit()">Cancella</a></td><td><a href="activeLaunch.php?rispondiMittente=1">Rispondi</a></td><td><a href="activeLaunch.php?inoltraMessaggio=1">Inoltra</a> <a href="activeLaunch.php?archiviaMessaggi=1">Archivia</a></td>
                                </tr></form>
                            </table>
                         <?php  } } //Here another method is declared....
Clicking on "Cancella", $_GET['cancellaMessaggio'] = 1, so in the main script the code is:

Code: Select all

if($_GET['cancellaMessaggio'] == 1) {
                $check = $_POST['check'];
                foreach ($check as $key => $value)
                $objMessaggio->cancellaMessaggi($check);
                }

And now, the method cancellaMessaggi():

Code: Select all

public function cancellaMessaggi($check){
                        $queryRighe = "SELECT 
                                        *
                                       FROM
                                        utenti_messaggi
                                      ";
                        $risultatoQueryRighe = mysql_query($queryRighe);
                        $conteggioRighe = mysql_fetch_row($risultatoQueryRighe);
                        for($i=0;$i<$conteggioRighe;$i++){
                        $del_id = $check[$i];
                        $queryCancellazione = "DELETE FROM utenti_messaggi WHERE idMessaggio = '$del_id'";
                        $risultatoCancellazione = mysql_query($queryCancellazione);
                        if(!$risultatoCancellazione) { echo "Non riesco a cancellare il messaggio con id pari a ".$del_id;}
             }
As mentioned, the script actually works (erases what has to erase) but times out. Do you have an idea why? Thank you in advance.

Re: A for loop exceeds maximum time limit

Posted: Sat Jan 10, 2009 9:03 am
by Chalks
orobian wrote:

Code: Select all

$conteggioRighe = mysql_fetch_row($risultatoQueryRighe);
for($i=0;$i<$conteggioRighe;$i++){
As mentioned, the script actually works (erases what has to erase) but times out. Do you have an idea why? Thank you in advance.
Probably because "mysql_fetch_row" doesn't actually return an integer... it returns an array. Try mysql_num_rows instead.

Re: A for loop exceeds maximum time limit

Posted: Sat Jan 10, 2009 9:44 am
by orobian
That was the point. Thank you so much, Chalks!