PHP cannot display cyrilic when reading data from SQL Server - php

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>

Related

How to display website links stored in MySQL database table on website but show as hyperlinks to click?

This is a demo of a code Im working on, I have links in MySQL table and want to display them on a website table, but instead of showing the links I want to display a name same as Hyperlinks from Excel.
example: If I would just use a href=www.google.com Link
My code:
<!DOCTYPE HTML>
<html>
<head>
<title>Pagina teste</title>
<div align="center">
<font size="+3">Lista de Websites</font>
</div>
</head>
<body>
<style>
table, td, th {border: 1px solid black; border-collapse: collapse; }
tr:nth-child(even) {background-color: #dcdddd;}
</style>
<table>
<tr>
<th><font size="+2">Website Name</font></th>
<th><font size="+2">Link</font></th>
</tr>
<?php
$conn = mysqli_connect("localhost", "root", "", "test");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_GET['order'])){
$order = $_GET['order'];
}else{
$order = 'websitename';
}
if(isset($_GET['sort'])){
$sort = $_GET['sort'];
}else{
$sort = 'ASC';
}
$sql = "SELECT websitename, link FROM websites ORDER BY $order $sort";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>" . $row["websitename"]. "</td>
<td>" . $row["link"]. "</td>
</tr>";
}
echo "</table>";
} else { echo "0 results"; }
$conn->close();
?>
</table>
</body>
</html>
The bull in the China shop method, surround the output with the tag markup:
echo "<tr>
<td>" . $row["websitename"]. "</td>
<td>" . $row['websitename'] . "</td>
</tr>";
You can use anchor link inside table.
<?php
echo "<tr>
<td>" . $row["websitename"]. "</td>
<td><a href=".$row["link"]." target='_blank'>" . $row["link"]. "</a></td>
</tr>";
?>
Try this
<!DOCTYPE HTML>
<html>
<head>
<title>Pagina teste</title>
<div align="center">
<font size="+3">Lista de Websites</font>
</div>
</head>
<body>
<style>
table, td, th {border: 1px solid black; border-collapse: collapse; }
tr:nth-child(even) {background-color: #dcdddd;}
</style>
<table>
<tr>
<th><font size="+2">Website Name</font></th>
<th><font size="+2">Link</font></th>
</tr>
<?php
$conn = mysqli_connect("localhost", "root", "", "test");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_GET['order'])){
$order = $_GET['order'];
}else{
$order = 'websitename';
}
if(isset($_GET['sort'])){
$sort = $_GET['sort'];
}else{
$sort = 'ASC';
}
$sql = "SELECT websitename, link FROM websites ORDER BY $order $sort";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>".$row["websitename"]."</td>
<td><a href=".$row["link"]." target='_blank'>" . $row["link"]. "</a></td>
</tr>";
}
echo "</table>";
} else { echo "0 results"; }
$conn->close();
?>
</table>
</body>
</html>

Generate dynamic table with loop in php

