Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Warunek z tablicy
Forum PHP.pl > Forum > PHP
Largo
Witam,

Mam pewien problem dotyczący IP.Downloads. Otóż sam chcę dodać sprawdzanie dwóch rzeczy. Ile user musi mieć postów, aby pobrać ( domyślne ) albo ile Forum Gold ( dodatkowe ). Dodałem w bazie wszystko dobrze, ale schody zaczynają się przy sprawdzaniu warunków. Oto kod:
  1. <?php
  2. foreach( $my_restrictions as $k => $v )
  3.        {
  4.            if( $v > 0 )
  5.            {
  6.                if( array_key_exists( 'min_posts', $my_restrictions ) AND array_key_exists( 'min_fg', $my_restrictions ) )
  7.                {                                        
  8.                    if( ( $this->ipsclass->member['posts'] < $v ) OR ( $this->ipsclass->member['fg'] < $v ) )
  9.                        {
  10.                            $this->produce_error( 'dl_restrict_min_posts' );
  11.                            var_dump($my_restrictions);
  12.                            $this->restricted = 1;
  13.                            return;
  14.                        }
  15.                }
  16. ?>


Zwracana wartość z tablicy $my_restrictions:

  1. <?php
  2. array(4) { ["enabled"]=>  int(1) ["min_posts"]=>  int(40) ["min_fg"]=>  int(20) ["posts_per_dl"]=>  int(0) }
  3. ?>


Wg. mnie jest ona poprawna. Pierwszy klucz jest OK, bo sprawdza całą resztę iteracji. A następny? W czym problem? Każde var_dump na wartości ( $v ) daje int(1). Minimalna ilość postów i FG się zgadza tą w bazie. Ilość postów i FG w bazie usera też się zgadza ( sprawdzałem ), typ się tylko zmienia z int na string. Ale to chyba nie ma większego znaczenia. Dlaczego $v ( wartość ) nie dopasowuje się?

PS. Przepraszam, po iteracji następnej pokazuje int(40), czyli postęp. Jak potem bez iteracji przejść do następnej wartości?

Problem? Jak przejść do następnej wartości? Bo wg. mnie on sprawdza aktualną wartość. Funkcja next() nie działa.

Pozdrawiam,
Largo
ziqzaq
Przyznam, że nie rozumiem po co iterujesz po tablicy $my_restrictions.
Nie wystarczy ci poprostu sprawdzić odpowiednie wartości przy odpowiednich kluczach (min_posts, min_fg)?
Wydaje mi się że chodzi ci o coś takiego (głowy nie dam tongue.gif):
  1. <?php
  2. // Jeśli w tablicy mamy min_posts i min_fg to sprawdzamy
  3. // (tutaj uwaga: a jak ich nie będzie to ktoś może sobie pobrać?)
  4. if( array_key_exists( 'min_posts', $my_restrictions ) AND array_key_exists( 'min_fg', $my_restrictions ) )
  5. {
  6.    if( ( $this->ipsclass->member['posts'] < $my_restrictions['min_posts'] ) OR ( $this->ipsclass->member['fg'] < $my_restrictions['min_fg'] ) )
  7.    {
  8.         $this->produce_error( 'dl_restrict_min_posts' );
  9.         var_dump($my_restrictions);
  10.         $this->restricted = 1;
  11.         return;
  12.     }
  13. }
  14. ?>


Edit:
Acha i jeszcze może warunek bo widzę, że to może być potrzebne:
  1. <?php
  2. if( array_key_exists( 'min_posts', $my_restrictions ) AND array_key_exists( 'min_fg', $my_restrictions ) )
  3. {
  4.    // Zgaduje ze jesli ktoras z wartosci jest rowna 0 to restrykcje nie obowiazuja
  5.    if ( $my_restrictions['min_posts'] > 0 AND $my_restrictions['min_fg'] > 0 )
  6.    {
  7.     ....
  8. ?>


Edit2: literówka
Largo
Witaj,

Niestety, nie tędy droga, wartość zwrócona -> NULL. Sprawdzanie czy restrykcja istnieje jest dużo wcześniej, ale skoro tak mówisz, to może błędem było nie nie udostępnienie całej funkcji. Oto i ona i iteracja musi się odbyć. Myślę nad magicznym słowem continue :-)

  1. <?php
  2. function sort_restrictions()
  3.    {
  4.        $my_groups = array( $this->ipsclass->member['mgroup'] );
  5.        
  6.        if( $this->ipsclass->member['mgroup_others'] )
  7.        {
  8.            $other_mgroups = explode( ",", $this->ipsclass->clean_perm_string( $this->ipsclass->member['mgroup_others'] ) );
  9.            
  10.            $my_groups = array_merge( $my_groups, $other_mgroups );
  11.        }
  12.        
  13.        $can_download = 1;
  14.        
  15.        // First, loop through groups and determine what restrictions are placed on member (better overrides worse)
  16.        // Then, loop through the restrictions and see if they're blocked
  17.        // If blocked, set can_download to 0, break loop, and show error
  18.        
  19.        $my_restrictions     = array();
  20.        
  21.        $less_is_more        = array( 'min_posts', 'min_fg', 'posts_per_dl' ); // Tu dodaję min_fg też
  22.        
  23.        foreach( $my_groups as $gid )
  24.        {
  25.            $group = $this->ipsclass->cache['group_cache'][ $gid ];
  26.              
  27.            $this_restrictions = array();
  28.            $this_restrictions = unserialize( $group['idm_restrictions'] );
  29.            
  30.            if( is_array( $this_restrictions ) AND count( $this_restrictions ) )
  31.            {
  32.                if( $this_restrictions['enabled'] == 1 )
  33.                {
  34.                    foreach( $this_restrictions as $k => $v )
  35.                    {
  36.                        if( isset($my_restrictions[$k]) AND $my_restrictions[$k] == 0 )
  37.                        {
  38.                            // Zero is always best - it means no restriction
  39.                            continue;
  40.                        }
  41.                        else if( in_array( $k, $less_is_more ) )
  42.                        {
  43.                            // Lower the better for post-based restrictions
  44.                            
  45.                            if( isset( $my_restrictions[$k] ) )
  46.                            {
  47.                                if( $v < $my_restrictions[$k] )
  48.                                {
  49.                                    $my_restrictions[$k] = $v; // Widzisz jak wygląda pętla foreach? :-)
  50.                                }
  51.                            }
  52.                            else
  53.                            {
  54.                                $my_restrictions[$k] = $v;
  55.                            }
  56.                        }
  57.                        else
  58.                        {
  59.                            // Higher the better for bw/dl restrictions
  60.                            
  61.                            if( $v > intval($my_restrictions[$k]) )
  62.                            {
  63.                                $my_restrictions[$k] = $v;
  64.                            }
  65.                        }
  66.                    }
  67.                }
  68.            }
  69.        }
  70.        
  71.        // Now we should have this member's restrictions in place.
  72.        // Let's check...if all are 0, go ahead and return now
  73.        
  74.        if( !is_array($my_restrictions) OR !count($my_restrictions) )
  75.        {
  76.            // No restrictions
  77.            return;
  78.        }
  79.        else
  80.        {
  81.            $at_least_one = 0;
  82.            
  83.            foreach( $my_restrictions as $k => $v )
  84.            {
  85.                if( $v > 0 )
  86.                {
  87.                    $at_least_one = 1;
  88.                    break;
  89.                }
  90.            }
  91.            
  92.            if( $at_least_one == 0 )
  93.            {
  94.                // All restrictions disabled
  95.                return;
  96.            }
  97.        }
  98.        
  99.        // Still here?  Ok, check restrictions
  100.        
  101.        // Before we loop, let's get the counts we'll need (easier to do this in three queries)
  102.        
  103.        // If this is a guest, check IP too
  104.        
  105.        $ip_check    = '';
  106.        
  107.        if( !$this->ipsclass->member['id'] )
  108.        {
  109.            $ip_check = " AND dip='{$this->ipsclass->ip_address}'";
  110.        }
  111.        
  112.        $one_day    = time() - 86400;
  113.        $daily         = $this->ipsclass->DB->build_and_exec_query( array( 'select' => 'COUNT(*) as dl, SUM(dsize) as bw', 'from' => 'downloads_downloads', 'where' => 'dmid='.$this->ipsclass->member['id'].' AND dtime > '.$one_day . $ip_check ) );
  114.        
  115.        $one_week    = time() - 604800;
  116.        $weekly        = $this->ipsclass->DB->build_and_exec_query( array( 'select' => 'COUNT(*) as dl, SUM(dsize) as bw', 'from' => 'downloads_downloads', 'where' => 'dmid='.$this->ipsclass->member['id'].' AND dtime > '.$one_week . $ip_check ) );
  117.        
  118.        $one_month    = time() - 2592000;
  119.        $monthly    = $this->ipsclass->DB->build_and_exec_query( array( 'select' => 'COUNT(*) as dl, SUM(dsize) as bw', 'from' => 'downloads_downloads', 'where' => 'dmid='.$this->ipsclass->member['id'].' AND dtime > '.$one_month . $ip_check ) );
  120.        
  121.        foreach( $my_restrictions as $k => $v )
  122.        {
  123.            if( $v > 0 ) // Perwsza iteracja zwraca 1, prawidłowo
  124.            {
  125.                if( array_key_exists( 'min_posts', $my_restrictions ) AND array_key_exists( 'min_fg', $my_restrictions ) ) // Jak tutaj dać, aby w tej iteracji dał następny element czyli wartość ( $v ) min_fg? Próbować z continue?
  126.                {                                            
  127.                    if( $this->ipsclass->member['posts'] < $my_restrictions['min_posts'] OR $this->ipsclass->member['fg'] < $my_restrictions['min_fg'] )
  128.                        {
  129.                            $this->produce_error( 'dl_restrict_min_posts' );
  130.                                    var_dump($current_value);
  131.                            $this->restricted = 1;
  132.                            return;
  133.                        }
  134.                }
  135.  
  136.                if( $k == 'posts_per_dl' )
  137.                {
  138.                    // Get last download stamp
  139.                    
  140.                    $download = $this->ipsclass->DB->build_and_exec_query( array( 'select' => 'MAX(dtime) as dtime', 'from' => 'downloads_downloads', 'where' => 'dmid='.$this->ipsclass->member['id'] . $ip_check ) );
  141.                    
  142.                    if( $download['dtime'] )
  143.                    {
  144.                        $posts = $this->ipsclass->DB->build_and_exec_query( array( 'select' => 'COUNT(*) as num', 'from' => 'posts', 'where' => 'author_id='.$this->ipsclass->member['id'].' AND post_date>'.$download['dtime'] ) );
  145.                        
  146.                        if( $posts['num'] < $v )
  147.                        {
  148.                            $this->produce_error( 'dl_restrict_posts_p_dl' );
  149.                            $this->restricted = 1;
  150.                            return;
  151.                        }
  152.                    }
  153.                }
  154.                
  155.                if( $k == 'daily_bw' AND $daily['bw'] )
  156.                {
  157.                    if( $daily['bw'] > ($v*1024) )
  158.                    {
  159.                        $this->produce_error( 'dl_restrict_daily_bw' );
  160.                        $this->restricted = 1;
  161.                        return;
  162.                    }
  163.                }
  164.                
  165.                if( $k == 'weekly_bw' AND $weekly['bw'] )
  166.                {
  167.                    if( $weekly['bw'] > ($v*1024) )
  168.                    {
  169.                        $this->produce_error( 'dl_restrict_weekly_bw' );
  170.                        $this->restricted = 1;
  171.                        return;
  172.                    }
  173.                }
  174.                
  175.                if( $k == 'monthly_bw' AND $monthly['bw'] )
  176.                {
  177.                    if( $monthly['bw'] > ($v*1024) )
  178.                    {
  179.                        $this->produce_error( 'dl_restrict_monthly_bw' );
  180.                        $this->restricted = 1;
  181.                        return;
  182.                    }
  183.                }
  184.                
  185.                if( $k == 'daily_dl' AND $daily['dl'] )
  186.                {
  187.                    if( $daily['dl'] > $v )
  188.                    {
  189.                        $this->produce_error( 'dl_restrict_daily_dl' );
  190.                        $this->restricted = 1;
  191.                        return;
  192.                    }
  193.                }
  194.                
  195.                if( $k == 'weekly_dl' AND $weekly['dl'] )
  196.                {
  197.                    if( $weekly['dl'] > $v )
  198.                    {
  199.                        $this->produce_error( 'dl_restrict_weekly_dl' );
  200.                        $this->restricted = 1;
  201.                        return;
  202.                    }
  203.                }
  204.                
  205.                if( $k == 'monthly_dl' AND $monthly['dl'] )
  206.                {
  207.                    if( $monthly['dl'] > $v )
  208.                    {
  209.                        $this->produce_error( 'dl_restrict_monthly_dl' );
  210.                        $this->restricted = 1;
  211.                        return;
  212.                    }
  213.                }
  214.                
  215.                if( $k == 'limit_sim' )
  216.                {
  217.                    $ip_extra = !$this->ipsclass->member['id'] ? " AND dsess_ip='{$this->ipsclass->ip_address}'" : '';
  218.                    
  219.                    $this->ipsclass->DB->build_query( array( 'select' => '*', 'from' => 'downloads_sessions', 'where' => "dsess_mid={$this->ipsclass->member['id']}{$ip_extra}" ) );
  220.                    $this->ipsclass->DB->exec_query();
  221.                    
  222.                    while( $r = $this->ipsclass->DB->fetch_row() )
  223.                    {
  224.                        $this->dl_sessions[] = $r;
  225.                    }
  226.  
  227.                    $sess_count = 0;
  228.                    
  229.                    if( count($this->dl_sessions) )
  230.                    {
  231.                        foreach( $this->dl_sessions as $session )
  232.                        {
  233.                            // If this is a request for the same file and the HTTP_RANGE header is sent don't count
  234.                            // It's probably a download manager.  If HTTP_RANGE isn't set, member is trying to download two copies simultaneously
  235.                            
  236.                            if( intval($this->ipsclass->input['id']) == $session['dsess_file'] AND $this->ipsclass->my_getenv('HTTP_RANGE') )
  237.                            {
  238.                                continue;
  239.                            }
  240.                            
  241.                            $sess_count++;
  242.                        }
  243.                    }
  244.                    
  245.                    if( $sess_count >= $v )
  246.                    {
  247.                        $this->produce_error( 'dl_restrict_sim' );
  248.                        $this->restricted = 1;
  249.                        return;
  250.                    }
  251.                }
  252.            }
  253.        }
  254.    }
  255. ?>


Opisałem kod co i jak. Liczę dalj na porady, a ja próbuję z continue i zamknąć min_fg w funkcji.

Pozdrawiam,
Largo
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.