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%`
}
Related
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.
I am just making a railway reservation project to my college therefor I have to get passenger details and make a 2d array and send it to another page to show the summary but I just don't know how to do that.
my code is
<?php
include('connection.php');
session_start();
$train_number = $_GET['train_number'];
$train_name = $_GET['train_name'];
$coachid = $_GET['coachid'];
$date = $_SESSION['date'];
$day = $_SESSION['day'];
$coachtype = $_SESSION['coachtype'];
$useremail = $_SESSION['useremail'];
if($_SERVER['REQUEST_METHOD']='POST')
{
if(!empty($_POST['proceed']))
{
$i = 1;
while($i<7)
{
"What to write here";
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Buy Tickets </title>
<link rel="stylesheet" href="css.css">
</head>
<body>
<div class="wrapper">
<div class="header">
<img src="banner.jpg" height="100%" width="100%"/>
</div>
<div class="navbar">
<ul>
<li>Dashboard </li>
<li>Buy Ticket </li>
<li>Cancel ticket</li>
<li>Edit Profile</li>
<li id="last">Logout</li>
</ul>
</div>
<div>
<?php
echo "<h4 align='center'><u>Booking for </u></h4>";
echo "<table align='center'>
<thead>
<tr style='border: 1px solid #dddddd;text-align: left; padding: 8px;'>
<th style='border: 1px solid #dddddd;text-align: left; padding: 8px;'> Train Number </th>
<th style='border: 1px solid #dddddd;text-align: left; padding: 8px;'> Train name </th>
<th style='border: 1px solid #dddddd;text-align: left; padding: 8px;'> Date </th>
<th style='border: 1px solid #dddddd;text-align: left; padding: 8px;'> Coach type </th>
</tr>
</thead>";
echo "<tr>";
echo "<td style='border: 1px solid #dddddd;text-align: left; padding: 8px;'>".$train_number."</td>";
echo "<td style='border: 1px solid #dddddd;text-align: left; padding: 8px;'>".$train_name."</td>";
echo "<td style='border: 1px solid #dddddd;text-align: left; padding: 8px;'>".$date."</td>";
echo "<td style='border: 1px solid #dddddd;text-align: left; padding: 8px;'>".$coachtype."</td>";
echo "</tr>";
echo "</table>";
echo "<h4 align='center'><u>Passenger details </u></h4>";
echo "<table align='center'>
<thead>
<tr style='border: 1px solid #dddddd;text-align: left; padding: 8px;'>
<th style='border: 1px solid #dddddd;text-align: left; padding: 8px;'> No </th>
<th style='border: 1px solid #dddddd;text-align: left; padding: 8px;'> Passenger Name </th>
<th style='border: 1px solid #dddddd;text-align: left; padding: 8px;'> Age </th>
<th style='border: 1px solid #dddddd;text-align: left; padding: 8px;'> Gender type </th>
</tr>
</thead>";
$iforlist = 1;
while($iforlist<7)
{
echo "<form method='post'>";
echo "<tr>";
echo "<td style='border: 1px solid #dddddd;text-align: left; padding: 8px;'>".$iforlist."</td>";
echo "<td style='border: 1px solid #dddddd;text-align: left; padding: 8px;'><input type='text' name='passenger_name.".$iforlist."' value='' placeholder='Enter name '></td>";
echo "<td style='border: 1px solid #dddddd;text-align: left; padding: 8px;'><input type='number' name='passenger_age.".$iforlist."' value='' max='100' min='1' placeholder='Enter passenger age '></td>";
echo "<td style='border: 1px solid #dddddd;text-align: left; padding: 8px;'><select name='passenger_gender.".$iforlist."'>
<option value='Male'>Male</option>
<option value='Female'>Female</option>
</select></td></td>";
echo "</tr>";
$iforlist++;
}
echo "</table>";
echo "<div align='center' style='margin-top:10px;'><input type='submit' align='center' name='proceed' value='Proceed' style='width:100px;height:40px;' placeholder='Enter name '></div>";
echo "</form>";
?>
</div>
</div>
</body>
</html>
So, tell me how can i make fetch data if only 2 or 3 field are filled and make a 2d array of it.
if you want any other information i'll give you..
Here is an example of how to use 2D Arrays in PHP
$cars = array
(
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
Now to access the data, you must specify the row and column:
<?php
echo $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2].".<br>";
echo $cars[1][0].": In stock: ".$cars[1][1].", sold: ".$cars[1][2].".<br>";
echo $cars[2][0].": In stock: ".$cars[2][1].", sold: ".$cars[2][2].".<br>";
echo $cars[3][0].": In stock: ".$cars[3][1].", sold: ".$cars[3][2].".<br>";
?>
In this case:
array("Volvo",22,18)
That is a single row (0) and it has three columns (Volvo at row=0 column=0, 22 at row=0 colum=1, etc)
I took the example from here: https://www.w3schools.com/php/php_arrays_multi.asp
Let me know if this helps.
I am new to programming, I have managed to create a page which fetches the data from MySQL.
I have used a jQuery plugin to freeze the first column and first row of my table.
The problem I am facing is, when I try to increase the width of a specific column it is not working.
Please help me to understand how do I increase width for specific column. This is my PHP and CSS for your reference.
Thanks.
PHP
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title>Region Wise</title>
<link href="css/test.css" rel="stylesheet" media="screen" />
<header class="header">
<div class="titulosHeader">
<h2>Region Wise</h2>
</header>
</head>
<main class="main">
<div class="ContenedorTabla">
<?php
$sql = 'SELECT * from rgn_report';
$result = mysqli_query($con,$sql);
echo "<table id='pruebatabla' class='fht-table hoverTable'>
<thead>
<tr>
<th class='celda_encabezado_general' rowspan='3' style='height:100px;'>REGION</th>
<tr>
<th class='celda_encabezado_general' colspan='4'>KA</th>
<th class='space'></th>
<th class='celda_encabezado_general' colspan='4'>MI</th>
<th class='space'></th>
<th class='celda_encabezado_general' colspan='4'>OT</th>
<th class='space'></th>
<th class='celda_encabezado_general' colspan='4'>FA</th>
<th class='space'></th>
<th class='celda_encabezado_general' colspan='4'>HA</th>
</tr>
<tr>
<th class='celda_encabezado_general'>SOH</th>
<th class='celda_encabezado_general'>CY</th>
<th class='celda_encabezado_general'>CT</th>
<th class='celda_encabezado_general'>WC</th>
<th class='space'></th>
<th class='celda_encabezado_general'>SOH</th>
<th class='celda_encabezado_general'>CY</th>
<th class='celda_encabezado_general'>CT</th>
<th class='celda_encabezado_general'>WC</th>
<th class='space'></th>
<th class='celda_encabezado_general'>SOH</th>
<th class='celda_encabezado_general'>CY</th>
<th class='celda_encabezado_general'>CT</th>
<th class='celda_encabezado_general'>WC</th>
<th class='space'></th>
<th class='celda_encabezado_general'>SOH</th>
<th class='celda_encabezado_general'>CY</th>
<th class='celda_encabezado_general'>CT</th>
<th class='celda_encabezado_general'>WC</th>
<th class='space'></th>
<th class='celda_encabezado_general'>SOH</th>
<th class='celda_encabezado_general'>CY</th>
<th class='celda_encabezado_general'>CT</th>
<th class='celda_encabezado_general'>WC</th>
</tr>
</thead>";
echo "<tbody>";
while ($row = mysqli_fetch_array($result))
{
$rowhighlight = array("RGN1","RGN2","Grand Total");
if (in_array($row['rgn'],$rowhighlight)){
echo "<tr>";
echo "<td class='highlight_cell'>" . $row['rgn']."</td>";
echo "<td class='highlight_cell'>" . number_format($row['KA_SOH'])."</td>";
echo "<td class='highlight_cell'>" . number_format($row['KA_CY'])."</td>";
echo "<td class='highlight_cell'>" . number_format($row['KA_CT'])."</td>";
echo "<td class='highlight_cell'>" . $row['KA_CVR']."</td>";
echo "<td class='space'></td>";
echo "<td class='highlight_cell'>" . number_format($row['MI_SOH'])."</td>";
echo "<td class='highlight_cell'>" . number_format($row['MI_CY'])."</td>";
echo "<td class='highlight_cell'>" . number_format($row['MI_CT'])."</td>";
echo "<td class='highlight_cell'>" . $row['MI_CVR']."</td>";
echo "<td class='space'></td>";
echo "<td class='highlight_cell'>" . number_format($row['OT_SOH'])."</td>";
echo "<td class='highlight_cell'>" . number_format($row['OT_CY'])."</td>";
echo "<td class='highlight_cell'>" . number_format($row['OT_CT'])."</td>";
echo "<td class='highlight_cell'>" . $row['OT_CVR']."</td>";
echo "<td class='space'></td>";
echo "<td class='highlight_cell'>" . number_format($row['FA_SOH'])."</td>";
echo "<td class='highlight_cell'>" . number_format($row['FA_CY'])."</td>";
echo "<td class='highlight_cell'>" . number_format($row['FA_CT'])."</td>";
echo "<td class='highlight_cell'>" . $row['FA_CVR']."</td>";
echo "<td class='space'></td>";
echo "<td class='highlight_cell'>" . number_format($row['HA_SOH'])."</td>";
echo "<td class='highlight_cell'>" . number_format($row['HA_CY'])."</td>";
echo "<td class='highlight_cell'>" . number_format($row['HA_CT'])."</td>";
echo "<td class='highlight_cell'>" . $row['HA_CVR']."</td>";
echo "</tr>";}
else
{
echo "<tr>";
echo "<td class='excel_cell'>" . $row['rgn']."</td>";
echo "<td class='excel_cell'>" . number_format($row['KA_SOH'])."</td>";
echo "<td class='excel_cell'>" . number_format($row['KA_CY'])."</td>";
echo "<td class='excel_cell'>" . number_format($row['KA_CT'])."</td>";
echo "<td class='excel_cell'>" . $row['KA_CVR']."</td>";
echo "<td class='space'></td>";
echo "<td class='excel_cell'>" . number_format($row['MI_SOH'])."</td>";
echo "<td class='excel_cell'>" . number_format($row['MI_CY'])."</td>";
echo "<td class='excel_cell'>" . number_format($row['MI_CT'])."</td>";
echo "<td class='excel_cell'>" . $row['MI_CVR']."</td>";
echo "<td class='space'></td>";
echo "<td class='excel_cell'>" . number_format($row['OT_SOH'])."</td>";
echo "<td class='excel_cell'>" . number_format($row['OT_CY'])."</td>";
echo "<td class='excel_cell'>" . number_format($row['OT_CT'])."</td>";
echo "<td class='excel_cell'>" . $row['OT_CVR']."</td>";
echo "<td class='space'></td>";
echo "<td class='excel_cell'>" . number_format($row['FA_SOH'])."</td>";
echo "<td class='excel_cell'>" . number_format($row['FA_CY'])."</td>";
echo "<td class='excel_cell'>" . number_format($row['FA_CT'])."</td>";
echo "<td class='excel_cell'>" . $row['FA_CVR']."</td>";
echo "<td class='space'></td>";
echo "<td class='excel_cell'>" . number_format($row['HA_SOH'])."</td>";
echo "<td class='excel_cell'>" . number_format($row['HA_CY'])."</td>";
echo "<td class='excel_cell'>" . number_format($row['HA_CT'])."</td>";
echo "<td class='excel_cell'>" . $row['HA_CVR']."</td>";
echo "</tr>";
}
}
echo "</tbody>";
echo "</table>";
mysqli_close($con);
?>
</div>
</main>
<script src="js/jquery-1.11.0.min.js"></script>
<script src="js/jquery.CongelarFilaColumna.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#pruebatabla").CongelarFilaColumna({Columnas:1});
});
</script>
</body>
</html>
CSS
*{
margin: 0;
padding: 0;
border:0;
}
.header, .main {
display: inline-block;
height: auto;
width: 100%;
}
.titulosHeader {
height: auto;
}
.ContenedorTabla {
height: 540px;
border: 1px solid green;
overflow: auto;
position: relative;
max-width:auto;
}
.fht-table,
.fht-table thead,
.fht-table tfoot,
.fht-table tbody,
.fht-table tr,
.fht-table th,
.fht-table td
{
margin: 0;
}
.fht-table{
height: auto;
width: auto;
border-collapse: collapse;
border-spacing: 0;
border: 0 none;
table-layout:fixed;
}
.fht-table th,.fht-table td {
overflow: hidden;
table-layout:fixed;
}
.fht-table-wrapper,
.fht-table-wrapper .fht-thead,
.fht-table-wrapper .fht-tfoot,
.fht-table-wrapper .fht-fixed-column .fht-tbody,
.fht-table-wrapper .fht-fixed-body .fht-tbody,
.fht-table-wrapper .fht-tbody {
overflow: hidden;
position: relative;
}
.fht-table-wrapper .fht-fixed-body .fht-tbody,
.fht-table-wrapper .fht-tbody {
overflow: auto;
}
.fht-table-wrapper .fht-table .fht-cell {
overflow: hidden;
height: 1px;
}
.fht-table-wrapper .fht-fixed-column,
.fht-table-wrapper .fht-fixed-body {
top: 0;
left: 0;
position: absolute;
}
.fht-table-wrapper .fht-fixed-column {
z-index: 1;
}
.fht-fixed-body .fht-thead table {
margin-right: 20px;
border: 0 none;
}
.celda_encabezado_general {
background-color: #4BACC6;
border: 1px solid black;
color: #ffffff;
text-align: center;
font-size: 12px;
font-family: Calibri, sans-serif;
white-space: nowrap;
}
._Separador{
background-color: #fff;
height: 12px;
border-left: 1px solid #ccc;
border-right: 1px solid #ccc;
width: 7px;
}
._Separador div{
width: 4px;
}
.celda_normal {
background-color: #fff;
border: 1px solid #ccc;
padding: 2px 4px;
}
.excel_cell{
border: 1px solid black;
color: #222;
text-align: center;
font-size: 11px;
font-weight: normal;
padding: 5px;
empty-cells: show;
font-family: Calibri, sans-serif;
white-space: nowrap;
}
._cell_header{
background-color: #EEE;
}
._cell_Default{
background-color: #FFF;
text-align: left;
}
.excel_cell div{
width: 30px;
height: 20px;
}
.highlight_cell{
border: 2px solid black;
color: #222;
text-align: center;
font-size: 11px;
font-weight: bold;
padding: 5px;
empty-cells: show;
font-family: Calibri, sans-serif;
white-space: nowrap;
}
.hoverTable tr:hover {
background-color: grey;
}
.space{
border: none;
empty-cells: show;
background-color:grey;
width:10px;
}
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.
$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++;
}