I need my table to include submissions into three columns then a new row start with three more columns, etc. So submission 1, first column, submission 2, second column, submission 3, 3 column. Then a new row and the process starts again. (HTML Example Code below)
Every time I try with my php code my page comes up blank so I am missing something somewhere.
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, name, website, url, email, date, description FROM basic";
$result = $conn->query($sql);
if ($result->num_rows>0){
<tr>
while ($row=$result->fetch_assoc()) {
echo "<table><tr><th><b>Company:</b> " . $row["name"]. "</th></tr><tr><td>
<b>URL:</b> <a href='".$row["url"]."'>".$row["url"]."</a></td></tr><tr><td>
<b>Email:</b> " . $row["email"]. "</td></tr><tr><td><b>Launch Date:</b> " .
$row["date"]. "</td></tr><tr><td><b>Announcement:</b> " .
$row["description"]. "</td>
</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
Example: This is what I am trying to get it to look like.
<style>
table, td, th {
border: 10px solid #ffffff;
text-align: left;
}
table {
border-collapse: initial;
width: 100%;
}
td {
padding: 15px;
width: 25%;
line-height: 2;
}
th {
background-color: grey;
color: white;
padding: 5px;
width: 25%;
}
</style>
<body>
<table class="ex1">
<tr>
<th>Company</th>
<th>Company</th>
<th>Company</th>
</tr>
<tr>
<td>
<b>URL:</b><br>
<b>Location:</b><br>
<b>Inductry:</b><br>
<b>Start Date:</b><br>
<b>Announcement:</b><br>
<br>
</td>
<td>
<b>URL:</b><br>
<b>Location:</b><br>
<b>Inductry:</b><br>
<b>Start Date:</b><br>
<b>Announcement:</b><br>
<br>
</td>
<td>
<b>URL:</b><br>
<b>Location:</b><br>
<b>Inductry:</b><br>
<b>Start Date:</b><br>
<b>Announcement:</b><br>
<br>
</td>
</table>
Your PHP code is not generating HTML in proper format. Please try below:
// Query executed here and resultset returned
if ($result->num_rows>0){
echo "<table>";
while ($row=$result->fetch_assoc()) {
echo "<tr><th><b>Company:</b> " . $row["name"]. "</th></tr>
<tr><td><b>URL:</b> <a href='".$row["url"]."'>".$row["url"]."</a></td></tr>
<tr><td><b>Email:</b> " . $row["email"]. "</td></tr>
<tr><td><b>Launch Date:</b> " . $row["date"]. "</td></tr>
<tr><td><b>Announcement:</b> " . $row["description"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
Related
When reading and displaying data which is in cyrilic from a Microsoft SQL Server, PHP fails to display it correctly. You can see part of the text displayed (only the non cyrilic stuff)
Here is what I have tried:
setting the values in the table nchar, varchar, text and char
adding a utf-8 tag in the html code
using the PHP driver for SQL with the ODBC driver
tried these collations: Cyrillic_General_100_CI_AI_SC_UTF8, cyrillic general with utf-8 and the windows code page (1251) with cyrillic general
<html>
<style>
table { width: 20em; border-collapse: collapse; }
th {
border-bottom: 2px solid #000;
padding: 0.5em 0 0.1em 0;
font-size: 1.2em;
}
td {
border-bottom: 2px solid #ccc;
padding: 0.5em 0 0.1em 0;
}
th:nth-child(n + 2),
td:nth-child(n + 2) {
text-align: center;
}
[data-has-link="no"] { background-color: #F77; }
[data-has-link="yes"] { background-color: #7F7; }
#score, #name { width: 50%; }
</style>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="jo.js"></script>
<title>AV.31.U</title>
</head>
<body>
<table style="width: 100%; height:50%" border="1" cellpadding="3">
<caption>AV.31.U</caption>
<thead>
<tr>
<td><div id="current_date"></p>
<script>
$(function() {
var $table = $('table');
$table.find('#current_date').text(getCurrentDate());
colorRows($table); // Color the rows!
});
function getCurrentDate () {
return new Date().toLocaleDateString('en-GB', {
year: 'numeric',
month: 'numeric',
day: 'numeric'
});
}
function colorRows($table) {
var hasLink;
$table.find('tbody > tr').each(function(rowIndex) {
const $row = $(this);
$row.find('td').each(function(colIndex) {
const $cell = $(this).removeAttr('data-has-link');
const cellValue = $cell.text().trim();
if (isFinite(cellValue)) {
// Color cell based on individual data
const hasLink = cellHasLink(parseInt(cellValue, 10));
if (hasLink !== 'maybe') {
$cell.attr('data-has-link', hasLink);
}
}
});
// Color row based on 7th column
var i = parseInt($row.find('td:nth-child(7)').text(), 10);
$row.attr('data-has-link', cellHasLink(i));
});
}
function cellHasLink(value) {
switch (value) {
case 0 : return 'no';
case 1 : return 'yes';
default : return 'maybe';
}
}
</script></td>
<td colspan="6">Home</td>
<td> </td>
</tr>
<tr>
<th>Наименование</th>
<th>Описание</th>
<th>Версия</th>
<th>Верс. опис.</th>
<th>Модел</th>
<th>Видео</th>
<th>Видео отвори</th>
<th>Снимк.</th>
<th>Снимк. отвори</th>
<th>Специф.</th>
<th>Специф. отвори</th>
</tr>
</thead>
<tbody>
<tr>
<?php
$username = 'censored';
$password = 'censored';
$servername = 'censored';
$database = 'SQL_KASI';
ini_set('display_errors', '1');
error_reporting(E_ALL);
$db = odbc_connect("Driver={SQL Server};Server=$servername;Database=$database;", $username, $password) or die ("could not connect<br />");
$stmt = "Select * from machine_vertical";
$result = odbc_exec($db, $stmt);
if ($result == FALSE) die ("could not execute statement $stmt<br />");
while (odbc_fetch_row($result)) // while there are rows
{
print "<tr>\n";
print " <td>" . odbc_result($result, "mach_name") . "\n";
print " <td>" . odbc_result($result, "mach_descr") . "\n";
print " <td>" . odbc_result($result, "mach_version") . "\n";
print " <td>" . odbc_result($result, "mach_version_descr") . "\n";
print " <td>" . odbc_result($result, "mach_model") . "\n";
print " <td>" . odbc_result($result, 'mach_video_yn') . "\n";
print " <td>" . odbc_result($result, "mach_video_open") . "\n";
print " <td>" . odbc_result($result, "mach_pic_yn") . "\n";
print " <td>" . odbc_result($result, 'mach_pic_open') . "\n";
print " <td>" . odbc_result($result, "mach_spec_yn") . "\n";
print " <td>" . odbc_result($result, "mach_spec_open") . "\n";
print "</tr>\n";
}
odbc_free_result($result);
odbc_close($db);
?>
</tr>
</tbody>
</table>
</body>
</html>
I have a problem in inserting table from my database to div.
html code:
<div class="content">
<?php
$query = "SELECT name, surname FROM players";
$response = #mysqli_query($dbc, $query);
if($response) {
echo' <table align="left"
cellspacing="5" cellpadding="8" >
<tr><td align="left"><b>First Name</b></td>
<td align="left"><b>Last Name</b></td></tr>';
while($row = mysqli_fetch_array($response)) {
echo '<tr><td align="left">' .
$row['imie'] . '</td><td align="left">' .
$row['nazwisko'] . '</td><td align="left">';
echo '</tr>';
}
echo '</table>';
} else {
echo "Couldn't issue database query";
echo mysqli_error($dbc);
}
mysqli_close($dbc);
?>
</div>
css code:
.content {
width: 1000px;
margin-left: auto;
margin-right: auto;
background-color: #ffffff;
color: #000000;
border-bottom: 14px solid #333333;}
Is there a way to insert this table to this div? Because when I'm loading this, the table is under my div'content'. The table should be in blank field 'a'.
Screenshot
Correctly indenting your code would have allowed you o spot the error relatively easily - there is an open td element. Either remove the last td from the echo statement in the loop or add additional td pair to first row in table so that they balance ( unless you use colspan attributes )
<style>
.content table tr td{ align:left;padding:5px;color:black;background:white; }
</style>
<div class='content'>
<?php
$sql = 'select `name`, `surname` from `players`';
$response = mysqli_query( $dbc, $sql );
if( $response ) {
echo "
<table align='left' cellspacing='5' cellpadding='8' >
<tr>
<td><b>First Name</b></td>
<td><b>Last Name</b></td>
</tr>";
while( $row = mysqli_fetch_array( $response ) ) {
echo "
<tr>
<td>{$row['imie']}</td>
<td>{$row['nazwisko']}</td>
</tr>";
}
echo "
</table>";
} else {
printf( 'Couldn\'t issue database query: %s', mysqli_error( $dbc ) );
}
mysqli_close( $dbc );
?>
</div>
There is an open <td> tag in your code you should close it before closing the <tr>
Why am I getting those extra layers in the table output? I need to remove those extra layers from the table.What am I doing wrong? What is going on here exactly? Updated code.
<?php
$con=mysqli_connect("localhost","root", "", "cop");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql= mysqli_query ($con, "SELECT FirstName, Practice FROM Members");
echo "
<style>th {
font: bold 11px 'Trebuchet MS', Verdana, Arial, Helvetica,
sans-serif;
color: #6D929B;
border-right: 1px solid #C1DAD7;
border-bottom: 1px solid #C1DAD7;
border-top: 1px solid #C1DAD7;
letter-spacing: 2px;
text-transform: uppercase;
text-align: left;
padding: 6px 6px 6px 12px;
background: #CAE8EA url(images/bg_header.jpg) no-repeat;
}
th.nobg {
border-top: 0;
border-left: 0;
border-right: 1px solid #C1DAD7;
background: none;
}</style>
<table border='1'>
<tr>
<th>FirstName</th>
<th>Practice</th>
</tr>";
while($row = mysqli_fetch_array($sql))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['Practice'] . "</td>";
echo "</tr>";
}
echo "</table>";
Unusual Table output
If you were to do something like this you would be able to tell if there were empty column values in the data
while( $row = mysqli_fetch_array($sql) ){
if( !empty( $row['FirstName'] ) && !empty( $row['Practice'] ) ){
$firstname=$row['FirstName'];
$practice=$row['Practice'];
} else{
$firstname=$practice='empty';
}
echo "
<tr>
<td>{$firstname}</td>
<td>{$practice}</td>
</tr>";
}
Alternatively just output the row if it is known to contain values rather than having empty cells
while( $row = mysqli_fetch_array($sql) ){
if( !empty( $row['FirstName'] ) && !empty( $row['Practice'] ) ){
$firstname=$row['FirstName'];
$practice=$row['Practice'];
echo "
<tr>
<td>{$firstname}</td>
<td>{$practice}</td>
</tr>";
}
}
I pasted your code on my Notepad++ and in it were errors which the browser cannot forgive. In fact you can even receive a 1 month suspended sentence from your browser. Below is my version of your code. I have ignored the style. You can add style after the code is working.
<?php
$con=mysqli_connect("localhost","root", "", "cop");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql= mysqli_query($con, "SELECT FirstName, Practice FROM Members");
$result = '';
if(mysqli_num_rows($sql) > 0){
while($row = mysqli_fetch_array($sql)){
$FirstName = $row['FirstName'];
$Practice = $row['Practice'];
$result .= "<tr><td>" . $FirstName . "</td><td>" . $Practice . "</td></tr>";
}
}
if(isset($result)){
echo "<table border='1'>";
echo "<tr><th>FirstName</th><th>Practice</th></tr>";
echo $result;
echo "</table>";
}
?>
I am trying to get information from mysql including a blob image which i shall echo with php and will have an onclick event within the php, redirecting it to another page. The onlick event will contain a mysql result which it will carry with it as seen in the code below.
My main issue is with the syntax of the code or if there is another way to do it all together. please keep in mind the output when the script is run is similiar to that of google images, bing images etc. Thank you.
<?php
$con=mysqli_connect("localhost","root","*******","media");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM movies ORDER BY `movies`.`title` ASC");
echo "<table border='3' style='margin: auto; text-align: left;background: white; padding: 3em;'>
<tr>
<th><b>Movie Title</b></th>
<th><b>Language</b></th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td style='padding-right: 2em;'><img src="data:image/jpeg;base64,' . base64_encode( $row['image'] ) . '" width="160px" height="200px";" onclick="window.location='lookup.php?pattern=" . $row['title'] . "';>";
</td>
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Untested but here is one way you could clean up your code move style and javascript (and use some jquery) into the head:
<?php
$con=mysqli_connect("localhost","root","*******","media") or die("Failed to connect to MySQL: " . mysqli_connect_error());
$result = mysqli_query($con,"SELECT * FROM movies ORDER BY `movies`.`title` ASC");
?>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('img').each(function() {
var img = $(this);
img.click(function() {
window.location="lookup.php?pattern=" + img.attr('title');
});
});
});
</script>
<style>
table {
margin: auto;
text-align: left;
background: white;
padding: 3em;
border: 2px solid #000000;
}
table tr td {
padding-right: 2em;
}
table tr td img {
width: 160px;
height: 200px;
}
</style>
</head>
<body>
<table>
<tr>
<th>Movie Title</th>
<th>Language</th>
</tr>
<?php
while($row = mysqli_fetch_array($result)) {
echo "
<tr>
<td>
<img src=\"data:image/jpeg;base64," . base64_encode( $row['image'] ) . "\" title=\"" . $row['title'] . "\">
</td>
</tr>
";
}
?>
</table>
</body>
</html>
<?php mysqli_close($con); ?>
Or if you don't want to use javascript, you could always wrap the image around the anchor tag instead:
<td>
<a href='lookup.php?pattern={$row['title']}'>
<img src=\"data:image/jpeg;base64," . base64_encode( $row['image'] ) . "\">
</a>
</td>
You could further separate PHP and HTML code:
<?php
$con=mysqli_connect("localhost","root","*******","media");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
$result = mysqli_query($con,"SELECT * FROM movies ORDER BY `movies`.`title` ASC");
?>
<table border='3' style='margin: auto; text-align: left;background: white; padding: 3em;'>
<tr>
<th><b>Movie Title</b></th>
<th><b>Language</b></th>
</tr>
<?php
while($row = mysqli_fetch_array($result)) {
$img = 'data:image/jpeg;base64,'
. base64_encode( $row['image'] );
?>
<tr>
<td style='padding-right: 2em;'>
<img src="<?php echo $img; ?>"
style="width: 160px; height: 200px;"
onclick="window.location='lookup.php?pattern=<?php echo $row['title']?>;'"
/>
</td>
</tr>
<?php } ?>
</table>
<?php
mysqli_close($con);
?>
You could also use some sort of templating engine to do this, but the results would be pretty much the same - I don't see much point in writing, say,
<strong>{{ title }}</strong>
instead of
<strong><?php echo $title; ?></strong>
I am trying to create a border around the top row ONLY! Currently, a border will be displayed only around the outside of the table. But I also want a border around the first row. Can someone help me do this? I want the row with 'Team, Correct Picks, and Points' to have a border around it.
<body>
<?=$leaguename?>
<center><table cellspacing="0" style="width:400px; border:1px solid gray">
<?
echo "<tr border=\"1\"><td> Team </td><td>Correct Picks</td><td>Points</td></tr>";
while($row = mysql_fetch_array($memberslist)) {
if ($row['User_ID'] == $id) {
echo "<tr style=\"border:1px solid gray\" bgcolor=\"gray\"><td>" . $row['User_ID'] . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";
} else {
echo "<tr><td>" . $row['User_ID'] . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";
}
}
?>
</table></center>
</body>
This is presentational, so mixing it into the PHP is a bad idea. Instead, use CSS:
tr:first-child td { border-top: 1px solid black; border-bottom: 1px solid black; }
tr:first-child td:first-child { border-left:1px solid black; }
tr:first-child td:last-child { border-right:1px solid black; }
IE7+8 support for the *-child selectors can be a bit buggy. If it's not working in those browsers, consider using JavaScript polyfills.
You may also want to put the headers inside <thead> and <th> elements to make the <tr> with data the first row in a <tbody> element.
e.g.
<table>
<thead>
<tr>
<th>Team</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
</tr>
</tbody>
</table>