Table and buttons in html and php with bootstrap - php

In this code that have to print a table and 3 buttons, the 3 buttons appear above the table but i want them under the table. What can I fix?
And how can i print them inline? I tried with a div with style display inline but doesn't work.
echo "<div class='container'>";
echo "<br>";
echo "<table class='table table-hover' style='border: 1px solid black; margin-left:auto; margin-right:auto;'><caption><h2>Esperienze di Lavoro</h2>
</caption> <thead> <tr>";
echo "<th style='border: 1px solid black;'> Username </th><th style='border: 1px solid black;'> Password </th><th style='border: 1px solid black;'> Nome </th> <th style='border: 1px solid black;'> Cognome </th> <th style='border: 1px solid black;'> Data di Nascita </th> <th style='border: 1px solid black;'> Email </th> <th style='border: 1px solid black;'> Foto </th>";
echo "</tr> </thead> <tbody> <tr>";
echo "<td style='border: 1px solid black;'>".$_COOKIE["username"]."</td>";
echo "<td style='border: 1px solid black;'>".$_COOKIE["password"]."</td>";
echo "<td style='border: 1px solid black;'>".$_COOKIE["name"]."</td>";
echo "<td style='border: 1px solid black;'>".$_COOKIE["surname"]."</td>";
echo "<td style='border: 1px solid black;'>".$_COOKIE["birth"]."</td>";
echo "<td style='border: 1px solid black;'>".$_COOKIE["mail"]."</td>";
echo "<td style='border: 1px solid black;'><img src=".$_COOKIE["foto"]." style='width:128px;height:128px;'></td>";
echo "</tr></tbody</table>";
echo "
<div class='form-group'>
<div class='col-sm-offset-2 col-sm-10'><button type='button' name='mod_nome' class='btn btn-primary' onclick='Funzione1()';>Modifica Informazioni</button></div>
</div>";
echo "<div class='form-group'>
<div class='col-sm-offset-2 col-sm-10'><button type='button' name='studio' class='btn btn-primary' onclick='Funzione2()';>Aggiungi Esperienze </button></div>
</div>";
echo "
<div class='form-group'>
<div class='col-sm-offset-2 col-sm-10'> <button type='button' name='comp' onclick='Funzione7()' class='btn btn-primary';>Compagnie in cui i nostri iscritti lavorano</button></div>
</div>";
I'm using bootstrap.

Your button code is fine, you just need to close <tbody></tbody>
echo "</tr></tbody></table>";
To align the buttons,
<style>
#btnSearch,
#btnClear{
display: inline-block;
vertical-align: top;
}
</style>
<div class="row">
<div class="col-sm-12 text-center">
<button id="btnSearch" class="btn btn-primary btn-md center-block" Style="width: 100px;">button-1</button>
<button id="btnClear" class="btn btn-danger btn-md center-block" Style="width: 100px;">button-2</button>
<button id="btnClear" class="btn btn-danger btn-md center-block" Style="width: 100px;">button-3</button>
</div>
</div>

Related

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.

Show dinamis data to modal

I want to display data from database into modal. I have a table, has 12 fields. One of them is titled articles that have many characters. So it will take up much space if shown in the table. So, I want to display the record from the article field into modal. This is my code:
<?php
error_reporting(0);
mysql_connect("localhost","root","");
mysql_select_db("hellomedan");
$que = "SELECT * FROM `wisata`";
$q = mysql_query($que);
$no = 1;
while ($qq = mysql_fetch_array($q)) {
?>
<tr style="background:#D79407;">
<td style="text-align:center; border:1px solid #999;padding:4px 8px;"><?php echo $no ?></td>
<td style="border:1px solid #999;padding:4px 8px;"><?php echo $qq['nama'] ?></td>
<td style="text-align:center; border:1px solid #999;padding:4px 8px;"><img src="img/ic_search_white_24dp_1x.png"></td>
<td style="text-align:center; border:1px solid #999;padding:4px 8px;"><?php echo $qq['rating'] ?></td>
<td style="text-align:center; border:1px solid #999;padding:4px 8px;"><?php echo $qq['jenis'] ?></td>
<td style="border:1px solid #999;padding:4px 8px;"><?php echo $qq['alamat'] ?></td>
<td style="text-align:center; border:1px solid #999;padding:4px 8px;"><?php echo $qq['telepon'] ?></td>
<td style="text-align:center; border:1px solid #999;padding:4px 8px;"><?php echo $qq['jambuka'] ?></td>
<td style="text-align:center; border:1px solid #999;padding:4px 8px;"><?php echo $qq['jamtutup'] ?></td>
<td style='text-align:center; border:1px solid #999;padding:4px 8px; cursor:pointer;' data-id=".$qq['id'].">
<div id="id01" class="modal">
<div class="container" style="background-color:#FFBD30">
<button style="margin-top: 0px; margin-left:0px; width:100px; float:right;" type="button" onclick="document.getElementById('id01').style.display='none'" class="tombol_wisata">Tutup</button>
<p style="text-align: justify;padding-top:70px"><?php echo $qq['artikel'] ?></p>
</div>
</div>
<img onclick="document.getElementById('id01').style.display='block'" src="img/ic_search_white_24dp_1x.png">
</td>
<td style="text-align:center; border:1px solid #999;padding:4px 8px;"><img src="img/ic_place_white_24dp_1x.png"></td>
<td style="text-align:center; border:1px solid #999;padding:4px 8px;">
<a style="text-decoration:none;" href="edit_wisata.php?id=<?php echo $qq['id']; ?>"><img src="img/ic_mode_edit_white_24dp_1x.png"></a>
<a style="text-decoration:none;" href="php/proses_delete_wisata.php?id=<?php echo $qq['id']; ?>"><img src="img/ic_delete_white_24dp_1x.png"></a>
</td>
</tr>
<?php $no++; } ?>
The problem is, when I click the magnifying glass button, modal displays the same file. Modal displays data from the first field on all the magnifying glass buttons. How to solve it? Is my code correct? Because I made up my own code :(

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%`
}

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>

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