Results 1 to 7 of 7

Thread: While Loop, CSS layout

  1. #1

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    While Loop, CSS layout

    Ok i have to do a while loop with mysql and what not, i can do that on my own, but the site is using CSS, and i dont know how i can display the info.

    here is the code without the PHP:

    Code:
    <div class="left_side">
    	<div class="right_articles">
    		<p><img src="images1/image.gif" alt="Image" title="Image" class="image" /><b>Profile Name </b><br />
    		Summary of the featured profile. </p>
    	</div>
    </div>
    <div class="right_side">
    	<div class="right_articles">
    		<p><img src="images1/image.gif" alt="Image" title="Image" class="image" /><b>Profile Name </b><br />
    		Summary of the featured profile.  </p>
    	</div>
    </div>
    The problem is the ' class="left_article" ' and ' class="right_article" '.

    I am needing to get 2 records from the db and display them there.

    hope i made myself clear enough
    My usual boring signature: Something

  2. #2
    Lively Member {yak}'s Avatar
    Join Date
    Aug 2005
    Posts
    119

    Re: While Loop, CSS layout

    Do the query before the html and break in with the variables:
    PHP Code:
    <?php
    // assumed all connection and error handling....
    $result mysql_query('your query');
    $row mysql_fetch_row($result);

    ?>

    <div class="left_side">
        <div class="right_articles">
            <p><img src="images1/image.gif" alt="Image" title="Image" class="image" /><b><?php echo $row[0]; ?> </b><br />
            Summary of the featured profile. </p>
        </div>
    </div>
    <div class="right_side">
        <div class="right_articles">
            <p><img src="images1/image.gif" alt="Image" title="Image" class="image" /><b><?php echo $row[0]; ?> </b><br />
            Summary of the featured profile.  </p>
        </div>
    </div>
    I assumed you are returning one row with your query.

    http://us.php.net/manual/en/function...-fetch-row.php
    Just as well can use mysql_fetch_assoc:
    http://us.php.net/manual/en/function...etch-assoc.php
    {yak}

  3. #3
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: While Loop, CSS layout

    if he's making a while loop, he is returning more than one row.

    so, if you just have a left and right side, you can use a variable to indicate which side might be being used.

    PHP Code:
    <?php
      $sql 
    "SELECT values FROM table WHERE 1 LIMIT 2";
      
    $q mysql_query($q);
      
    $side 0//start on left side. left = 0; right = 1;
      
    while($a mysql_fetch_array($q)){
        
    $class = ($side == 0) ? 'left' 'right';
    ?>
    <div class="<?php echo $class?>">
      <div class="right_articles">
        <?php echo $a['values']; ?>
      </div>
    </div>
    <?php ?>

  4. #4
    Lively Member {yak}'s Avatar
    Join Date
    Aug 2005
    Posts
    119

    Re: While Loop, CSS layout

    Hard to say, based on the context of the html it doesn't seem like a while loop situation. I more assumed 2 columns and not records.

    Good luck.
    {yak}

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: While Loop, CSS layout

    PHP provides an alternative syntax that makes inserting control structures into markup a breeze:

    PHP Code:
    <div class="right_side">
      <?php foreach($right_articles as $article): ?>
        <div class="right_article">
          <p><?php echo $article['blahblah'?></p>
          <!-- etc. -->
        </div>
      <?php endforeach; ?>
    </div>
    As ever, do all of your processing before outputting the markup; preferably, this should be done in separate code files.

  6. #6

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: While Loop, CSS layout

    i am using to to display 2 random records from the database. I will try Kows' code Think it might work.
    My usual boring signature: Something

  7. #7

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: While Loop, CSS layout

    kows code worked. Thanks again kows
    My usual boring signature: Something

Posting Permissions

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



Click Here to Expand Forum to Full Width