Loop through results differently each time

I wonder if somebody could help me please. I’ve been asked to change a pages appears to make it less formulaic. At the moment all of the information is taken from the database and ‘tilled’ across the page. I want to change the tiles so that I have six different rows. One with one result, another with two, one with three and so on.

I don’t know if this is possible or if it is where I would even start so would be grateful for any help at all please.

At the moment the code I’ve got for the current page is:

 <?php for( $loop=1; $loop<=$ccols; $loop++ ) { ?>
          <div style="width: 285px; float: left; margin: 0 <?php if( $loop==$ccols ) { echo "0"; } else { echo "30"; } ?>px 35px 0;">

            <?php if( is_array( $GLOBALS['column'.$loop] ) ) { $ctr=1; foreach( $GLOBALS['column'.$loop] as $key=>$value ) { ?>
              <div class="news" style="position: relative;">
                <?php if( $value['type']=="video" ) { ?>
                <div class="news-image">
                  <div class="player play-button"<?php if( !empty( $value['image'] ) ) { ?> style="background-image: url( /images/<?php echo $value['image'] ?> );"<?php } ?>>
                    <video>
                      <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'] ) ?>" />
                    </video>
                  </div>
                </div>
                <?php } elseif( !empty( $value['image'] ) ) { ?>
                <div class="news-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="285" height="160" /></a>
                </div>
                <?php } ?>
                  
                <div class="news-text" style="background: none; border: 0; position: relative; top: -30px;height: 160px; padding: 0;">

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

                  <?php if( !empty( $value['subtitle'] ) ) { ?>
                  <p><?php echo htmldisplay( $value['subtitle'] ) ?></p>
                  <?php } ?>


                  <?php if( is_array( $tagmap[$value['id']] ) ) { $cnt=0; ?>
                  <p>
                    Tagged:&nbsp;
                    <?php foreach( $tagmap[$value['id']] as $key2=>$value2 ) { ?>
                    <a href="<?php echo $value['section_link'] ?>/<?php echo htmldisplay( $value2 ) ?>"><?php echo htmldisplay( $value2 ) ?></a><?php if( $cnt<count( $tagmap[$value['id']] )-1 ) { ?>&nbsp;|&nbsp;<?php } ?>
                    <?php $cnt++; } ?>
                  </p>
                  <?php } ?>


                  <?php if( $value['type']=="news" || $value['type']=="article" || $value['type']=="video" ) { ?><h4 style="font-size: 12px; color: #FFF; margin-top: 5px;">Uploaded <?php echo date( "j F Y",$value['date'] ) ?></h4><?php } ?>

                  <?php if( $value['type']=="books" ) { ?>
                  <div style="color: #959595; vertical-align: middle;">
                    <?php if( $htmlver[$value['id']] ) { ?>
                    <a href="/books/<?php echo htmldisplay( $value['linkword'] ) ?>">Read</a><?php if( !empty( $value['pdf'] ) && !empty( $value['pdf_size'] ) ) { ?>&nbsp;|&nbsp;<?php } ?>
                    <?php } ?>

                    <?php if( !empty( $value['pdf'] ) && !empty( $value['pdf_size'] ) ) { ?>
                    <a href="/downlaods/<?php echo $value['pdf'] ?>" rel="nofollow"><img src="/images/pdf.gif" />&nbsp;Download PDF</a> (<?php echo htmldisplay( bytesize( $value['pdf_size'] ) ) ?>)
                    <?php } ?>
                  </div>
                  <?php } ?>
                </div>
              </div>
            <?php $ctr++; if( $ctr==4 ) { $ctr=1; } } } ?>

          </div>
          <?php } ?>

Make a procedurally generated page less formulaic. Well. You take on the easy jobs, don’t ya? :stuck_out_tongue:

“I have 6 different rows” “one with one result, another with two…”

So… you have 6 rows. Of lengths 1-6 (21 results total)? In sequence? Randomized? Randomized results? Randomized rows? Pull a bunny out of the hat?

You can make the code do anything you want it to. First you have to figure out what it is you want it to do.

Hehehe :smile:

Ideally I’d like the structure to be like this:

