Display images from database into a Table on PHP - php

I have some problems creating a table that will display pictures from a php database
This is my code:
$query = "SELECT * FROM images ORDER BY name ASC ";
$result = $db->query($query);
$num_result = $result->num_rows;
echo "<h1> Images</h1>";
$array = array();
for ($i = 0; $i < $num_result; $i++){
$row = $result->fetch_assoc();
$name = $row['name'];
$URL = $row['imageURL'];
$array[] = $URL;
}
//this loop is printing the images correctly in order
foreach ($array as $image){
echo '<img src="'.$image.'"/>';
}
What I am trying to accomplish is to create a table with 2 Columns that will print the images there, something like this
echo '<table>';
echo ' <tr>';
echo ' <td>image 1</td>';
echo ' <td>image 2</td>';
echo ' </tr>';
echo ' <tr>';
echo ' <td>image 3</td>';
echo ' <td> image 4</td>';
echo ' </tr>';
// and so on if there is more images
echo '</table>';
Any suggestions will help , Thanks!

i think you want something like
$i=0;
echo"<table>";
echo"<tr>";
foreach ($array as $image)
{
if($i%2==0 && $i>0 )
echo"</tr><tr>";
echo"<td>";
echo '<img class="coupons" src="'.$image.'"/>';
echo"</td>";
$i++;
}
echo"</tr>";
echo"</table>";

Related

What to make checkbox for each row

In our website, we have textbox to ask input values from user. If user input the data Q54497899, rest of the results must be shown from database. Rightnow, I want to add checkbox on each result. I tried to add with foreach but it still doesn't work for me. I will provide codes below which were written in the correlationwafer_result.php. Want to add checkbox beside each Q54497899
<?php
// ini_set("memory_limit","512M");
include("_dbconn.php");
include("//sgewsnant21.amk.st.com/ewsweb/wwwroot/library/common7/db_config.inc");
include("//sgewsnant21.amk.st.com/ewsweb/wwwroot/library/common7/standard_defines.inc");
session_start();
$productlotid = isset ($_GET['productlotid'])? $_GET['productlotid']:'';
$sql = "SELECT * FROM productdb.tbl_correlationwafer WHERE `lotid` = '$productlotid'";
$result1 = mysqli_query($conn,$sql);
$cnt = 0;
echo "<table id='corwafer'>";
while ($row = mysqli_fetch_assoc($result1)) {
echo "<tr>";
echo "<th colspan='2'>Lot ID:</th>";
echo "<th colspan='2'>Product:</th>";
echo "<th colspan='4'>EWSFLOW </th>";
echo "<th>Zone</th>";
echo "</tr>";
$field1name = $row["lotid"];
$field2name = $row["product"];
$field3name = $row["ewsflow"];
$field4name = $row["zone"];
echo '<tr>
<td colspan="2">'.$field1name.'</td>
<td colspan="2">'.$field2name.'</td>
<td colspan="4">'.$field3name.'</td>
<td >'.$field4name.'</td>
</tr>';
foreach($productlotid as $k => $v){
if($k == $row["lotid"]){
echo "<input type=\"checkbox\" id=\"chkproductlotid" . $cnt . "\" name=\"chkproductlotid\" value=\"$v\"><font face=\"arial\" size=\"2\" color=\"#3C5F84\">". $k . "</font>";
}
}
$cnt++;
}
echo "</table>";
flush();
mysqli_close($conn);
?>
Try :
$('chkproductlotid').val();
$productlotid = isset ($_GET['productlotid'])? $_GET['productlotid']:'';
Your $productlotid is more like a string not an array, does it?
You should put your checkbox inside td, and I think your checkbox value should be able to take from the first While Loop, no need that Foreach..

Two articles in my php array

