I want to change the text colors in my table
That is all my php code:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<form action="" method="POST">
Lista:<br>
<input type="text" name="camp_1">
<br>
<input type="submit" value="Submit" />
<?php
if (isset($_POST['camp_1'])){
$var = $_POST['camp_1'];
$var = str_replace(' ','', $var);
$x = explode(',', $var);
$odd = 1;
$sum = 0;
echo '<table>';
for($i = 0; $i < count ($x) - 1; $i = $i + 2){
if ( $odd ){
echo
'<tr>
<th>'.$x[$i].'</td>
<th>'.$x[$i + 1].'</td>
</tr>';
}
else{
echo
'<tr>
<td>'.$x[$i].'</td>
<td>'.$x[$i + 1].'</td>
</tr>';
}
$sum += $x[$i + 1];
$odd = 1 - $odd; // comutes between 0 and 1
}
if ( $odd ){
echo
'<tr >
<th>Total</th>
<th>'.$sum.' $</th>
</tr>';
}
else{
echo
'<tr>
<td>Total</td>
<td>'.$sum.' $</td>
</tr>';
}
echo '</table>';
}//if isset
?>
</form>
</body>
</html>
And my CSS file
Names style.css.
table th{
background-color: green;
color: #58FAD0;
}
td {
color: #CC2EFA;
}
It just ignores whatever I write in my CSS file, regarding td and th
I must add that both files are in the same folder
table, th, tr{
background-color: green;
color: #58FAD0;
}
td {
color: #CC2EFA;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Savings</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>$100</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
<td>$150</td>
</tr>
<tr>
<td>Joe</td>
<td>Swanson</td>
<td>$300</td>
</tr>
<tr>
<td>Cleveland</td>
<td>Brown</td>
<td>$250</td>
</tr>
</table>
</body>
</html>
Related
I'm retrieving all the rows from sql database through a query, and i'm using a loop to get all the rows
I have a style.css file which i'm specifying a style for the , however, only the first row is receiving the style and the style is not being applied to the rest.
<!DOCTYPE html>
<html>
<link href="styles.css" rel="stylesheet">
<head>
<title>IIACSS search center</title>
</head>
<body>
<form method="post">
<label>Search</label>
<input type="text" name="search">
<input type="submit" name="submit">
</form>
<?php
$con = new PDO("mysql:host=localhost;dbname=data",'root','admin123');
if (isset($_POST["submit"])) {
$str = $_POST["search"];
$sth = $con->prepare("SELECT * FROM `questions` WHERE question LIKE '%$str%'");
$sth->setFetchMode(PDO:: FETCH_OBJ);
$sth -> execute();
?>
<table>
<tr>
<th>Project</th>
<th>Question</th>
<th>Sample</th>
</tr>
<?php
for ($a=0;$row = $sth->fetch();$a++) {
?>
<tr>
<td><?php echo $row->Proj_Name; ?></td>
<td><?php echo $row->Question;?></td>
<td><?php echo $row->sample;?></td>
<br><br>
</tr>
</table>
<?php
}
}
?>
body {
background-color: white;
}
h1 {
color: black;
}
td {
color: black;
font-family: sans-serif;
}
th {
color: blue;
font-family: sans-serif;
}
It is not a CSS problem, it is because you are closing your </table> in the loop, so it is ending your table after the first tr.
Just remove it from the loop:
<?php
$con = new PDO("mysql:host=localhost;dbname=data",'root','admin123');
if (isset($_POST["submit"])) {
$str = $_POST["search"];
$sth = $con->prepare("SELECT * FROM `questions` WHERE question LIKE '%$str%'");
$sth->setFetchMode(PDO:: FETCH_OBJ);
$sth -> execute();
?>
<table>
<tr>
<th>Project</th>
<th>Question</th>
<th>Sample</th>
</tr>
<?php
for ($a=0;$row = $sth->fetch();$a++) {
?>
<tr>
<td><?php echo $row->Proj_Name; ?></td>
<td><?php echo $row->Question;?></td>
<td><?php echo $row->sample;?></td>
<br><br>
</tr>
<?php
} ?>
</table>
<?php }
?>
Specifically I want a multiplication table based on the user's input. All I need is the table because the output is already correct.
Let's say my input multiplier is 2.
This is my code when attempting to do the table.
<!DOCTYPE html>
<html>
<head>
<title> Multiplication Table </title>
</head>
<body>
<form method="POST" action="#">
Enter Multiplier: <input type="text" name="mult"> <br>
<input type="submit" name="m" value="Multiply">
</form>
<table border="1">
<?php
if(isset($_POST["m"])){
$mult = 0;
$x = 1;
$mulpli = 1;
$pro = 0;
$mult=$_POST["mult"];
while($mulpli <= $mult)
{
echo "<tr>". "<th>". " ";
echo "<th>". $mulpli;
while($x <= $mult)
{
$pro=$x*$mulpli;
echo "<tr>";
echo "<th>". $x;
echo "<th>". $pro;
$x=$x+1;
}
$x = $x-$mult;
$mulpli=$mulpli+1;
}
}
?>
</th></th></tr></th></th></tr>
</table>
</body>
</html>
Table:
My desired output:
This should do it. I made use of PHP's alternative syntax for cleaner view code:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Multiplication Table</title>
<style type="text/css">
table, th, td { border: 1px solid black; }
th, td { text-align: center; }
</style>
</head>
<body>
<form method="post">
<label>Enter multiplier limit: <input type="text" name="limit"></label>
<button type="submit">Show Table</button>
</form>
<?php if (isset($_POST['limit'])):
$limit = max(1, (int)$_POST['limit']);
?>
<table>
<tr>
<?php for ($x = 0; $x <= $limit; $x++): ?>
<th scope="col"><?= $x ?: '' ?></th>
<?php endfor ?>
</tr>
<?php for ($y = 1; $y <= $limit; $y++): ?>
<tr>
<th scope="row"><?= $y ?></th>
<?php for ($x = 1; $x <= $limit; $x++): ?>
<td><?= $y * $x ?></td>
<?php endfor ?>
</tr>
<?php endfor ?>
</table>
<?php endif ?>
</body>
</html>
I'm currently in an introductory course into PHP and I'm having trouble with my current assignment. Its rather simple in logic, but I can't find where my error is. The abstract is to loop from one to ten, display whether the number is even or odd, and display these facts in a table. So, row one would be 1 - odd
This is my current Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>1 To 10 Even Odd</title>
<link rel="stylesheet" type="text.css" href="common.css" />
<style type="text/css">
th { text-align: center; background-color: #999; }
th, td ( padding: 0.6em; )
tr.alt td { background: #ddd }
</style>
<h3>1 To 10</h3>
<table cellspacing="1" border="1" style="width: 20em; border: 1px solid #999;">
<tr>
<th>Number</th>
<th>Even Or Odd?</th>
</tr>
<?php
$max = 10 ;
$intCounter = 0;
while ( $intCounter < $max){
$intCounter++
?>
<tr <?php if ( $intCounter % 2 == 0 ) echo ' class="alt"' ?> >
<td> <?php echo $intCounter ?> </td>
<td> <?php echo "even" ?> </td>
</tr>
<tr <?php if ( $intCounter % 2 == 1 ) ?> >
<td> <?php echo $intCounter ?> </td>
<td> <?php echo "odd" ?> </td>
</tr>
<?php
}
?>
</body>
</html>
This current code is displaying all the numbers twice. So I'll get 1, odd, 1, even.
Thanks for the help in advance! I appreciate all the help!
row one would be 1 - odd
Put this statement $intCounter++ at the end of while loop, otherwise it would print only 9 rows. Also, your second <tr <?php if ( $intCounter % 2 == 1 ) ?> > ... </tr> is redundant here.
If you want to start your table with the odd styled row, then change your code in the following way,
// your code
$max = 10 ;
$intCounter = 1;
while($intCounter <= $max){
?>
<tr <?php if($intCounter % 2 == 0){ echo ' class="alt"'; } ?>>
<td> <?php echo $intCounter; ?> </td>
<td> <?php if($intCounter % 2 == 0){ echo "even"; }else{ echo "odd"; } ?> </td>
</tr>
<?php
$intCounter++
}
// your code
Since you want to display nos from 1 to 10, you should do the following modifications:
Set $intCounter = 1 instead of $intCounter = 0
Set while ($intCounter <= $max) instead of while ($intCounter < $max)
Mention the increment operation $intCounter++; just before the loop ends
Try this:
<?php if ($intCounter % 2 == 0 ) { ?>
<tr class='alt'>
<td> <?php echo $intCounter ?> </td>
<td> <?php echo "even" ?> </td>
</tr>
<?php } else { ?>
<tr>
<td> <?php echo $intCounter ?> </td>
<td> <?php echo "odd" ?> </td>
</tr>
<?php } ?>
This is my code to get data from a database to a html table. but column size expand with the size of the details. i need to get a fixed size columns. please help me to get fixed size columns.
<?php
ini_set ('display_errors',0);
?>
<!DOCTYPE html>
<?php
require_once 'issue_tracker_connection.php';
mysql_select_db('issue_tracker');
$sql = "SELECT * FROM issue_tracker";
$records = mysql_query($sql);
?>
<html>
<head>
<style>
table th, td{
table-layout:fixed;
border: 1px solid black;
border-collapse: collapse;
align:center;
}
</style>
</head>
<body>
<?php
echo '<table >';
$columns = array();
$resultset = array();
while ($row = mysql_fetch_assoc($records)) {
if (empty($columns)) {
$columns = array_keys($row);
echo '<tr><th>'.implode('</th><th>', $columns).'</th></tr>';
}
$resultset[] = $row;
echo '<tr><td >'.implode('</td><td>', $row).'</td></tr>';
}
echo '</table>';
?>
</body>
</html>
Try this:
<?php
echo '<table width="100%" cellspacing="0" cellpadding="2" border="0">';
$columns = array();
$resultset = array();
while ($row = mysql_fetch_assoc($records)) {
if (empty($columns)) {
$columns = array_keys($row);
echo '<tr><th>'.implode('</th><th>', $columns).'</th></tr>';
}
$resultset[] = $row;
echo '<tr><td >'.implode('</td><td>', $row).'</td></tr>';
}
echo '</table>';
?>
You can also set size of tag...
Try it now...
<tr>
<td width="50%">Your Code Here</td>
<td width="50%">Your Code Here</td>
</tr>
Try this..
<table class="fixed">
<col width="20px" />
<col width="30px" />
<col width="40px" />
<tr>
<td>text</td>
<td>text</td>
<td>text</td>
</tr>
</table>
and CSS:
table.fixed { table-layout:fixed; }
table.fixed td { overflow: hidden; }
Check original answer here.
my index.php page has something like this:
if(isset($_GET['page'])) {
switch ($_GET['page']) {
case 'login':
include('login.php');
break;
case 'log_out':
$_SESSION['loggedin'] = false;
include('loggedout.html');
break;
case 'profile':
include('profile.php');
break;
case 'contact_us':
include('contact.html');
break;
default:
include('home.html');
break;
}
} else {
include('home.html');
}
Now I can easily call on a new page when a link is pressed or form is submitted i.e:
Home Contact Us
but what if I had an all in one form... It would need to be able to send a request through PHP code to my index.php to show the logged in page or reshow the login.php page but I cant seem to find anything that would allow me to send a request in php script i.e:
<?php
if(..) {
send request to index.php i.e index.php?page=profile
}
?>
The reason I need this is my index.php uses divs as formatting and thus if I dont request pages through index.php they are displayed in a new html (i.e it doesnt have my navigation bars etc)
As per a comment by #HydraIO and others how would this be done using JQuery?
Here is some code:
lgoin.php:
<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Login</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1"
/>
<!--
//thanks http://decoraora.com/fototapete_115x175.php for the image http://decoraora.com/fototapete/images/664.jpg -->
<style type="text/css">
.table {
background: url("bg.jpg") no-repeat;
width: 600px;
height: 250px;
}
</style>
</head>
<body>
<?php
if (isset($_POST['formLogin'])) {
$un = $_POST['username'];
$pwd = $_POST['password'];
if($pwd!=''&&$un!='') {
if (md5($pwd)==='29ef52e7563626a96cea7f4b4085c124') {
$_SESSION['loggedin'] = true;
$_SESSION['login_error'] = 0;
$_SESSION['username'] = $un;
$_SESSION['password'] = $pwd;
//include('profile.php');
//header("Location: index.php/page=profile");
//header("Location: http://localhost/index.php?index.php?page=profile");
} else {
$_SESSION['login_error'] = 1;
echo 'this will dipslay in its own page';
//include('login.php');
}
}else {
$_SESSION['login_error'] = 2;
echo 'display in its own page';
//include('login.php');
?>
<form action="login.php" method="post">
<table bgcolor="white" align="center" border="1px" width="50%" style="height: 40%;">
<tr>
<td colspan="2" align="center">Login for CC Chat</td>
</tr>
<tr>
<td>Username:</td>
<td>Password:</td>
</tr>
<tr>
<td><input type="text" name="username" maxlength="50"/></td>
<td><input type="password" name="password" maxlength="50"/></td>
</tr>
<tr>
<td>Forgot your password?</td>
<td><input type="submit" name="formLogin" value="Login"/></td>
</tr>
<?php
if(isset($_SESSION['login_error'])) {
switch ($_SESSION['login_error']) {
case 1:
echo '<tr>';
echo '<td>';
echo '<font color="red">Password does not match</font>';
echo '</td>';
echo '</tr>';
$_SESSION['login_error']=0;
break;
case 2:
echo '<tr>';
echo '<td>';
echo '<font color="red">Password and/or Username cannot be empty</font>';
echo '</td>';
echo '</tr>';
$_SESSION['login_error']=0;
break;
default:
break;
}
}
?>
<tr>
<td colspan="2" align="center">© CopyLeft David Kroukamp 11039662 2013 - 2015</td>
</tr>
</table>
</form>
<?php
}
}else {
?>
<form action="login.php" method="post">
<table bgcolor="white" align="center" border="1px" width="50%" style="height: 40%;">
<tr>
<td colspan="2" align="center">Login for CC Chat</td>
</tr>
<tr>
<td>Username:</td>
<td>Password:</td>
</tr>
<tr>
<td><input type="text" name="username" maxlength="50"/></td>
<td><input type="password" name="password" maxlength="50"/></td>
</tr>
<tr>
<td>Forgot your password?</td>
<td><input type="submit" name="formLogin" value="Login"/></td>
</tr>
<?php
if(isset($_SESSION['login_error'])) {
switch ($_SESSION['login_error']) {
case 1:
echo '<tr>';
echo '<td>';
echo '<font color="red">Password does not match</font>';
echo '</td>';
echo '</tr>';
$_SESSION['login_error']=0;
break;
case 2:
echo '<tr>';
echo '<td>';
echo '<font color="red">Password and/or Username cannot be empty</font>';
echo '</td>';
echo '</tr>';
$_SESSION['login_error']=0;
break;
default:
break;
}
}
?>
<tr>
<td colspan="2" align="center">© CopyLeft David Kroukamp 11039662 2013 - 2015</td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>
index.php:
<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>CC Chat</title>
<meta
http-equiv="content-type" content="text/html; charset=iso-8859-1"
/>
<!--
Thanks too http://stackoverflow.com/a/7851347/1133011 for the help
on layout which acts more like frames but avoids them and using divs. As frames have
known draw backs see here http://stackoverflow.com/questions/4600648/frames-with-php we
should thus rather use include in php
!-->
<style type="text/css" media="screen">
/* Generic pane rules */
body { margin: 0 }
.row, .col { overflow: hidden; position: absolute; }
.row { left: 0; right: 0; }
.col { top: 0; bottom: 0; }
.scroll-x { overflow-x: auto; }
.scroll-y { overflow-y: auto; }
.header.row { height: 75px; top: 0; background-color: #E5E5E5; }
.menu.row { height: 75px; top: 75px; background-color: #EDEDED; }
.body.row { top: 150px; bottom: 50px; background-color: #F5F5F5; }
.footer.row { height: 75px; bottom: 0; background-color: #EBEBEB; }
A:link {text-decoration: none}
A:visited {text-decoration: none}
A:active {text-decoration: none}
A:hover {font-size:24; font-weight:bold; color: red;}
</style>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<div class="header row">
<?php
include("header.html");
?>
</div>
<div class="body row scroll-y">
<?php
if(isset($_GET['page'])) {
switch ($_GET['page']) {
case 'login':
include('login.php');
break;
case 'log_out':
$_SESSION['loggedin'] = false;
include('loggedout.html');
break;
case 'profile':
include('profile.php');
break;
case 'contact_us':
include('contact.html');
break;
default:
include('home.html');
break;
}
} else {
include('home.html');
}
?>
</div>
<div class="menu row">
<?php
include("nav.php");
?>
</div>
<div class="footer row">
<?php
include("footer.php");
?>
</div>
</body>
</html>
UPDATED with JQuery attempt in if statement which checks password hash:
<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Login</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1"
/>
<!--
//thanks http://decoraora.com/fototapete_115x175.php for the image http://decoraora.com/fototapete/images/664.jpg -->
<style type="text/css">
.table {
background: url("bg.jpg") no-repeat;
width: 600px;
height: 250px;
}
</style> <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<?php
if (isset($_POST['formLogin'])) {
$un = $_POST['username'];
$pwd = $_POST['password'];
if($pwd!=''&&$un!='') {
if (md5($pwd)==='29ef52e7563626a96cea7f4b4085c124') {
$_SESSION['loggedin'] = true;
$_SESSION['login_error'] = 0;
$_SESSION['username'] = $un;
$_SESSION['password'] = $pwd;
//include('profile.php');
//header("Location: index.php/page=profile");
//header("Location: http://localhost/index.php?index.php?page=profile");
//exit();
echo "<script type='text/javascript'>";
echo "$.get('index.php?page=profile', function(data) {
$('.body.row').html(data);
alert('Load was performed.');
});";
echo "</script>";
} else {
$_SESSION['login_error'] = 1;
echo 'this will dipslay in its own page';
//include('login.php');
}
}else {
$_SESSION['login_error'] = 2;
echo 'display in its own page';
//include('login.php');
?>
<form action="login.php" method="post">
<table bgcolor="white" align="center" border="1px" width="50%" style="height: 40%;">
<tr>
<td colspan="2" align="center">Login for CC Chat</td>
</tr>
<tr>
<td>Username:</td>
<td>Password:</td>
</tr>
<tr>
<td><input type="text" name="username" maxlength="50"/></td>
<td><input type="password" name="password" maxlength="50"/></td>
</tr>
<tr>
<td>Forgot your password?</td>
<td><input type="submit" name="formLogin" value="Login"/></td>
</tr>
<?php
if(isset($_SESSION['login_error'])) {
switch ($_SESSION['login_error']) {
case 1:
echo '<tr>';
echo '<td>';
echo '<font color="red">Password does not match</font>';
echo '</td>';
echo '</tr>';
$_SESSION['login_error']=0;
break;
case 2:
echo '<tr>';
echo '<td>';
echo '<font color="red">Password and/or Username cannot be empty</font>';
echo '</td>';
echo '</tr>';
$_SESSION['login_error']=0;
break;
default:
break;
}
}
?>
<tr>
<td colspan="2" align="center">© CopyLeft David Kroukamp 11039662 2013 - 2015</td>
</tr>
</table>
</form>
<?php
}
}else {
?>
<form action="login.php" method="post">
<table bgcolor="white" align="center" border="1px" width="50%" style="height: 40%;">
<tr>
<td colspan="2" align="center">Login for CC Chat</td>
</tr>
<tr>
<td>Username:</td>
<td>Password:</td>
</tr>
<tr>
<td><input type="text" name="username" maxlength="50"/></td>
<td><input type="password" name="password" maxlength="50"/></td>
</tr>
<tr>
<td>Forgot your password?</td>
<td><input type="submit" name="formLogin" value="Login"/></td>
</tr>
<?php
if(isset($_SESSION['login_error'])) {
switch ($_SESSION['login_error']) {
case 1:
echo '<tr>';
echo '<td>';
echo '<font color="red">Password does not match</font>';
echo '</td>';
echo '</tr>';
$_SESSION['login_error']=0;
break;
case 2:
echo '<tr>';
echo '<td>';
echo '<font color="red">Password and/or Username cannot be empty</font>';
echo '</td>';
echo '</tr>';
$_SESSION['login_error']=0;
break;
default:
break;
}
}
?>
<tr>
<td colspan="2" align="center">© CopyLeft David Kroukamp 11039662 2013 - 2015</td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>
I see the alert but am redirected to a blank html like before (even than before at least the form was reloaded
It sounds like you might want to evaluate all the data inside your index page first and do a page redirect by setting a header inside php to the same index page with the get query updated to whatever you want it to be.
header("Location: http://www.example.com/index.php?page=profile");
exit();
make sure you don't output any html with echo or any other way before you do the redirect. If your script heavily uses echo before that you can look into outputbuffering your echos.
PHP is a server-side language, and not dynamic. You want to use javascript (jQuery in my example).
$_GET = <?php echo json_encode($_GET); ?>;
if($_GET['val'] == some_value){
$.get(index.php?getvar=getval, {
complete: function(){
$('#div').val("YOUR OUTPUT"); // *
}
});
}
*This can be any code you feel like, you could replace the div with the entire php file if you chose to