php - Show values of the db in organization with Bootstrap row class for every 3 columns

350

I wrote some code which retrieves data from the database and gives the output to the web page using php. I use bootstrap to show the data in some organized way on the web page. But the code returns some syntax errors.

function break_array($array, $page_size) {
  $arrays = array();
  $i = 0;

  foreach ($array as $index => $item) {
    if ($i++ % $page_size == 0) {
      $arrays[] = array();
      $current = & $arrays[count($arrays)-1];
    }
    $current[] = $item;
  }
  return $arrays;
}
?>

<?php

foreach (break_array($row = $sql->fetch(), 3) as $columns) {
  <div > *** Error is gives on here ***
  foreach ($columns as $article) {
    <div >
      <p>{{ strip_tags(str_limit($article->body, $limit = 90, $end = '...')) }}</p>
    </div>
  }
  </div>
}

How can I correct this?

77

Answer

Solution:

Your 'code' is mixing HTML with PHP in an incorrect way. PHP needs to be interpreted server side to generate HTML, this HTML is then sent to the browser and is the source-code for the browser.

You can mix PHP & HTML in a couple of ways. Here some examples:

1. Enter and leaver php parsing mode

Note that all PHP-code that needs to be interpreted is between<?php ... ?> tags, this is interpreted at the server side.

<?php foreach (break_array($row = $sql->fetch(), 3) as $columns) { ?>
    <div class="row">
    <?php foreach ($columns as $article) { ?>
        <div class="col-md-4">
            <p><?php {{ strip_tags(str_limit($article->body, $limit = 90, $end = '...')) }} ?></p>
        </div>
    <?php } ?>
    </div>
<?php } ?>

2. Echo HTML as strings in PHP

Note that all HTML-code is inside a phpecho() statement:

<?php
foreach (break_array($row = $sql->fetch(), 3) as $columns) {
    echo '<div class="row">';
    foreach ($columns as $article) {
        echo '<div class="col-md-4">';
        echo '  <p>' . strip_tags(str_limit($article->body, $limit = 90, $end = '...')) . '</p>';
        echo '</div>';
    }
    echo '</div>';
}
?>

People are also looking for solutions to the problem: javascript - How to create an excel file based on the HTML table and save it in the project directory

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.