PHP alternating colors - 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);
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++;
}

Related

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

How to get data from form and make and array

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.

Unable to block while loop repeats

Working on invoice module which has two different tables master_table and detail_table. Know i want to display master_table's data above the table then in the table structure showing data of detail_table for each three rows header.
My code looks this.
$n = 0;
$htmlpage = 1;
$query = $dbConnection->prepare("SELECT * FROM master WHERE (DocNo BETWEEN ? AND ?) ORDER BY DocNo ASC");
$query->execute(array($fdcu,$tdcu));
while($rows = $query->fetch())
{
$n++;
$noInvs = $rows['No_of_Inv'];
$dno = $rows['DocNo'];
$name = $rows['Customer_Name'];
Step-1: Now i'm pulling client data from another table
$qry = $dbConnection->prepare("SELECT * FROM client WHERE client_name=?");
$qry->execute(array($name));
$row = $qry->fetch();
$add = $row['address'];
$city = $row['city'];
$proj = $rows['Project'];
$projNo = $rows['Project_No'];
$sermnth = $rows['Service_Month'];
$grandtotal = $rows['Total_Amt'];
$rupee = number2word($grandtotal);
$index = 1;
$ratetot = 0;
$subtotal = 0;
$lastPage = ceil($noInvs/3);
$pageCount = 0;
Step-2: Now check the loop iteration
while($index <= $noInvs)
{
$cnt = 1;
$body = "<div class='main-container'><table width='100%' style='padding:0 0 0 0;' align='center' cellpadding='0' cellspacing='0'>
<tr>
<td valign='top' align='center' style='border:1px solid #000;'>
<strong style='font-size:30px;'>Data</strong>
</td>
</tr>
<tr>
<td valign='top' align='left' style='padding: 0 0px 0 0px;border-left: solid 1px #000000;border-right: solid 1px #000000;border-bottom: solid 1px #000000;'>
<table width='100%' align='left' cellpadding='0' cellspacing='0'>
<tr>
<td valign='top' style='width:50%; padding:5px; font-size:13px; border-right: solid 1px #000000;text-align:left;'><strong>CUSTOMER:</strong> <br /><p style='font-size:12px;'>$name<br />$add, $city</p></td>
<td valign='top' style='width:50%; font-size:13px;'>
<table width='100%' cellpadding='0' cellspacing='0'>
<tbody>
<tr>
<td style='border-right: solid 1px #000000;padding:5px;width:40%;font-size:12px;'><br />Invoice No:<br />Invoice Date:</td>
<td align='left' style='padding:5px;font-size:12px;'><br />$ino<br />$invdate</td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td valign='top' align='left' style='border-left: solid 1px #000000;border-right: solid 1px #000000;border-bottom: solid 1px #000000;'>
<table width=100% cellpadding='0' cellspacing='0' >
<tbody>
<tr style='border-right:solid 1px #000000;border-bottom:solid 1px #000000;'>
<td style='width:5%;text-align:center; border-right:solid 1px #000000;border-bottom:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>SN.</td>
<td style='width:45%;text-align:center; border-right:solid 1px #000000;border-bottom:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>Description of Services</td>
<td style='width:13%;text-align:center; border-right:solid 1px #000000;border-bottom:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>Per</td>
<td style='width:10%;text-align:center; border-right:solid 1px #000000;border-bottom:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>Quantity</td>
<td style='width:12%;text-align:center; border-right:solid 1px #000000;border-bottom:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>Rate</td>
<td style='width:15%;text-align:center; border-bottom:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>Amount in Rs.</td>
</tr>";
Step-4: check the loop condition
while(true)
{
Step-5: checking the iteration count
if($cnt>3)
{
$pageCount++;
break;
}
Step-6: Pulling data from details table
$query1 = $dbConnection->prepare("SELECT * FROM details WHERE DocNo=? AND DocNO_Index=?");
$query1->execute(array($dno,$index));
$rowCnt = $query1->rowCount();
while($rows1 = $query1->fetch())
{
$desg = $rows1['Item'];
$des = $rows1['Description'];
$Quantity = $rows1['Quantity'];
$Rate = $rows1['Rate'];
$per = $rows1['Per'];
$gross = $rows1['Gross'];
$SERVICE_TAX = $rows1['SERVICE'];
$SERVICE_TAX = ($SERVICE_TAX==null)? 0.0 : $SERVICE_TAX;
$SB_Tax = $rows1['SB_Tax'];
$SB_Tax = ($SB_Tax==null)? 0.0 : $SB_Tax;
$Krishi_Kalyan_Cess = $rows1['Krishi_Kalyan_Cess'];
$Krishi_Kalyan_Cess = ($Krishi_Kalyan_Cess==null)? 0.0 : $Krishi_Kalyan_Cess;
$ratetot = $ratetot + $Rate;
$subtotal = $subtotal + $gross;
if($cnt == 1 && $index > 1)
{
$body.= "<tr><td style='width:5%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'></td><td style='width:45%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'>Opening Balance</td><td style='width:13%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'></td><td style='width:10%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'></td><td style='width:12%;text-align:right;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'>". number_format($ratetot - $Rate, 2)." /-</td><td style='width:15%;text-align:right;padding:3px 5px 3px 5px;font-size:12px;'>". number_format($subtotal-$gross, 2)." /-</td></tr>";
}
$body.= "<tr><td style='width:5%;height:90px;vertical-align:top;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'>$index</td><td style='width:45%;height:90px;vertical-align:top; border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'>";
if($proj!=null)
$body.= "$proj<br/>";
if($desg!=null)
$body.= "$desg<br/>";
if($des!=null)
$body.= "$des<br/>";
if($projNo!=null)
$body.= "$projNo<br/>";
if($sermnth!=null)
$body.= "$sermnth<br/>";
$body.= "</td><td style='width:13%;height:90px;vertical-align:top;text-align:center; border-right:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>$per</td><td style='width:10%;height:90px;vertical-align:top; border-right:solid 1px #000000;padding:3px 5px 3px 5px;text-align:center;;font-size:12px;'>$Quantity</td><td style='width:12%;height:90px;vertical-align:top;text-align:right; border-right:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>". number_format($Rate,2)."/-</td><td style='width:15%;height:90px;vertical-align:top; padding:3px 5px 3px 5px; text-align:right;;font-size:12px;'>". number_format($gross,2)."/-</td></tr>";
if($cnt == 3 || $index == $noInvs)
{
$body.= "<tr><td style='width:5%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'></td><td style='width:45%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'>";
if($index != $noInvs)
{
$body.= "Closing Balance";
}
$body.= "</td> <td style='width:13%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'></td><td style='width:10%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'></td><td style='width:12%;border-right:solid 1px #000;border-top:solid 1px #000;text-align:right;padding:3px 5px 3px 5px;font-size:12px;'>". number_format($ratetot,2)."/-</td><td style='width:15%;border-top:solid 1px #000;text-align:right;padding:3px 5px 3px 5px;font-size:12px;'>". number_format($subtotal,2)."/-</td></tr>";
}
}
$index++;
$cnt++;
}
$body .= "</tbody></table></td></tr>";
$stax = round($subtotal*($SERVICE_TAX/100),2);
$sbc = round($subtotal*($SB_Tax/100),2);
$kkc = round($subtotal*($Krishi_Kalyan_Cess/100),2);
$grandtotal = round(($subtotal+$stax+$sbc+$kkc),2);
$rupee = number2word($grandtotal);
$body .="<tr><td valign='top' align='left' style='border-left: solid 1px #000000;border-right: solid 1px #000000;border-bottom: solid 1px #000000;;font-size:12px;'><table cellpadding='0' cellspacing='0' width='100%'><tr><td rowspan='2' style='width:50%;height:50px; border-right:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>";
if($pageCount == $lastPage)
{
$body .= "<strong style='font-size:12px;'>Narration : </strong><br/>Amount in Words (Rupees):<br/>";
}
$body .= "<br/>$rupee";
$body .= "</td><td style='width:35%; border-bottom:solid 1px #000000;border-right:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>";
if($pageCount==$lastPage)
{
$body .= "Sub Total : <br />Service Tax # 14% <br />Swachh Bharat Cess # 0.5% <br />Krishi Kalyan Cess # 0.5% <br />";
}
$body .= "</td><td style='width:15%;text-align:right; border-bottom:solid 1px #000000;padding:3px 5px 3px 5px;;font-size:12px;'>";
if($pageCount==$lastPage)
{
$body .= "". number_format($subtotal,2)."/-<br />".number_format($stax,2)."/-<br />".number_format($sbc,2)."/-<br />".number_format($kkc,2)."/-<br />";
}
$body .= "</td></tr><tr><td style='width:35%; border-right:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>";
if($pageCount==$lastPage)
{
$body .= "Total Amount";
}
$body .= "</td><td style='width:15%;text-align:right; padding:3px 5px 3px 5px;font-size:12px;'>";
if($pageCount == $lastPage)
{
$body .= number_format($grandtotal,2)."/-";
}
$body .= "</td></tr></table></td></tr><tr><td valign='top' align='left' style='border-left: solid 1px #000000;border-right: solid 1px #000000;border-bottom: solid 1px #000000;;font-size:12px;'><table width='100%' cellpadding='0' cellspacing='0'>
<tbody>
<tr>
<td style='width:50%; padding:3px 5px 3px 5px;border-right:1px solid #000;font-size:12px;'>
<strong>Thank you</strong>
</td>
<td valign='bottom' align='center' style='width:50%; text-align:center;font-size:12px;'>
<strong>AUTHORISED SIGNATORY</strong>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
<p> </p></div>";
$body .= "<html><head><title>$ino</title><style type='text/css'>.main-container{display: block;} #media print{.main-container{display: block;}#page{size:portrait;margin:10px auto;padding:0}.main-container{page-break-after:always!important;}}</style><script language='javascript' type='text/javascript'>function PrintPage() { window.print(); } </script></head><body style='font-size: 12px ! important;'>$body</body></html><!-- onload='window.print()'-->";
echo "$body";
}
}
Issue is loop iteration.
My code output like this
Any suggestions on how to do this or to point me in the right direction would be greatly appreciated.
You're trying to re-use an active database connection. That doesn't work. You need two connections.
Specifically, you're in the process of reading a result set from a query on your connection,
$query = $dbConnection->prepare("some query");
$query->execute(array($fdcu,$tdcu));
while($rows = $query->fetch()) {
...
when you issue another query on the same $dbConnection ...
$qry = $dbConnection->prepare("another query");
$qry->execute(array($name));
You Can't Do That™ while the first query is still active. Open up and use a different connection for the queries issued from inside your loop.

Unable to upload .avi files using php form

have tried uploading .avi video formats but the page just refreshes and the file is not uploaded all other types type images and everything is getting uploaded im posting my code here look for file uploding sections with cwf and ve
<?php
$n=5;
$exp_no="";
$shot_no="";
$file_name1="";
$file_name2="";
$remarks="";
$cwf="";
$vedio="";
$conn=mysqli_connect("localhost","root","","project");
if(!$conn){
die("connection failed");
echo "<br/>";
}
$query="select stage from experimentdetails where NO='{$exp_no}'";
$res=mysqli_query($conn,$query);
if(mysqli_num_rows($res)>0){
$r=mysqli_fetch_row($res);
$n=$r[0];
}
session_start();
$exp_no=$_SESSION["expno_add"];
$s=mysqli_query($conn,"select ShotNo from shotdetails where ExpNo='{$exp_no}'");
$res=mysqli_fetch_row($s);
$shot_no=$res[0];
if(isset($_POST["submit"]))
{
$remarks=$_POST["remarks"];
$cap=array(8);
$off=array(8);
$cv=array(8);
$cptp=array(8);
$pul=array(8);
$del=array(8);
$vel=array(8);
for($i=0;$i<8;$i++)
{
if($i<$n)
{
$j=$i+1;
$cap[$i]=$_POST["cv{$j}"];
$off[$i]=$_POST["off{$j}"];
$cv[$i]=$_POST["cc{$j}"];
$cptp[$i]=$_POST["cpt{$j}"];
$pul[$i]=$_POST["pw{$j}"];
$del[$i]=$_POST["del{$j}"];
$vel[$i]= $_POST["vel{$j}"];
}
else{
$cap[$i]=0;
$off[$i]=0;
$cv[$i]=0;
$cptp[$i]=0;
$pul[$i]=0;
$del[$i]=0;
$vel[$i]=0;
}
}
if($_FILES['cwf']['name']!="")
{
$fname=$_FILES["cwf"];
$uploaddir = 'cwf/';
if (is_uploaded_file($fname['tmp_name']))
{
$filname = basename($fname['name']);
$uploadfile = $uploaddir . basename($fname['name']);
if (move_uploaded_file ($fname['tmp_name'], $uploadfile))
$r = "File " . $filname . " was successfully uploaded and stored.<br>";
else
$r = "Could not move ".$fname['tmp_name']." to ".$uploadfile."<br>";
}
else
$r = "File ".$fname['name']." failed to upload.";
$file_name1='cwf/'.$file_name1;
}
if($_FILES['ve']['name']!="")
{
$fname=$_FILES["ve"];
$uploaddir = 'upload/';
if (is_uploaded_file($fname['tmp_name']))
{
$filname = basename($fname['name']);
$uploadfile = $uploaddir . basename($fname['name']);
if (move_uploaded_file ($fname['tmp_name'], $uploadfile))
$r = "File " . $filname . " was successfully uploaded and stored.<br>";
else
$r = "Could not move ".$fname['tmp_name']." to ".$uploadfile."<br>";
}
else
$r = "File ".$fname['name']." failed to upload.";
$file_name2='upload/'.$file_name2;
}
$query="insert into shotdetails values($shot_no,$cap[0],$cap[1],$cap[2],$cap[3],$cap[4],$cap[5],$cap[6],$cap[7],$off[0],$off[1],$off[2],$off[3],$off[4],$off[5],$off[6],$off[7],$cptp[0],$cptp[1],$cptp[2],$cptp[3],$cptp[4],$cptp[5],$cptp[6],$cptp[7],$pul[0],$pul[1],$pul[2],$pul[3],$pul[4],$pul[5],$pul[6],$pul[7],$del[0],$del[1],$del[2],$del[3],$del[4],$del[5],$del[6],$del[7],$vel[0],$vel[1],$vel[2],$vel[3],$vel[4],$vel[5],$vel[6],$vel[7],$file_name1,$file_name2,$remarks,$exp_no,$cv[0],$cv[1],$cv[2],$cv[3],$cv[4],$cv[5],$cv[6],$cv[7])";
mysqli_query($conn,$query);
}
mysqli_close($conn);
?>
<html>
<head>
<title>ADD Shot</title>
<style>
select {
padding:3px;
margin: 0;
-webkit-border-radius:4px;
-moz-border-radius:4px;
border-radius:4px;
-webkit-box-shadow: 0 3px 0 #ccc, 0 -1px #fff inset;
-moz-box-shadow: 0 3px 0 #ccc, 0 -1px #fff inset;
box-shadow: 0 3px 0 #ccc, 0 -1px #fff inset;
background: #f8f8f8;
color:#888;
border:none;
outline:none;
display: inline-block;
-webkit-appearance:none;
-moz-appearance:none;
appearance:none;
cursor:pointer;
}
/* Targetting Webkit browsers only. FF will show the dropdown arrow with so much padding. */
#media screen and (-webkit-min-device-pixel-ratio:0) {
select {padding-right:18px}
}
label {position:relative}
label:after {
content:'<>';
font:11px "Consolas", monospace;
color:#aaa;
-webkit-transform:rotate(90deg);
-moz-transform:rotate(90deg);
-ms-transform:rotate(90deg);
transform:rotate(90deg);
right:8px; top:2px;
padding:0 0 2px;
border-bottom:1px solid #ddd;
position:absolute;
pointer-events:none;
}
label:before {
content:'';
right:6px; top:0px;
width:20px; height:20px;
background:#f8f8f8;
position:absolute;
pointer-events:none;
display:block;
}
.c ul li {
margin-top: 11px;
margin-right: 6px;
padding: 0px;
display: inline-block;
font-family: open-sans, Araial, Helvetica, sans-serif;
font-size:14px;
}
.c li a {
margin-left: 10px;
color:black;
padding-right: 20px;
text-decoration: none;
}
.c{
margin-left: 20px;
border-right: 1px solid rgba(1,1,1,0.20);
border-left: 1px solid rgba(1,1,1,0.20);
border-radius:5px;
width:98%;
margin-top: 10px;
}
#nav {
height: 40px;
border-radius:3px;
background-color: #f2f2f2;
border-bottom: 1px solid #ccc;
border-top: 1px solid #ccc;
}
</style>
</head>
<body>
<div class="c">
<ul id="nav">
<li style="border-right:1px solid rgba(1,1,1,0.65);">
Add Experiment
</li>
<li style="border-right:1px solid rgba(1,1,1,0.65);">
Modify Experiment
</li>
<li style="border-right:1px solid rgba(1,1,1,0.65);">
Delete Experiment
</li>
<li style="border-right:1px solid rgba(1,1,1,0.65);">
Create New Shot
</li>
<li style="border-right:1px solid rgba(1,1,1,0.65);">
Delete Shot
</li>
<li style="border-right:1px solid rgba(1,1,1,0.65);">
Add Component
</li>
<li style="border-right:1px solid rgba(1,1,1,0.65);">
Modify Component
</li>
<li style="border-right:1px solid rgba(1,1,1,0.65);">
Delete Component
</li>
</ul>
</div>
<center style="margin-top:5%">
<form action="add_shot1.php" method="POST" name="add" enctype="multipart/form-data">
<table cellspacing="4" cellpadding="4" class="cap">
<tr>
<th>Stage No.</th>
<th>Charging Voltage(kV)</th>
<th>Offset(mm)</th>
<th>Coil Current(kA)</th>
<th>Current Pulse Time period(ms)</th>
<th>Pulse Width(ms)</th>
<th>Delay(ms)</th>
<th>Velocity</th>
</tr>
<?php
for ($x = 1; $x <= $n; $x++) {
echo "<tr>";
echo "<td align='center'>" . $x . "</td>";
echo "<td align='center' ><input type='number' class='text' name='cv{$x}' value='' style=' height:30px;'/></td>";
echo "<td align='center'><input type='number' class='text' name='off{$x}' value=''style=' height:30px;'/></td>";
echo "<td align='center' ><input type='number' class='text' name='cc{$x}' value=''style=' height:30px;'/></td>";
echo "<td align='center' ><input type='number' class='text' name='cpt{$x}' value=''style=' height:30px;'/></td>";
echo "<td align='center' ><input type='number' class='text' name='pw{$x}' value=''style=' height:30px;'/></td>";
echo "<td align='center' ><input type='number' class='text' name='del{$x}' value=''style=' height:30px;'/></td>";
echo "<td align='center' ><input type='number' class='text' name='vel{$x}' value=''style=' height:30px;'/></td>";
echo"</tr>";
}
?>
<tr>
<td colspan='4'> Current wave form file: &nbsp <input type='file' name='cwf' value='' class='text'/> </td>
</tr>
<tr >
<td colspan='4' >Video File: <input type='file' name='ve' value='' class='text'/></td>
<td></td>
<td></td>
</tr>
<tr>
<td>Remarks</td>
<td><textarea rows="10" cols="40" name="remarks" value=""></textarea></td>
</tr>
<tr>
<td colspan=4 align='center'><input type="submit" name="submit" value="submit" /></td>
</tr>
</table>
</form>
</center>
</body>
</html>

