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);
?>
Related
I would like to retrieve my results from my DB in this format using Bootstrap.
Below is my PHP code that I'm currently using, the first entry I want the image to be bigger then the rest.
<?php
$article = mysqli_connect("localhost", "root", "", "blog");
// Check connection
if($article === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt select query execution
$sql = "SELECT * FROM news";
if($result = mysqli_query($article, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<container>";
echo "<row>";
echo "<th>id</th>";
echo "<th>title</th>";
echo "<th>body</th>";
echo "<th>image</th>";
echo "</div>";
while($row = mysqli_fetch_array($result)){
echo "<row>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['body'] . "</td>";
echo "<td>" . $row['image'] . "</td>";
echo "</row>";
}
echo "</div>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($article);
}
// Close connection
mysqli_close($article);
?>
I think you should have the condition to check for the first data in your loop to apply a bigger image size.
You can simply add condition:
$resultNum = 1;
while($row = mysqli_fetch_array($result)){
if($resultNum == 1) {
// TODO: show bigger image
} else {
// usual image
echo "<row>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['body'] . "</td>";
echo "<td>" . $row['image'] . "</td>";
echo "</row>";
}
$resultNum++;
}
Just make a counter and then do a condition using the first number of the counter
Your code simplyfied:
$query = mysqli_query($article, "SELECT * FROM news");
$n = 1; //start the counter
if(mysqli_num_rows($query) > 0){ //detect if have rows
foreach ($query as $key => $value) {
if($n == 1){
//print the big image
echo $value["id"];
}else{
//print the little image
echo $value["id"];
}
$n++;
}
}else{
echo "No data founded";
}
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>";
...
I have a table where all the values are selected from the database, but some cells are empty. How do i put a dropdown list inside that empty cell. The values from the dropdown must come from the database
This is my code:
<?php
include("css/style.php");
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "Iamthebest1009", "dktp");
// Check connection
if ($link === false) {
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$dropdown_list = '';
$sql = "SELECT * FROM orden";
$result_list = mysqli_query($link, $sql);
if (mysqli_num_rows($result_list) > 0) {
$dropdown_list = '<select>';
while ($row = mysqli_fetch_array($result_list)) {
unset($id, $name);
$id = $row['id'];
$name = $row['id'];
$dropdown_list .= '<option value="' . $id . '">' . $name . '</option>';
}
$dropdown_list .= '</select>';
}
// Attempt select query execution
$sql = "SELECT * FROM Norm LEFT JOIN Cluster ON norm.cluster_id = cluster.id LEFT JOIN Orden ON norm.orden_id = orden.id ORDER BY norm_name";
if ($result = mysqli_query($link, $sql)) {
if (mysqli_num_rows($result) > 0) {
echo '<form method="POST">';
echo "<table>";
echo "<tr>";
echo "<th>Norm id</th>";
echo "<th>Norm</th>";
echo "<th>Omschrijving</th>";
echo "<th>Clusteren</th>";
echo "<th>Ordenen</th>";
echo "</tr>";
while ($row = mysqli_fetch_array($result)) {
if ($row['orden_name']) {
$data_list = $row['id'];
} else {
$data_list = $dropdown_list;
}
echo "<tr>";
echo "<td>" . $row['norm_id'] . "</td>";
echo "<td>" . $row['norm_name'] . "</td>";
echo "<td>" . $row['description'] . "</td>";
echo "<td>" . $row['cluster_name'] . "</td>";
echo "<td>" . $data_list . "</td>";
echo "</tr>";
}
echo "</table>";
echo '<input type="submit" </input><form>';
// Free result set
mysqli_free_result($result);
} else {
echo "No records matching your query were found.";
}
} else {
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
if(isset($_POST['submit']))
{
$sql = "INSERT INTO norm (orden_id) VALUES ('$data_list')";
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}
// Close connection
mysqli_close($link);
?>
you can check this code. when $row['cluster_name'] empty then generate dropdown and first create dropdown then check your data exit or not but not tested
<?php
include("css/style.php");
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "Iamthebest1009", "dktp");
// Check connection
if ($link === false) {
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$dropdown_list = '';
$sql = "SELECT * FROM orden";
$result_list = mysqli_query($link, $sql);
if (mysqli_num_rows($result_list) > 0) {
$dropdown_list = '<select>';
while ($row = mysqli_fetch_array($result_list)) {
unset($id, $name);
$id = $row['id'];
$name = $row['orden_name'];
$dropdown_list .= '<option value="' . $id . '">' . $name . '</option>';
}
$dropdown_list .= '</select>';
}
// Attempt select query execution
$sql = "SELECT * FROM Norm LEFT JOIN Cluster ON norm.cluster_id = cluster.id LEFT JOIN Orden ON norm.orden_id = orden.id ORDER BY norm_name";
if ($result = mysqli_query($link, $sql)) {
if (mysqli_num_rows($result) > 0) {
echo "<table>";
echo "<tr>";
echo "<th>Norm id</th>";
echo "<th>Norm</th>";
echo "<th>Omschrijving</th>";
echo "<th>Clusteren</th>";
echo "<th>Ordenen</th>";
echo "</tr>";
while ($row = mysqli_fetch_array($result)) {
if ($row['cluster_name']) {
$data_list = $row['cluster_name'];
} else {
$data_list = $dropdown_list;
}
echo "<tr>";
echo "<td>" . $row['norm_id'] . "</td>";
echo "<td>" . $row['norm_name'] . "</td>";
echo "<td>" . $row['description'] . "</td>";
echo "<td>" . $data_list . "</td>";
echo "<td>" . $row['orden_name'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else {
echo "No records matching your query were found.";
}
} else {
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
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>";
}
I am trying to let the user input an account number and below the business name will show.
Here is the code I am using which is called from the form:
<?php
$q = intval($_GET['q']);
$con = mysql_connect("localhost","cl49-xxx","xxx");
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"cl49-xx");
$sql="SELECT * FROM member WHERE personID = '".$q."'";
$result = mysqli_query($con,$sql);
echo "Business Name:<table>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['businesstype'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
When I run this code it just shows business name:.
I think you have issue near while loop. Instead of "while($row = mysqli_fetch_array($result)) ". Why do not you use this:
$row = mysqli_fetch_array($result);
$i= 0;
while( $i<count($row))
{
echo "<tr>";
echo "<td>" . $row['businesstype'] . "</td>";
echo "</tr>";
$i++;
}
echo "</table>";