Dashed line not showing HTML, PHP, MySql - php

I have a database that is out putting a specific row. My code does that but I want a dashed border around it for my table. The code below should make a dashed line around this row but I don't know why it not working. Can someone help?
$count = 1
echo "<table>";
echo "<tr> <th>Pos</th> <th>Team</th> <th>PLD</th> <th>W</th> <th>D</th>
<th>L</th> <th>F</th> <th>A</th> <th>GD</th> <th>PTS</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
if($count == 5) {
echo "<tr style='border-style:dashed'><td>";
echo $row['Pos'];
echo "</td><td>";
echo $row['Team'];
echo "</td><td>";
echo $row['PLD'];
echo "</td><td>";
echo $row['W'];
echo "</td><td>";
echo $row['D'];
echo "</td><td>";
echo $row['L'];
echo "</td><td>";
echo $row['F'];
echo "</td><td>";
echo $row['A'];
echo "</td><td>";
echo $row['GD'];
echo "</td><td>";
echo $row['PTS'];
echo "</td></tr>";
}
else {
echo "<tr><td>";
}

Direct styling of is not allowed.
You should put the border on the elements and use border-right and border-left to remove the border in between columns.
<tr>
<td style="border: 1px dashed black; border-right: none">Left</td>
<td style="border: 1px dashed black; border-left: none; border-right: none">Middle</td>
<td style="border: 1px dashed black; border-left: none; border-right: none">Middle</td>
<td style="border: 1px dashed black; border-left: none">Right</td>
</tr>
You really should use html classes and a CSS file though.

Related

How to get <thead> and <tfoot> to not overlap cells when using <td rowspan>