Need to put color to a html table through css

<?php
$con=mysqli_connect("localhost","root","","db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$co_name = mysqli_real_escape_string($con, $_POST['co_name']);
$co_address = mysqli_real_escape_string($con, $_POST['co_address']);
$co_website = mysqli_real_escape_string($con, $_POST['co_website']);
$co_phoneno = mysqli_real_escape_string($con, $_POST['co_phoneno']);
$co_contactperson = mysqli_real_escape_string($con, $_POST['co_contactperson']);
$therapist_id = mysqli_real_escape_string($con, $_POST['therapist_id']);
$result = mysqli_query($con,"SELECT * FROM therapist_office WHERE therapist_id='".$user_id."'");
echo"<table id='miyazaki' style='border-collapse: collapse; border: 1px solid; margin-left:180px; width:1000px; margin-top:30px;'>";
echo"<thead style='border:1px solid; background-color:#ffe3ab;' >";
echo"<tr>";
echo"<th style='border: 1px solid; padding: .65em;' >Office Name</th>";
echo"<th style='border: 1px solid; padding: .65em;' >Address</th>";
echo"<th style='border: 1px solid; padding: .65em;' >Website</th>";
echo"<th style='border: 1px solid; padding: .65em;' >PhoneNo</th>";
echo"<th style='border: 1px solid; padding: .65em;'>Contact Person</th>";
echo"</tr>";
echo"</thead>";
while($row = mysqli_fetch_array($result))
{
echo"<tbody>";
echo"<tr>";
echo "<td style=' padding: .65em;'>" . $row['co_name'] . "</td>";
echo "<td style=' padding: .65em;'>" . $row['co_address'] . "</td>";
echo "<td style=' padding: .65em;'>" . $row['co_website'] . "</td>";
echo "<td style=' padding: .65em;'>" . $row['co_phoneno'] . "</td>";
echo "<td style=' padding: .65em;'>" . $row['co_contactperson'] . "</td>";
echo"</tr>";
echo"</tbody>";
}
echo"</table>";
mysqli_close($con);
?>
I have this table that is simple but i am not able to add colors through css to it, at present i have used inline css will convert it to external css later. i wish to show the table head with different color and the rows of the table that contain data should be of 2 colors that run alternatively. Would appreciate if someone could help me
P.S some people wanted to see my other sheet, here it is
<style>
table { border-collapse: collapse; font-family: Futura, Arial, sans-serif; border: 1px solid ; margin-left:180px; width:1000px; margin-top:30px;}
caption { font-size: larger; margin: 1em auto; }
th, td { padding: .65em; }
th, thead { background-color: ffecc4; border: 1px solid ; }
tr:nth-child(odd) { background: #ccc; }
tr:hover { background: #aaa; }
td { border-right: 1px solid #777; }
</style>
In html TABLE you cannot give background-color or color to <thead>. <thead> doesn't not take any colors. You can do..
#miyazaki thead th{
background-color:red;
color:#000;}
And for alternate colors you can apply
#miyazaki tr:nth-child(odd){
background: #b8d1f3;
}
#miyazaki tr:nth-child(even){
background: #dae5f4;
}
Do it with jquery:
<script>
$(document).ready(function() {
$("#miyazaki tr:even").css("background-color", "#CCC");
$("#miyazaki tr:odd").css("background-color", "#FFF");
});
</script>
You can do it with CSS:
#miyazaki tr:nth-child(even) {background: #CCC}
#miyazaki tr:nth-child(odd) {background: #FFF}
Reference
Note: No support in IE 8 for this.
Or, if you have jQuery:
<script>
$(document).ready(function() {
$("#miyazaki tr:even").css("background-color", "#CCC");
$("#miyazaki tr:odd").css("background-color", "#FFF");
});
</script>
if you add classes to you code e.g
`echo"<th style='border: 1px solid; padding: .65em;' class='tblHead'>Office Name</th>";`
and
`echo "<td style=' padding: .65em;'>" . $row['co_name'] . " class='tblRow' </td>";`
Then you can write some CSS to set styles for those classes.
like:
.tblHead {
background-color: yellow;
}
.tblRow{
background-color: green;
}

Categories