I have a generated php table which I would like to apply style in my style sheet, so for example top:15px, left:10px ect..., not sure how call the table and link it with css -
echo "<table border=1>";
for ($i=0;$i<count($calls);$i++){
for ($j=0;$j<count($days);$j++){
$k = $days[$i].$times[$j];
if (array_key_exists($k,$date)){
echo "<td colspan='{$date[$k][1]}'>".
"{$date[$k][0]}</td>";
$j+=$date[$k][1]-1;
}else
echo "<td style='color:gray'>$k</td>";
}
echo "</tr>";
}
echo "</table>";
any help much appreciated, thank you
echo '<table style="top: 15px; left:10px;">';
or
echo '<table class="someClass">';
and then use CSS
.someClass{
top: 15px;
left: 10px;
}
Give the table an id
echo "<table id=\"my_table\" border=1>";
And then use this in the <head> tag of the document:
<style type="text/css">
#my_table {
top: 15px;
...
}
</style>
echo "<table style=\"border: 1px; top:15px; left: 10px;\">";
Is this statement not working?
echo "<table border=1 style=\"top:15px; left:10px\">";
for ($i=0; $i<count($calls); $i++) {
for ($j=0;$j<count($days);$j++) {
$k = $days[$i].$times[$j];
if (array_key_exists($k,$date)) {
echo "<td colspan='{$date[$k][1]}'>".
"{$date[$k][0]}</td>";
$j+=$date[$k][1]-1;
} else {
echo "<td style='color:gray'>$k</td>";
}
}
echo "</tr>";
}
echo "</table>";
Related
I did the nth child rule in css but it the colors are not alternate. Colors are all the same each rows and columns. My code is a multiplication table with a supposed alternate color. Im having a hard time knowing what I should do to modify or add in my php code to create the desired output (alternate colors in rows and columns in the multiplication table).
Below is my php code:
<?php
for($row=0; $row<=10; $row++){
echo "<tr>";
for($column=0; $column<=10; $column++){
if($row==0 && $column==0){
echo "<td></td>";
}
else{
echo "<td>". $row * $column. "</td>";
}
}
}
echo "</tr>";
?>
CSS code:
table, th, td {
border: 1px solid black;
}
td {
padding: 15px;
}
.bold{
font-weight: bold;
font-size: 1.3em;
}
table tr:nth-child(odd) {
background-color: yellow;
}
table tr:nth-child(even) {
background-color: red;
}
you forget to use the <table> tag and your CSS address it from the table.
<?php
echo "<table>";
for($row=0; $row<=10; $row++){
echo "<tr>";
for($column=0; $column<=10; $column++){
if($row==0 && $column==0){
echo "<td></td>";
}
else{
echo "<td>". $row * $column. "</td>";
}
}
echo "</tr>";
}
echo "</table>";
?>
I want to make all the rows in the same size, but rows height with data is large. I want to fix this row size for empty rows. Table class name is demo.
Demo
<?php
while($fet=mysql_fetch_assoc($sql1))
{
$id=$fet['c_id'];
$address=$fet['address'];
$chk=$fet["c_name"];
$in=$in+1;
echo "<tr>";
echo "<td style='align:center'><a class='astext' href='client_view.php?cid=".$fet["c_id"]."'>".$fet["c_name"]."</a></td>";
echo "<td style='align:center'><a class='ima' href='client_details.php?cid=".$fet["c_id"]."'><img src='image/edit1.png' alt='edit' style='width:20px; height:20px' title=Edit></a></td><td style='align:center'>
<a class='ima' href='clients.php?del=".$fet["c_id"]."'><img src='image/delete1.png' alt='delete' style='width:20px;height:20px' title=Delete></a></td>";
echo "</tr>";
}
if ($in < 10) {
$empty_rows = 10 - mysql_num_rows($sql1);
for ($m = 0; $m < $empty_rows; $m++) {
echo '<tr><td></td><td></td><td></td></tr>';
}
}
?>
You need to add following CSS in your code.
.demo td{
height:20px;
}
Demo
Add fixed height to tr like
table tr {
height: 40px;
box-sizing: border-box;
}
check fiddle: http://jsfiddle.net/4gqgzdL8/1/
If you need box-sizing you can use it otherwise don't use
Try to set height and line-height for td like this: Demo
.demo td {
line-height:28px;
height:28px;
}
I have a php array that includes inputs for posting. It uses a counter for each array record, and this counter is applied to the name of the input to be used in performing some actions with the post - this is working great.
The issue is that I would like to keep the users' existing inputs and re-populate the input fields in the array if their post doesn't pass validation.
I have done this before with static fields, simply storing the post variable and echoing it in the "value" --- but I can't figure out how to do this when working with an array. Anyone have any ideas?
$counter = 0;
echo "<form method='post'>";
echo "<table class='mainlist' width='680'>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr height='60'>";
echo "<td class='mainlist'><input type=text name=options[$counter] autocomplete=off onclick='this.select()' class='txt'></td>";
echo "</tr>";
$counter = $counter + 1;
}
echo "</table>";
Full code per request:
<?php
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$userid = $_SESSION['login_user'];
$companyid = $_POST['companyid'];
$options = $_POST['options'];
$counter = $_POST['hiddencounter'];
$runningtotal=0;
$totaloptions = array_sum($options);
$result = mysqli_query($connection, "SELECT options_balance FROM user_options_balance WHERE user_id = '".$userid."'");
for ($i=0; $i<$counter; $i++)
{
if(empty($options[$i]))
{ /* IF NO INPUT ON OPTIONS */
/* DO NOTHING */
}
else
{
$checknewcompanies = mysqli_query($connection, "SELECT company_id FROM user_company_total_invested WHERE user_id = '".$userid."' and company_id = '" .$companyid[$i]."'");
if($checknewcompanies->num_rows == 1)
{ // do nothing
}
else
{
$runningtotal = $runningtotal + 1;
}
} /* END OF ELSE IF NOT EMPTY OPTIONS */
} /* END OF FOR LOOP */
$checkcurrentcompanies = mysqli_query($connection, "SELECT company_id FROM user_company_total_invested WHERE user_id = '".$userid."'");
$countcompanies = $checkcurrentcompanies->num_rows;
$countcheck = $runningtotal + $countcompanies;
if($countcheck <= 4)
{
while($row = mysqli_fetch_array($result))
{
$balance = $row['options_balance'];
}
if ($totaloptions>$balance)
{
$notenoughoptions= "<div style='background-color:#FF0000; border-radius: 15px; padding: 10px; color: #FFFFFF; font-size: 12px;'>Oops! You don't have enough options! Try investing less!</div>";
}
else
{
// loop through array
for ($i=0; $i<$counter; $i++)
{
if(empty($options[$i])){ /* IF NO INPUT ON OPTIONS */
/* DO NOTHING */
}
else {
if(!ctype_digit($options[$i]) or !is_numeric($options[$i])){
$charactercheck= "<div style='background-color:#FF0000; border-radius: 15px; padding: 10px; color: #FFFFFF; font-size: 12px;'>Oops! Please enter only positive numbers to invest!</div>";
}
else {
$checkcompanies = mysqli_query($connection, "SELECT company_id FROM company_main WHERE company_id = '".$companyid[$i]."'");
if($checkcompanies->num_rows != 1)
{
$companynotexist= "<div style='background-color:#FF0000; border-radius: 15px; padding: 10px; color: #FFFFFF; font-size: 12px;'>Oops! That company doesn't exist!</div>";
}
else
{
// loop through array
for ($i=0; $i<$counter; $i++)
{
if(empty($options[$i]))
{ /* IF NO INPUT ON OPTIONS */
/* DO NOTHING */
}
else
{
$query = "INSERT INTO user_company_invested(user_id, company_id, user_company_options_invested)
VALUES($userid,$companyid[$i],$options[$i])";
mysqli_query($connection, $query);
} /* END OF ELSE IF NOT EMPTY OPTIONS */
} /* END OF FOR LOOP */
$balancecheck = mysqli_query($connection, "SELECT options_balance FROM user_options_balance WHERE user_id = '".$userid."'");
while($row = mysqli_fetch_array($balancecheck))
{
$balance2 = $row['options_balance'];
}
if($balance2 > 0)
{
header('Location: user_invest.php');
}
else
{
header('Location: user_market.php');
}
} // end company check
} //end character check
} //end empty option check
} //end loop
} /* END OF NOT ENOUGH OPTIONS CHECK */
}
else
{
$toomanycompanies = "<div style='background-color:#FF0000; border-radius: 15px; padding: 10px; color: #FFFFFF; font-size: 12px;'>Oops! You can invest in a maximum of 4 companies per week. Please choose fewer companies, or invest more in some of your existing companies!</div>";
/* echo "Maximum number of companies you can invest in is 4";
echo "<br />";
echo "Companies you already are invested in: ".$countcompanies;
echo "<br />";
echo "New companies you are trying to invest in: ".$runningtotal;
echo "<br />";
echo "Total: ".$countcheck;*/
}
} /* END OF ISSET CHECK */
else
{
}
?>
<?php
$result = mysqli_query($connection,"SELECT * from company_main");
$counter=0;
echo "<form method='post'>";
echo "<table class='mainlist' width='680'>";
while($row = mysqli_fetch_array($result))
{
echo "<tr height='60'>";
echo "<td class='mainlist' width=140 align='center'>" . "<img src='".$row['company_logo']."' width='40'/>" . "</td>";
echo "<td class='mainlist' align='left' width=390 style='font-size: 15px;'>" . $row['company_name'] . "</td>";
echo "<input type=hidden name=companyid[$counter] value=" . $row['company_id'] . " />";
echo "<td class='mainlist'><input value='{$_POST['options[$counter]']}' type=text name=options[$counter] autocomplete=off onclick='this.select()' class='txt' style=' background-color: #FCFCFC;
border: solid 1px #CCCCCC;
font-size: 12px;
padding: 5px;
height: 20px;
text-align: right;'></td>";
echo "</tr>";
$counter=$counter+1;
}
echo "</table>";
echo "<input type='hidden' name='hiddencounter' value='$counter'>";
echo "
<table>
<tr>
<td width='630' height='50'></td>
<td align='right' width='60' style='color: #848580; font-size: 20px;'>Total: </td>
<td align='right' width='40' style='color: #94D90B; font-size: 20px; font-weight: bold; padding-right:20px;'><span id='sum'>0</span></td><td width='10'></td>
</tr><tr height='20px'></tr><tr>
<td width='570' align='center' style='color: #94D90B; font-size: 12px;'>";?>
<?php echo $notenoughoptions; ?>
<?php echo $charactercheck; ?>
<?php echo $toomanycompanies; ?>
<?php echo "
</td>
<td colspan='2' width='100' align='right'><input name='userinvestoptionsdynamic' type='submit' value='Invest!'></td><td width='10'></td>
</tr>
<tr height='20px'></tr>
</table>";
echo "</form>";
?>
The correct syntax is:
echo "{$arrayname($keyname)}";
So for example echo('value=' . $_POST['options'][$counter]); becomes:
echo "value={$_POST['options'][$counter]}";
i'm trying to make a form for my website, to transfer some data easily.
I am trying to call in a small javascript when the form submits, but it doesn't run.
below this you can see the PhP function in wich i make my form (in a small table)
and below that i'll enter my javascript.
Any help on this matter would be useful
thanks in advance
php:
function addrow($itemText , $itemPrice , $itemID , $odd)
{
if ($odd)
{
echo "<tr class=\"content-row online\" id=\"958\" bgcolor=#A7A7A7>";
}
else
{
echo "<tr class=\"content-row online\" id=\"958\" bgcolor=#BFBFBF>";
}
echo "<td style=\"width: 70%;\">$itemText</td>";
echo "<td style=\"width: 10%; padding-left: 5px;\"><b>$itemPrice</b></td>";
if (hasRole($itemID))
{
echo "<td style=\"width: 15%; padding-left: 5px;\"><b>Unlocked</b></td>";
}
else
{
echo "<td style=\"width: 15%; padding-left: 5px;\"><b><form name=\"buy\" action=\"php-scripts/BuyItem.php\" onsubmit=\"return buyItem($itemPrice)\" >";
echo "<input type=\"hidden\" name=\"UserID\" value =\"".getUID()."\">";
echo "<input type=\"hidden\" name=\"itemID\" value = \"".$itemID."\" >";
echo "<input type=\"hidden\" name=\"reqKarma\" value = \"".$itemPrice."\" >";
echo "<input name=\"Send\" type=\"submit\" value=\" Buy Now \" /></form></b></td>";
}
echo "</tr>";
}
javascript:
function buyItem(reqKarma)
{
var currKarma = <?php getKarma(); ?>;
alert(currKarma +"");
if (currKarma < reqKarma)
{
alert('You do not have enough Karma to buy this title.');
return false;
}
}
Full document:
<?php
include "php-scripts/DBConnection.php";
$con = getconnection();
mysql_select_db("brokendi_BD", $con);
loadpage();
function loadpage()
{
echo "<table cellpadding=\"0\" cellspacing=\"0\" style=\"width: 98%\" >";
echo "<tr class=\"info-row\" bgcolor=#252525 style=\"color:white; height: 15px;\">";
echo "<td style=\"width: 70%; height: 10px; padding-left: 5px;\"><b>Item Name</b></td>";
echo "<td style=\"width: 10%; height: 10px; padding-left: 5px;\"><b>Item Price</b></td>";
echo "<td style=\"width: 15%; height: 10px; padding-left: 5px;\"><b> </b></td>";
echo "</tr>";
addrow("test",1,1,false);
echo "</table>";
}
function addrow($itemText , $itemPrice , $itemID , $odd)
{
if ($odd)
{
echo "<tr class=\"content-row online\" id=\"958\" bgcolor=#A7A7A7>";
}
else
{
echo "<tr class=\"content-row online\" id=\"958\" bgcolor=#BFBFBF>";
}
echo "<td style=\"width: 70%;\">$itemText</td>";
echo "<td style=\"width: 10%; padding-left: 5px;\"><b>$itemPrice</b></td>";
if (hasRole($itemID))
{
echo "<td style=\"width: 15%; padding-left: 5px;\"><b>Unlocked</b></td>";
}
else
{
echo "<td style=\"width: 15%; padding-left: 5px;\"><b><form name=\"buy\" action=\"php-scripts/BuyItem.php\" onsubmit=\"return buyItem($itemPrice)\" >";
echo "<input type=\"hidden\" name=\"UserID\" value =\"".getUID()."\">";
echo "<input type=\"hidden\" name=\"itemID\" value = \"".$itemID."\" >";
echo "<input type=\"hidden\" name=\"reqKarma\" value = \"".$itemPrice."\" >";
echo "<input name=\"Send\" type=\"submit\" value=\" Buy Now \" /></form></b></td>";
}
echo "</tr>";
}
function getKarma()
{
$result = mysql_query("SELECT * FROM userpoints WHERE uid='getUID()'");
$row = mysql_fetch_array($result);
$currentkarma = (int)$row['points'];
return $currentkarma;
}
function getUID()
{
global $user;
if ($user->uid)
{
$userID=$user->uid;
return $userID;
}
else
{
header('Location: http://brokendiamond.org/?q=node/40');
}
}
function hasRole($roleID)
{
$usersid = getUID();
$returnValue = false;
$result = mysql_query("SELECT * FROM users_roles");
while ($row = mysql_fetch_array($result))
{
if ($row['uid'] == $usersid)
{
if ($row['rid'] == $roleID)
{
$returnValue = true;
break;
}
}
}
return $returnValue;
}
function enoughKarma($requiredKarma)
{
if ( getKarma() >= $requiredKarma)
{
return true;
}
else
{
return false;
}
}
?>
<script type="text/javascript">
function buyItem(reqKarma)
{
var currKarma = <?php getKarma(); ?>;
alert(currKarma +"");
if (currKarma < reqKarma)
{
alert('You do not have enough Karma to buy this title.');
return false;
}
}
</script>
You need to return true; from the buyItem function if you wan't the form to submit. You return false without enough karma, but nothing with enough, which means the function returns undefined and that prevents the form from submitting.
Try:
function buyItem(reqKarma)
{
var currKarma = <?php getKarma(); ?>;
alert(currKarma +"");
if (currKarma < reqKarma)
{
alert('You do not have enough Karma to buy this title.');
return false;
}
return true;
}
Depending on what the function actually does, you may need to change <?php getKarma(); ?> to <?php echo getKarma(); ?>.
It's really not a good idea to write full HTML in PHP, it's just messy.
Change:
echo "<td style=\"width: 15%; padding-left: 5px;\"><b><form name=\"buy\" action=\"php-scripts/BuyItem.php\" onsubmit=\"return buyItem($itemPrice)\" >";
to:
?>
<script type="text/javascript">
function buyItem<?php echo $itemID;?> {
buyItem(<?php echo json_encode($itemPrice);?>);
}
</script>
<td style="width: 15%; padding-left: 5px;"><b><form name="buy" action="php-scripts/BuyItem.php" onsubmit="return buyItem<?php echo $itemID;?>()">
<?php
I've done two things here:
Removed HTML from PHP code.
Added a json_encode to values being passed to JavaScript. This may seem trivial, but for more complex data json_encode is a real life saver.
The reason your script is broken is probable because of a syntax error occurring in your JavaScript.
What does <?php getKarma(); ?> do?
I am looking for a solution to use the alternating colors row design in my table which is from a csv.
my code:
<?php
echo "<table style='text-align: left; width: 99%;' border='1'>\n\n
<thead>
<tr>
<th>data</th>
</tr>
</thead>";
$f = fopen("local/datafiles.csv", "r");
while (($line = fgetcsv($f)) !== false) {
echo "<tr class='odds'>";
foreach ($line as $cell) {
echo "<td style='font-weight: bold;'>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
fclose($f);
echo "\n</table>";
?>
so how can i have the tr class to alternate from odds and even?
thanks
have a variable
int count =0;
increment count on every iteration and take modolous by
count % 2
if(count % 2 == 0)
color = red //(make color of row = red)
else
color = yellow //(make color of row = yello)
count=count+1;
thats just an idea, u can accomodate this in ur code
You could use the css pseudo-selector :nth-of-type(odd|even)
http://reference.sitepoint.com/css/pseudoclass-nthoftype
If the ID of the table is styledTable, your stylesheet would look like:
#styledTable {
text-align: left;
width: 99%;
border: 1px solid black;
}
#styledTable tr:nth-of-type(even) {
background-color: red;
}
#styledTable tr:nth-of-type(odd) {
background-color: blue;
}
<?php
echo "<table style='text-align: left; width: 99%;' border='1'>\n\n
<thead>
<tr>
<th>data</th>
</tr>
</thead>";
$f = fopen("local/datafiles.csv", "r");
$i = 0;
while (($line = fgetcsv($f)) !== false) {
echo "<tr style='background-color: ".(($i%2)?'green':'blue').";'>";
foreach ($line as $cell) {
echo "<td style='font-weight: bold;'>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
$i++;
}
fclose($f);
echo "\n</table>";
?>
Or you could assign a class just the same way and style it using css. This is much better practice.
I will give you a better but much simpler solution, through CSS. First of all uniquely identify your table, i will show you a more general option.
table tr { backgroun: #ddd; }
table tr:nth-child(2n) { background: #ccc; }