So I'm making a localhost site to keep track of my families medications (upwards of 50 of them). I'm working on a page of it to #print out tables for each of us (4 total individuals) for when we fill our med containers each Sunday. All that said, my wife asked me to get the <th> to show on each page (top and bottom).
I did some research and found a great answer to the basic problem: How to use HTML to print header and footer on every printed page of a document?
This is a great answer and I started there with the below table that is echo'd with PHP. I took out the PHP for now, but can add it if someone really needs it.. I use the PHP because the data is coming out of a SQL>Database>Table.
HTML (from PHP echo)
<table>
<thead>
<tr>
<th colspan='15' class='patientHeader'>[Family Member Name]'s Meds</th>
</tr>
<tr>
<th>Drug Names</th>
<th>Prescriber</th>
<th>Dose/Pill</th>
<th>Frequency</th>
<th>Rx Number</th>
<th>Time</td>
<th class='tinyBoxes'>M</th>
<th class='tinyBoxes'>T</th>
<th class='tinyBoxes'>W</th>
<th class='tinyBoxes'>T</th>
<th class='tinyBoxes'>F</th>
<th class='tinyBoxes'>S</th>
<th class='tinyBoxes'>S</th>
<th class='nf'>Next Fill</th>
</tr>
</thead>
<!-- BODY CONTENT -->
<tfoot>
<tr>
<th>Drug Names</th>
<th>Prescriber</th>
<th>Dose/Pill</th>
<th>Frequency</th>
<th>Rx Number</th>
<th>Time</td>
<th class='tinyBoxes'>M</th>
<th class='tinyBoxes'>T</th>
<th class='tinyBoxes'>W</th>
<th class='tinyBoxes'>T</th>
<th class='tinyBoxes'>F</th>
<th class='tinyBoxes'>S</th>
<th class='tinyBoxes'>S</th>
<th class='nf'>Next Fill</th>
</tr>
</tfoot>
</table>
CSS
#media print {
.menubar,
h1.viewable,
#pagewrapper,
#footer,
.subbar,
#screenblock {
display: none !important;
}
body {
background-color: white;
color: black;
font: 8pt Georgia, "Times New Roman", Times, serif;
line-height: 1.3;
}
#page {
margin: 1cm;
size: landscape;
}
h1 {
font-size: 24pt;
}
h2 {
font-size: 14pt;
margin-top: 25px;
}
aside h2 {
font-size: 18pt;
}
.body {
display: block;
width: 8.5in;
height: 11in;
text-align: center;
}
/*************************VIEW TABLE STUFF*************************/
table {
width: 100%;
border-spacing: 0px;
border: 1px sold black;
page-break-inside: avoid;
box-decoration-break: clone;
-webkit-box-decoration-break: clone;
page-break-after: always;
margin-bottom: 25px;
}
tr {
page-break-inside: avoid;
}
th {
font-weight: bold;
border: 1px solid black;
border-collapse: collapse;
height: 50px;
vertical-align: middle;
text-align:center !important;
}
td {
border: 1px solid black;
border-collapse: collapse;
vertical-align: middle;
text-align: center;
padding: 5px 0;
}
.smallBean {
font-size: smaller;
font-style: italic;
margin: 0px;
padding: 0px;
text-align: center;
font-size: 10pt;
}
.tinyBoxes {
width: 0.5cm;
height: 0.5cm;
font-weight: bold;
vertical-align: middle;
padding: .1cm;
}
.nf {
width: 100px !important;
}
.nf::after {
font-size: smaller;
font-style: italic;
margin: 0px;
padding: 0px;
text-align: center;
content: "\A or \A Refil Needed";
white-space: pre;
}
.onePage {
page-break-before: always !important;
page-break-after: always !important;
page-break-inside: avoid !important;
}
.patientHeader {
font-size: 0.4in;
font-variant: small-caps;
}
}
Here's the problem I run in to...
Within the gathering of the data and filling of the table, I use <td rowspan=''> and break down each medicine by when they are taken. When the table breaks on a row with this, the remaining <td rowspan='1'> get overlapped by the <thead>.
IMGUR Image of what the print looks like
**EDIT - ADDITIONAL INFORMATION **
I did chose the <thead> and <tfoot> option from that link I posted because I want them attached to the content and not the page.
To clarify a comment
$am = $row['am'];
$noon = $row['noon'];
$pm = $row['pm'];
$bed = $row['bed'];
$prn = $row['prn'];
$other = $row['other'];
if ($other != 1) {
if ($am == 1) {
echo "<td rowspan = '1'>AM</td>";
$am = 0;
} elseif ($noon == 1) {
echo "<td rowspan = '1'>Noon</td>";
$noon = 0;
} elseif ($pm == 1) {
echo "<td rowspan = '1'>PM</td>";
$pm = 0;
} elseif ($bed == 1) {
echo "<td rowspan = '1'>Bed</td>";
$bed = 0;
} elseif ($prn == 1) {
echo "<td rowspan = '1'>PRN</td>";
$prn = 0;
}
}
echo "<td tinyBoxes'></div><td class='tonyBoxes'></td><td class='tonyBoxes'></td><td class='tonyBoxes'></td>
<td class='tonyBoxes'></td><td class='tonyBoxes'></td><td class='tonyBoxes'></td>";
echo "<td rowspan='" . $rows . "' style='width:125px !important'>" . $nextFill . "</td>";
echo "</tr>";
if (($rows > 1) && ($other != 1)) {
if ($noon == 1) {
echo "<tr>";
echo "<td rowspan = '1'>Noon</td>";
echo "<td tinyBoxes'></div><td class='tonyBoxes'></td><td class='tonyBoxes'></td><td class='tonyBoxes'></td>
<td class='tonyBoxes'></td><td class='tonyBoxes'></td><td class='tonyBoxes'></td>";
echo "</tr>";
}
if ($pm == 1) {
echo "<tr>";
echo "<td rowspan = '1'>PM</td>";
echo "<td tinyBoxes'></div><td class='tonyBoxes'></td><td class='tonyBoxes'></td><td class='tonyBoxes'></td>
<td class='tonyBoxes'></td><td class='tonyBoxes'></td><td class='tonyBoxes'></td>";
echo "</tr>";
}
if ($bed == 1) {
echo "<tr>";
echo "<td rowspan = '1'>Bed</td>";
echo "<td tinyBoxes'></div><td class='tonyBoxes'></td><td class='tonyBoxes'></td><td class='tonyBoxes'></td>
<td class='tonyBoxes'></td><td class='tonyBoxes'></td><td class='tonyBoxes'></td>";
echo "</tr>";
}
if ($prn == 1) {
echo "<tr>";
echo "<td rowspan = '1'>PRN</td>";
echo "<td tinyBoxes'></div><td class='tonyBoxes'></td><td class='tonyBoxes'></td><td class='tonyBoxes'></td>
<td class='tonyBoxes'></td><td class='tonyBoxes'></td><td class='tonyBoxes'></td>";
echo "</tr>";
}
}
The issue falls during these <td rowspan = '1'> tags
Some work and I figured out a bit of a workaround - not a great one, but it does achieve the purpose I was going for...
I removed the <tfoot> - it wasn't necessary and was where the problem was being caused.
I found an error in my <td class='tinyBoxes'> cells - some said "tonyBoxes" and a few were missing the class='` portion
echo "<td class='tinyBoxes'></div><td class='tinyBoxes'></td><td class='tinyBoxes'></td><td class='tinyBoxes'></td>
<td class='tinyBoxes'></td><td class='tinyBoxes'></td><td class='tinyBoxes'></td>";
echo "<td rowspan='" . $rows . "' style='width:125px !important;font-size:0.15in;'>" . $nextFill . "</td>";
echo "</tr>";
if (($rows > 1) && ($other != 1)) {
if ($noon == 1) {
echo "<tr class='tiny'>";
echo "<td rowspan = '1'>Noon</td>";
echo "<td class='tinyBoxes'></div><td class='tinyBoxes'></td><td class='tinyBoxes'></td><td class='tinyBoxes'></td>
<td class='tinyBoxes'></td><td class='tinyBoxes'></td><td class='tinyBoxes'></td>";
echo "</tr>";
}
if ($pm == 1) {
echo "<tr class='tiny'>";
echo "<td rowspan = '1'>PM</td>";
echo "<td class='tinyBoxes'></div><td class='tinyBoxes'></td><td class='tinyBoxes'></td><td class='tinyBoxes'></td>
<td class='tinyBoxes'></td><td class='tinyBoxes'></td><td class='tinyBoxes'></td>";
echo "</tr>";
}
if ($bed == 1) {
echo "<tr class='tiny'>";
echo "<td rowspan = '1'>Bed</td>";
echo "<td class='tinyBoxes'></div><td class='tinyBoxes'></td><td class='tinyBoxes'></td><td class='tinyBoxes'></td>
<td class='tinyBoxes'></td><td class='tinyBoxes'></td><td class='tinyBoxes'></td>";
echo "</tr>";
}
if ($prn == 1) {
echo "<tr class='tiny'>";
echo "<td rowspan = '1'>PRN</td>";
echo "<td class='tinyBoxes'></div><td class='tinyBoxes'></td><td class='tinyBoxes'></td><td class='tinyBoxes'></td>
<td class='tinyBoxes'></td><td class='tinyBoxes'></td><td class='tinyBoxes'></td>";
echo "</tr>";
}
}
AND
/*
echo "
<tfoot>
<tr>
<th>Drug Names</th>
<th>Prescriber</th>
<th>Dose/Pill</th>
<th>Frequency</th>
<th>Rx Number</th>
<th>Time</td>
<th class='tinyBoxes'>M</th>
<th class='tinyBoxes'>T</th>
<th class='tinyBoxes'>W</th>
<th class='tinyBoxes'>T</th>
<th class='tinyBoxes'>F</th>
<th class='tinyBoxes'>S</th>
<th class='tinyBoxes'>S</th>
<th class='nf'>Next Fill</th>
</tr>
</tfoot>";
*/

