Jump to content

Newbie needs help please....... looping through results


sonicthehedgehog

Recommended Posts

I wonder if somebody could help me please. I'm really new to PHP and have only been working with it for just over a week. My boss has asked to change the appearance of a page on his personal site to make it less formulaic. At the moment all of the information is taken from the database and 'tiled' 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>                    Tags:                     <?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 ) { ?> | <?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'] ) ) { ?> | <?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" /> Download PDF</a> (<?php echo htmldisplay( bytesize( $value['pdf_size'] ) ) ?>)                    <?php } ?>                  </div>                  <?php } ?>                </div>              </div>            <?php $ctr++; if( $ctr==4 ) { $ctr=1; } } } ?>           </div>          <?php } ?>
I'd be really grateful if somebody could help with this please.
Ideally the format I'd like is one main, one medium horizontal (image left text right), three small, two medium vertical (two different coloured backgrounds), one medium horizontal (image right text left) and then two rows of three small ones
Edited by sonicthehedgehog
Link to comment
Share on other sites

This sounds like an HTML/CSS layout issue more than a PHP issue. Have you created a static HTML page that looks like you want? If not, I would start there, and once you have that layout working then you can worry about how to use PHP to fill in the content.

Link to comment
Share on other sites

It should be pretty easy to add the PHP to fill in the content then. I wouldn't use a loop for a structure like that though, I'd just have one block of PHP code to get all of the data and save it in variables, and then in the HTML I would have statements to print the variables in the appropriate places.

Link to comment
Share on other sites

Well, from the code above it looks like there are global variables called $column then a number ($column1, $column2, etc), where each of those is an array of arrays that have indexes like "type", "video", "image", etc. The code to get all of those isn't included, but it looks like it is looping through each column, then each item in each column, and printing the HTML for them. I guess you would use most of that code to process each item, but the code to process each column would go exactly where you want it on the page instead of everything being in a single loop.If you want to see what all of the arrays are, you can use print_r to print each one, e.g.:

echo '<pre>';$i = 1;do {  if (isset($GLOBALS['column' . $i])) {    $continue = true;    print_r($GLOBALS['column' . $i]);  }  else {    $continue = false;  }  $i++;} while ($continue);echo '</pre>';
Link to comment
Share on other sites

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.

Using the code you said about gave me an array of each of the items I want but I'm not sure how to get that into each div?

Link to comment
Share on other sites

The code you posted first has a for loop that looks like it loops over the number of columns, and then a foreach loop to handle each column. You would remove the for loop and put the foreach loop for each column where you want that content displayed. So instead of this:

foreach( $GLOBALS['column'.$loop] as $key=>$value )
You would use this wherever you want to display whatever is in 'column1':
foreach( $GLOBALS['column1'] as $key=>$value )
And the same for the other columns.
Link to comment
Share on other sites

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.

Originally there was more than one column but that's now been changed. I guess the loop is for if the number of columns changes again.

I don't know if it helps but this bit of code is what controls the number of columns:

  # merge into columns  $ccols=1; $col=1;  if( is_array( $articles ) ) {    foreach( $articles as $key=>$value ) {      $GLOBALS['column'.$col][]=$articles[$key];      $col++;      if( $col>$ccols ) { $col=1; }    }  }
Edited by sonicthehedgehog
Link to comment
Share on other sites

You can access anything you want, you can access $column1[0], $column1[1], etc, you can just access them directly and put them where you want. It doesn't need to be in a loop.

I did do that but it showed 14 results for column1 and then nothing for column2, column3 etc. There's only one column but that has all 14 results. The first result should be on one line, the second should be on line two, the 3rd, 4th and fifth should be on line three etc.
I tried this:
<?php echo '<pre>';print_r($articles);echo '</pre>';?>
and it gave the same results as
$i = 1;do {  if (isset($GLOBALS['column' . $i])) {    $continue = true;    print_r($GLOBALS['column' . $i]);  }  else {    $continue = false;  }  $i++;} while ($continue);
so I don't know if I could use something like foreach( $GLOBALS['column1'] as $key=>$value ) but saying:
If 1st result display one
If 2nd result display one
If 3rd - display three results
If 6th - show two results (both displayed differently)
If 8th - one result
If 9th - three results
If 12th - three results
Edited by sonicthehedgehog
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...