How to display data in PHP from a database where every 4 data that appears then the other data is placed below

Related searches

Currently I'm making a PHP application which displays data from a database by script as below:

<!DOCTYPE html>
<html>
  <head>
    <style>
      table{
      border-collapse:separate;
      border-spacing: 10px 15px;
      }
      th,td{
      width: 150px;
      text-align: center;
      border: 1px solid black;
      padding: 5px;
      }
    </style>
  </head>


<body>

<table border="1">

<?php
$conn = mysqli_connect('localhost','root','qwerty','example');
$data = mysqli_query($conn,"select letter from office");
while($d = mysqli_fetch_array($data)){
?>

<td><?php echo $d['letter']; ?></td>

<?php
}
?>
</table>
</body>

The result of which is as follows:

A B C D E F G H I J K L

I want the result is like the following:

A B C D

E F G H

I J K L

where there are only there are 4 data in each row.

If there is more than 4 data, the data placed in the below.

how do I do this?

<!DOCTYPE html>
    <html>
      <head>
        <style>
          table{
          border-collapse:separate;
          border-spacing: 10px 15px;
          }
          th,td{
          width: 150px;
          text-align: center;
          border: 1px solid black;
          padding: 5px;
          }
        </style>
      </head>


    <body>

    <table border="1">
    <tr>
    <?php
    $conn = mysqli_connect('localhost','root','qwerty','example');
    $data = mysqli_query($conn,"select letter from office");
    $i=1;
    while($d = mysqli_fetch_array($data)){
    ?>

    <td><?php echo $d['letter']; ?></td>
    <?php
    if($i==4){
    echo '</tr><tr>';
    $i=0;
    }
    $i++;
    }
    ?>
    </tr>
    </table>
    </body>

How to retrieve, sort, and display information from a database using , 3.2 How can I retrieve the code for the HTML table without displaying it? include_once("stanford.dbquery.php"); // Connect to the database using Read How to securely store data into MySQL for more information. To format date fields in the table, call the function set_date_format , as shown below. How to display data in PHP from a database where every 4 data that appears then the other data is placed below Currently I'm making a PHP application which displays data from a database by script as below:

<!DOCTYPE html>
<html>
  <head>
    <style>
      table{
      border-collapse:separate;
      border-spacing: 10px 15px;
      }
      th,td{
      width: 150px;
      text-align: center;
      border: 1px solid black;
      padding: 5px;
      }
    </style>
  </head>


<body>

<table border="1">

<?php
$counter=1;
$conn = mysqli_connect('localhost','root','qwerty','example');
$data = mysqli_query($conn,"select letter from office");
while($d = mysqli_fetch_array($data)){
?>
if($counter <= 4){
  <td><?php echo $d['letter']; ?></td>
}else{
  echo"\n";
  $counter=0;
 }
<?php
}
?>
</table>
</body>

I am not sure about the code running but t think this will help you. and you can use tag also an echo section.

mysql_fetch_array - Manual, To access the other column(s) of the same name, you must use the numeric index I have found a way to put all results from the select query in an array in one line. the internal data pointer for the array is advanced, incrementally, until there are no so in array $r is one more entry then rows returned from the database. This source code will help us on how to show data from the Database using PDO Query. Let's start with: Creating our Table. We are going to make our database. To create a database: Open the PHPMyAdmin. Create a database and name it as "registration_pdo". After creating a database name, then we are going to create our table.

You can run a if condition to achieve that like below. What you are trying to do is to break the line after each four records. So there are lot of ways to accomplish this. But I think using modules operator you can achieve this easily. For more about these operators check w3c here

<?php
$i = 1;
$conn = mysqli_connect('localhost','root','qwerty','example');
$data = mysqli_query($conn,"select letter from office");
while($d = mysqli_fetch_array($data)){
?>

<td>
<?php

if ($i%4 ==0) { 
echo '<br>'; 
}else{
 echo $d['letter'];} ?></td>
}
$i++;
<?php
}
?>

mysql_fetch_assoc - Manual, See also MySQL: choosing an API guide and related FAQ for more information. To access the other column(s) of the same name, you either need to access the result with echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } While a row of data exists, put that row in $row as an associative array 5. Create a Main Index Page. Now create a main index. php page which contain the actual script of PHP which will show data from database into HTML table and also it will store all values in HTML array and on form submission, it will catch all selected values and print them on web page, you can also store it in your database.

So in your while loop you're telling it just to print td html tags. Which are the table data tags.

To add rows you need to use the tr tags also. tr, is table row. It is like an excel sheet. tr is the row and td is the individual cells.

So going back to your code.

$data = mysqli_fetch_array($data)

foreach ($data as $id->$d) {
    if($id == 0) {
        echo '<tr><td>' . $d['letter'] . '</td>'; 
    } 
    else if($id%4 == 0) {
        echo '</tr><tr><td>' . $d['letter'] . '</td>'; 
    }
    else {
        echo '<td>' . $d['letter'] . '</td>'; 
    }
}
echo '</tr>'; 
?>

Variables From External Sources - Manual, When a form is submitted to a PHP script, the information from that form is There were some other ways of accessing user input in old PHP versions. For example, let's post a form to itself and upon submission display the data: from a database and wish to add a "Delete" and "Modify" button next to each of the names in� Very often you will need to use a MySQL table to store data inside it and then output that data by using a PHP script. To display the table data it is best to use HTML, which upon filling in some data on the page invokes a PHP script which will update the MySQL table. To populate a new database table with data you will first need an HTML page

Getting Data From MySQL Database, Getting Data From MySQL Database - Data can be fetched from MySQL tables by executing SQL SELECT statement through PHP function mysql_query. Try out following example to display all the records from employee table. NOTE − Always remember to put curly brackets when you want to insert an array value� There are two ways to do this. And the best part is both are described in the answers of the question. The first approach is directly echoing everything into your webpage.

Or should I check my previous data first and then update specific data? Answer is obvious that first I have to look at previous data then I will update that. But how to get data and display in form. Don’t worry in this post I am going to show how to retrieve data from database and display in php form. I am taking my previous example of insert

A simple HTML form with user details. On submit, a PHP code is called to insert the data and to display MySQL data in an HTML table.

Comments
  • its better if you could accept a one of answer as the final answer.
  • If you need better solution which includes CSS let me know. With you same code of PHP you can achieve what you have asked for with a little CSS.
  • @NipunTharuksha, I will try every answer on my server.
  • @HemantSankhla, If you want to help me please let me know
  • Thank you very much. You saved my time.