Can someone help me please?
I need a loop for a checkerboard pattern project.
Right now i have this:
<?php
$uitvoer="<table summary=''>\n";
$j=0;
$uitvoer .= "\t<tr>\n";
for($i=0;$i<8;$i++)
{
$uitvoer .= "\t\t<td class='kleur".(($i+$j)%2)."'> </td>\n";
}
echo <<<END
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Page title</title>
<style type="text/css">
<!--
td
{
width: 50px;
height: 50px;
border:4px groove red;
}
td.kleur0
{
background-color: white;
}
td.kleur1
{
background-color: black;
}
-->
</style>
</head>
<body>
$uitvoer
</body>
</html>
END;
?>
Then i get this:
So far so good.
But i need 8 rows with different lines and colors
The result need to be like this:
How can i do that the easiest and fastest way in a loop or a array??
You just need a second loop that adds table rows (<tr>). The beginning of your file should look like this:
$uitvoer="<table summary=''>\n";
for ($j=0; $j < 8; $j++) {
$uitvoer .= "\t<tr>\n";
for($i=0;$i<8;$i++) {
$uitvoer .= "\t\t<td class='kleur".(($i+$j)%2)."'> </td>\n";
}
$uitvoer .= "\t</tr>\n";
}
So, inside the loops, $i will hold your table cell (<td> tag) and $j will hold your table row (<tr> tag).
Related
So I have to roll 6 six sided die and display their corresponding images. I also have to save up to 10 previous roles and append the new ones each time I roll. I'm so far able to create the array of 6 random numbers and link them to their images. I just need to save that role on page refresh. I've just started learning PHP but from what I've gathered I know sessions are needed. All the examples I looked at didn't help.
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dice Roller</title>
</head>
<style>
h1, input {
margin-left: auto;
margin-right: auto;
}
img {
width: 70px;
height: 70px;
}
</style>
<body>
<h1>Dice Roller</h1>
<input type="button" value="Roll">
<?php
$newRoll = array(rand(1, 6), rand(1, 6), rand(1, 6), rand(1, 6), rand(1, 6), rand(1, 6));
$oldRoll = $newRoll;
foreach($newRoll as $num) {
echo '<img src="img/dicefaces/dice0' . $num . '.png" alt="">';
}
echo "<br>";
foreach($oldRoll as $num) {
echo '<img src="img/dicefaces/dice0' . $num . '.png" alt="">';
}
?>
</body>
</html>
You can use $_SESSION to 'store' the old rolls, following logic might help you on your way:
We store the history in $_SESSION['oldrolls'] allowing us to display up to 10 previous rolls (you can set the number you want to show with $max)
Also created a button to allow for session clear, to start with a clean slate.
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dice Roller</title>
</head>
<style>
table, td, th {
table-layout: fixed;
width: 100%;
border-collapse: collapse;
border: 3px solid black;
text-align: center;
}
h1, input {
margin-left: auto;
margin-right: auto;
}
img {
width: 70px;
height: 70px;
}
</style>
<body>
<h1>Dice Roller</h1>
<!-- allow user to clear the session -->
<form action='' method ='POST'>
<input type="submit" name='clear' value="clear session">
</form>
<!-- submit a roll -->
<br />
<form action='' method ='POST'>
<input type="submit" name='submit' value="Roll">
</form>
<?php
if(isset($_POST['clear'])){
$_SESSION = []; // clear session array
}
if(isset($_POST['submit'])) {
$newRoll = array(rand(1, 6), rand(1, 6), rand(1, 6), rand(1, 6), rand(1, 6), rand(1, 6));
$max = 10; // store up to 10 previous rolls
$count = isset($_SESSION['oldrolls']) ? count($_SESSION['oldrolls']) : 0;
if($count <= $max) {
$_SESSION['oldrolls'][] = $newRoll; // add new
} else {
array_shift($_SESSION['oldrolls']); // remove oldest
$_SESSION['oldrolls'][] = $newRoll; // add new
}
$_SESSION['oldrolls-reverse'] = array_reverse($_SESSION['oldrolls']); // show last first
echo 'Current roll...';
echo '<br />';
foreach ($newRoll as $num) {
echo $num;
echo '<br />';
}
echo "<br>";
echo 'previous rolls...';
echo '<br />';
echo '<table>';
foreach ($_SESSION['oldrolls-reverse'] as $key => $num) {
if($key === 0) continue;
echo '<tr>';
foreach($num as $roll) {
echo '<td>' . $roll . '</td>';
}
echo '</tr>';
}
echo '</table>';
}
?>
</body>
</html>
Hi im a begginer in php and html i would like to ask how do i put this table into my html sidelist. when i put my table inside that space this always appear ( "; foreach($testaroni as $x=>$y) { echo ""; echo "" . $y . ""; } echo ""; echo ""; ?>)
This is my table. This is the problem (2)
<?php
session_start();
$testaroni = explode("', '" , $_SESSION["data"]);
?>
<?php
echo "<table border = '1'>";
foreach($testaroni as $x=>$y) {
echo "<tr>";
echo "<td>" . $y . "</td>";
}
echo "</tr>";
echo "</table>";
?>
And i would like to put it inside here.
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.links line {
stroke: #999;
stroke-opacity: 0.6;
}
.nodes circle {
stroke: #fff;
stroke-width: 1.5px;
}
text {
font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
font-size: 10px;
}
</style>
<?php
session_start();
$testaroni = explode("', '" , $_SESSION["data"]);
?>
<html>
<head>
<title>Search</title>
</head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<body>
<!-- Sidebar -->
<div class="w3-sidebar w3-blue-grey w3-bar-block " style="width:15%">
<h4 class="w3-bar-item">Sidelist</h4>
<table border = "1">
<?php
foreach($testaroni as $x=>$y) {
echo '<tr>';
echo '<td>' . $y . '</td>';
}
echo '</tr>';
?>
</table>
</div>
</form>
</body>
</html>
You just need to modify your explode function and your code will work as you want
session_start();
//lets say your $_SESSION["data"] contains
$_SESSION["data"] = 'Value1,Value2,Value3';
//$testaroni = explode("', '" , $_SESSION["data"]);// just use single or double quotes without space
$testaroni = explode("," , $_SESSION["data"]);
You try var_dump( $_SESSION["data"]) and you should check empty $_SESSION["data"].I try put
$testaroni = array("1","2","3");//it work fine
You can read more about my this here
Seems like my eyes are failing me took me a while to see it.
You shouldn't use " when trying to use HTML tags in PHP
What you need to use is '.
<table border = "1">
<tr>
<?php
foreach($testaroni as $x=>$y) {
echo '<td>'.$y.'</td>';
}
?>
</tr>
</table>
The reason for this is that " will treat anything inside it as string while ' will treat it as is.
Refer here for a better explanation.
Also, follow the answer above for your session since it's also wrong.
Your session variable is a CSV string. simply exploding on the comma isn't going to get what you're looking for. You can verify this with var_dump($testaroni);.
Use str_getcsv.
<?php
// always start with php logic;
// it makes the script much easier to follow and
// if you need to redirect, you haven't put out any output yet.
session_start();
// check the docs for info on this function
$testaroni = str_getcsv($_SESSION['data']);
// then, when everything is calculated, print out your output
?>
<html>
<head>
<title>Search</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<style>
.links line {
stroke: #999;
stroke-opacity: 0.6;
}
.nodes circle {
stroke: #fff;
stroke-width: 1.5px;
}
text {
font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
font-size: 10px;
}
</style>
</head>
<body>
<!-- Sidebar -->
<div class="w3-sidebar w3-blue-grey w3-bar-block " style="width:15%">
<h4 class="w3-bar-item">Sidelist</h4>
<table border = "1">
<?php foreach($testaroni as $y): ?>
<tr>
<td><?= $y ?></td>
</tr>
<?php endforeach; ?>
</table>
</div>
</form>
</body>
</html>
I am attempting to create a table that consists of 2 forms where the first form gets info from the second. so i have table1 and table 2. I want to make it to be users choice . a user picks the number of rows and columns(I went up to 7 rows and columns in dropbox) and color from a drop down box and whatever they pick it generates. My problem is when I select something instead of showing me a table row and columns with the colors its showing me 1111 can anyone suggest something? Maybe my logic is wrong please help.
<?php
$color= substr(filter_input(INPUT_GET,'color',FILTER_SANITIZE_NUMBER_INT),0,10);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="keywords" content="Table">
<meta name="description" content="table Creator">
<link rel="stylesheet" type="text/css" href="style.css">
<style>
tr:nth-child(even) {
background-color:#000000;
color:#000000;
<?php
echo $color
?>;
}
table, th, td {
border: 1px solid #000000; ;
padding: 10px;
}
table {
border-collapse: collapse;
}
</style>
<title>table</title>
</head>
<body>
<header>
<h1> Projects</h1>
</header>
<nav>
</nav>
<section>
<h2> Table Creator</h2>
<?php
$rows= substr(filter_input(INPUT_GET,'rows',FILTER_SANITIZE_NUMBER_INT),0,3);
$cols= substr(filter_input(INPUT_GET,'cols',FILTER_SANITIZE_NUMBER_INT),0,3);
echo "<Table>";
for ($x=1; $x<=$rows; $x++)
{
echo "<tr>";
for ($y=1; $y<=$cols; $y++)
echo "<td>$rows $x $cols $y</td>";
}
{
echo "<tr>";
}
echo "</Table>";
?>
</section>
<footer>
</footer>
<p></p>
</body>
</html>
I have an array of float values in PHP and I want to turn the array into a bar chart using CSS. Each float value represents a percentage, so if a value is 50.0 then the bar should be 50% of a certain height. I am new to CSS, so I was wondering if anyone could show me how I would go about doing this.
I believe it should look something like this. I doubt that this code would work, but it's an example.
<?php
$values = array(50.0, 20.0, 30.0, 45.0); //This is the array of percentage values
?>
<style>
<?php
for ($i = 0; $i < count($values); $i++) //Create a new bar for each value
{?>
#bar
{
height: <?php 300 * ($values[$i] / 100.0); ?> px; //Specify the height of each bar
width: 30px;
}
<?php
}?>
</style>
Edit:
I've added the code suggested by Tom, but my web page currently produces no bars. Here is the full code for my PHP file. Why does it not produce any CSS content?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Visualisation result</title>
<style>
.bar
{
color: #00ff21;
width: 30px;
}
</style>
</head>
<body>
<?php
$values = array(50.0, 20.0, 30.0, 45.0);
for ($i = 0; $i < count($values); $i++)
{
$currentHeight = 300 * $values[$i] / 100.0; ?>
<div class="bar" style="height: <?php echo($currentHeight); ?> "></div>
<?php
}
?>
</body>
</html>
Your approach won't work, as you will be writing out several different rules for the same selector. It is invalid in HTML to have multiple values with the same ID "bar" anyway.
I guess you could do something like this:
.bar {
width: 30px;
}
<?php
for ($i = 0; $i < count($values); $i++) { ?>
<div class="bar" style="height:<?= 300 * $values[$i] / 100.0 ?>"></div>
<?php
} ?>
This generates the elements using the loop, setting their height using the style attribute. I have used the shorthand <?= instead of <?php echo (you weren't doing either in your question).
The purpose of this script:
Write a script that counts from 1 to 10 in steps of 1. For each number, display whether that
number is an odd or even number, and also display a message if the number is a prime number.
Display this information within an HTML table.
<!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>Exercise 1</title>
<link rel="stylesheet" type="text/css" href="common.css" />
<style type="text/css">
th { text-align: left; background-color: #999; }
th, td { padding: 0.4em; }
tr.alt td { background: #ddd; }
</style>
</head>
<body>
<h2>Exercise 1</h2>
<table cellspacing="0" border="0" style="width: 20em; border: 1px solid #666;">
<tr>
<th>Number</th>
<th>Parity</th>
<th>Primality</th>
</tr>
<?php
$n=10;
for ($i=1;$i<=$n;$i++){
echo ($i%2 != 0)? '<tr class = "alt">':'<tr>'; ?>
<td><?php echo $i; ?></td>
<td><?php echo ($i%2 != 0)? "Odd":"Even";?></td>
<td><?php
$k=0;
for ($j=1;$j<=$i;$j++){
if ($i%$j=0) $k++; //Where the error occurs
}
echo ($k=2 || $k=1)?"Prime":"Composite";?>
</td></tr>
<?php
}
?>
</table>
</body>
</html>
$i%$j=0
Assigns $j as 0 and then tries to do $i % 0;
You want
($i % $j) == 0
There's another mistake in the same way with the echo.
Consider also formatting your code with spaces to make it more readable.
if ($i%$j=0) should probably be if ($i%$j==0)