Jump to content


Photo

Counting the value when calling a function

php

  • Please log in to reply
1 reply to this topic

#1 mark107

mark107
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 04 May 2018 - 02:52 PM

Hi all,

 

I need your help. I am working on a function to add the day value that start from 0 to 6. I want to count it up the value for each day so I can connect to each url for each day, but on my code it will only show the value as 0 for each day. 

 

Here is the code:

 

<?php


$channels = json_decode(file_get_contents('channels.json'), true);
$day = 0;


function get_shows($channel_id, DateTime $dt, $duration=1440, $skip_finished = true) 
{
    $url = 'http://example.comapi/GS?cid=' . $channel_id . '&offset=+00.00&day=' .$day++;
    


    echo $url;
    $day++;




}




if(!isset($_GET['id']) || !isset($channels[$_GET['id']])) 
{




}
else
{
    // today + 6 following days
   for($day=0; $day<=6; $day++) 
   {
      // display date      
      echo '<h3>', $dt->format('Y-m-d (l)'), '</h3>';
      
      // display shows
      foreach(get_shows($channel_id, $dt, $duration) as $show) 
      {
            
      }
      $dt->add(new DateInterval('P1D'));
      $dt->setTime(0, 0, 0);
    }
}
?>

 

 
 
 
Output:
 
 
2018-05-04 (Friday)
http://example.com/a...et= 00.00&day=0

2018-05-05 (Saturday)
http://example.com/api/GS?cid=44630&offset=+00.00&day=0

2018-05-06 (Sunday)
http://example.com/api/GS?cid=44630&offset=+00.00&day=0

2018-05-07 (Monday)
http://example.com/api/GS?cid=44630&offset=+00.00&day=0

2018-05-08 (Tuesday)
http://example.com/api/GS?cid=44630&offset=+00.00&day=0

2018-05-09 (Wednesday)
http://example.com/api/GS?cid=44630&offset=+00.00&day=0

2018-05-10 (Thursday)
http://example.com/api/GS?cid=44630&offset=+00.00&day=0

 

 

 

 

Here is what I want to achieve:

 

 

2018-05-04 (Friday)

http://example.com/a...et= 00.00&day=0

2018-05-05 (Saturday)

http://example.com/api/GS?cid=44630&offset=+00.00&day=1

2018-05-06 (Sunday)

http://example.com/api/GS?cid=44630&offset=+00.00&day=2

2018-05-07 (Monday)

http://example.com/api/GS?cid=44630&offset=+00.00&day=3

2018-05-08 (Tuesday)

http://example.com/api/GS?cid=44630&offset=+00.00&day=4

2018-05-09 (Wednesday)

http://example.com/api/GS?cid=44630&offset=+00.00&day=5

2018-05-10 (Thursday)

http://example.com/api/GS?cid=44630&offset=+00.00&day=6

 

 

Can you please show me an example how I can add the value up for each day that start from 0 to 6 when I am calling on get_show function??


Edited by mark107, 04 May 2018 - 03:00 PM.


#2 requinix

requinix
  • Administrators
  • Impoverished Administrator
  • 9,874 posts
  • LocationWA

Posted 04 May 2018 - 03:43 PM

Variables outside of functions are not available inside of functions.

Make $day be a function parameter just like the others. Increment it outside the function too, because doing it inside only affects the value the function has at that moment.
"Basically, I think the general rule of thumb is: if someone really wants the blood that's inside of your body, and they're like a vampire, or a dracula, or some sort of man-squito, then that's probably okay. A dracula and a man-squito are made for removing things like blood and swords from inside your body. That's basically fine. If something wants to get at your blood and they're, say, some kind of murdersaurus, or maybe a really big frog, that's where the problems start to arise. A really big frog is not made for removing blood, and your blood knows this, which is why it is so vehement about wanting to stay in your body instead of coming out. Unfortunately this will not deter a really big frog because a really big frog is full of things like prizes, and value, and quite a lot of hatred, and it would really rather like to replace any and all of those things with your blood, and basically by any means possible." --slumbermancer




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users