Page 1 of 1

Having a problem with a for loop...and need help

Posted: Sat Mar 28, 2009 9:56 pm
by inquisitivedzign
I am having a problem with a for loop just like the title says.

Want I want to do is query the database for instances of posts where the conditions are met. Then for each one set $postTitle and $postContent to the according table columns. If my words are a bit clumsy I hope my code can do better. Any help is appreciated.

p.s. dont worry about $referrer and $pageName those variables are solid. I just want the output to do what I want.

http://pastie.org/430222 - The code

The image of the table - http://www.newcustomers2you.com/table.jpg

Re: Having a problem with a for loop...and need help

Posted: Sat Mar 28, 2009 11:04 pm
by tech603

Code: Select all

<?php 
    }
     else {
        foreach($row2 as $key => $value) {
          $postTitle = $row2['postTitle'];
          $postContent = $row2['postContent'];
?>
 
    <div id="about" class="post">
      <h2 class="title"><?php echo $postTitle; ?>
      </h2>
      <div class="entry">
        <?php echo $postContent; ?>
      </div>
 
Your problem is that you are trying to echo the found values outside of the for loop. You should try this:

Code: Select all

<?php 
    }
     else {
        foreach($row2 as $key => $value) {
 
echo '<div id="about" class="post">
        <h2 class="title">' . $row2['postTitle'] . '</h2>
        <div class="entry">' . 
         $row2['postContent'] . '
</div>     
           
?>
This will allow your for loop to display each found item to the browser along with the html.

Hope that helps.

Re: Having a problem with a for loop...and need help

Posted: Sun Mar 29, 2009 12:46 am
by inquisitivedzign
my loop actually already did what that does. Except it is slightly cleaner.

The only problem I have right now is that It displays the same output 6times...if I output key as value then it goes through the table outputting each key as the title and each value as the content.

I want it only to display postTitle and Content