Web Analytics Made Easy -
StatCounter positioning looped objects - CodingForum

Announcement

Collapse
No announcement yet.

positioning looped objects

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • positioning looped objects

    Say if I'm displaying data from a database by using a loop, it places all the results going down. But what if I want it to display the first three horizontally then line break and display the next three and so on... Any ideas on how to do this?

    Here's my code:

    PHP Code:
    $query mysql_query("SELECT * FROM redeem");
    while(
    $row mysql_fetch_assoc($query)) {

    $id $row["id"];
    $title $row["title"];
    $image_location $row["image_location"];
    $description $row["desc"];
    $price $row["price"];

      echo 
    "

        
    $title <br />
        <img src='
    $image_location' width='260' <br />

        Info: 
    $description <br /><br />
        Price: 
    $price<br />

        "
    ;


    Thanks.

  • #2
    You'd need to make use of a secondary loop that counts the objects and decides if they're on the same row or a new one.
    "Tango says double quotes with a single ( ' ) quote in the middle"
    '$Name says single quotes with a double ( " ) quote in the middle'
    "Tango says double quotes ( \" ) must escape a double quote"
    '$Name single quotes ( \' ) must escape a single quote'

    Comment


    • #3
      Originally posted by tangoforce View Post
      You'd need to make use of a secondary loop that counts the objects and decides if they're on the same row or a new one.
      Yeah I figured that out, but the loop automatically displays them down. They need to be displayed horizontally before you can tell it to line break.

      Comment


      • #4
        You need to put them in a table like this:
        PHP Code:
        <table>
            <tr>
            <?php
            
            $i 
        0;
            
            
        $query mysql_query('SELECT * FROM redeem');
            while(
        $row mysql_fetch_assoc($query))
            {
                
        $id $row['id']; 
                
        $title $row['title']; 
                
        $image_location $row['image_location']; 
                
        $description $row['desc']; 
                
        $price $row['price']; 
                
                echo <<<END
                <td>
                    
        $title <br /> 
                    <img src="
        $image_location" width="260" /> <br /> 
                
                    Info: 
        $description<br /><br /> 
                    Price: 
        $price<br />
                </td>
        END;

                
        $i++;
                if(
        $i 2// third iteration
                
        {
                    
        $i 0// reset counter
                    
        echo '</tr><tr>'// end row
                
        }
            }
            
            
        // fill in the columns in the last row
            
        if($i 0)
            {
                for(; 
        $i 3$i++)
                    echo 
        '<td>&nbsp;</td>';
            }
            
            
        ?>
            </tr>
        </table>
        Or use floated divs. ^Not tested.

        Comment


        • #5
          Originally posted by Inigoesdr View Post
          You need to put them in a table like this:
          PHP Code:
          <table>
              <tr>
              <?php
              
              $i 
          0;
              
              
          $query mysql_query('SELECT * FROM redeem');
              while(
          $row mysql_fetch_assoc($query))
              {
                  
          $id $row['id']; 
                  
          $title $row['title']; 
                  
          $image_location $row['image_location']; 
                  
          $description $row['desc']; 
                  
          $price $row['price']; 
                  
                  echo <<<END
                  <td>
                      
          $title <br /> 
                      <img src="
          $image_location" width="260" /> <br /> 
                  
                      Info: 
          $description<br /><br /> 
                      Price: 
          $price<br />
                  </td>
          END;

                  
          $i++;
                  if(
          $i 2// third iteration
                  
          {
                      
          $i 0// reset counter
                      
          echo '</tr><tr>'// end row
                  
          }
              }
              
              
          // fill in the columns in the last row
              
          if($i 0)
              {
                  for(; 
          $i 3$i++)
                      echo 
          '<td>&nbsp;</td>';
              }
              
              
          ?>
              </tr>
          </table>
          Or use floated divs. ^Not tested.
          Worked perfectly!! Thank you!!

          Comment

          Working...
          X