Jump to content

[SOLVED] problem with a foreach loop[please help!!!]


inquisitive

Recommended Posts

Here is my problem

 

I want to query my table in the database and for each occurance of a row where the conditions are met...I want that information to display. I am kinda bad with words so let me show you me code and a picture of the table...and you should be able to take it from there.... Thanks.  I am not once of those guys looking for a quick answer with 0 effort.  Some direction even would be nice.

 

The picture of the table can be found at http://www.newcustomers2you.com/table.jpg

 

<?php

 

  $sql2 = "SELECT * FROM `pagePosts` Where '$referrer' = `companyUrl` AND '$pageName' = `pageName` ";

    $result2 = $db->query($sql2);

    $row2 = $result2->fetch();

    if (!$result2) {

        $errors[] = 'Fatal Errors Please contact the web administrator!';

?>

   

      <div id="about" class="post">

      <h2 class="title"><?php echo 'There was an error!'; ?></h2>

      <div class="entry">

        <p><?php echo implode('<br/>',$errors); ?></p>

      </div>

    </div>

<?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>

    </div>

<?php

        }

      } 

?>

There are a very errors in your code.  I'm not sure what database wrapper class you're using so I've only used mysql_fetch_assoc() :

 

<?php

  $sql2 = "SELECT * FROM `pagePosts` Where `companyUrl` = '$referrer' AND `pageName` = '$pageName'";
     $result2 = $db->query($sql2);
     if (!$result2) { // I'm not entirely sure about this bit and whether it would work or not
        $errors[] = 'Fatal Errors Please contact the web administrator!';
?>
     
      <div id="about" class="post">
      <h2 class="title"><?php echo 'There was an error!'; ?></h2>
      <div class="entry">
        <p><?php echo implode('<br/>',$errors); ?></p>
      </div>
    </div>
<?php
    } else {
        while($row2 = mysql_fetch_assoc($result2)) {
          $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>
    </div>
<?php
        }
      }   
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.