While looping problem in php

Posted on 2012-09-08
Last Modified: 2012-09-09
I want execute to my file(updation2.php) in cron job



This file contains javascript. when i execute this file in my browser, the database content will update correctly.
The output is:
Correct content image
But the javascript file does not support in cron job.

So i am using while looping method(updation2.php). When i am execute this file, the database content will not update correctly.
The output is:
Incorrect content image
I need the solution when execute the updation2.php ,the database content should be updated correctly. like below image
Correct content
Question by:jnj_web_solutions
    1 Comment
    LVL 25

    Accepted Solution

    Be careful with variables and scope with PHP.  As you are doing it all within the one main body of the script the variables are defined and used over and over again.
    The problem I see with your script is that every time through the loops, you haven't initialised the variables ... e.g.
          while($obj=mysql_fetch_object($res)) {
                $left .= "'".$obj->SId."',";
    The $left variable will keep getting longer and longer on every loop through (the while($obj6=mysql_fetch_object($res6)) loop) ... it needs to be re-initialised back to "" at the beginning of the loop.
    Similar problems exist for other variables like $Total, $Rv, $Lv ... etc there are possibly more, I stopped looking to be honest.  They need to be set back to zero as every loop through keeps adding to the values from the last loop ... and on.

    On an aside note, stylistically the code could be made more simpler and easier ... but that is personal preference.

    On an other aside, more could have been done in the database too ... e.g. the big tree walk query could be used to return the totals, e.g.
    select sum(LPV) as Lv, sum(RPV) as Rv, sum(LAmount + RAmount) as Total
    from (
    select leftn as SId, LPV, RPV, LAmount, RAmount
    from amount
    inner join (select @sid:=(select group_concat(leftn,',',rightn)
                              from amount
                              where find_in_set(sid, @nsid) >= 1)
                     , @nsid:=@sid as fset
                from amount, (select @nsid:='AMSXATUMI',@sid='')v )gr
            on find_in_set(leftn,fset)
    where leftn <> ''
    select rightn as SId, LPV, RPV, LAmount, RAmount
    from amount
    inner join (select @sid:=(select group_concat(leftn,',',rightn) 
                              from amount
                              where find_in_set(sid, @nsid) >= 1)
                     , @nsid:=@sid as fset
                from amount, (select @nsid:='AMSXATUMI',@sid='')v )gr
            on find_in_set(rightn,fset)
    where rightn <> ''

    Open in new window


    Featured Post

    PRTG Network Monitor: Intuitive Network Monitoring

    Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

    Join & Write a Comment

    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now