tags:

views:

44

answers:

6

I am using the code below to print from an array but its printing too many of the columns because i am using a foreach loop...

How can i print just 1 col1 and 1 col2 but keeping the right strings being printed which is working?

<div class="feature-text">
<?php 
//Now print the associated cms_page_parts
$qpp = $conn->query("SELECT * FROM cms_page_part WHERE page_id=$id"); 
$rpp = $qpp->fetchAll(PDO::FETCH_ASSOC);

foreach ($rpp as $row) {
?>
  <div id="col1">
    <p><?php echo $row['name']=='body' ? $row['content_html'] : NULL; ?></p>
  </div>
  <div id="col2">
    <p class="testimonial"><?php echo $row['name']=='sidebar' ? $row['content_html'] : NULL; ?></p>
  </div>
<?php 
}
?>
</div>
A: 

break ends execution of the current for, foreach, while, do-while or switch structure. http://php.net/manual/en/control-structures.break.php

The MYYN
A: 

You could limit your query to return only 2 rows with the LIMIT 0, 2 clause.

Or you could using a simple counter inside the loop:

$counter = 0;
foreach ($rpp as $row) {
   if ($counter < 2) {
?>
  <div id="col1">
    <p><?php echo $row['name']=='body' ? $row['content_html'] : NULL; ?></p>
  </div>
  <div id="col2">
    <p class="testimonial"><?php echo $row['name']=='sidebar' ? $row['content_html'] : NULL; ?></p>
  </div>
<?php 
  $counter++;
  }
else
    break;
}
Davide Gualano
waste of mysql resources getting all rows and then discarding all but two
Question Mark
if you limit the query, then what is the counter for?
Question Mark
I said "or" :)The best solution is of course limiting the resultset with LIMIT, I added the counter solution just for the sake of completeness :)
Davide Gualano
+1  A: 

On every iteration through the loop, you're printing two divs, when I suspect you only want one. How about this instead:

foreach ($rpp as $row) {
    if ($row['name'] == 'body') {
        echo '<div id="col1">'
             . '<p>' . $row['content_html'] . '</p>'
             . '</div>';
    } else {
        echo '<div id="col2">'
             . '<p class="testimonial">' . $row['content_html'] . '</p>'
             . '</div>';
    }
}
nickf
This is perfect thanks.
Andy
A: 
foreach ($rpp as $row) {
    if ($row['name'] == 'body') {
        echo '<div id="col1">';
        echo '<p>' . $row['content_html'] . '</p>'
        echo '</div>';
    } else if ($row['name'] == 'sidebar')  {
        echo '<div id="col2">';
        echo '<p class="testimonial">' . $row['content_html'] . '</p>';
        echo '</div>';
    }
}

? It will be easier if you put some sample of input data and the wanted result ;)

Toms
A: 

if you limit your query to 2 and only pull out body and sidbar you will be laughing:

SELECT * FROM cms_page_part WHERE page_id=$id AND (name ='body' OR name='sidebar' ) LIMIT 2
Question Mark
A: 

If i understand the problem correctly , something like this should work:

<div class="feature-text">
    <?php 
    //Now print the associated cms_page_parts
    $qpp = $conn->query("SELECT * FROM cms_page_part WHERE page_id=$id"); 
    $rpp = $qpp->fetchAll(PDO::FETCH_ASSOC);

    $col1 = "";
    $col2 = "";
    foreach ($rpp as $row) {
        $col1 .= '<p>'.($row['name']=='body' ? $row['content_html'] : NULL).'</p>';
        $col2 .= '<p class="testimonial">'.($row['name']=='sidebar' ? $row['content_html'] : NULL).'</p>';
    }
    ?>
    <div id="col1">
        <?php echo $col1 ?>
    </div>
    <div id="col2">
       <?php echo $col2 ?>
    </div>
</div>
Sabeen Malik