one main
one medium horizontal (image left, text right)
three small
two medium vertical (both with different coloured backgrounds)
one medium horizontal (image right, text left)
three small
three small

There are probably hundreds of results in total but should be 14 on each page

Thanks for helping with this

well for starters, you just listed 7 items for your 6 rows.

If your LAYOUT is the same, but the RESULTS need to be randomized, pull all the results (I assume you’re getting this info from a DB), [FPHP]shuffle[/FPHP] the array, and then stick whatever got put into array slot 1 as the first thing, the next into the next, etc.

If the LAYOUT is to be randomized, you’re going to need parameters for what you can put where, as sticking vertical things in with horizontal things at random gets messy.

The layout will stay the same and the results will be ordered by the date they were uploaded. I don’t know if that make any difference.

Also I wasn’t sure what you meant about ‘you just listed 7 items for your 6 row’. Sorry I think my brain’s over heating!! I counted 14 items, 1 on the first row, 1 on the second, 3 on the third, 2 on the fourth, 1 on the fifth then 3 on the sixth and agin on the seventh

Okay… then if the layout is static, and the results are in a predefined order… what is it exactly you need help with?

How do I actually get them into that layout? At the moment I’ve got every single result looking the same. I’m pretty new to php so don’t know how to do this but essentially the results are just being looped through and put into the same layout, but I don’t know how to change the code so that it varies how the results look depending on what line they’re on if that makes sense.

Thank you for helping me with this

Design the entire page layout first. (use placeholders for the data if necessary) Then put the data into the layout; Rather than trying to change the layout as you process the data.

Sorry to ask such a dumb question but that’s the part I’m having problems with. How do I actually get the data into each layout style? I’ve used divs for each section, but don’t know how to dynamically populate it.

I can output the results as an array using this:

<?php
$i = 1;
do {
  if (isset($GLOBALS['column' . $i])) {
    $continue = true;
    print_r($GLOBALS['column' . $i]);
  }
  else {
    $continue = false;
  }
  $i++;
} while ($continue);

?>

But don’t know how to then get that into each div

Try this:

  require 'different_function_layouts.php'; // to be written

  $i = 1;
  do {
    if (isset($GLOBALS['column' . $i])) {
      $continue = true;
      # print_r($GLOBALS['column' . $i]);

      switch( $i )
      {
        case 1: // one medium horizontal (image left, text right)
          fn_div_1($GLOBALS, $i);
        break; 

        case 2: // three small
          fn_div_2($GLOBALS, $i);
        break; 

        case 3: // two medium vertical (both with different coloured backgrounds)
          fn_div_3($GLOBALS, $i);
        break; 

        case 4: // one medium horizontal (image right, text left)
          fn_div_4($GLOBALS, $i);
        break; 

        case 5: // three small
          fn_div_5($GLOBALS, $i);
        break; 

        case 6: // three small
          fn_div_5($GLOBALS, $i);
        break; 
      }// endswitch

    }else{
      $continue = false;
    }
    $i++;

  } while ($continue);

Thank you, I’m guessing the 'different_function_layouts.php file is the file that holds the actual layout?

Also how would I actually write the function for each of them so that it renders the style?

1 Like

Sample:

file: ‘different_function_layouts.php’;

function fn_div_XXX ($GLOBALS, $i)
{
  if( is_array( $tagmap[$value['id']] ) ) { 
    $cnt=0;
    echo '<p>Tagged:&nbsp;';
      foreach( $tagmap[$value['id']] as $key2=>$value2 ) {
        echo '<a href="' .$value['section_link'] .'/' .htmldisplay( $value2 ) .'">';
          echo htmldisplay( $value2 );
        echo '</a>';

        if( $cnt<count( $tagmap[$value['id']] )-1 ) {
          echo '&nbsp;|&nbsp;';
        }

        $cnt++; 
      }//endforeach
    echo '</p>';
  }//endif
}//endfunc

Thank you so much!

1 Like

I’m really sorry to be a pain but I tried that and it didn’t work - I think because there’s only one column (with 14 results in it). Is it possible to do something like this:

get all results, then for first row show just 1st item. Then second row show 2nd result, for third row show 3rd, 4th and 5th, fourth row show 6th and 7th etc?

