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.
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 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>
I'm having trouble styling the information that I'm pulling from the database. If anyone can help I'd really appreciate it. I tried defining $style within the while loop, and then assigning it the $questions, but nothing happens on the webpage. I'm new with coding in general, and while I have some knowledge of css, I don't know how you use it within php script.
style for the background I was trying to put behind each question*
#frm1
{
background: #D9D9D9;
margin:auto;
top:150px; left:200px; width:880px; height:60px;
position:absolute;
font-family: "Comic Sans MS", cursive, sans-serif;
font-size: 9px;
font-style: italic;
line-height: 24px;
font-weight: bold;
text-decoration: none;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
padding:10px;
border: 1px solid #999;
border: inset 1px solid #333;
-webkit-box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.5);
-moz-box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.5);
box-shadow: 0px 0px 40px rgba(0, 0, 0, 0.7);
}
PHP code retrieving info from database*
if (mysql_num_rows($result) >= 0)
{
$toggle = false;
while ($rows = mysql_fetch_array($result, MYSQL_ASSOC) and $i<10 )
{
$i++;
$toggle = !$toggle;
if($toggle)
$style = "id:frm1;";
else
$style = "background: white;";
questions .= "<a style='$style'> </a>";
questions .= "Titlee: " ."<a href='show_question2.php?question_id=" . $rows["question_id"] . "'>". $rows['title'] . "</a> <br> ";
questions .= "Details: " . "<a href='show_question2.php?question_id=" . $rows["question_id"] . "'>". $rows['detail'] . "</a> <br> ";
questions .= "Category: " . "<a href='show_question2.php?question_id=" . $rows["question_id"] . "'>". $rows['categories'] . "</a> <br> <br> <br> ";
}
echo questions;
}
while I have some knowledge of css, I don't know how you use it within
php script.
Okay.
Your PHP script is a PHP script on the server, and results in a regular HTML page for the user. [See the bottom of the answer, I'll try to give you a quick overview]
You can use CSS exactly as you would with a plain HTML page, and it will work just fine despite being backed by PHP.
This means do not use style="$style". Style attributes are Bad.
As it looks like you want to construct your CSS conditionally, my suggestion is either:
Change a class using PHP, and have an external stylesheet which acts on that class
Put the styles you're conditionally changing inside <style> tags in your header, and change those with PHP.
This answer will use the first option
(Edited to take into account new information)
In your PHP code, before your links:
if($toggle) {
$questions.='<div id="frm1">';
}
else {
$questions.='<div id="frm2">';
}
In your PHP code, after your links:
$questions .= "</div>";
And finally, in either your external stylesheet, or your in-head <style> tags:
#frm1 {
...
}
#frm2 {
...
}
Quick overview of server-side languages
So, web programming. This is generally done in two ways. client side (read: javascript) and server side (in your case, read: php, but there's a lot more to this).
With a client side language like javascript, the code actually gets sent to the web browser. The web browser then modifies the contents of the page according to what the script says for it to do. This means your users can see the code, even turn it off in their web browser or execute other javascript in its place.
With a server side language, there's a different workflow.
The user asks for your webpage (identified by its URL)
The web server (read: your webhosting) receives this request, and looks up what the webpage is
Finding that the webpage is a php page, the server executes the php code
The php code gives the server an html page (which you have built, as you can see, your php script outputs HTML)
The server sends the resulting html code to the user
Note that the web browser, which is the component doing all of the processing of HTML and CSS, never sees the php. By the time your php script reaches your users, it's just an html page.
Because the web browser only sees an HTML page, there is no functional difference between using CSS on your php script, and using CSS on a regular HTML page.
This will work:
if (mysql_num_rows($result) >= 0) {
$toggle = false;
while ($rows = mysql_fetch_array($result, MYSQL_ASSOC) and $i<10 ) {
$i++;
$toggle = !$toggle;
if($toggle)
$style = "background: #D9D9D9;"; else
$style = "background: green;";
questions .= "<a href='#' style='display:block;$style'> </a>";
questions .= "Titlee: " ."<a href='show_question2.php?question_id=" . $rows["question_id"] . "'>". $rows['title'] . "</a> <br> ";
questions .= "Details: " . "<a href='show_question2.php?question_id=" . $rows["question_id"] . "'>". $rows['detail'] . "</a> <br> ";
questions .= "Category: " . "<a href='show_question2.php?question_id=" . $rows["question_id"] . "'>". $rows['categories'] . "</a> <br> <br> <br> ";
}
echo questions;
}
The problem was your a-tag doesn't have a href attribute and since it's displayed inline (default-behaviour) the background CSS property won't work.
Instead of style, build classes and define them in css.
if ($toggle)
$questionClass="redBackground";
else
$questionClass="greenBackground";
$questions.="<a class='$questionClass'>";
Also, definitely look into mysqli or pdo. mysql_ functions are deprecated and not nearly as cool!
You can do -
if (mysql_num_rows($result) >= 0)
{
$toggle = false;
while ($rows = mysql_fetch_array($result, MYSQL_ASSOC) and $i<10 )
{
$i++;
$toggle = !$toggle;
if($toggle)
$style = "bg";
else
$style = "bg_green";
echo("<a class='".$style."'> </a>");
echo("Titlee: " ."<a href='show_question2.php?question_id=" . $rows["question_id"] . "'>". $rows['title'] . "</a> <br> ");
echo("Details: " . "<a href='show_question2.php?question_id=" . $rows["question_id"] . "'>". $rows['detail'] . "</a> <br> ");
echo("Category: " . "<a href='show_question2.php?question_id=" . $rows["question_id"] . "'>". $rows['categories'] . "</a> <br> <br> <br> ");
}
In this file add -
<style type="text/css">
.bg {
background: #D9D9D9;
}
.bg_green {
background: green;
}
</style>
As ben said use class.
first create a class
<style>
.gray{background: #D9D9D9;}
.green{background: green;}
</style>
Then try this
if (mysql_num_rows($result) >= 0)
{
$toggle = false;
while ($rows = mysql_fetch_array($result, MYSQL_ASSOC) and $i<10 )
{
$i++;
$toggle = !$toggle;
$style = ($toggle)?"green":"gray";
$questions .= "<a class='".$style."'> Put some thing here </a>";
$questions .= "Titlee: " ."<a href='show_question2.php?question_id=" . $rows["question_id"] . "'>". $rows['title'] . "</a> <br> ";
$questions .= "Details: " . "<a href='show_question2.php?question_id=" . $rows["question_id"] . "'>". $rows['detail'] . "</a> <br> ";
$questions .= "Category: " . "<a href='show_question2.php?question_id=" . $rows["question_id"] . "'>". $rows['categories'] . "</a> <br> <br> <br> ";
}
echo $questions;
}
Please try it, I have not tested but it should work, according to your need.
You can alternate on your counter $i with $i % 2 to switch between two CSS classes. This will give you 0, 1, 0, 1, 0, 1, ... and so select the first and second CSS class name in turn.
PHP:
$css_class = array('frm1', 'second');
while ($rows = mysql_fetch_array($result, MYSQL_ASSOC) and $i<10 )
{
$i++;
questions .= "<a class='$css_classes[$i % 2]'> </a>";
questions .= "Titlee: " ."<a href='show_question2.php?question_id=" . $rows["question_id"] . "'>". $rows['title'] . "</a> <br> ";
questions .= "Details: " . "<a href='show_question2.php?question_id=" . $rows["question_id"] . "'>". $rows['detail'] . "</a> <br> ";
questions .= "Category: " . "<a href='show_question2.php?question_id=" . $rows["question_id"] . "'>". $rows['categories'] . "</a> <br> <br> <br> ";
}
and in your CSS file you define the two classes
.frm1 {
background: #D9D9D9;
margin:auto;
top:150px; left:200px; width:880px; height:60px;
position:absolute;
...
}
.second {
background: white;
}
Is it possible to call a function when a different option value is select from a select box? the select box consists of 'approved' 'pending' and 'dissapproved'. I have a form which will automatically flag the submission as 'pending' when entered in the database.
When changed by an admin from 'pending' to 'approved' or 'disapproved' this will run a SQL query to update the information in the table. What table structure would be best suited to this? and how can I ensure that when the option value is changed a CSS class is applied to the affected row of table cells. Which will remain the same unless changed. I have tried with jQuery however I do not feel it meets my requirements
Code:
<?php
$result = mysql_query("SELECT * FROM Orders")
or die (mysql_error());
?>
<form method ="post" action="sendemail.php">
<table style ="background-color:#ffffff;">
<tr>
<th>Order Number</th>
<th>Order Date</th>
<th>Ordered By</th>
<th>Supplier</th>
<th>Total Price</th>
<th>Requested By</th>
<th>Status</th>
</tr>
<?php
while ($row = mysql_fetch_assoc($result))
{
echo '<tr>';
echo '<td><input type="text" name="order_n`enter code here`o[]" value="' . $row['Orderno'] . '"/> </td>';
echo '<td><input type="text" name="order_date[]" value="' . $row['Orderdate'] . '"/></td>';
echo '<td><input type="text" name="order_ordered_by[]" value="' . $row['Orderedby'] . '"/></td>';
echo '<td><input type="text" name="order_supplier[]" value="' . $row['Supplier'] . '"/></td>';
echo '<td><input type="text" name="order_total_price[]" value="' . $row['totalprice'] . '"/></td>';
echo '<td><input type="text" name="order_requested_by[]" value="' . $row['requestedby'] . '"/></td>';
echo '<td>';
echo '<select name="order_status[]">';
echo '<option></option>';
echo '<option>Approved</option>';
echo '<option>Pending</option>';
echo '<option>Dissaproved</option>';
echo '</select>';
echo '</td>';
echo '</tr>';
}
?>
Current jQuery to change table row colour, however I would like to add a CSS class to each option value and when the status is changed, this is reflected in the database, applying the correct class.
<script type="text/javascript">
$(document).ready(function() {
var color = ['none', '#DEF3CA', '#EECFA1', '#FFCCCC'];
$('table').on('change','select', function() {
$(this).parents('tr').css('background', color[$(':selected', this).index()]);
});
});
</script>
Table CSS:
<style type = "text/css">
table,td {
border:1px solid #999;
font-family: Tahoma,Arial,verdana;
font-size: 12px;
width:auto;
font-weight: none;
line-height: 1.4em;
font-style: normal;
border-collapse:separate;
}
td {
width:250px;
padding:15px;
color:#000;
border:1px solid;
border-bottom:1px solid;
}
</style>
SendEmail script to email each person based on 'Requestedby'
<?php
if(isset($_POST['order_requested_by']))
{
$row_count = count($_POST['order_requested_by']);
for($i=0; $i<$row_count; $i++)
{
$to = $_POST['order_requested_by'][$i];
$subject = "Order Status";
$message = "Your order number: " . $_POST['order_no'][$i] ." ". "is" ." ". $_POST['order_status'][$i];
$orderdate = "Order date: " . $_POST['order_date'][$i];
$from = "no-reply#.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$orderdate,$headers);
echo "Mail Sent.";
}
?>
Send the selection value to another page via a variable and then
$selection = $_POST['variable']
if ($selection == "1") {
....
}
elseif ($selection == "2") {
....
}
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>