Loop through results differently each time

Thank you so so much

1 Like

I’m probably doing somethign wrong but I can’t get this to work. Whenever I try it I get each results displayed in every format.

This is what I’m using:

<?php for( $loop=1; $loop<=$ccols; $loop++ ) { ?>

<?php if( is_array( $GLOBALS['column'.$loop] ) ) { $ctr=1; foreach( $GLOBALS['column'.$loop] as $key=>$value ) { ?>
<?php if( $query_results-001 ) { require 'header.php'; } ?>
<?php if( $query_results-002 ) { require 'sub.php'; } ?>
<?php if( $query_results-003 ) { require 'sub3.php'; } ?>
<?php $ctr++; if( $ctr==4 ) { $ctr=1; } } } ?>

<?php } ?>

header.php is:

<div class="fbox2" style="position: relative;">
<?php if( $value['type']=="video" ) { ?>
<div class="fbox2-image">
<div class="player is-splash play-button"<?php if( !empty( $value['image'] ) ) { ?> style="background-image: url( /images/dynamic/<?php echo $value['image'] ?> );"<?php } ?>>
<source type="video/flash" src="mp4:<?php echo htmldisplay( $value['video'] ) ?>" />
<source type="video/mp4" src="<?php echo $stream_url_http ?>/<?php echo htmldisplay( $value['video'] ) ?>" />
<?php } elseif( !empty( $value['image'] ) ) { ?>
<div class="fbox2-image">
<a href="<?php echo htmldisplay( $value['link'] ) ?>"><img src="/images/<?php echo htmldisplay( $value['image'] ) ?>" alt="<?php echo htmldisplay( $value['title'] ) ?>" title="<?php echo htmldisplay( $value['title'] ) ?>" width="948" height="514" /></a>
<?php } ?> 

<div class="fbox2-text">

<?php if( !empty( $value['title'] ) ) { ?>
<p class="cat-title"><a href="<?php echo htmldisplay( $value['link'] ) ?>"><?php echo htmldisplay( $value['title'] ) ?></a></p>
<?php } ?>

<h4 style="font-weight: normal;">By <?php if( !empty( $value['author'] ) ) { echo $value['author']; } else { ?>Admin<?php } ?> on <span style="font-style: italic; font-weight: bold;"><?php echo date( "F j, Y h:i a",$value['date'] ) ?></span> <a href="/sendpage.php?url=<?php echo urlencode( "http://www.lamuscle.com/" ).$value['link'] ?>" rel="nofollow" style="text-decoration: underline; color: #FFF;">Email.</a></h4>
<a href="<?php echo htmldisplay( $value['link'] ) ?>" style="color: #FFF;"><div class="lounge_read_more">Read more &raquo;</div></a>

<p style="margin-top: 36px;"><?php echo htmldisplay( $value['subtitle'] ) ?></p>

and that works fine but what I get it result 1 displayed using the header.php file then using the sub.php file, then sub3.php I then get result 2 does the same and so on


Try this:

Working Demo

With your code you will have to insert the relevant if ($condition ) to include the correct file.
The $condition can be found in your magazine.php file.

Try this:

  for( $loop=1; $loop<=$ccols; $loop++ )
    if( is_array( $_GLOBALS['column'.$loop] ) )
      foreach( $GLOBALS['column'.$loop] as $key=>$value )

        if( 1===$loop ) { require 'header.php';}

            if( 2===$loop ) { require 'sub.php';}

            if( 3===$loop ) { require 'sub3.php';}

            if( $ctr==4 ) { $ctr=1; }

         }//endif $_GLOBALS

  }//endfor $loop

I’m really sorry but I tried that and it didn’t work at all. It just showed the first format for all of the images and that was it.

So <?php if( $query_results-001 ) { } >, <?php if( $query_results-002 ) { } > etc gets the different layouts but displays them for every results before going onto the next. And <?php if( 1===$loop ) { require 'header.php';} ?> displays all of the images but only in one format.

I’m really grateful to you helping with this. I’m willing to pay for your time/help but I’m just desperate to get it sorted.

Thank you again

Okay… let’s try this in a slightly cleaner fashion.

$pages = array('sub3.php','header.php','sub.php');
$ctr = 1;
$ptr = 1;
while (array_key_exists('column'.$ctr,$GLOBALS)) { //I really dislike using the globals array. May need to look at how you're filling the 'column' variables, stuff them in an array, it'll be easier.
   foreach($GLOBALS['column'.$ctr] AS $value) {
       require($pages[($ptr % 4)]);

EDIT: Okay, you’ve got an array inside the array of arrays…seems unnecessary, but ok.

I spotted an error in the script:

<?php if( $query_results-001 ) { require 'header.php'; } ?>

// should be 

<?php if(  $query_results_001 ) { require 'header.php'; } ?>

// where
//  $query_results_001  is a logical value 
//  $query_results -001 which is a logical value minus 001

[quote=“coding_noobie, post:25, topic:195046”]
I’m really grateful to you helping with this. [/quote]

It is impossible to supply a complete solution without data. May I suggest a link to the existing page and the PHP source code used to generate the page. It may then be possible to create a validated, simplified, solution with included files containing the different rows.

Payment - free and voluntary otherwise…

1 Like

Thank you so much, I’m so grateful to you for helping me with this. It now works perfectly, thanks to you!!

There was on thing I wasn’t sure about though. In order to get the correct number of articles I had to change the code slightly. I changed the require($pages[($ptr % 4)]); line so that it had 15 instead but I also had to add the first layout file twice as it skipped that and I didn’t know if there was a reason for that?

I’ve not got this:

<?php $pages = array('/header.php','/header.php','/medium.php','page_blocks/small_1.php','/small_2.php','/small_3.php','/medium_dark.php','/medium_light.php','/medium_left.php','/small_1.php','/small_2.php','/small_3.php','/small_1.php','/small_2.php','/small_last.php'); $ctr = 1; $ptr = 1; while (array_key_exists('column'.$ctr,$GLOBALS)) { //I really dislike using the globals array. May need to look at how you're filling the 'column' variables, stuff them in an array, it'll be easier. foreach($GLOBALS['column'.$ctr] AS $value) { require($pages[($ptr % 15)]); $ptr++; } $ctr++; } ?>

PHP arrays start at zero, try the following:

Try the following:

<?php $pages = array(
echo '<pre>'; print_r($pages); echo '</pre>'; // pre formats the output with linefeeds

// Output:

Is there any reason why the third item is missing a leading slash?

I am glad you managed to find a solution.

Thank you, I’ll try that.

I moved the files from one folder to another, I must have forgotten to remove the original folder name. Thanks for pointing it out.

It\s thanks to you that I was able to get this working!

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.