I'm trying to make a webshop for an assignment and I can't figure out how to display that there are 2 of the same product in my checkout. This is my code I want to display something like: Article1 .. 2x Article2 ... 5x etc.
<?php
session_start();
//read out session with article numbers
$array = $_SESSION['mandje'];
echo '<center>';
echo '<h1> Uw mandje: </h1>';
echo '<table style="border: 2px solid black">';
//array that checks if there are two of the same article numbers
$mandje = array();
//read out array
foreach ($array as $artikel)
{
echo '</br>';
echo $artikel;
if (in_array($artikel, $mandje)){
//I need to display the article that i have multiple times here i guess
} else {
//getting the articles out of the database
require ('config.php');
$query = "SELECT * FROM mphp6_meubels WHERE artikelnr = $artikel";
$results = mysqli_query($mysqli, $query);
while($meubel = mysqli_fetch_array($results)){
$video = $meubel['naam'];
$nmr = $meubel['artikelnr'];
echo '<tr>';
echo '<td> <img src="Meubels/'.$video.'.jpg" width="150" height="150" alt="Meubel"></td>';
echo '</tr>';
}
}
//adding the article to array so i can check if there are multiple of the same articles in array
$mandje[] = $artikel;
}
echo '</table>';
echo '</center>';
Just group by artikelnr field.
foreach ($array as $artikel)
{
require ('config.php');
$query = "SELECT mphp6_meubels.*, count(*) as cnt FROM mphp6_meubels WHERE artikelnr = $artikel GROUP BY artikelnr";
$results = mysqli_query($mysqli, $query);
while($meubel = mysqli_fetch_array($results)){
$video = $meubel['naam'];
$nmr = $meubel['artikelnr'];
echo '<tr>';
echo '<td>' . $artikel . '</td>';
echo '<td> <img src="Meubels/'.$video.'.jpg" width="150" height="150" alt="Meubel"></td>';
echo '<td> ' . $meubel['cnt'] . '</td>';
echo '</tr>';
}
}

How to integrate array_column() and array_filter() into dynamic table generation of sql results

I want to read out data from an sql-database an show them in a table. This works well. Now, I would like to show only those columns with at least one value in it and not the empty ones (containing NULL, 0, empty string). This works with the following example:
enter code here
<TABLE width="500" border="1" cellpadding="1" cellspacing="1">
<?php
$query = mysql_query("SELECT * FROM guestbook", $db);
$results = array();
while($line = mysql_fetch_assoc($query)){
$results[] = $line;
$Name = array_column($results, 'Name');
$Home = array_column($results, 'Home');
$Date = array_column($results, 'Date');
$Emptycolumn = array_column($results, 'Emptycolumn');
$Comment = array_column($results, 'Comment');
$City = array_column($results, 'City');
}
echo "<TR>";
if(array_filter($Name)) {echo "<TH>Name</TH>";}
if(array_filter($Home)){echo "<TH>Home</TH>";}
if(array_filter($Date)){echo "<TH>Date</TH>";}
if(array_filter($Emptycolumn)){echo "<TH>Emptycolumn</TH>";}
if(array_filter($Comment)){echo "<TH>Comment</TH>";}
if(array_filter($City)){echo "<TH>City</TH>";}
echo "</TR>";
$query = mysql_query("SELECT * FROM guestbook", $db);
while($line = mysql_fetch_assoc($query)){
echo "<TR>";
if(array_filter($Name)) {echo "<TD>".$line['Name']."</TD>";}
if(array_filter($Home)) {echo "<TD>".$line['Home']."</TD>";}
if(array_filter($Date)) {echo "<TD>".$line['Date']."</TD>";}
if(array_filter($Emptycolumn)) {echo "<TD>".$line['Emptycolumn']."</TD>";}
if(array_filter($Comment)) {echo "<TD>".$line['Comment']."</TD>";}
if(array_filter($City)) {echo "<TD>".$line['City']."</TD>";}
echo "</TR>";
}
?>
</TABLE>
Since the column-names of my table are highly variable (depending on the query), the table is generated by looping through the result-array, first the column-names, then the values in the rows:
enter code here
$sql = "SELECT DISTINCT $selection FROM $tabelle WHERE
$whereclause"; //will be changed to PDO
$result = mysqli_query($db, $sql) or die("<b>No result</b>"); //Running
the query and storing it in result
$numrows = mysqli_num_rows($result); // gets number of rows in result
table
$numcols = mysqli_num_fields($result); // gets number of columns in
result table
$field = mysqli_fetch_fields($result); // gets the column names from the
result table
if ($numrows > 0) {
echo "<table id='myTable' >";
echo "<thead>";
echo "<tr>";
echo "<th>" . 'Nr' . "</th>";
for($x=0;$x<$numcols;$x++){
$key = array_search($field[$x]->name, $custom_column_arr);
if($key !== false){
echo "<th>" . $key . "</th>";
}else{
echo "<th>" . $field[$x]->name . "</th>";
}
}
echo "</tr></thead>";
echo "<tbody>";
$nr = 1;
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $nr . "</td>";
for ($k=0; $k<$numcols; $k++) { // goes around until there are no
columns left
echo "<td>" . $row[$field[$k]->name] . "</td>"; //Prints the data
}
echo "</tr>";
$nr = $nr + 1;
} // End of while-loop
echo "</tbody></table>";
}
}
mysqli_close($db);
Now, I tried to integrate the array_column() and array_filter()-blocks of the example above into the loops, but unfortunately, it didn´t work. I´m sure, this is easy for a professional and I would be very grateful, if someone could help me with this problem!
Thank you very much in advance!!

Writing the attributes of a database in PHP

I am writing an application in which user can enter a database name and I should write all of its contents in table with using PHP.I can do it when I know the name of database with the following code.
$result = mysqli_query($con,"SELECT * FROM course");
echo "<table border='1'>
<tr>
<th>blablabla</th>
<th>blabla</th>
<th>blablabla</th>
<th>bla</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['blablabla'] . "</td>";
echo "<td>" . $row['blabla'] . "</td>";
echo "<td>" . $row['blablabla'] . "</td>";
echo "<td>" . $row['bla'] . "</td>";
echo "</tr>";
}
echo "</table>";
In this example I can show it since I know the name of table is course and it has 4 attributes.But I want to be able to show the result regardless of the name the user entered.So if user wants to view the contents of instructors there should be two columns instead of 4.How can I accomplish this.I get the table name with html.
Table:<input type="text" name="table">
Edit:Denis's answer and GrumpyCroutons' answer are both correct.You can also ask me if you didnt understand something in their solution.
Quickly wrote this up, commented it (This way you can easily learn what's going on, you see), and tested it for you.
<form method="GET">
<input type="text" name="table">
</form>
<?php
//can be done elsewhere, I used this for testing. vv
$config = array(
'SQL-Host' => '',
'SQL-User' => '',
'SQL-Pass' => '',
'SQL-Database' => ''
);
$con = mysqli_connect($config['SQL-Host'], $config['SQL-User'], $config['SQL-Pass'], $config['SQL-Database']) or die("Error " . mysqli_error($con));
//can be done elsewhere, I used this for testing. ^^
if(!isSet($_GET['table'])) { //check if table choser form was submitted.
//In my case, do nothing, but you could display a message saying something like no db chosen etc.
} else {
$table = mysqli_real_escape_string($con, $_GET['table']); //escape it because it's an input, helps prevent sqlinjection.
$sql = "SELECT * FROM " . $table; // SELECT * returns a list of ALL column data
$sql2 = "SHOW COLUMNS FROM " . $table; // SHOW COLUMNS FROM returns a list of columns
$result = mysqli_query($con, $sql);
$Headers = mysqli_query($con, $sql2);
//you could do more checks here to see if anything was returned, and display an error if not or whatever.
echo "<table border='1'>";
echo "<tr>"; //all in one row
$headersList = array(); //create an empty array
while($row = mysqli_fetch_array($Headers)) { //loop through table columns
echo "<td>" . $row['Field'] . "</td>"; // list columns in TD's or TH's.
array_push($headersList, $row['Field']); //Fill array with fields
} //$row = mysqli_fetch_array($Headers)
echo "</tr>";
$amt = count($headersList); // How many headers are there?
while($row = mysqli_fetch_array($result)) {
echo "<tr>"; //each row gets its own tr
for($x = 1; $x <= $amt; $x++) { //nested for loop, based on the $amt variable above, so you don't leave any columns out - should have been <= and not <, my bad
echo "<td>" . $row[$headersList[$x]] . "</td>"; //Fill td's or th's with column data
} //$x = 1; $x < $amt; $x++
echo "</tr>";
} //$row = mysqli_fetch_array($result)
echo "</table>";
}
?>
$tablename = $_POST['table'];
$result = mysqli_query($con,"SELECT * FROM $tablename");
$first = true;
while($row = mysqli_fetch_assoc($result))
{
if ($first)
{
$columns = array_keys($row);
echo "<table border='1'>
<tr>";
foreach ($columns as $c)
{
echo "<th>$c</th>";
}
echo "</tr>";
$first = false;
}
echo "<tr>";
foreach ($row as $v)
{
echo "<td>$v</td>";
}
echo "</tr>";
}
echo "</table>";
<?php
$table_name = do_not_inject($_REQUEST['table_name']);
$result = mysqli_query($con,'SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME='. $table_name);
?>
<table>
<?php
$columns = array();
while ($row = mysql_fetch_assoc($result)){
$columns[]=$row['COLUMN_NAME'];
?>
<tr><th><?php echo $row['COLUMN_NAME']; ?></th></tr>
<?php
}
$result = mysqli_query($con,'SELECT * FROM course'. $table_name);
while($row = mysqli_fetch_assoc($result)){
echo '<tr>';
foreach ($columns as $column){
?>
<td><?php echo $row[$column]; ?></td>
<?php
}
echo '</tr>';
}
?>
</table>

Display SQLITE output in column rather than row

At the moment I have the below script which auto generates the table names and row data automatically by looking at a sqlite table. So regardless of if you have 2 or 10 columns this script works.
At the moment the script outputs the results like this:
Output currently appears as a Row
I have tried altering the script so that it outputs the results like below. Can someone assist or guide me in the right direction to achieve this?
Is it possible to output the results of the query in the below format: going down in a column rather than across as a row ?
Output should appear as a Column
<?
$ED = $_GET['ED'];
$ID = $_GET['ID'];
$table_name = $_GET['table'];
?>
<table border="1">
<tr>
<td>
<table>
<?php // Display all sqlite column names for chosen table
$tablesquery = $db->query("PRAGMA table_info($table_name)");
while ($table = $tablesquery->fetchArray(SQLITE3_ASSOC)) {
if ($table['name'] == "ID") {
echo "<tr><td>" . $table['name'] . "</td></tr>";
} else {
$table_name_header = ucwords(strtolower(str_replace('_', ' ', $table['name'])));
echo "<tr><td>" . $table_name_header . "</td></tr>";
}
}
?>
</table>
</td>
<td>
<table>
<?
// Display all sqlite data for chosen table
$tablesquery = $db->query("PRAGMA table_info($table_name)");
$columns = array();
while ($table = $tablesquery->fetchArray(SQLITE3_ASSOC)) {
$columns[] = $table['name'];
}
// Display * from USERS
// $results = $db->query('SELECT * FROM ADMIN_LOGIN WHERE ID = "57"');
$results = $db->query('SELECT * FROM ' . $table_name . ' WHERE ID = "' . $ID . '"');
while ($row = $results->fetchArray()) {
// echo "<tr>";
$test = $row[0];
foreach ($columns as $col)
echo "<tr><td>" . $row[$col] . "</td></tr>";
}
// echo "</tr>";
?>
</table>
</td>
</tr>
</table>
Modifying the code to the below by putting the data into an combined array and then pulling it back via a loop it will display as required:
<?
// Display all sqlite data for chosen table
$tablesquery = $db->query("PRAGMA table_info($table_name)");
$columns = array();
while ($table = $tablesquery->fetchArray(SQLITE3_ASSOC)) {
$columns[] = $table['name'];
}
// Display * from USERS
// $results = $db->query('SELECT * FROM ADMIN_LOGIN WHERE ID = "57"');
$results = $db->query('SELECT * FROM ' . $table_name . ' WHERE ID = "' . $ID . '"');
while ($row = $results->fetchArray()) {
// echo "<tr>";
$test = $row[0];
foreach ($columns as $col)
$column_data[] = $row[$col];
// echo "<tr><td>" . $row[$col] . "</td></tr>";
}
// echo "</tr>";
?>
<?
$array = $columns;
$array2 = $column_data;
$result = array_combine($array, $array2);
//print_r($result);
?><br><br>
<center>
<table border="0" cellpadding="2" cellspacing="2" color="#4B708D">
<thead>
<?
foreach($result as $key => $value) {
echo "<tr><td bgcolor='#c6d5e1'>$key</td><td bgcolor='#FFFFFF'>$value</td></tr>";
}
?>
</thead>
</table>

Categories