I’m really grateful for you helping with this.

Can you supply more information of the source data and a link to the current results page?

Thank you so much, I can’t give a link as it’s on a private network while it’s in development but I’ve attached the file anyway. I’ve put the new style in one div and the current/old style in another.

Thank youmagazine.php (29.1 KB)

Many thanks for the data. It is very late here so will have a look tomorrow.

Meanwhile, maybe another member could help?

I have had a look and it is not a simple task.

I would recommend:

  1. extracting individual DIV blocks into separate BLOCK-XXX.php files
  2. remming each require ‘BLOCK-XXX.php’
  3. validating the remaining page and remove all errors
  4. un-rem each BLOCK-XXX.php, validating, etc
  5. continue until all BLOCK-XXX.php are un-remmed and no validation errors

There are validation errors in your script, too many </p>

Use https://validator.w3.org/ to validate your script

Once all errors are removed then as @StarLion mentioned, create your new page template and introduce the BLOCK-XXX.php one at a time.

Sample:

<div style="display: block;" class="current">
  <?php for( $loop=1; $loop<=$ccols; $loop++ ) { ?>

  <!--  SPECIAL NOTE - 
           ADD A SPACE before and after  < ?php  .... ? >  TO PREVENT ACTIVATING PHP 
          <div style="width: 285px; float: left; margin: 0 
              < ?php if( $loop==$ccols ) { echo "0"; } else { echo "30"; } ? >
         px 35px 0;"> 
   -->

    <?php if( is_array( $GLOBALS['column'.$loop] ) ) { $ctr=1; foreach( $GLOBALS['column'.$loop] as $key=>$value ) { ?>
       <?php require 'BOCK-001.php'; ?>

        <?php } elseif( !empty( $value['image'] ) ) { ?>
          <?php require 'BOCK-002.php'; ?>
        <?php } ?> 
          
        <div class="fbox2-text">

          <?php if( !empty( $value['title'] ) ) { ?>
            <?php require 'BOCK-003.php'; ?>
          <?php } ?>

          <?php if( is_array( $tagmap[$value['id']] ) ) { $cnt=0; ?>
              <?php require 'BOCK-004.php'; ?>
          <?php } ?>


It may seem quite a lot of work but it will be far easier to debug and to make radical changes.

Best of luck :slight_smile:

Thank you for having a look at it for me, I’m really grateful.

Oe thing I don’t understand though - using the example you gave above would I still be able to change it so that it gave me the different rows I’m after?

@coding_noobie coding_noobie1h #19

Yes.

Basically in your 500 page script, the first 200 lines extract information from your database tables into five arrays.

The remaining 300 lines display your web-page including the results from the queries.

I suggest the following layout which will make future changes and modifications a lot simpler:

<?php
  require 'results-from-database-tables.php'; 

  require 'header.php';
?>
<body>

  <div 'logo'>
    <?php require 'logo.php'; ?>
  </div>

  <div 'top-banner'>
    <?php require 'top-banner.php'; ?>
  </div>


  <div 'main-content'><!-- ====================== -->

    <div 'row_results_001'>
      <?php
        if( $query_results-001 ) { 
          require 'results_001.php';
        } 
      ?>
    </div>

    <div 'row_results_002'>
      <?php
        if( $query_results-002 ) { 
          require 'results_002.php';
        } 
      ?>
    </div>

    <div 'row_results_003'>
      <?php
        if( $query_results-003 ) { 
          require 'results_003.php';
        } 
      ?>
    </div>

    <div 'row_results_004'>
      <?php
        if( $query_results-004 ) { 
          require 'results_004.php';
        } 
      ?>
    </div>

    <div 'row_results_005'>
      <?php
        if( $query_results-005 ) { 
          require 'results_005.php';
        } 
      ?>
    </div>

  </div><!-- 'main-content' ====================== -->


  <div 'footer'>
    <?php require 'footer.php'; ?>
  </div>

</body></html>

As you can see the row order can be changed and remming results_XXX.php which are not required will make debugging easier. Once the the selected result_XXX.php is doing exactly what your want it to do then move on to the next result_XXX.php.