Please forgive me if I'm asking a stupid question! But I really tried hard and still failing
I was trying to create an error message that would appear in a loop using PHP
The original code looks like this:
// go through lines that had errors
if (property_exists($result, 'return') && is_object($result->return) && property_exists($result->return, 'failed') && $result->return->failed > 0) {
foreach ($result->return->failedRows as $failedRow) {
foreach ($failedRow->errors as $rowErrors)
$message .= "\nline: " . ($failedRow->line) . ", column: " . $rowErrors->column . ", value: '" . $rowErrors->value . "'" . ", message: '" . $rowErrors->details[0]->translated . "'";
}
}
if ($message != "")
throw new Exception('Error details: ' . $message . ' [' . $method . ']');
The error message looks like this right now!
what I'm trying to achieve is to have the error message looking like this:
Again sorry if this seems very simple for any of you but my PHP skills are rather limited and I'm trying to learn all these tricks that may seem very simple to create!
ok here's what I've tried but it's wrong of course!
// go through lines that had errors
if (property_exists($result, 'return') && is_object($result->return) && property_exists($result->return, 'failed') && $result->return->failed > 0) {
foreach ($result->return->failedRows as $failedRow) {
foreach ($failedRow->errors as $rowErrors)
$message .= "<th>Line</th>" .
"<td>" . ($failedRow->line) . "</td>" .
"<th>Column</th>" .
"<td>".$rowErrors->column. "</td>".
"<th>Value</th>" .
"<td>".$rowErrors->value . "</td>" .
"<th>Error message</th>" .
"<td>".$rowErrors->details[0]->translated."</td>";
}
}
if (property_exists($result, 'errors') && is_object($result->errors) && count($result->errors) > 0) {
foreach ($result->errors as $error)
$message .= "\nerror: " . $error;
}
if ($message != "")
echo "<div class='container'><strong>Error details:</strong></br>
<table style='width:100%;'>
<tr>
$message
</tr>
</table></div>;
and here's the result of my misurable try :!
You can try the following and then add your styling classes to it. That should create your table!
if (property_exists($result, 'return') && is_object($result->return) && property_exists($result->return, 'failed') && $result->return->failed > 0) {
foreach ($result->return->failedRows as $failedRow) {
foreach ($failedRow->errors as $rowErrors)
$message .= "<tr>" .
"<td>" . ($failedRow->line) . "</td>" .
"<td>".$rowErrors->column. "</td>".
"<td>".$rowErrors->value . "</td>" .
"<td>".$rowErrors->details[0]->translated."</td>" .
"</tr>";
}
}
if ($message != "")
echo "<div class='container'><strong>Error details:</strong></br>
<table id='t01' style='width:100%;'>
<tr>
<th>Line</th>
<th>Column</th>
<th>Value</th>
<th>Error message</th>
$message
</tr>
</table></div>";
You can also add the following CSS in the front end:
<style>
table {
width:100%;
}
table, th, td {
border: 1px solid #fff;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
table#t01 tr:nth-child(even) {
background-color: #eee;
}
table#t01 tr:nth-child(odd) {
background-color:#fff;
}
table#t01 th {
background-color: #ED7D31;
color: white;
}
</style>
You didn't post anything you tried, so the general way to do it is:
Inside the if-condition (if there are errors), but before the foreach loop, echo the beginning of the table, including the header row, like
echo "<table>
<tr>
<th>Header for Row 1<th>
<th>Header for Row 2<th>
<th>Header for Row 3<th>
</tr>";
Then inside the foreach loop, echo a <tr> tag first, then before every column content variable a <td>, after every column content variable a closing </td> and eventually a cloasing <tr>
After the foreach loop (but still inside the if condition), echo the closing </table> tag.

Unusual table output from mysql php

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>";
}
?>

Table 3 Columns Loop

I have read all the post about coding three columns for PHP. However, I can't seem to find any that will allow me to create three column loop. Everything I do either gives me an error or a blank white page. I also need to have a little space between the column loops. Here is what I have so far, can you tell me what I am missing?
table, td, th {
border: 1px solid #000;
text-align: left;
}
table {
border-collapse: initial;
width: 100%;
}
td {
padding: 10px;
width: 5%;
line-height: 2;
}
th {
background-color: grey;
color: white;
padding: 15px;
width: auto;
}
$sql = "SELECT name, email, dropdown, description FROM basic";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>";
$columns=3;
// output data of each row
while($row = $result->fetch_assoc()) {
if ($row > 0 && ($columns) == 3) {
echo "<th>". $row["name"]. "</th><tr> <td>email: ". $row["email"]. "</td><tr> <td>category: " . $row["dropdown"] . "</td><tr><td>Announcement: " . $row["description"] . "</td></tr>";
}
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
$sql = "SELECT name, email, dropdown, description FROM basic";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>";
$columns=3;
$x = 0;
// output data of each row
echo "<tr>";
while($row = $result->fetch_assoc()) {
if ($row > 0 && ($columns) == 3) {
echo "<th>". $row["name"]. "</th><tr> <td>email: ". $row["email"]. "</td><tr> <td>category: " . $row["dropdown"] . "</td><tr><td>Announcement: " . $row["description"] . "</td></tr>";
if ($x == 3) {
echo "</tr>";
$x = 0;
}
$x++;
}
}
if ($x < 3) {
echo "</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
Explaination :-
within the while loop , we will check the variable $x which we set it to 0 outside the loop and increment it for every iteration ,
when that variable be equal to 3 we will print the end of row </tr>

PHP Column Styling

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";
}

Categories