0

Hello everybody I'm having a big issue trying to solve this. I know that I have to use JavaScript but I just can't understand how to. This is my script:

<?php 
$posts = query_posts('order=DESC&cat=-2');
$cols = 4;
$filas = array_chunk($posts, $cols);
$columnas = array();
foreach( $filas as $row => $articulos ) {
foreach( $articulos as $ix => $articulo ) {
  $columnas[$ix] = isset($columnas[$ix]) ? $columnas[$ix] : array();
  $columnas[$ix][] = $articulo;
}
}
?>
<?php foreach($columnas as $posts):?> etc, etc ... <?php endforeach; ?>

Where $cols value will be changing according user's window screen. I.E. = If the window screen is (some value) then $cols = (some value)

I will really appreciate any help on this.

Cheers!

  • Well, use some Javascript to get the resolution, then send it via AJAX or as GET request to a PHP script, there store it in a session variable. And only afterwards you can use it in your script. – mario Apr 25 '13 at 18:49
  • I suggest setting a cookie via JS and then picking up the value of that cookie in PHP. Just set the cookie via JS as close to the top of your HTML document as possible, I'd place it just after your charset declaration if you use one. – Jasper Apr 25 '13 at 18:59
  • Might I also suggest using CSS. Media Queries are great for this but you can also float elements to support old browsers.' – Jasper Apr 25 '13 at 19:05

1 Answers1

0

You could do something like using an ajax script onload of page. This will make your site slower, but it should have the right effect:

HTML

<div class="container">
    <!-- Place columns here -->
</div>

jQuery

$(document).load(function(){
    var winWidth = $(window).width();
    $.post('phpscript', {width: winWidth}, function(data) {
        $('.container').html(data);
    });
});

PHP

<?PHP
    //Your PHP script to format your page in however many columns
    //Have your script here take the width of your page
    //Do do math to find how many columns
    //Query whatever you need to get those columns
    //Return the HTML
?>

This will allow you to return whatever column layout you want. Although, I'm sure it would just be easier to make some good CSS to make as many columns as needed.

Jasper
  • 74,169
  • 13
  • 144
  • 142
ntgCleaner
  • 5,444
  • 6
  • 38
  • 76
  • By `This will make your site slower` do you mean that creating an AJAX request will noticeably slow down the rendering of the site? If so, I doubt that it'll be noticeable. However if you're taking about the lag of the AJAX request then you're right, the user's latency will play a big role here. Good call on the CSS, that's the correct technology to use here. – Jasper Apr 25 '13 at 19:03
  • Yes, I mean the AJAX request will lag depending on user latency. Especially depending on how many objects you are pulling form the Database. And Thank you! – ntgCleaner Apr 25 '13 at 19:27