SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Counter Problem

  1. #1
    SitePoint Member
    Join Date
    Oct 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Counter Problem

    Hey,

    i am a beginner to php and cannot seem to find out why my counter keeps resetting to 0

    Below is my code:
    please note this code works, the first window.alert gives me 0, the loop runs twice, but everytime it comes back to window.alert it gives me 0...

    i have tried to put the window.alert after the $count++; in the php tags and it gives me 1, meaning the counter does add 1..but again when it loops the second time, the $count shows as 0 ??

    so frustrating any ideas???

    Code Below:

    Code:
    <script language="JavaScript" type="text/javascript">
    		function LoadSchedule()
    		{
    			<?php 
    				session_start();
    				$visitorE = Array();
    				$visitorE = $_SESSION['visitorSchedule'];
    				$count = 0;
    			?>
    			
    			var eventCount = '<?php  echo count($visitorE);?>';
    			
    			var scheduleDiv = document.getElementById("schedule-layout");
    			for(var intCount=0;intCount<eventCount;intCount++)
    			{
    				window.alert('<?php echo $count;?>');
    				<?php
    				$schedule = Array();
    				$schedule = $visitorE[$count];
    				?>
    				scheduleDiv.innerText += "<?php echo 'Event: ' . $schedule[0]; echo ' | Date: ' . $schedule[1]; echo ' | Time: ' . $schedule[2]; echo ' | Venue: ' . $schedule[3]; echo ' | Location: ' . $schedule[4];?>";
    				<?php $count++;?>
    				
    			}
    			
    			
    		}
    	</script>

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    The reason why your above code isn't working properly is because you're telling the PHP variable $count to increment by one which will never actually do anything apart from maybe outputting 0 in the page source as the life cycle of the JavaScript comes after the server side execution, what needs to happen is your above variables should all be declared as client side variables so that the browser has access to them or you need to use a snippet of PHP to generate all the JavaScript so that the array data is publicly visible. See the below:

    Using PHP code that hides all the server side variables but generates the JavaScript you need.
    PHP Code:
    <?php

    session_start
    ();

    ?>
    <script type="text/javascript">
        var scheduleDiv = document.getElementById('schedule-layout');

        <?php

        
    // Attempt to get the visitor schedule
        
    $visitorE = isset($_SESSION['visitorSchedule']) ? $_SESSION['visitorSchedule'] : false;
        
    $count    0;

        if (
    $visitorE && count($visitorE) > 0) {
            foreach (
    $visitorE as $visitor) {
                echo 
    'alert("' $count '")';
                echo 
    'scheduleDiv.innerText += "Event: ' $visitor[0] . ' | Date: ' $visitor[1] . ' | Time: ' $visitor[2] . ' | Venue: ' $visitor[3] . ' | Location: ' $visitor[4] .'<br>"';

                
    $count++;
            }
        }

        
    ?>
    </script>
    Using JavaScript that exposes the array data to the browser:
    Code JavaScript:
    var count       = 0,
        scheduleDiv = document.getElementById('schedule-layout'),
        visitorE    = <?php echo isset($_SESSION['visitorSchedule']) ? json_encode($_SESSION['visitorSchedule']) : 'false' ?>;
     
     
    if (visitorE && visitorE.length > 0) {
        for (var i = 0, m = visitorE.length; i < m; i++) {
            // Store the current visitor data
            var schedule = visitorE[i];
     
            alert(count);
            scheduleDiv.innerText += 'Event: ' + schedule[0] + ' | Date: ' + schedule[1] + ' | Time: ' + schedule[2] + ' | Venue: ' + schedule[3] + ' | Location: ' + schedule[4] + '<br>';
     
            count++;
        }
    }

    Personally I would choose the PHP code because it doesn't expose the data but the choice is up to you in the end.

  3. #3
    SitePoint Member
    Join Date
    Oct 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you will try it out now now and let you know how it goes...Thank you very much for your reply!

  4. #4
    SitePoint Member
    Join Date
    Oct 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    do i not need to include the function name? otherwise how will i call the script?

  5. #5
    SitePoint Member
    Join Date
    Oct 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its now working 100% thank you for your time and knowledge!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •