How do I input SQL Database information into a php table - php

I have both the Database information inputted which includes a team logo and the corresponding team name.
Despite various google searches and forum searching, I cannot find a solution.
Any help would be greatly appreciated.
<!DOCTYPE html>
<html>
<body>
<?php
$connection = mysqli_connect("localhost","root","");
mysqli_select_db($connection,"6nationsDB");
$result=mysqli_query($connection,"select * from teams");
$counter = 1;
while($row=mysqli_fetch_array($result))
{
$image="images/$counter.png";
print"<img src=\"$image\" width=\"15px\" height=\"15px\"\/>";
print($row['name']);
print("<br>");
$counter++;
}
$rows = 10; // amout of tr
$cols = 10;// amjount of td
function drawTable($rows, $cols){
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
for($td=1;$td<=$cols;$td++){
echo "<td align='center'>".'<img src=\"$image\" width=\"15px\" height=\"15px\"\/>'."</td>";
}
echo "</tr>";
}
echo "</table>";
}
drawTable(6, 2);
mysqli_close($connection);
?>
</body>
</html>

Related

PHP - linking a table to HTML and doing multiplication

I have the following php code that generates a 10X10 table:
<?php
echo "<table border =\"1\" style='border-collapse: collapse'>";
for ($row=1; $row <= 10; $row++) {
echo "<tr> \n";
for ($col=1; $col <= 10; $col++) {
$a = "$row * $col";
echo "<td><a href = '$a'>$a</a></td> \n";
}
echo "</tr>";
}
echo "</table>";
?>
How to recreate this table in HTML, such that links will work?
Every field in a table needs to do a multiplication, e.g. field '5*6' gives result '30'. How to write a php class that will do this operation? So, for row*column, return variable result.
in your Calculator.php
<?php
Class Calculator {
public function calculate($row, $col){
return $result = $row * $col;
}
}
Change index.php as follow.
<?php include_once('Calculator.php'); ?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
a{
cursor: pointer;
}
</style>
</head>
<body>
<div>
<input type="text" value="<?php if( (isset($_GET['col']) && $_GET['row'])){ echo Calculator::calculate($_GET['row'],$_GET['col']); } ?>">
</div>
<?php
echo "<table border =\"1\" style='border-collapse: collapse'>";
for ($row=1; $row <= 10; $row++) {
echo "<tr> \n";
for ($col=1; $col <= 10; $col++) {
$a = "$row * $col";
echo "<td><a href=?row=$row&col=$col>".$a;
//if( (isset($_GET['col']) && $_GET['row'] && $row==$_GET['row'] && $col==$_GET['col'])){ echo Calculator::calculate($row,$col); }else{ echo $a ;}
echo "</a></td> \n";
}
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
$a = $row * $col;
Just remove the double quotes so it will multiple the numbers and not consider it a string.

How to check table cell in database and output results?

I am bit new to php and sql.
I am simply reading from a database table and echoing images to a page. The images are then styled with a text aligned in the middle saying 'Play Video'.
In the table 'adcTable' some entries do not have a video stored.
I would like to have the rows/entries with video say 'Play Video' and the ones without just show the image.
Not to sure how bess to explain. Hope to get some assistance.
<php
$sql = "SELECT client_id, images, video FROM adcTable";
$result = $conn->query($sql);
$count = 1;
echo "<table border = '0' width='720'><tr>";
while($row = $result->fetch_assoc()) {
echo "<td><div class='ccontainer'>"; ?><img class="cimage" src= "<?php echo $row ["images"];?> " ><?php echo "
<div class='middle'>
<div class='text'>Play Video</div>
</div>
</div>
</td>";
if ($count++ % 2 == 0) {
echo "</tr><tr>";
}
echo "</tr></table>";
?>
Thanks guys, I was able to use the example provided by BusinessPlanQuickBuilder to solve.
$sql = "SELECT client_id, files, file FROM adcTable";
$result = $conn->query($sql);
$count = 1;
echo "<table border = '0' width='720'><tr>";
while($row = $result->fetch_assoc()) {
if ($row['file'] == "VidUploads/"){
echo "<td>"; ?><img class="cimage" src= "<?php echo $row ["files"];?> " ><?php echo "
</td>";
echo '<script type="text/javascript">',
'$( ".text" ).css( "border", "3px solid red" );',
'</script>';
} else{
echo "<td><div class='ccontainer'>"; ?><img class="cimage" src= "<?php echo $row ["files"];?> " ><?php echo "
<div class='middle'>
<div class='text'>Video</div>
</div>
</div>
</td>";
}
if ($count++ % 2 == 0) {
echo "</tr><tr>";
}
}
echo "</tr></table>";
?>
Use if empty condition on video column. This is the fundamental code, add your formatting as required.
<?php
while($row = $result->fetch_assoc()) {
if (empty ($row['video']) ) {
echo $row ["images"];
} else {
echo "Play Video";
}
}
?>
SO reference to related post

PHP dynamic table header and dynamic table rows based on header - tried everything

I am trying to build a editable table with dynamic headers and dynamic rows based on header, but I lost my way in the process.
I got the dynamic headers:
<div class="jumbotron">
<div class="container">
<table class='table'>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Emails</th>
<?php
include("../connection/connection.php");
$get_title = "SELECT DISTINCT(title) FROM attendance";
$query1 = mysqli_query($con,$get_title);
if ($query1) {
# code...
while ($row=mysqli_fetch_array($query1)) {
# code...
$title[] = $row['title'];
}
$count = count($title);
for ($i=0; $i <$count ; $i++) {
# code...
echo "<th>".$title[$i]."</th>";
}
}
?>
</tr>
</thead>
<tbody>
<?php
$get_emails = "SELECT * FROM subjects";
$query2 = mysqli_query($con,$get_emails);
if ($query2) {
# code...
while ($row=mysqli_fetch_array($query2)) {
# code...
$emails = $row['email'];
$id = $row['id'];
$get_name = "SELECT * FROM users";
$query3 = mysqli_query($con,$get_name);
if ($query3) {
# code...
while ($row=mysqli_fetch_array($query3)) {
# code...
$fullname = $row['user_fullname'];
echo "<tr>";
echo "<td contenteditable>".$id."</td>";
echo "<td contenteditable>".$fullname."</td>";
echo "<td contenteditable>".$emails."</td>";
echo "</tr>";
}
} else {
echo "<tr>";
echo "<td contenteditable>".$id."</td>";
echo "<td contenteditable></td>";
echo "<td contenteditable>".$emails."</td>";
echo "</tr>";
}
}
} else {
echo "<tr>";
echo "<td contenteditable></td>";
echo "<td contenteditable></td>";
echo "<td contenteditable></td>";
echo "</tr>";
}
?>
</tbody>
</table>
</div>
</div>
Now I need to echo the table td which would be dynamic based on the headers after the emails - td inside the tr.
Any help is appreciated.
The major problem you have is that the array element isn't going to be called title but rather DISTINCT(title) because you didn't alias it as something else, the code below shows it being aliased back as title, and as well eliminating what appears to be an unnecessary loop.
$get_title = "SELECT DISTINCT(title) title FROM attendance";
$query1 = mysqli_query($con,$get_title);
if ($query1) {
# code...
while ($row=mysqli_fetch_array($query1)) {
# code...
echo "<th>".$title[$i]$row['title']."</th>";
}
}

PHP result set and HTML positioning

I have problem with positioning html tags I did try to solve the problem but for me the code looks fine however the output tells something different I did try do to sort out the output of SQL query however I wasn't able to achieve this and when I did asked for help in the forum in reply I get that it would be much easier to do it in PHP. So I have produce following code but I am not getting required results:
<body>
<div class="container">
<?php
$sdate = '';
foreach($rows as $row) {
if($row['shieldDate'] != $sdate){
$sdate = $row['shieldDate'];
echo '<h2>';
echo $row['shieldDate'],'&nbsp','opponent','&nbsp',$row['shieldTeam'];
echo '</h2>';
echo "
<p>The .table class adds basic styling (light padding and only horizontal dividers) to a table:</p>
<table class='table'>
<thead>
<tr>
<th>Player</th>
<th>Score</th>
</tr>
</thead>
<tbody>
";
echo "<tr>";
echo "<td>";
echo $row["firstname"],'&nbsp', $row["lastname"];
echo "</td>";
echo "<td>";
echo $row["score"];
echo "</td>";
echo "</tr>";
}else{
echo "<tr>";
echo "<td>";
echo $row["firstname"],'&nbsp', $row["lastname"];
echo "</td>";
echo "<td>";
echo $row["score"];
echo "</td>";
echo "</tr>";
}
}
?>
</tbody>
</table>
</div>
</body>
I am trying to acheive following output:
However I am getting:
You don't close your tbody and table tags between two dates.
Each time you encounter a new date, you open a new table tag, but you never close it before having a new h2 date title. Add a line to close it.
$sdate = '';
foreach($rows as $row) {
if($row['shieldDate'] != $sdate){
// It's a new date
if (!empty($sdate)) {
// It's not the first date: close previous table
echo '</tbody></table>';
}
$sdate = $row['shieldDate'];
echo '<h2>';
echo $row['shieldDate'],'&nbsp','opponent','&nbsp',$row['shieldTeam'];
echo '</h2>';
echo "<p>The .table class adds basic styling (light padding and only horizontal dividers) to a table:</p>
<table class='table'>
<thead>[...]</thead>
<tbody>";
[...]
} else {
[...]
}
}
if (!empty($sdate)) {
// There has been at least one date (at least one table): close it
echo '</tbody></table>';
}
Note that the [...] content is the same in if and else instructions.
You could do this to have something cleaner. The less duplicate code you have, the more easy it is to read. The more easy to read your code is, the better.
foreach($rows as $row) {
if($row['shieldDate'] != $sdate){
// Do your h2 and open table stuff
}
echo "<tr>";
echo "<td>";
// etc. No need to put it in the if AND in the else instructions.
}

display 2 or 3 images per row using php mysql

hi i'm disaplyed images from mysql db table but it displays on by one means one row has one image. but i need 3 or 4 image per row. my coding is below. please give some idea.
<?php
include_once("config.php");
$result=mysql_query("SELECT * FROM merchant");
while($res=mysql_fetch_array($result))
{
?>
<?php echo $res['description'];?></p>
<img src="<?php echo $res['image'];?>" width="80" height="80"/>
<?php } ?>
Do it in table like this, You might need to fix it a little bit, but it way how it will work
<table>
<?php
include_once("config.php");
$result=mysql_query("SELECT * FROM merchant");
$count = 0;
while($res=mysql_fetch_array($result))
{
if($count==3) //three images per row
{
print "</tr>";
$count = 0;
}
if($count==0)
print "<tr>";
print "<td>";
?>
<?php echo $res['description'];?></p>
<img src="<?php echo $res['image'];?>" width="80" height="80"/>
<?php
$count++;
print "</td>";
}
if($count>0)
print "</tr>";
?>
</table>
use a <table> to display.
<?php
include_once("config.php");
$result=mysql_query("SELECT * FROM merchant");
$count = 0;
echo '<table>';
while($res = mysql_fetch_array($result))
{
if($count % 2 == 0) echo '<tr>';
?>
<td>
<p><?php echo $res['description'];?></p>
<img src="<?php echo $res['image']; ?>" width="80" height="80"/>
</td>
<?php
if($count % 2 == 0) echo '</tr>';
} ?>

Categories