1

I am curious is it possible to create a loop in a separate PHP page that will create divs? Here is what I have been working on. I know that it will show the results for the first row in the data table. However, it does not show the following rows. I would assume since it does not show the following this won't work.

Separte PHP page

$query = "SELECT `ID`, `fullname`, `comment`, `date` FROM `reviews`";
$result = mysql_query($query);
if(!$result)
  {
    die('Error: ' .mysql_error());
  }
  $row_count = mysql_num_rows($result);
  $row_users = mysql_fetch_array($result);

  for($i=0; $i<$row_count; $i++)
  {
    $results = '<div class="col-lg-4">Name:'.($row_users['fullname']).'<br>'.($row_users['comment']).'<br>Date: '.($row_users['date']).'</div>';
  }

Webpage

  <div class="row">
    <?php {echo $results;}?>
  </div>

2 Answers 2

1

Create Div for Each MySQL Row

In PHP file that contains proper database information.

function getTable() {
    $query = "SELECT `ID`, `fullname`, `comment`, `date` FROM `reviews`";
    $result = mysql_query($query);

    $table = '';

    if ($result) {
        while($row_users = mysql_fetch_array($result)) {
            $table .= "<div class='row'>";
            $table .= '<div class="col-lg-4">Name:'.($row_users['fullname']).'<br>'.($row_users['comment']).'<br>Date: '.($row_users['date']).'</div>';
            $table .= "</div>";
        }
    } else {
        die('Error: ' .mysql_error());
    }

    return $table;
}

In HTML display file

<?php echo getTable(); ?>

PHP mysql Extension is deprecated!!!

The extension is deprecated as of PHP 5.5.0 and is removed in PHP 7.0.0.

Do not use the mysql class in PHP. It is not safe. Please use a library like mysqli or, my favorite, PDO

Sign up to request clarification or add additional context in comments.

6 Comments

What do you mean by PHP MySQL is deprecated!!!? PHP MYSQL is not deprecteed! Just mysql() functions are deprecated!!
@JayPatel Wow I worded that horribly. Thank you for commenting
I will looking into adopting one of the following if I can make this work. What I mean is that I have a separate page where I have most of my PHP written that I include with the webpage /testimonials.php
@MatthewKelsay I have updated my answer. Now the code is in a function. So as long as the proper file is included or required just call the function and it will return the complete table.
That works perfect! Will take a look a into using one of the library's.
|
0

You can try the following code:

$query = "SELECT `ID`, `fullname`, `comment`, `date` FROM `reviews`";
$result = mysql_query($query);
if(!$result)
  {
    die('Error: ' .mysql_error());
  }
  $results = ""
  while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $results .= '<div class="col-lg-4">Name:'.($row['fullname']).'<br>'.($row['comment']).'<br>Date: '.($row['date']).'</div>';
  }

Here, you have to concate results string in each loop. You should user mysqli_query() instead of mysql_query() as mysql_ is deprecated.

3 Comments

You are missing the div for the row. This will just make a really big single row.
When I try this it will only show the last row in the data table.
@HurricaneDevelopment: Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.