SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot MetalAges's Avatar
    Join Date
    Jan 2003
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    During "while" I need to throw in some code every 4th result

    Hi there,

    I have a database where I am displaying the results of an array:

    <?php
    // request results
    $result = mysql_query('SELECT band, thumburl FROM bandmain ORDER BY band');
    if (!$result) {
    exit('<p>Error performing query: ' . mysql_error() . '</p>');
    }
    // set counter default
    $count = 0;
    // Display bands
    while ($row = mysql_fetch_array($result)) {
    echo '<div class="bandbox"><img src="images/ppic/' . $row['thumburl'] . '" width="150" height="100" class="borblk"><br>' . "\n" . $row['band'] . "</div>\n";
    }
    ?>
    My dilemma is, every 4th result I need to throw in a bit of CSS code:
    <br style="clear:both" />
    I have the MySQL/PHP book from Sitepoint in front of me and I know I have to implement the
    $count = 0;
    while ($count <=4) {
    STUFF HERE
    }
    but I am not sure how to complete for what I am doing. I need it to throw in that code every fourth result but I need it to continue running the while command until it is done. Can someone help me out with what I am missing here? Thank you!

    Page I am working on: http://www.insideoutmusic.com/bandmain-test.php

  2. #2
    SitePoint Wizard guelphdad's Avatar
    Join Date
    Oct 2003
    Location
    St. Catharines, ON Canada
    Posts
    1,706
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    Code:
    while ($row = mysql_fetch_array($result)) {
       if $count % 4 == 0 {
    	  echo '<br style="clear:both" />';
    	  $count++
       } else {
    	  echo '<div class="bandbox"><img src="images/ppic/' . $row['thumburl'] . '" width="150" height="100" class="borblk"><br>' . "\n" . $row['band'] . "</div>\n";
    	  $count++
       }
    }
    The % is the modulus operator dividing your count by 4 and looking for remainder of 0. If so echo out your break tag and increment the $count otherwise print out your other info and then increment the $count variable.

    Oh and this should go in PHP thread since the question isn't mysql related.

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,014
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    nice one dave

    too bad i don't do php or i'd fix your code for you -- it looks like every 4th result row, it prints the BR instead of the result row, rather than in addition to the result row

    and why a separate $count variable? doesn't php have a returned result row number?

    but you're right, this doesn't belong in the mysql forum at all

    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    maybe slightly more efficient than daveman's code, and it does display each and every row

    PHP Code:
       // set counter default
      
    $count 0;
      
      
    // Display bands
      
    while ($row mysql_fetch_array($result))
      {
        if (
    == ($count++ % 4))
        {
          echo 
    ' <br style="clear:both" />';
        }
     echo 
    '<div class="bandbox"><img src="images/ppic/' $row['thumburl'] . '" width="150" height="100" class="borblk"><br>' "\n" $row['band'] . "</div>\n";
      } 

  5. #5
    SitePoint Zealot MetalAges's Avatar
    Join Date
    Jan 2003
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You guys rock, thank you so much. This is why I love Sitepoint and all that comes with it

  6. #6
    SitePoint Zealot MetalAges's Avatar
    Join Date
    Jan 2003
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Aa quick follow-up question to this thread. Everything works like I wanted thanks for the code. I have two sections I have as you can see here: http://www.insideoutmusic.com/bandmain-test.php

    The code that currently accomplishes this is:
    <div id="mainband">
    <?php
    // Select IOMA database
    if (!mysql_select_db('insideou_ioma')) {
    exit('<p>Unable to locate the band' .
    'database at this time </p>');
    }
    ?>
    <?php
    // request results
    $result = mysql_query('SELECT band, thumburl FROM bandmain WHERE active = "t" ORDER BY band');
    if (!$result) {
    exit('<p>Error performing query: ' . mysql_error() . '</p>');
    }
    // set counter default
    $count = 0;

    // Display bands
    while ($row = mysql_fetch_array($result))
    {
    if (0 == ($count++ % 4))
    {
    echo ' <br style="clear:both" />';
    }
    echo '<div class="bandbox"><img src="images/ppic/' . $row['thumburl'] . '" width="150" height="100" class="borblk"><br>' . "\n" . $row['band'] . "</div>\n";
    }
    ?>
    <br style="clear:both" />
    </div>
    <div id="mainbandB">
    <?php
    // Select IOMA database
    if (!mysql_select_db('insideou_ioma')) {
    exit('<p>Unable to locate the band' .
    'database at this time </p>');
    }
    ?>
    <?php
    // request results
    $result = mysql_query('SELECT band, thumburl FROM bandmain WHERE active = "f" ORDER BY band');
    if (!$result) {
    exit('<p>Error performing query: ' . mysql_error() . '</p>');
    }
    // set counter default
    $count = 0;

    // Display bands
    while ($row = mysql_fetch_array($result))
    {
    if (0 == ($count++ % 4))
    {
    echo ' <br style="clear:both" />';
    }
    echo '<div class="bandbox"><img src="images/ppic/' . $row['thumburl'] . '" width="150" height="100" class="borblk"><br>' . "\n" . $row['band'] . "</div>\n";
    }
    ?>
    <br style="clear:both" />
    </div>
    As you can see the difference in the two sections is one is showing the results if the "active" field is "t" and the other shows if it is "f". Other than that the other difference is the CSS DIV is set to a separate id (mainbandB) for the second section.

    My question is, is there an easier way to accomplish this as opposed to building an entire duplicate block of almost the same code for the second (mainbandB) section? I spent the last couple hours playing with if/else statements to no avail. Only thing I can come up with results in this: http://www.insideoutmusic.com/bandmain-test2.php

    The code for that is:
    <?php
    // request results
    $result = mysql_query('SELECT band, thumburl, active FROM bandmain ORDER BY band');
    if (!$result) {
    exit('<p>Error performing query: ' . mysql_error() . '</p>');
    }
    // set counter default
    $count = 0;

    // Display bands
    while ($row = mysql_fetch_array($result))
    {
    if (0 == ($count++ % 4))
    {
    echo ' <br style="clear:both" />';
    }
    if ($row['active'] = "t") {
    echo '<div id="mainband">' . "\n" . '<div class="bandbox"><img src="images/ppic/' . $row['thumburl'] . '" width="150" height="100" class="borblk"><br>' . "\n" . $row['band'] . "</div>\n" . '<br style="clear:both" /></div>';
    }
    echo '<div id="mainbandB">' . "\n" . '<div class="bandbox"><img src="images/ppic/' . $row['thumburl'] . '" width="150" height="100" class="borblk"><br>' . "\n" . $row['band'] . "</div>\n" . '<br style="clear:both" /></div>';
    }
    ?>
    There's probably going to be a simple solution to this but I am still learning and still suffer from PHPNoobitis .

    Thanks all!

  7. #7
    SitePoint Wizard guelphdad's Avatar
    Join Date
    Oct 2003
    Location
    St. Catharines, ON Canada
    Posts
    1,706
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    swdev, yes your code looks a lot more sensible than mine. I didn't have PHP on this machine and hadn't used it in a while. Rusty I guess.

  8. #8
    SitePoint Zealot MetalAges's Avatar
    Join Date
    Jan 2003
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just checking to see on the question here, I am still stumped. Or, do I have it correct and that's the way it should be?

  9. #9
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your 2 example codes actuall do slightly different things

    Code 1
    This displays the ACTIVE='t' band details using the style bandbox all wrapped up in a style mainband div.
    This is then followed by the ACTIVE='f' band details using the style bandbox all wrapped up in a style mainbandB div.

    Code 2
    This displays all the band details using style mainband if ACTIVE='t' and using style mainbandB if ACTIVE='f'.

    Which do you actually want to do?


Bookmarks

Posting Permissions

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