When I tried to display data from the table to the database, to the table on the site page, I ran into nose to nose with the output problem. the plate I created is not quite standard
& lt; table & gt; & lt; tr & gt; & lt; td & gt; entry1 & lt; / td & gt; & lt; td & gt; entry2 & lt; / td & gt; & lt; td & gt; entry3 & lt; / td & gt; & lt; td & gt; entry4 & lt; / td & gt; & lt; tr & gt; & lt; / table & gt;
My table consists of several rows (no matter how many) and 4 columns (that’s the problem with them)
The task is to output all data from the database table to the table on the page one by one (as if in a list, but in the form of a table) (a schematic drawing is attached)
I’m trying to output values like this:
& lt; table & gt;
& lt;? php
require 'lib / connect.php';
// SQL query
$ strSQL = "SELECT * FROM news ORDER BY date DESC";
// Execute the query ($ rs dataset contains the result)
$ rs = mysql_query ($ strSQL);
// Loop over recordset $ rs
// Each row becomes an array ($ row) using the mysql_fetch_array function
while ($ row = mysql_fetch_array ($ rs)) {
// Write the value of the FirstName column (which is now the $ row array)
echo "& lt; tr & gt;";
echo "& lt; tr & gt;";
echo "". $ row ['znachenie']. "";
echo "". $ row ['znachenie']. "";
echo "". $ row ['znachenie']. "";
echo "". $ row ['znachenie']. "";
echo "& lt; / td & gt;";
echo "& lt; / tr & gt;";
mysql_close ();
}? & gt; & lt; / table & gt;
But here it turns out that the entire first row of the table is equal to a repeated one value, how can you make the values alternate, for example, you may need to assign to echo "". $ row ['znachenie']. "" ;
parenthesis []
with something inside?
Answer 1
First option:
& lt;? php
$ arr = [1, 2, 3, 4, 5, 6, 7];
echo "& lt; tr & gt;";
$ idx = 1;
foreach ($ arr as $ value) {
printf ("& lt; td & gt;% s & lt; / td & gt;", $ value);
if ($ idx% 4 === 0) {
echo "& lt; / tr & gt; & lt; tr & gt;";
}
$ idx ++;
}
echo "& lt; / tr & gt;";
Second option:
Use something like
http://getbootstrap.com/css/#grid
& lt;? php
$ arr = [1, 2, 3, 4, 5, 6, 7];
echo "& lt; div class = 'row' & gt;";
foreach ($ arr as $ value) {
// by 12
// printf ("& lt; div class = 'col-md-1' & gt;% s & lt; / div & gt;", $ value);
// by 6
// printf ("& lt; div class = 'col-md-2' & gt;% s & lt; / div & gt;", $ value);
// by 4
printf ("& lt; div class = 'col-md-3' & gt;% s & lt; / div & gt;", $ value);
// by 3
// printf ("& lt; div class = 'col-md-4' & gt;% s & lt; / div & gt;", $ value);
// by 2
// printf ("& lt; div class = 'col-md-6' & gt;% s & lt; / div & gt;", $ value);
}
echo "& lt; / div & gt;";
PS: Most importantly, stop interfering with presentation and logic. Take a look at the template engines.
Answer 2, authority 100%
For MDB (design-advanced Bootstrap variant)
https://mdbootstrap.com
Create a database “company” with a table “userinfo” and
columns:
userinfo_id, userinfo_name, userinfo_position, userinfo_region, userinfo_age, userinfo_begin_date
still need to include CSS
& lt;! - MDBootstrap Datatables - & gt;
& lt; link href = "css / addons / datatables.min.css" rel = "stylesheet" & gt;
Enter in the body of the file, for an advanced table:
& lt; table id = "dtMaterialDesignExample" class = "table table-striped" cellspacing = "0" width = "100% "& gt;
& lt; thead & gt;
& lt; tr & gt;
& lt; th class = "th-sm" & gt; #
& lt; / th & gt;
& lt; th class = "th-sm" & gt;
& lt; / th & gt;
& lt; th class = "th-sm" & gt; Position
& lt; / th & gt;
& lt; th class = "th-sm" & gt; Scope
& lt; / th & gt;
& lt; th class = "th-sm" & gt; Age
& lt; / th & gt;
& lt; th class = "th-sm" & gt; Getting started
& lt; / th & gt;
& lt; / tr & gt;
& lt; / thead & gt;
& lt; tbody & gt;
& lt;? php
$ conn = mysqli_connect ("localhost", "root", "", "company");
// Check connection
if ($ conn- & gt; connect_error) {
die ("Connection failed:". $ conn- & gt; connect_error);
}
$ sql = "SELECT userinfo_id, userinfo_name, userinfo_position,
userinfo_region, userinfo_age, userinfo_begin_date
FROM userinfo ";
$ result = $ conn- & gt; query ($ sql);
if ($ result- & gt; num_rows & gt; 0) {
// output data of each row
while ($ row = $ result- & gt; fetch_assoc ()) {
echo "& lt; tr & gt; & lt; td & gt;" ... $ row ["userinfo_id"]
... "& lt; / td & gt; & lt; td & gt;" ... $ row ["userinfo_name"]
... "& lt; / td & gt; & lt; td & gt;" ... $ row ["userinfo_position"]
... "& lt; / td & gt; & lt; td & gt;" ... $ row ["userinfo_region"]
... "& lt; / td & gt; & lt; td & gt;" ... $ row ["userinfo_age"]
... "& lt; / td & gt; & lt; td & gt;" ... $ row ["userinfo_begin_date"]
... "& lt; / td & gt; & lt; / tr & gt;";
}
echo "& lt; / tbody & gt;";
} else {echo "0 results"; }
$ conn- & gt; close ();
? & gt;
and Java script
// Material Design example
$ (document) .ready (function () {
$ ('# dtMaterialDesignExample'). DataTable ();
$ ('# dtMaterialDesignExample_wrapper'). find ('label'). each (function () {
$ (this) .parent (). append ($ (this) .children ());
});
$ ('# dtMaterialDesignExample_wrapper .dataTables_filter'). find ('input'). each (function () {
$ ('input'). attr ("placeholder", "Search");
$ ('input'). removeClass ('form-control-sm');
});
$ ('# dtMaterialDesignExample_wrapper .dataTables_length'). addClass ('d-flex flex-row');
$ ('# dtMaterialDesignExample_wrapper .dataTables_filter'). addClass ('md-form');
$ ('# dtMaterialDesignExample_wrapper select'). removeClass (
'custom-select custom-select-sm form-control form-control-sm');
$ ('# dtMaterialDesignExample_wrapper select'). addClass ('mdb-select');
$ ('# dtMaterialDesignExample_wrapper .mdb-select'). materialSelect ();
$ ('# dtMaterialDesignExample_wrapper .dataTables_filter'). find ('label'). remove ();
});
still need to include JS
& lt;! - MDBootstrap Datatables - & gt;
& lt; script type = "text / javascript" src = "js / addons / datatables.min.js" & gt; & lt; / script & gt;