Does anyone know how to have different colours on alternate rows in a table
You can use jquery to add classes to the rows like this:
<script type="text/javascript">
$(document).ready(function () {
$("table.evenodd tr:not([th]):odd").addClass("odd");
$("table.evenodd tr:not([th]):even").addClass("even");
});
</script>
And then use CSS to style:
.even {background-color: #e6e6e6;}
.odd {background-color: #ffffff;}
http://www.w3.org/Style/Examples/007/evenodd
table.evenodd tr:nth-child(even) { background: #CCC; }
table.evenodd tr:nth-child(odd) { background: #FFF; }
You need to apply a style to every second row. You can do that by adding a counter to your loop, and if that counter-variable % 2 == 0, you add the style to the row in question, like this:
$i = 0;
while ($row = mysql_fetch_array($result)){
$style = "";
if($i%2==0)
{
$style = ' style="background-color: #CCC"';
}
echo "<tr".$style."><td>";
echo "<center>" .$row['name']. "</center>";
echo "</td><td>"
echo "<center>" .$row['age']. "</center>";
echo "</td><td>"
echo "<center>" .$row['car']. "</center>";
echo "</td></tr>"
$i++;
}
Typically I would have an even and odd css class that defined the different colours then in PHP when outputting the <tr> I would apply this to the element. You'd typically use a row counter in your loop and mod2 the counter.
$rows = 0;
while ($row = mysql_fetch_array($result)) {
$rows++;
$even_odd = ($rows % 2) ? 'odd' : 'even' ;
echo "<tr class=$even_odd><td>";
echo "<center>" .$row['name']. "</center>";
echo "</td><td>"
echo "<center>" .$row['age']. "</center>";
echo "</td><td>"
echo "<center>" .$row['car']. "</center>";
echo "</td></tr>"
}
Related
This is my pagination program :
if(isset($_POST['view']))
{
$per_page = 20;
if(isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page = 1;
}
$start = $per_page * $page;
$start = $start - $per_page;
if(isset($_GET['seller']) ) {
$seller = $_GET['seller'];
} else {
$seller = ($_POST['seller']);
}
echo $seller."_".$start;
$query = "SELECT Kala.mark ,Kala.sharhe_kala, Kala_forooshande.date From Kala Join Kala_forooshande ON Kala.id_kala=Kala_forooshande.id_kala WHERE Kala_forooshande.id_forooshande=(SELECT id_forooshande From Forooshande where forooshande='". $seller ."') LIMIT $start , $per_page";
$result = mysqli_query($db,$query);
$query2 = "SELECT COUNT(*) as total FROM Kala Join Kala_forooshande ON Kala.id_kala=Kala_forooshande.id_kala WHERE Kala_forooshande.id_forooshande=(SELECT id_forooshande From Forooshande where forooshande='". $seller ."')";
$result2 = mysqli_query($db,$query2);
if ($result->num_rows > 0) {
echo " <form method='post' enctype='multipart/form-data'>
<table class='blueTable'><thead><tr><th>شماره</th><th>شرح کالا</th><th>مارک</th><th>تاریخ</th></tr></thead>";
while ($row = $result->fetch_assoc()) {
$tarikh=gregorian_to_jalali((substr($row["date"],0,4)),(substr($row["date"],5,2)),(substr($row["date"],8,2)));
$j=$start++;
echo "<tr><td>" . $j . "</td><td>" . $row["sharhe_kala"] . "</td><td>" . $row["mark"] . "</td><td>" .$tarikh[0]."-".$tarikh[1]."-".$tarikh[2] . "</td></tr>";
}
echo "</table></form> ";
$total = mysqli_fetch_assoc($result2);
$total_page = (ceil($total['total'] / $per_page));
echo "<table class='pagination'>";
$prev = $page - 1;
if ($page <= 1) {
echo "
<td> << </td>
";
} else {
echo "
<td> << </td>
";
}
for ($i = 1; $i <= $total_page; $i++) {
if ($i == $page) {
echo "
<td class='active'>$i</td>";
} else {
echo "
<td>" . $i . "</td>";
}
}
$next = $page + 1;
if ($page >= $total_page) {
echo "
<td>>></td>
";
} else {
echo "
<td> >></td>
";
}
echo "</table>";
}
}
The problem is when the user click on page 2 , the <a> tag sends <a href=\"?page=" . $i . "&seller=".$seller."\">" to see the page number 2 , but it doesn't work because of the if clauseif(isset($_POST['view']) at the begining
So it just works for first page which the view button is clicked .
Is it possible to send button value in the link to simulate on button click event ?
Based on your comment on the question:
i want to be shown only if user click on view button
Then you want more view buttons. Replace your links with their own forms which post the values your server-side code expects. So instead of this:
echo "
<td>" . $i . "</td>";
You might have something like this:
echo "
<td>
<form method=\"post\">
<input type=\"hidden\" name=\"page\" value=\"" . $i "\" />
<input type=\"hidden\" name=\"seller\" value=\"" . $seller "\" />
<input type=\"submit\" name=\"view\" value=\"" . $i "\" />
</form>
</td>";
You can use CSS to style your buttons to look like links if you prefer. Tools like Boostrap make that very easy, just adding classes like "btn btn-link" to any clickable element for example.
But ultimately if you want your links to submit a form them make them forms.
I have a table in which I am printing a button at the end of each row. I wish this button to be small.
And my text is not getting truncated when the table is printed despite using overflow ellipsis. I don't want the horizontal scroll bar.
My title bar of the table, the topmost row is always getting hidden as I scroll down.
Any help is much appreciated. Here is my CSS script https://jsfiddle.net/v6sahfyc/1/
Edit: Adding the php code here:
// <?php
// echo "<div id='awesometable'>";
// ?>
<!-- <CAPTION>RESERVATION TABLE</CAPTION> -->
<?php
echo "<br><br><p style=\"font-family:verdana; font-size:100%; margin-left: 5em;\"><i><strong>Reservation Table.</strong></i></p>";
echo "<table id='reservation' class='inlineTable'>"; //style='border: solid 1px black;'
//echo "<thead>";
echo "<tr>";
echo "<th class=\"row-1 uid\">UserId</th>";
echo "<th class=\"row-2 rname\">Resource <br> Name</th>";
echo "<th class=\"row-3 rtype\">Resource <br> Type</th>";
echo "<th class=\"row-4 rloc\">Resource <br> Location</th>";
echo "<th class=\"row-5 treq\">Time <br> Required </th>";
echo "<th class=\"row-6 REM\">Delete? </th>";
echo "</tr>";
//echo "</thead>\n";
//echo "<col = width: 3em />";
?>
<?php
$username = "root";
$password = "";
$host = "localhost";
$connector = mysql_connect($host,$username,$password) or die("Unable to connect");
$selected = mysql_select_db("mydb", $connector) or die("Unable to connect");
$selectSQL = "SELECT user_ID, ResourceName, ResourceType, ResourceLocation, ReservationTime FROM reservation_table";
if( !( $selectRes = mysql_query( $selectSQL ) ) ){
echo 'Retrieval of data from Database Failed - #'.mysql_errno().': '.mysql_error();
}else{
?>
<tbody>
<?php
if( mysql_num_rows( $selectRes )==0 ){
}else{
while( $row = mysql_fetch_assoc( $selectRes ) ){
echo "<tr><td>{$row['user_ID']}</td><td>{$row['ResourceName']}</td><td>{$row['ResourceType']}</td><td>{$row['ResourceLocation']}</td><td>{$row['ReservationTime']}</td>";
$active = $_SESSION['usr'] == $row['user_ID'];
echo "<td>"."<button class='btn' type='submit' id='1'".($active ? "" : "disabled").">"."Delete"."</button>"."</td>";
}
}
?>
</tbody>
</table>
<?php
mysql_close($connector);
}
?>
In the following HTML table snippet, I will like to add footnotes (or simple comments) for columns "stat 1", "stat 2" and "stat 3" to explain each of the stats in more detail. Preferably, want the comments/details to appear right below the table with each comment correctly referring to the right column. Any suggestion on how to achieve this? Thanks.
if(!empty($output))
{
echo '<table cellpadding="0" cellspacing="0" class="imagetable">';
echo '<caption>Table 1</caption>';
echo '<tr><th>Date</th><th>Stat 1</th><th>Stat 2</th><th>Stat 3</th><th></tr>';
foreach ($output as $result)
{
$temp = explode(" ",$result);
echo '<tr>';
for($i=0;$i<count($temp);$i++){
echo '<td>',$temp[$i],'</td>';
}
echo '</tr>';
}
echo '</table><br />';
}
You could use tfoot (http://www.w3schools.com/tags/tag_tfoot.asp)
if(!empty($output))
{
echo '<table cellpadding="0" cellspacing="0" class="imagetable">';
echo '<caption>Table 1</caption>';
echo '<thead><tr><th>Date</th><th>Stat 1</th><th>Stat 2</th><th>Stat 3</th></tr></thead>';
echo '<tfoot><tr><td> </td><td>*note 1</td><td>*note 2</td><td>*note 3</td></tr></tfoot>';
foreach ($output as $result)
{
$temp = explode(" ",$result);
echo '<tr>';
for($i=0;$i<count($temp);$i++){
echo '<td>',$temp[$i],'</td>';
}
echo '</tr>';
}
echo '</table><br />';
}
I have PHP coding that works but I have had no luck transferring this to a HTML form. Any advise?
<?php
$db_host = "localhost";
$db_username = "combsb_combsb";
$db_pass = "pat60086";
$db_name = "combsb_sample";
#mysql_connect ("$db_host", "$db_username", "$db_pass") or die ("Could not connect to MySQL");
#mysql_select_db("$db_name") or die ("No Database");
Echo"Successful Connection";
$sql = "SELECT compname FROM Crew";
$result = mysql_query($sql);
echo "<select name='compname'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['compname'] . "'>" . $row['compname'] . "</option>";
}
echo "</select>";
?>
First I'd make sure to add the HTML boilerplate around your PHP. Then I think you mean mysql_fetch_row instead of mysql_fetch_array. Your final file should look something like:
<!DOCTYPE html>
<html>
<head></head>
<body>
<?php
// *Cut out for brevity, remember to paste back in*
$result = mysql_query($sql);
?>
<form action="" method="POST">
<?php
echo "<select name='compname'>";
while ($row = mysql_fetch_row($result)) {
echo "<option value='" . $row['compname'] . "'>" . $row['compname'] . "</option>";
}
echo "</select>";
?>
</form>
</body>
</html>
Post your output / errors next time or if this doesn't work
View image: http://i.stack.imgur.com/yxE4u.jpg
Although this is in PHP, I'm trying to (with HTML) get two words (which use the same class) to push next to each other without calling the class seperately or using the two words in the same if/if else function. I'm sure it's just a simple HTML element.
<?php
if (something){
echo "<p class='message'>hello</p>";
}
if (somethingelse){
echo "<p class='message'>world</p>";
}
?>
As the comments aren't what I'm after, I'll show what I'm using:
<?php
if (!empty($message)){
echo "<p class='message'>" . $message . "</p>";
}
if (!empty($errors)){
echo "<p class='message'>";
foreach ($errors as $error){
echo " - " . $error . "<br />";
}
echo "</p>";
}
?>
One statement or both at the same time may prove true. Hopefully you can see why I can't wrap it all around an HTML tag.
i think you want to do this
<p class="messasge"> <?php echo $message; ?> </p>
RE-FIXED according to updated specifications :
<?php
if (!empty($message)){
echo "<p class='message'>" . $message;
}
if (!empty($errors)){
if (empty($message) echo "<p class='message'>";
foreach ($errors as $error){
echo " - " . $error . "<br />";
}
}
if ((!empty($message))||(!empty($errors))) echo "</p>";
?>
if JUST message is true it'll print <p class='message'>MESSAGE</p>
if JUST errors is true, it'll print <p class='message'>ERRORS</p>
if BOTH are true, it'll print <p class='message'>MESSAGEERRORS</p>
I think you probably want some variant on the following code:
<?php
$output = '';
if (something)
$output .= 'hello';
if (somethingelse)
$output .= 'world';
if ($output)
echo "<p class='message'>$output</p>";
?>
For your example, you could replace all of the echos in the first two conditions with appending to $message, then only output if there is something to output. There are numerous options but I think this is the most flexible / elegant.
In your case it can be simplified to,
<?php
$output = $message;
if (!empty($errors))
foreach ($errors as $error)
$output .= " - $error<br />";
if (!empty($output))
echo "<p class='message'>$output</p>";
?>
Maybe the dl list is useful
<?php
if (!empty($message)){
echo "<dl class='message'><dt>" . $message . "</dt>";
}
if (!empty($errors)){
foreach ($errors as $error){
echo "<dd> - " . $error . "</dd>";
}
echo "</dl>";
}
?>
<?php
$message = "";
if (something) $message = "hello";
if (somethingelse) $message += " world";
echo "<p class='message'>".$message."</p>";
?>