What i currently have
what im trying to make
I need it when i throw for example 2 and 3 it goes down 2 and marks it from left to right using black borders, and then it goes 3 to the right and mark from top to bottem using black borders, at the point they cross i need it to be a red border.
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="form.css">
</head>
<body>
<div class="wrapper">
<?php
if(isset($_POST['submit'])) {
$dice1 = mt_rand(1,6);
$dice2 = mt_rand(1,6);
echo "<table class='table'>";
for ($i=0; $i < 6 ; $i++) {
echo "<tr>";
for ($j=0; $j < 6 ; $j++) {
echo "<td> <img class='icon cell color".mt_rand(1,4)."' src='img/icon".mt_rand(1,4).".svg ' </td>";
}
echo "</tr>";
}
echo "</table>";
echo "<img class='dice1' src='img/dice".$dice1.".gif'>";
echo "<img class='dice2' src='img/dice".$dice2.".gif'>";
}
?>
<form method="get" action="/php1/eindopdracht/cal.php">
<button type="submit" class="button1">renew</button>
</form>
<form method="POST">
<button type='submit' class='button2' name='submit' value='submit'>throw</button>
</form>
</div>
</body>
</html>```
You can check if dice1 value is equal to $i you can add a certain class that makes the color black. Same if dice2 value is equal to $i. If then dice1 is equal to $i and dice2 value is equal to $j choose a different class to make the border red.
function chooseBorder($i,$j,$dice1,$dice2) {
$class = "";
if ($dice1 == $i && $dice2 == $j) {
$class = "border-red";
return $class;
}
if ($dice1 == $i || $dice2 == $j) {
$class = "border-black";
return $class;
}
return $class;
}
for ($i=0; $i < 6 ; $i++) {
echo "<tr>";
for ($j=0; $j < 6 ; $j++) {
$class = chooseBorder($i,$j,$dice1,$dice2);
echo "<td> <img class='icon cell color".mt_rand(1,4). " " . $class . "' src='img/icon".mt_rand(1,4).".svg ' </td>";
}
echo "</tr>";
}
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="form.css">
</head>
<body>
<div class="wrapper">
<?php
if(isset($_POST['submit'])) {
$dice1 = mt_rand(1,6);
$dice2 = mt_rand(1,6);
function chooseBorder($i,$j,$dice1,$dice2) {
$class = "";
if ($dice1 == $i && $dice2 == $j) {
$class = "border-red";
return $class;
}
if ($dice1 == $i || $dice2 == $j) {
$class = "border-black";
return $class;
}
return $class;
}
for ($i=1; $i < 7; $i++) {
echo "<tr>";
for ($j=1; $j < 7 ; $j++) {
$class = chooseBorder($i,$j,$dice1,$dice2);
echo "<td> <img class='icon cell color".mt_rand(1,4). " " . $class . "' src='img/icon".mt_rand(1,4).".svg ' </td>";
}
echo "</tr>";
}
echo "</table>";
echo "<img class='dice1' src='img/dice".$dice1.".gif'>";
echo "<img class='dice2' src='img/dice".$dice2.".gif'>";
}
?>
<form method="get" action="/php1/eindopdracht/cal.php">
<button type="submit" class="button1">renew</button>
</form>
<form method="POST">
<button type='submit' class='button2' name='submit' value='submit'>throw</button>
</form>
</div>
</body>
</html>
Related
I wanted to create a multiplication table with a custom function and also get the number of rows and columns from the user, and I want if each row was an even number then its field would be red (background) and if it was odd number it would have a green background and i also write some codes but i'm not sure if it's true or not:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<form method="post">
<input type="number" name="rows" placeholder="Rows">
<input type="number" name="columns" placeholder="Columns">
<button type="submit" name="button">
Create
</button>
</form>
<table border="1px">
<?php
if (isset($_POST["button"])) {
$userRows = $_POST["rows"];
$userColumns = $_POST["columns"];
function multiplicationTable($rows, $columns)
{
$zarb = $rows * $columns;
return $zarb;
}
$x = 1;
while ($x <= $userRows) {
echo "<tr>";
$y = 1;
while ($y <= $userColumns) {
if ($x % 2 == 0) {
echo "<td style='background-color: red;'>" . multiplicationTable($x, $y) . "</td>";
} else {
echo "<td style='background-color: green;'>" . multiplicationTable($x, $y) . "</td>";
}
$y++;
}
$x++;
echo "</tr>";
}
}
?>
</table>
</body>
</html>
How about changing the while loops to this:
while ($x <= $userRows) {
echo "<tr>";
$y = 1;
while ($y <= $userColumns) {
$val = multiplicationTable($x, $y);
if ($val % 2 == 0) {
echo "<td style='background-color: red;'>" . $val . "</td>";
} else {
echo "<td style='background-color: green;'>" . $val . "</td>";
}
$y++;
}
$x++;
echo "</tr>";
}
Well, your code looks good. Click here to see how it looks like...
the final result is in this link, and even if some of the numbers are even, they're still displayed in green and i don't know what should i do
https://imgur.com/D1Sweee
The table below contains buttons with values. When a button is clicked I want to proceed "predictSeason.php". Say I want to print/echo the team I selected there (i.e the value of clicked button) - how should I do this?
<table class ="teamTable">
<form action="predictSeason.php" method="post">
<?php
$number_per_row = 5;
$i = 0;
foreach ($soccerseason->getTeams() as $team) {
if (($i % $number_per_row) == 0) {
echo '<tr>';
}
?>
<td class="crestTd"style="background-image:url(<?php echo $team->crestUrl ?>);">
<button id="button<?php echo $i ?>" name="button<?php echo $i ?>" class="tableButton" value="<?php echo $team->name ?>"></button>
</td>
<?php
if (($i % $number_per_row) == $number_per_row - 1) {
echo '</tr>';
}
$i = $i + 1;
}
?>
</form>
</table>
You should write
<table class ="teamTable">
<form action="predictSeason.php" method="post">
<?php
$number_per_row = 5;
$i = 0;
foreach ($soccerseason->getTeams() as $team) {
if (($i % $number_per_row) == 0) {
echo '<tr>';
}
?>
<td class="crestTd"style="background-image:url(<?php echo $team->crestUrl ?>);">
<button id="button<?php echo $i ?>" name="button<?php echo $i ?>" class="tableButton" value="<?php echo $team->name ?>" type="submit" ></button>
</td>
<?php
if (($i % $number_per_row) == $number_per_row - 1) {
echo '</tr>';
}
$i = $i + 1;
}
?>
</form>
</table>
I am creating a table of checkboxes, where the first column will contain checkboxes that can check / uncheck the current row.
This is my code so far, which is not working.
<!DOCTYPE html>
<html>
<head>
<script language="JavaScript">
function toggle(source, $id) {
checkboxes = document.getElementsByName($id);
for (var i = 0, n = checkboxes.length; i < n; i++) {
checkboxes[i].checked = source.checked;
}
}
</script>
</head>
<body>
<?php
echo "<table>";
for ($x = 0; $x < 3; $x++) {
$id = $x;
echo "
<tr>";
echo "
<td>";
echo '<input type="checkbox" onClick="toggle(this, $id)"/> Toggle All';
echo '
</td>
';
for ($y = 0; $y < 7; $y++){
echo "
<td>";
echo '<input type="checkbox" name=$id value="bar1"> Bar 1<br/>';
echo '
</td>
';
}
echo "
</tr>
";
}
echo "</table>";
?>
</body>
</html>
I am aware that the code might contain several errors and am trying to find them. In the meantime, i would appreciate any suggestions.
try this code
<!DOCTYPE html>
<html>
<head>
// your java script code for toggle is working only change is used id not $id.
<script language="JavaScript">
function toggle(source, id) {
checkboxes = document.getElementsByName(id);
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = source.checked;
}
}
</script>
</head>
<body>
<?php
echo "<table>";
for ($x = 0; $x < 3; $x++) {
$id = $x;
echo "<tr>";
echo "<td>";
echo '<input type="checkbox" onClick="toggle(this, '.$id.')" /> Toggle All';
// change here onClick="toggle(this, $id)" to onClick="toggle(this, '.$id.')"
echo '</td>';
for ($y = 0; $y < 7; $y++){
echo "<td>";
echo '<input type="checkbox" name="'.$id.'" value="bar1"> Bar 1<br/>';
// also change the name=$id to name="'.$id.'"
echo '</td>';
}
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
I am not sure about your javascript, but it for sure needs the change I made to it.
<!DOCTYPE html>
<html>
<head>
<script language="JavaScript">
// change to-> Id from-> $id
function toggle(source, Id) {
checkboxes = document.getElementsByName(Id);
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = source.checked;
}
}
</script>
</head>
<body>
<table>
<?php
for ($x = 0; $x < 3; $x++) {
$id = $x; ?>
<tr>
<td>
<!--$id won't work in single quotes like that so you have to break it out.
See comment below, but you may need to base the toggle on the class-->
<input type="checkbox" onClick="toggle(this, '<?php echo $id; ?>')" /> Toggle All
</td><?php
for ($y = 0; $y < 7; $y++){ ?>
<td>
<!-- You will likely need to array this name, because if the
names are all the same, it will only take one value -->
<input type="checkbox" name="<?php echo $id; ?>[]" class="sub-<?php echo $id; ?>" value="bar1"> Bar 1<br/>
</td><?php } ?>
</tr><?php
} ?>
</table>
I made a simple table math calculator.
Only now I want when you echo the number the ODD numbers are red and the even numbers display green.
Is this possible with css ? or do I have to rebuild the whole code ?
Code HTML + PHP
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<center>
<form action = "pagina2.php" method = "post">
<input name = "invoer" type = "text" value = "">
<input name = "knop" type = "submit" value = "Verstuur">
</form>
</center>
</body>
</html>
//PHP
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<center>
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST")
{
if (isset($_POST['invoer']))
{
if (is_numeric($_POST['invoer']))
{
for ($i = 1; $i <= 10; $i++)
{
echo $i . " x " . $_POST['invoer'] . " = " . ($i * $_POST['invoer']) . "<br />";
}
}
else
{
echo "Vul een getal in!";
}
}
else
{
echo "Niks ingevoerd!";
}
}
?>
</center>
<br>
<center>
<input action="action" type="button" value="Opnieuw" onclick="history.go(-1);" />
</center>
</body>
</html>
Greetings.
Change this echo statment
echo $i . " x " . $_POST['invoer'] . " = " . ($i * $_POST['invoer']) . "<br />";
To
$x=$i . " x " . $_POST['invoer'] . " = " . ($i * $_POST['invoer']) . "<br />";
if($x % 2 ==0 )
{
echo '<font color="green">'.$x.'</font>';
}
else echo '<font color="red">'.$x.'</font>';
I would use modulas to shorten your code
$number % 2 == 0 #means even
Then use a css class like .odd or .even and inject that into your template
I assume you are referring to the variable $i:
echo '<div style="color: '.($i%2==0 ? 'green' : 'red').">". $i . " x " . $_POST['invoer'] . " = " . ($i * $_POST['invoer']) . "</div><br />";
I am trying to print the contents of items by their category in a web page in this manner:
Category1:
Item1 item2 item3
item4 item5...
Category2
item1 item2 ...
This my PHP code:
$cat="";
$maxcols = 3;
$i = 0;
while ($row = $result->fetch_assoc()) {
if ($i == $maxcols) {
$i = 0;
echo "</tr><tr>";
}
if($row['name']!=$cat)
{
echo "<table>
<tr><td collspan='3'>".$row['name']."</td></tr>
<tr>";
}
echo "<td>".$row['title']."</td>";
$cat=$row['name'];
$i++;
}
while ($i <= $maxcols) {
echo "<td> </td>";
$i++;
echo "</table>";
}
What I am getting is:
<table>
<tr><td collspan='3'>Archivers</td></tr>
<tr><td>7-Zip</td><td>IZArc</td><td>dfssdfsdf sdfsdf</td></tr><tr><td>fgdgdfgd</td><td>sdfsdfsdfsdf dsfsdfsd</td><table>
<tr><td collspan='3'>Benchmark</td></tr>
<tr><td>Fresh Diagnose</td><td> </td></table>
What I want to get is:
<table>
<tr><td collspan='3'>Archivers</td></tr>
<tr><td>7-Zip</td><td>IZArc</td><td>dfssdfsdf sdfsdf</td></tr>
<tr><td>fgdgdfgd</td><td>sdfsdfsdfsdf dsfsdfsd</td>**<td> </td></tr>
</table>**
<table>
<tr><td collspan='3'>Benchmark</td></tr>
<tr><td>Fresh Diagnose</td><td> </td><td> </td>
</table>
First of all, you have very 'messy' code. I suppose you need some more code to add the part between the stars, but I think you should use a cleaner approach.
I suppose you are using mysql, which supports the GROUP_CONCAT()-function. It would be wise to use that in this case.
Make a query:
SELECT name, GROUP_CONCAT( title SEPERATOR '|') as titles FROM your_database GROUP BY name
The results per category are now in one row.
//Amount of rows
$maxcols = 3;
while( $row = $result->fetch_assoc() ) {
//The seperator needs to be something that isn't in the values it seperates
//This creates an array with the titles for this category
$titles = explode( '|', $row['titles'] );
echo '
<table>
<tr>
<td colspan="' .$maxcols. '">' .$row['name']. '</td>
</tr>';
//$offset + $i is the position in the array of titles
$offset = 0;
//This loop ensures each row is properly opened and closed
while( $offset < count( $titles ) ) {
echo '<tr>';
//This will ensure each row has $maxcols td's in it
for( $i = 0; $i < $maxcols; $i++ ) {
if( isset( $titles[ $offset + $i ] ) ) {
echo '<td>' .$titles[ $offset + $i ]. '</td>';
} else {
echo '<td> </td>';
}
}
$offset += $maxcols;
echo '</tr>';
}
echo '</table>';
}
Thank you very much, Sumurai8! Your idea works great. Here is my php code:
<?php
include_once('include/db.inc.php');
$sql="SELECT m.name, GROUP_CONCAT(s.title order by s.title SEPARATOR '|' ) as titles FROM menu m, software s where m.id=s.category
GROUP BY m.name
order by m.name";
$result = $mysqli->query($sql);
$cat="";
$maxcols = 3;
//$i = 0;
echo "
<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>
<html>
<head>
<title>content</title>
<meta content='text/html; charset=utf-8' http-equiv='content-type'>
<meta content='Software Details' name='description'>
<link href='styles/fontstyle.css' rel='stylesheet' type='text/css'>
<link href='styles/style.css' rel='stylesheet' type='text/css'>
</head>
<body class='body'>";
while( $row = $result->fetch_assoc() ) {
//The seperator needs to be something that isn't in the values it seperates
//This creates an array with the titles for this category
$titles = explode( '|', $row['titles'] );
echo "
<table>
<tr><td colspan='" .$maxcols. "'>" .$row['name']. "</td></tr> \n";
//$offset + $i is the position in the array of titles
$offset = 0;
//This loop ensures each row is properly opened and closed
while( $offset < count( $titles ) ) {
echo "<tr>";
//This will ensure each row has $maxcols td's in it
for( $i = 0; $i < $maxcols; $i++ ) {
if( isset( $titles[ $offset + $i ] ) ) {
echo "<td>" .$titles[ $offset + $i ]. "</td>";
} else {
echo "<td> </td>";
}
}
$offset += $maxcols;
echo "</tr> \n";
}
echo "</table><br> \n";
}
This is the output, just the way I want it:
<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>
<html>
<head>
<title>content</title>
<meta content='text/html; charset=utf-8' http-equiv='content-type'>
<meta content='Software Details' name='description'>
<link href='styles/fontstyle.css' rel='stylesheet' type='text/css'>
<link href='styles/style.css' rel='stylesheet' type='text/css'>
</head>
<body class='body'>
<table>
<tr><td colspan='3'>Archivers</td></tr>
<tr><td>7-Zip</td><td>IZArc</td><td>dfssdfsdf sdfsdf</td></tr>
<tr><td>fgdgdfgd</td><td>sdfsdfsdfsdf dsfsdf</td><td> </td></tr>
</table><br>
<table>
<tr><td colspan='3'>Benchmark</td></tr>
<tr><td>Fresh Diagnose</td><td> </td><td> </td></tr>
</table><br>
</body>
</html>