Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 29
  1. #1
    Senior Coder
    Join Date
    Jul 2003
    Location
    My pimped-out igloo in Canadia
    Posts
    1,966
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Trying to change the value of a constant based on screen resolution

    Hi

    My knowledge of PHP is infantile, however I believe that What I'm looking to do is not too difficult

    I'm trying to modify this script:
    PHP Code:
    <?php

    define
    ('VL_WALLPAPER_DIR''wallpapers/');
    define('VL_THUMBNAIL_DIR'VL_WALLPAPER_DIR.'thumbs/');

    function 
    buildWallpaperArray() {
        
    $wallpapers=array();
        
    $wallpapers'tiled' ] = array();
        
    $d=dirname(__FILE__).'/'.VL_WALLPAPER_DIR;
        
    $dir opendir($d);
         while (
    $f readdir($dir)) { 
          
    $matches null;
          if (
    eregi("(.*)\.jpg",$f,$matches)) {
              if( 
    file_existsdirname(__FILE__).'/'.VL_THUMBNAIL_DIR $f ) ) {
               
    $wallpapers'tiled' ][$matches[1]]->wallpaper VL_WALLPAPER_DIR $f;
               
    $wallpapers'tiled' ][$matches[1]]->thumbnail VL_THUMBNAIL_DIR $f;
              }
              else {
                  
    // handle automatic thumbnailing
              
    }
          }
        }
        
    closedir($dir);
        
        
    $wallpapers'bottom-left' ] = array();
        
    $d=dirname(__FILE__).'/'.VL_WALLPAPER_DIR.'bottom-left/';
        
    $dir opendir($d);
         while (
    $f readdir($dir)) { 
          
    $matches null;
          if (
    eregi("(.*)\.jpg",$f,$matches)) { 
              if( 
    file_existsdirname(__FILE__).'/'.VL_THUMBNAIL_DIR $f ) ) {
                   
    $wallpapers'bottom-left' ][$matches[1]]->wallpaper VL_WALLPAPER_DIR 'bottom-left/'$f
                   
    $wallpapers'bottom-left' ][$matches[1]]->thumbnail VL_THUMBNAIL_DIR $f
              }
          }
        }
        return 
    $wallpapers;
    }    

    $vl_wallpapers buildWallpaperArray();

    function 
    switch_wallpaper() {
        global 
    $vl_wallpapers;
        
    $i 0;
        foreach(
    $vl_wallpapers as $pos => $wallpapers ) {
            foreach( 
    $wallpapers as $key => $wallpaper ) {
                if( isset( 
    $_GET'thumbnail'.$i ] ) ) {
                    
    session_start();
                    
    $_SESSION'vl_wallpaper'] = $_GET'thumbnail'.$i ];
                    
    setcookie('vl_wallpaper'$_GET'thumbnail'.$i ], time() + 7000 24 60 60"/");
                    
    session_write_close();
                    
    $url remove_query_arg('thumbnail'.$i$_SERVER'REQUEST_URI'] );            
                    
    wp_redirect$url );
                    exit;
                }
                ++
    $i;
            }        
        }
        if( isset( 
    $_COOKIE'vl_wallpaper' ] ) ) {
            
    session_start();
            
    $_SESSION'vl_wallpaper'] = $_COOKIE'vl_wallpaper' ];
            
    session_write_close();
        }
    }
    if( 
    function_exists('add_action') ) {
        
    add_action'init''switch_wallpaper' );
    }

    function 
    spitOutWallpaperThumbs() {
        global 
    $vl_wallpapers;
        global 
    $vl_thumbpos;
        
    $countwallpapers '0';
        
        
    ?><div id="thumbs" class="<?php
        
    if( $vl_thumbpos == "right" )
            echo 
    'thumbright ';
        else if( 
    $vl_thumbpos == "sidebar" )
            echo 
    'sidebar ';
        else
            echo 
    'thumbleft ';
        if( 
    vl_get_theme_option('framedthumbs') == 'image' )
            echo 
    ' framed';
        
    ?>"><?php
        
    foreach($vl_wallpapers as $pos => $wallpapers ) {
            foreach( 
    $wallpapers as $key => $wallpaper ) {
                
    $id $countwallpapers;
                
    $title basename$wallpaper->thumbnail".jpg" );
                
    $src $wallpaper->thumbnail;
                
    vl_display_thumbnail$id$title$src );
                
    $countwallpapers++;
            }
        }
        if( 
    vl_get_theme_option'randomthumb' ) == "show" ) {
            
    $id "-1";
            
    $title 'Random';
            
    $src 'images/random.png';
            
    vl_display_thumbnail$id$title$src );
        }
        
        
    ?></div><?php
        ?>
    <script type="text/javascript"><?php
        ?>
    document.getElementById("thumbs").style.display = "block";<?php
        ?>
    </script><?php

    }

    function 
    vl_display_thumbnail$id$title$src ) {
    ?><a href="#" <?php
        ?>
    onclick="changebackground(this, '<?php echo $id?>'); return false;" <?php
        ?>
    onkeypress="if( event.keyCode == 13 ) { changebackground(this, '<?php echo $id?>'); return false; }" <?php
        ?>
    ><img id="thumbnail<?php echo $id?>" src="<?php echo get_stylesheet_directory_uri() . '/' $src?><?php
        ?>
    alt="<?php echo $title?><?php
        
    if( wallpaper_selection() == $id ) { 
            
    ?>class="selected"<?php
        
    }
        
    ?>/></a><?php    
    }

    ?>
    so that the variable define('VL_WALLPAPER_DIR', 'wallpapers/'); is not hardcoded as it is currently, but set by this script:
    Code:
    <script type="text/javascript">
    var wallpaperLocation;
    
    if (screen.width == 1600)
    wallpaperLocation = 'wallpapers1600/';
    else if (screen.width == 1280)
    wallpaperLocation = 'wallpapers1280/';
    else if (screen.width == 1152)
    wallpaperLocation = 'wallpapers1152/';
    else if (screen.width == 1024)
    wallpaperLocation = 'wallpapers1024/';
    else if (screen.width == 800)
    wallpaperLocation = 'wallpapers800/';
    </script>
    (the setting of the variables there may not be syntactically correct, but i tried my best :/

    Essentially I want to be able to have the script that switches backgrounds onclick (above) choose backgrounds from a specific directory based on what the users screen resolution is.

    How hard would it be to integrate the mini script into the bigger one?
    (p.s: if you need the rest of the script, let me know -- there's more but i figured that was the relevant part)

    Thanks a lot
    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, you're a mile away and you have their shoes :)

  • #2
    Senior Coder
    Join Date
    Jul 2003
    Location
    My pimped-out igloo in Canadia
    Posts
    1,966
    Thanks
    36
    Thanked 0 Times in 0 Posts
    would it be something like this?
    PHP Code:
    <?php

    if (screen.width == 1600)
    define('VL_WALLPAPER_DIR''wallpapers1600/');
    else if (
    screen.width == 1280)
    define('VL_WALLPAPER_DIR''wallpapers1280/');
    else if (
    screen.width == 1152)
    define('VL_WALLPAPER_DIR''wallpapers1152/');
    else if (
    screen.width == 1024)
    define('VL_WALLPAPER_DIR''wallpapers1024/');
    else if (
    screen.width == 800)
    define('VL_WALLPAPER_DIR''wallpapers800/');

    define('VL_THUMBNAIL_DIR'VL_WALLPAPER_DIR.'thumbs/');

    function 
    buildWallpaperArray() {
        
    $wallpapers=array();
        
    $wallpapers'tiled' ] = array();
        
    $d=dirname(__FILE__).'/'.VL_WALLPAPER_DIR;
        
    $dir opendir($d);
         while (
    $f readdir($dir)) { 
          
    $matches null;
          if (
    eregi("(.*)\.jpg",$f,$matches)) {
              if( 
    file_existsdirname(__FILE__).'/'.VL_THUMBNAIL_DIR $f ) ) {
               
    $wallpapers'tiled' ][$matches[1]]->wallpaper VL_WALLPAPER_DIR $f;
               
    $wallpapers'tiled' ][$matches[1]]->thumbnail VL_THUMBNAIL_DIR $f;
              }
              else {
                  
    // handle automatic thumbnailing
              
    }
          }
        }
        
    closedir($dir);
        
        
    $wallpapers'bottom-left' ] = array();
        
    $d=dirname(__FILE__).'/'.VL_WALLPAPER_DIR.'bottom-left/';
        
    $dir opendir($d);
         while (
    $f readdir($dir)) { 
          
    $matches null;
          if (
    eregi("(.*)\.jpg",$f,$matches)) { 
              if( 
    file_existsdirname(__FILE__).'/'.VL_THUMBNAIL_DIR $f ) ) {
                   
    $wallpapers'bottom-left' ][$matches[1]]->wallpaper VL_WALLPAPER_DIR 'bottom-left/'$f
                   
    $wallpapers'bottom-left' ][$matches[1]]->thumbnail VL_THUMBNAIL_DIR $f
              }
          }
        }
        return 
    $wallpapers;
    }    

    $vl_wallpapers buildWallpaperArray();

    function 
    switch_wallpaper() {
        global 
    $vl_wallpapers;
        
    $i 0;
        foreach(
    $vl_wallpapers as $pos => $wallpapers ) {
            foreach( 
    $wallpapers as $key => $wallpaper ) {
                if( isset( 
    $_GET'thumbnail'.$i ] ) ) {
                    
    session_start();
                    
    $_SESSION'vl_wallpaper'] = $_GET'thumbnail'.$i ];
                    
    setcookie('vl_wallpaper'$_GET'thumbnail'.$i ], time() + 7000 24 60 60"/");
                    
    session_write_close();
                    
    $url remove_query_arg('thumbnail'.$i$_SERVER'REQUEST_URI'] );            
                    
    wp_redirect$url );
                    exit;
                }
                ++
    $i;
            }        
        }
        if( isset( 
    $_COOKIE'vl_wallpaper' ] ) ) {
            
    session_start();
            
    $_SESSION'vl_wallpaper'] = $_COOKIE'vl_wallpaper' ];
            
    session_write_close();
        }
    }
    if( 
    function_exists('add_action') ) {
        
    add_action'init''switch_wallpaper' );
    }

    function 
    spitOutWallpaperThumbs() {
        global 
    $vl_wallpapers;
        global 
    $vl_thumbpos;
        
    $countwallpapers '0';
        
        
    ?><div id="thumbs" class="<?php
        
    if( $vl_thumbpos == "right" )
            echo 
    'thumbright ';
        else if( 
    $vl_thumbpos == "sidebar" )
            echo 
    'sidebar ';
        else
            echo 
    'thumbleft ';
        if( 
    vl_get_theme_option('framedthumbs') == 'image' )
            echo 
    ' framed';
        
    ?>"><?php
        
    foreach($vl_wallpapers as $pos => $wallpapers ) {
            foreach( 
    $wallpapers as $key => $wallpaper ) {
                
    $id $countwallpapers;
                
    $title basename$wallpaper->thumbnail".jpg" );
                
    $src $wallpaper->thumbnail;
                
    vl_display_thumbnail$id$title$src );
                
    $countwallpapers++;
            }
        }
        if( 
    vl_get_theme_option'randomthumb' ) == "show" ) {
            
    $id "-1";
            
    $title 'Random';
            
    $src 'images/random.png';
            
    vl_display_thumbnail$id$title$src );
        }
        
        
    ?></div><?php
        ?>
    <script type="text/javascript"><?php
        ?>
    document.getElementById("thumbs").style.display = "block";<?php
        ?>
    </script><?php

    }

    function 
    vl_display_thumbnail$id$title$src ) {
    ?><a href="#" <?php
        ?>
    onclick="changebackground(this, '<?php echo $id?>'); return false;" <?php
        ?>
    onkeypress="if( event.keyCode == 13 ) { changebackground(this, '<?php echo $id?>'); return false; }" <?php
        ?>
    ><img id="thumbnail<?php echo $id?>" src="<?php echo get_stylesheet_directory_uri() . '/' $src?><?php
        ?>
    alt="<?php echo $title?><?php
        
    if( wallpaper_selection() == $id ) { 
            
    ?>class="selected"<?php
        
    }
        
    ?>/></a><?php    
    }

    ?>
    Just a guess, lol
    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, you're a mile away and you have their shoes :)

  • #3
    Regular Coder
    Join Date
    Mar 2008
    Posts
    103
    Thanks
    1
    Thanked 8 Times in 8 Posts
    the code above would fail because php can NOT detect browser screen resolution(side effect of server side) but why can you detect browsers then? that is because the information is sent to the with the request to the server while resolution is not. screen.width wll have no effect in php.

  • #4
    Senior Coder
    Join Date
    Jul 2003
    Location
    My pimped-out igloo in Canadia
    Posts
    1,966
    Thanks
    36
    Thanked 0 Times in 0 Posts
    Good to know

    so okay, then can I get the screen resolution using JS and somehow have the PHP script read the variable set in the JS and use its value to set the proper directory?

    I know JS and PHP work together because thats how this script functions... so i presume that this is achievable, no?
    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, you're a mile away and you have their shoes :)

  • #5
    Regular Coder
    Join Date
    Mar 2008
    Posts
    103
    Thanks
    1
    Thanked 8 Times in 8 Posts
    you could try to use ajax.....get screen size then....send it back to php page to get content...though it would be slow and possibly depending on how you code it, server intense.....

  • #6
    Senior Coder
    Join Date
    Jul 2003
    Location
    My pimped-out igloo in Canadia
    Posts
    1,966
    Thanks
    36
    Thanked 0 Times in 0 Posts
    so then this is impossible?
    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, you're a mile away and you have their shoes :)

  • #7
    Regular Coder
    Join Date
    Mar 2008
    Posts
    103
    Thanks
    1
    Thanked 8 Times in 8 Posts
    I didn't say that either, I would have to say that the best way to go would be JAVASCRIPT redirects and then in noscript tags put links to all the resolutions. that would be easiest way to do it

  • #8
    Senior Coder
    Join Date
    Jul 2003
    Location
    My pimped-out igloo in Canadia
    Posts
    1,966
    Thanks
    36
    Thanked 0 Times in 0 Posts


    Okay so detecting the resolution via JS is the way to go... but then what do i do with the background script I currently have going? Is there no way to set a PHP variable externally? (i.e read from another variable)
    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, you're a mile away and you have their shoes :)

  • #9
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by canadianjameson View Post


    Okay so detecting the resolution via JS is the way to go... but then what do i do with the background script I currently have going? Is there no way to set a PHP variable externally? (i.e read from another variable)
    what about sending as parameters, using js, as GET variable?

    PS: I apologize if I misunderstand the problem.

    regards

  • #10
    Senior Coder
    Join Date
    Jul 2003
    Location
    My pimped-out igloo in Canadia
    Posts
    1,966
    Thanks
    36
    Thanked 0 Times in 0 Posts
    sweet! How do i do that? lol

    Sorry man, my coding abilities are really bad :/

    I'll go do a little research but i'm going to need some guidance

    edit: alright, I think I see how I can get the value using JS.... but how do i then pass it to the PHP script to modify the variable in question?
    Last edited by canadianjameson; 07-18-2008 at 05:32 PM.
    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, you're a mile away and you have their shoes :)

  • #11
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by canadianjameson View Post
    sweet! How do i do that? lol

    Sorry man, my coding abilities are really bad :/

    I'll go do a little research but i'm going to need some guidance

    edit: alright, I think I see how I can get the value using JS.... but how do i then pass it to the PHP script to modify the variable in question?
    reload and/or redirection to a url like mypage.php?w=1024&h=728

    not so sure how to do this in js, probably:
    Code:
    windows.location = 'http://...../mypage.php?w=1024&h=728';
    and in php you put:
    PHP Code:
    $w $_GET['w'];
    $h $_GET['h']; 
    regards

  • #12
    Senior Coder
    Join Date
    Jul 2003
    Location
    My pimped-out igloo in Canadia
    Posts
    1,966
    Thanks
    36
    Thanked 0 Times in 0 Posts
    This seems like a good approach but there is one snag (and maybe it's because i really don't know PHP at all).

    I'm using wordpress, so I don't know how much that hamstrings me in terms of selecting which page I redirect to because I cannot have multiple URLs for the same posts...

    is there any way to do this without affecting a change of the location?
    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, you're a mile away and you have their shoes :)

  • #13
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by canadianjameson View Post
    This seems like a good approach but there is one snag (and maybe it's because i really don't know PHP at all).

    I'm using wordpress, so I don't know how much that hamstrings me in terms of selecting which page I redirect to because I cannot have multiple URLs for the same posts...

    is there any way to do this without affecting a change of the location?
    because php is server side, any change is effective only if you make a new request to the server.

    don't know how this can be used in wordpress, because I don't know how wordpress work, , but:

    - you can use $_SERVER['PHP_SELF'] to to find what is the current page.
    - also $_SERVER['HTTP_REFERER'] to find where the request come from.

    http://www.php.net/manual/en/reserve...les.server.php

    regards

  • #14
    Senior Coder
    Join Date
    Jul 2003
    Location
    My pimped-out igloo in Canadia
    Posts
    1,966
    Thanks
    36
    Thanked 0 Times in 0 Posts
    Okay, Thanks -- I'm going to go and look through that link and try to figure this out; I'll be back in a few hours
    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, you're a mile away and you have their shoes :)

  • #15
    Senior Coder
    Join Date
    Jul 2003
    Location
    My pimped-out igloo in Canadia
    Posts
    1,966
    Thanks
    36
    Thanked 0 Times in 0 Posts
    Okay I think you're on to something here. Here's how I see it going down -- please correct me if my syntax or logic is flawed (it usually is, in fact I usually speak backwards and in tongues

    Alright, here's my setup: before wordpress is engaged we have a language selection page which provides us with the perfect opportunity here:

    I will have two buttons: English and French, coded as such (bottom)

    Code:
    <script type="text/javascript">
    if (screen.width >= 1600)
    	{
    		document.getElementById('english').href="http://www.mysite.com?w=1600";
    		document.getElementById('french').href="http://www.mysite.com?w=1600";
    	}
    else if (screen.width == 1280)
    	{
    		document.getElementById('english').href="http://www.mysite.com?w=1280";
    		document.getElementById('french').href="http://www.mysite.com?w=1280";
    	}
    
    else if (screen.width == 1152)
    	{
    		document.getElementById('english').href="http://www.mysite.com?w=1152";
    		document.getElementById('french').href="http://www.mysite.com?w=1152";
    	}
    
    else if (screen.width == 1024)
    	{
    		document.getElementById('english').href="http://www.mysite.com?w=1024";
    		document.getElementById('french').href="http://www.mysite.com?w=1024";
    	}
    
    else if (screen.width == 800)
    	{
    		document.getElementById('english').href="http://www.mysite.com?w=800";
    		document.getElementById('french').href="http://www.mysite.com?w=800";
    	}
    
    </script>
    
    <body>
    <a href="http://www.mysite.com" id="english">English</a>   |    <a href="http://www.mysite.com" id="french">French</a>
    now I think that using that script above, if my understanding of what you said is correct, I could simply do the following:

    PHP Code:
    <?php

    define
    ('VL_WALLPAPER_DIR'$_GET['w'];);
    how am I doing so far?
    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, you're a mile away and you have their shoes :)


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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