Show values with column name in mysqli using php automatically - php

I need to show all row values in my db table with column names dynamically. I am not really getting a point to do that. :(
Here is what I have done so far- it works for around 100K data. but when the row increases, it shows blank screen
$result = mysqli_query($con, "SELECT *FROM `master_1m1`");
echo "<table border='1'>";
$i = 0;
$khalid == 0;
while ($row = $result->fetch_assoc()) {
$khalid++;
if ($i == 0) {
$i++;
echo "<tr>";
foreach ($row as $key => $value) {
echo "<th>" . $key . "</th>";
}
echo "</tr>";
}
echo "<tr>";
foreach ($row as $value) {
echo "<td>" . $khalid . "</td>";
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}

Related

mysqli_fetch_field doesn't return the first column name

I'm using mysqli_fetch_field to auto populate headers names in a while loop. I'm getting the headers but it's always missing the first one. I'm using mysqli_fetch_row and getting the correct number of columns for the table data.
function tableQuery($sql){
$query = mysqli_query($conn, $sql);
$columns = 0;
$tableInfo = mysqli_fetch_field($query);
echo "<center><h1>Results for " . $tableInfo->table . "</h1></center>";
echo "<table>";
echo "<tr>";
while($headers = mysqli_fetch_field($query)) {
echo "<th>" . $headers->name . "</th>";
$columns++;
}
echo "</tr>";
while($row = mysqli_fetch_row($query)) {
echo "<tr>";
for ($n = 0; $n <= $columns; $n++) {
echo "<td>" . $row[$n] . "</td>";
}
echo "</tr>";
}
echo "</table>";
}
If I pass this $sql = "SELECT id, name, date FROM users" My output looks like this:
name | date
______________________________
1 | Michael | 1/1/2018
______________________________
2 | Jack | 2/5/2018
______________________________
3 | David | 4/15/2018
So it's missing the id table header for example.
When I var_dump($headers->name) I don't see id
Thank you #cdhowie, The problem is I'm fetching the column name in the $tableInfo so either remove the table info or do it this way:
function tableQuery($sql){
$query = mysqli_query($conn, $sql);
$columns = 0;
$headerSet = false;
echo "<table>";
echo "<tr>";
while($headers = mysqli_fetch_field($query)) {
if(!$headerSet){
echo "<h1> Results for " . $headers->name . "</h1>";
headerSet = true;
}
echo "<th>" . $headers->name . "</th>";
$columns++;
}
echo "</tr>";
while($row = mysqli_fetch_row($query)) {
echo "<tr>";
for ($n = 0; $n <= $columns; $n++) {
echo "<td>" . $row[$n] . "</td>";
}
echo "</tr>";
}
echo "</table>";
}
Replace:
while($headers = mysqli_fetch_field($query)) {
echo "<th>" . $headers->name . "</th>";
$columns++;
}
with:
do {
echo "<th>" . $tableInfo->name . "</th>";
$columns++;
} while($tableInfo = mysqli_fetch_field($query))
what you need is to set the pointer back to 0 (after you have done the query)
mysqli_field_seek($query, 0);
...
mysqli_field_seek($query,0);
echo "<table>";
echo "<tr>";
while($headers = mysqli_fetch_field($query)) {
echo "<th>" . $headers->name . "</th>";
$columns++;
}
echo "</tr>";
...

how to merge a foreach loop with a while loop to build an html table?

i have two arrays $_SESSION['fff'] which have the quantity and the other is $result which is extracted from mysql database including the name of the product, price and weight, i need to build a table which includes the data from the database and the quantity which is included in the session variable array
$wherein = implode(",", $_SESSION['cart']);
$sql = "select id, item_name, Price, Weight from items where id IN
($wherein)";
$result= mysqli_query($conn, $sql);
echo "<table style='width:100%' border='1' >";
echo "<tr>";
echo "<th> Product Name</th>";
echo "<th>Product Price </th>" ;
echo "<th>Weight </th>" ;
echo "<th>Quantity </th>" ;
echo "</tr>";
$sum = '';
$s= '';
while (($row = mysqli_fetch_array($result, MYSQLI_ASSOC))) &&
foreach($_SESSION['fff'] as $value){
$sum += $row['Price'];
$s += $row['Weight'];
echo "<tr>";
echo "<td>" . $row['item_name'] . "</td>";
echo "<td> $". $row['Price'] . "</td>" ;
echo "<td>". $row['Weight'] ;
echo "<td>".$value."</td>";
}
I have been trying for hours now searching online and trying different ways but couldn't get to a solution. Thank you.
allocate session value to variable like
$session_var = $_SESSION['fff'];
Don`t Use foreach()
Declare variable
$i=0;
while(($row = mysqli_fetch_array($result, MYSQLI_ASSOC)))
{
$sum += $row['Price'];
$s += $row['Weight'];
echo "<tr>";
echo "<td>" . $row['item_name'] . "</td>";
echo "<td> $". $row['Price'] . "</td>" ;
echo "<td>". $row['Weight'] ;
echo "<td>".$session_var[$i]."</td>";
$i++;
}
You can do it like so:
for($i = 0; $row = mysqli_fetch_array($result, MYSQLI_ASSOC); $i++) {
// access rows using $row
// access corresponding session data using $_SESSION['fff'][$i]
}
If your array keys in $_SESSION['fff'] are not numeric, add the following before the loop:
$session = array_values($_SESSION['fff']);
Then access values using $session[$i] within the loop.

MySQL output in PHP tables

I am currently designing a product selection page.
I wanna show my users a series of products that I have in my SQL table.
I want to output my SQL into php tables, but I dont want one long table.
I need one table per product.
In short terms,
Product 1, in one table
Break
Product 2, in other table.
<?php
$con=mysqli_connect("localhost","root","","headsets");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM `modeller");
echo "<table border='1'>";
$i = 0;
while($row = $result->fetch_assoc())
{
if ($i == 0) {
$i++;
echo "<tr>";
foreach ($row as $key => $value) {
echo "<th>" . $key . "</th>";
}
echo "</tr>";
}
echo "<tr>";
foreach ($row as $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
The sql part works fine, its just the way php output it I want to change
krasipenkovs code
My code
Try with this:
<?php
$con=mysqli_connect("localhost","root","","headsets");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM `modeller`");
$i = 0;
while($row = $result->fetch_assoc())
{
echo "<table border='1'>";
//if ($i == 0) {
//$i++;
//echo "<tr>";
//foreach ($row as $key => $value) {
//echo "<th>" . $key . "</th>";
//}
//echo "</tr>";
//}
echo "<tr>";
foreach ($row as $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
echo "</table><br />";
}
mysqli_close($con);
?>

How to deal with arrays in PHP language

<?php
$con = mysqli_connect('localhost', 'root', '');
if(!$con)
{
die("not ok");
}
mysqli_select_db($con,"uoh");
$q1 = "SELECT * FROM student_record INNER JOIN degree_plan ON
student_record.course_number = degree_plan.course_number
INNER JOIN courses ON student_record.course_number =
courses.course_number where student_record.id = 201102887 AND degree_plan.major='COE'";
$result = mysqli_query($con , $q1 ) ;
$data = array();
while($row = mysqli_fetch_array($result))
{
$data[$row["term_no"]][] = array(
'code' => $row["code"],
'grade' => $row["grade"]
);
}
echo '<table width="200" border="1">';
echo "<tr>";
echo "<th>courses</th>";
echo "<th>terms</th>";
echo "<th>grades</th>";
echo "</tr>";
foreach($data as $term=>$otherrow) {
$count = 0;
foreach ($otherrow as $data) {
if($count == 0) {
echo "<tr>";
echo "<td>" . $data["code"]. "</td>";
echo '<td rowspan="'.count($otherrow).'">' . $term. '</td>';
echo "<td>" . $data["grade"]. "</td>";
echo "</tr>";
}
else
{
echo "<tr>";
echo "<td>" . $data["code"]. "</td>";
echo "<td>" . $data["grade"]. "</td>";
echo "</tr>";
}
$count++;
}
}
echo "</table>";
?>
I have this code and it work very well but I faced problem when I want to add more column .
I tried to add fourth column(echo "<td>" . $row["crd"]. "</td>"; ) but there is no result .It give me empty cells. how I can do that?
I want add add this echo "<td>" . $row["crd"]. "</td>"; column to my code.
As mentioned in the comments, there are two errors that have been noticed.
You are re-declaring $data in your second foreach loop
You don't have $row initiated anywhere, and atempting to echo $row["crd"] will result in an empty cell.
Proposed Solution:
Change the name of the $data value in the foreach loop to $row and hence solve both problems at the same time:
foreach($data as $term=>$otherrow) {
$count = 0;
foreach ($otherrow as $row) {
if($count == 0) {
echo "<tr>";
echo "<td>" . $row["code"]. "</td>";
echo '<td rowspan="'.count($otherrow).'">' . $term. '</td>';
echo "<td>" . $row["grade"]. "</td>";
echo "</tr>";
}
else
{
echo "<tr>";
echo "<td>" . $row["code"]. "</td>";
echo "<td>" . $row["grade"]. "</td>";
echo "</tr>";
}
$count++;
}
}
And when you add echo "<td>" . $row["crd"]. "</td>"; now it should echo the value stored in the $row array (as long as the value was extracted from the table in the database in the first place of course).
Let me know if this worked for you.

Why is text written after a table tag in HTML source displaying before the table?

I wrote this code, but I faced a problem: when I run the code it shows me the result in the incorrect order.
<?php
$con = mysqli_connect('localhost', 'root', '');
if(!$con)
{
die("not ok");
}
mysqli_select_db($con,"uoh");
$q1 = "SELECT * FROM student_record INNER JOIN degree_plan ON
student_record.course_number = degree_plan.course_number
INNER JOIN courses ON student_record.course_number =
courses.course_number where student_record.id = 201102887 AND degree_plan.major='COE';";
$result = mysqli_query($con , $q1 ) ;
$data = array();
while($row = mysqli_fetch_array($result))
{
$data[$row["term_no"]][] = array(
'code' => $row["code"],
'grade' => $row["grade"],
'crd' => $row["crd"]
);
}
echo '<table>';
echo "<tr>";
echo "<th>courses</th>";
echo "<th>terms</th>";
echo "<th>grades</th>";
echo "<th>CRD</th>";
echo "</tr>";
foreach($data as $term=>$otherrow) {
$count = 0;
foreach ($otherrow as $row) {
if($count == 0) {
echo "<tr>";
echo "<td>" . $row["code"]. "</td>";
echo '<td rowspan="'.count($otherrow).'">' . $term. '</td>';
echo "<td>" . $row["grade"]. "</td>";
echo "<td>" . $row["crd"]. "</td>";
echo "</tr>";
}
else
{
echo "<tr>";
echo "<td>" . $row["code"]. "</td>";
echo "<td>" . $row["grade"]. "</td>";
echo "<td>" . $row["crd"]. "</td>";
echo "</tr>";
}
$count++;
}
}
echo "Hello";
?>
I wrote in the last of the code echo "Hello"; but when I run it, it displays Hello above the table, like this:
Hello
table
How I can make the the code first echo the table, then echo Hello?
like this
table
hello
Your <table> tag is not closed. In most browsers, this error will cause subsequent text to display before the table rows. This is why you are seeing "Hello" before your table content.
You can verify this by viewing the page source in your browser. The Hello will actually be after the table rows in the source, even though it shows up before them when the browser renders the source. In Firefox, the invalid HTML (<table> with no closing tag, invalid text inside a table) will be highlighted in red.

Categories