Php Dynamic Table Showing Server Date/Time Format

I have Created the Program where I am Fetching the Content from a text file from a cpanel server,
In my Table Fields It shows the Date Format in Server format i.e (YYYY/MM/DD) I want it to Display as
(DD/MM/YYYY).
This is My Code
<?php
$data = file_get_contents("file-path.txt");
$arr = explode(";", $data);
echo "<thead>";
echo "<tr style=\"border: 1px solid black;\">";
echo "<th style=\"border: 1px solid black;\">Doctor Name</th>";
echo "<th style=\"border: 1px solid black;\">Leave From (YYYY/MM/DD)</th>";
echo "<th style=\"border: 1px solid black;\">Leave To (YYYY/MM/DD)</th>";
echo '<th style="border: 1px solid black;">Remarks</th>';
echo "</tr>";
echo "</thead>";
echo "<tbody>";
for($value = 0; $value < count($arr)-1; $value=$value+4) {
echo "<tr style=\"border: 1px solid black;\">";
echo "<td style=\"border: 1px solid black;\">".$arr[$value]."</td>";
echo "<td style=\"border: 1px solid black;\">".$arr[$value+1]."</td>";
echo "<td style=\"border: 1px solid black;\">".$arr[$value+2]."</td>";
echo '<td style="border: 1px solid black;">'.$arr[$value+3].'</td>';
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
?>
You should use date_format() function bro
date_format($arr[$value+2],"d/m/Y");
date('d/m/Y', strtotime($arr[$value+2]));
Have a try, you can do it.

Error while using materialize css table

I have a php file designed to retrieve information from database. The problem is that if i use a normal table tags like the following it would work and show the page for me.
<?php /*Template Name: contactread */
get_header();
$servername = "localhost";
$username = "***** (for security purpose only)";
$password = "*****";
$dbname = "*****";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$query = "SELECT * FROM contact";
$result = mysqli_query($conn,$query);
echo "<table class='table'>
<tr>
<th class='tda' style='border-bottom: 1px solid #313131; border-top: 1px solid #313131; border-right: 1px solid #313131;'>نام</th>
<th class='tda' style='border-bottom: 1px solid #313131; border-top: 1px solid #313131; border-right: 1px solid #313131;'>ایمیل</th>
<th class='tda' style='border-bottom: 1px solid #313131; border-top: 1px solid #313131; border-right: 1px solid #313131;'>پیام</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td class='tda' style='border-bottom: 1px solid #313131; border-right: 1px solid #313131;'>" . $row['name'] . "</td>";
echo "<td class='tda' style='border-bottom: 1px solid #313131; border-right: 1px solid #313131;'>" . $row['email'] . "</td>";
echo "<td class='tda' style='border-bottom: 1px solid #313131; border-right: 1px solid #313131;'>" . $row['message'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
And When i use it like the following which is calling everything from the framework, it gives me the http 500 error. (I included the entire page for showing the first type of table so you have the idea of the page, however i dont see a reason to re write it all again)
echo "<table>
<thead>
<tr>
<th>نام</th>
<th>ایمیل</th>
<th>پیام</th>
</tr>
</thead>
<tbody>";
while($row = mysqli_fetch_array($result))
{
echo <tr>
echo <td>. $row['name'] .</td>
echo <td>. $row['email'] .</td>
echo <td>. $row['message'] .</td>
echo </tr>
}
echo "</tbody>
</table>";
Can someone help me why is this making the error? I couldnt find anything helpful in the internet.
EDIT: I have added materialize-css css files with the link in the header and i have included the header as well.
Simple mistake. You are not wrapping the data you want to echo in quotes, so try doing it like this.
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>$row[name]</td>";
echo "<td>$row[email]</td>";
echo "<td>$row[message]</td>";
echo "</tr>";
}

Error show in php if there is only one input blank

If any any one of them are blank then show error message. But it is not working.I mean, if I any one between input and option remain empty then the error will show. One of them must be selected.
Here is .
<?php include "head.htm";?>
<br>
<br>
<body bgcolor="#FEF5E7">
<title>Search Profile</title>
<form method="post" action="search.php">
<input type="text" placeholder="Enter Student's ID/Name" name="query" />
<select name="BRANCH">
<option value="">Select</option>
<option value="Banani">Banani</option>
<option value="RayerBazar">RayerBazar</option>
</select>
<input type="submit" value="Find" name="completedsearch" />
</form>
<?php
if(isset($_POST['completedsearch']))
{
$term = $_POST['query'];
$term1 = $_POST['BRANCH'];
$mysql = mysql_connect("localhost","password","null");
mysql_select_db("mydb");
$qu = mysql_query("SELECT * FROM `stu` WHERE STUDENTID LIKE '%{$term}%' OR STUDENTID LIKE '%{$term}%' OR BRANCH LIKE '%{$term1}%' OR STUDENTID LIKE '%{$term}%' "); //selects the row that contains ANYTHING like the submitted string
$qu1 = mysql_query("SELECT * FROM `stu` WHERE BRANCH LIKE '%{$term1}%' ");
if ($term == "" || $term1 == "") {
// no results
echo '<a style="color:red;font-size: 30px;">Please Put Name OR ID Here</a><br><body background="ghost.gif" style="background-repeat:no-repeat;background-size: cover">';
} else {
echo "<table style='width:100%'>
<th style='width:10%;font-size:20px;border: 2px solid red; '>Info ID</th>
<th style='width:20%;font-size:20px;border: 2px solid red; '>School ID</th>
<th style='width:20%;font-size:20px;border: 2px solid red; '>Name</th>
<th style='width:10%;font-size:20px;border: 2px solid red; '>Class</th>
<th style='width:10%;font-size:20px;border: 2px solid red; '>Shift</th>
<th style='width:10%;font-size:20px;border: 2px solid red; '>Branch</th>
<th style='width:20%;font-size:20px;border: 2px solid red; '>Search</th>
";
while($row = mysql_fetch_array($qu))
{
echo "<tr><td style='width:10%;font-size:15px;border: 2px solid blue;'><p style='text-slign: center;font-family: cursive;text-align: center'>";
echo $row['id'];
echo "</p></td>";
echo "<td style='width:20%;font-size:15px;border: 2px solid blue;'><p style='text-slign: center;font-family: cursive;text-align: center'>";
echo $row['STUDENTID'];
echo "</p></td>";
echo "<td style='width:20%;font-size:15px;border: 2px solid blue;'><p style='text-slign: center;font-family: cursive;text-align: center'>";
echo $row['STUDENTNAME'];
echo "</p></td>";
echo "<td style='width:10%;font-size:15px;border: 2px solid blue;'><p style='text-slign: center;font-family: cursive;text-align: center'>";
echo $row['CLASS'];
echo "</p></td>";
echo "<td style='width:10%;font-size:15px;border: 2px solid blue;'><p style='text-slign: center;font-family: cursive;text-align: center'>";
echo $row['SHIFT'];
echo "</p></td>";
echo "<td style='width:10%;font-size:15px;border: 2px solid blue;'><p style='text-slign: center;font-family: cursive;text-align: center'>";
echo $row['BRANCH'];
echo "</p></td>";
echo " <td style='width:20%;font-size:15px;border: 2px solid blue;'><p style='text-slign: center;font-family: cursive;text-align: center'><a href='id.php?id=";
echo $row['id'];
echo "'>Visit This Profile</a></p></td>";
}
while($row = mysql_fetch_array($qu1))
{
echo "<tr><td style='width:10%;font-size:15px;border: 2px solid blue;'><p style='text-slign: center;font-family: cursive;text-align: center'>";
echo $row['id'];
echo "</p></td>";
echo "<td style='width:20%;font-size:15px;border: 2px solid blue;'><p style='text-slign: center;font-family: cursive;text-align: center'>";
echo $row['STUDENTID'];
echo "</p></td>";
echo "<td style='width:20%;font-size:15px;border: 2px solid blue;'><p style='text-slign: center;font-family: cursive;text-align: center'>";
echo $row['STUDENTNAME'];
echo "</p></td>";
echo "<td style='width:10%;font-size:15px;border: 2px solid blue;'><p style='text-slign: center;font-family: cursive;text-align: center'>";
echo $row['CLASS'];
echo "</p></td>";
echo "<td style='width:10%;font-size:15px;border: 2px solid blue;'><p style='text-slign: center;font-family: cursive;text-align: center'>";
echo $row['SHIFT'];
echo "</p></td>";
echo "<td style='width:10%;font-size:15px;border: 2px solid blue;'><p style='text-slign: center;font-family: cursive;text-align: center'>";
echo $row['BRANCH'];
echo "</p></td>";
echo " <td style='width:20%;font-size:15px;border: 2px solid blue;'><p style='text-slign: center;font-family: cursive;text-align: center'><a href='id.php?id=";
echo $row['id'];
echo "'>Visit This Profile</a></p></td>";
}
}
}
?>
</tr>
</table>
if(isset($_POST['completedsearch'])){
if(empty($_POST['query']) || empty($_POST['BRANCH'])) {
echo "Show your error!";
}else {
//execute your code.
}
}
As you ask that if any one between input and option remain empty then the error will show than use
if(!empty($_POST['query']) || !empty($_POST['BRANCH']) ) {
// one of them or both select
} else {
die("please select one of them ")
}
http://php.net/manual/en/function.empty.php
Please prefer to use PDO or mysqli instead of this msql
if you want show error on first page instead of search.php than use Ajax
as you said than try this
if(empty($_POST['query']) && empty($_POST['BRANCH'])) {
die ("Please select any one or both")
} else if (!empty($_POST['query']) && !empty($_POST['BRANCH']) {
// it mean user selected both than try to write your query whith And
// SELECT * FROM `table` WHERE `student_id` LIKE '%$id%' AND `BRANCH` LIKE `$branch`
} else if (!empty($_POST['query'])) {
// user only write in text box
//SELECT * FROM `table` WHERE `student_id` LIKE '%$id%'
} else {
//user choose select option
//SELECT * FROM `table` WHERE `BRANCH` LIKE `%$branch%`
}

PHP alternating colors

$dbc = mysql_connect('localhost','root','') or die (mysql_error());
mysql_select_db('payroll') or die (mysql_error());
$sql = "SELECT * FROM employee ORDER BY employee_id DESC";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
echo "
<tr>
<td style=\"padding-left: 20px; border-bottom: 1px solid #999; border-right: 1px solid #999;\">".$row['first_name']." ".$row['last_name']."</td>
<td style=\"text-align: center; border-bottom: 1px solid #999; border-right: 1px solid #999; padding-top: 2px ; padding-bottom: 3px ;\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\"> <input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
</tr>
";
}
I wanted to add an alternating color in the loop. what code should I add?
$rowCount = 0;
$colorOne = '#ffffff';
$colorTwo = '#f3f3f3';
while($row = mysql_fetch_array($result)){
$rowColor = ($rowCount % 2) ? $colorOne : $colorTwo;
echo "<element bgcolor='$rowColor'></element>";
$rowCount++;
}
Edited after #Jayrox comment.
Use CSS classes instead of inline styles. They're easier to manipulate. Then style the .myclass_row_0 and .myclass_row_1 in your CSS for the alternate row color as well as .col1 and .col2 for the column styles.
$c=1; // or 0 (added after deceze's comment)
while($row = mysql_fetch_array($result)) {
$c=1-$c; // magic!
echo "
<tr class=\"myclass_row_$c\">
<td class=\"col1\">".$row['first_name']." ".$row['last_name']."</td>
<td class=\"col2\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\"> <input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
</tr>
";
}
Or using inline styles:
$colors = array('#ffffff','#f3f3f3');
$c=1;
while($row = mysql_fetch_array($result)) {
$c=1-$c;
echo "
<tr style=\"background-color:{$colors[$c]};\">
<!-- tds here -->
</tr>
";
}
Or using preset classes:
$styles = array('odd','even');
$c=1;
while($row = mysql_fetch_array($result)) {
$c=1-$c;
echo "
<tr class=\"{$styles[$c]}\">
<!-- ... -->
</tr>
";
}
There are already a lot of solutions, but this one use only two lines of css.
You can set specific css on even and odd rows:
tr:nth-child(even) {background: #CCC}
tr:nth-child(odd) {background: #FFF}
Usually, I will declare an index(int) value and increment for each time through the loop. Then do a check to see if index mod 2 = 1. If so, then output a tr with the style that you want to apply to show an alternating row.
$color = 0;
while($row = mysql_fetch_array($result)) {
if($color % 2 == 1){
echo "<tr>";
}else{
echo "<tr class=\"altRow\">";
}
echo "
<td style=\"padding-left: 20px; border-bottom: 1px solid #999; border-right: 1px solid #999;\">".$row['first_name']." ".$row['last_name']."</td>
<td style=\"text-align: center; border-bottom: 1px solid #999; border-right: 1px solid #999; padding-top: 2px ; padding-bottom: 3px ;\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\"> <input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
</tr>
";
$color++;
}
As it's bumped already, here is my 5 cents.
7 answers and not a single one using templates.
We can write a thousand articles of the templates necessity, but such an examples will always win.
So, based on the OP's code and stagas' answer:
business logic part:
$c = 1;
$DATA = array();
$sql = "SELECT * FROM employee ORDER BY employee_id DESC";
$result = mysql_query($sql) or trigger_error(mysql_error().$sql);
while($row = mysql_fetch_array($result)) {
$row['c'] = $c=1-$c;
$DATA[] = $row;
}
and template part:
<tr class="myclass_row_<?=$row['$c']?>">
<td class="col1"><?=$row['first_name']?> <?=$row['last_name']?></td>
<td class="col2">
<input type="button" name="edit" value="Edit" class="selbtn"> <input type="button" name="delete" value="Delete" class="selbtn">
</td>
</tr>
$color = 1;
while($row = mysql_fetch_array($result)) {
if($color == 1){
echo "
<tr>
<td style=\"padding-left: 20px; border-bottom: 1px solid #999; border-right: 1px solid #999;\">".$row['first_name']." ".$row['last_name']."</td>
<td style=\"text-align: center; border-bottom: 1px solid #999; border-right: 1px solid #999; padding-top: 2px ; padding-bottom: 3px ;\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\"> <input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
</tr>
";
$color = 2;
}
else
{
echo "
<tr>
<td style=\"padding-left: 20px; border-bottom: 1px solid #555; border-right: 1px solid #555;\">".$row['first_name']." ".$row['last_name']."</td>
<td style=\"text-align: center; border-bottom: 1px solid #555; border-right: 1px solid #555; padding-top: 2px ; padding-bottom: 3px ;\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\"> <input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
</tr>
";
}
$color = 1;
}
// here it is in less code
<?php
$dbc = mysql_connect('localhost','root','') or die (mysql_error());
mysql_select_db('payroll') or die (mysql_error());
$sql = "SELECT * FROM employee ORDER BY employee_id DESC";
$result = mysql_query($sql);
$colors = array('FFF', '000000'); // valid hex colors
$numOfColors = sizeOf($colors); $i = 0
while($row = mysql_fetch_array($result)) {
$i++;if($i>$numOfColors){$i=0;}
?>
<tr>
<td style="padding-left: 20px; border-bottom: 1px solid #<?=$colors[$i]?>; border-right: 1px solid #<?=$colors[$i]?>;"> <?=$row['first_name']?> <?=$row['last_name']?> </td>
<td style="text-align: center; border-bottom: 1px solid #<?=$colors[$i]?>; border-right: 1px solid #<?=$colors[$i]?>; padding-top: 2px ; padding-bottom: 3px ;"><input type="button" name="edit" value="Edit" class="selbtn"> <input type="button" name="delete" value="Delete" class="selbtn"></td>
</tr>
<?php
}
?>
Just keep track whether it is an alternating row with a Boolean. Initialize it to false before your loop, not it for each iteration, then you can set the row style based on its value. Something like:
...
$isAlternatingRow = false;
while($row = mysql_fetch_array($result)) {
echo "
<tr class=\"" . $isAlternatingRow ? "defaultRow" : "alternatingRow" . "\">
<td style=\"padding-left: 20px; border-bottom: 1px solid #999; border-right: 1px solid #999;\">".$row['first_name']." ".$row['last_name']."</td>
<td style=\"text-align: center; border-bottom: 1px solid #999; border-right: 1px solid #999; padding-top: 2px ; padding-bottom: 3px ;\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\"> <input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
</tr>
";
$isAlternatingRow = !($isAlternatingRow);
}
Then just define styles for tr.defaultRow td and tr.alternatingRow td.
$class="even"
while($row = mysql_fetch_array($result))
{
if($class == "even")
{
echo "<tr class='$class'>";
$class="odd"
}
else
{
echo "<tr class='$class'>";
$class="even";
}
...
}
I did it like this:
Remember to add a CSS class called "even", with styling of course.
<?php
include 'connect.php';
echo "<table id='hor-zebra'>";
$i = 0;
while($row = mysql_fetch_array($result))
{
if($i % 2 == 0)
{
echo "<tr class='even'>";
echo "<td>" . $row['something'] . "</td>";
echo "</tr>";
}
else
{
echo "<tr>";
echo "<td>" . $row['something'] . "</td>";
echo "</tr>";
}
$i++;
}
echo "</table>";
mysql_close($con);
?>
Using predefined color outside the loop.
$rowCount = 0;
$color = array('#ffffff','#f3f3f3');
while($row = mysql_fetch_array($result)){
$i = ($rowCount % 2);
echo "<element bgcolor='".$color["$i"]."'></element>";
$rowCount++;
}

Categories