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)
Related
Iam new to this stuff. But I want to send checked checkbox value(s) to different page. Iam illustrating what i desire with code below;
php_checkbox.php file
<!DOCTYPE html>
<html>
<head>
<title>Get Values of Multiple Checked Checkboxes</title>
<link rel="stylesheet" href="css/php_checkbox.css" />
</head>
<body>
<div class="container">
<div class="main">
<center>
<h2>PHP: Get Values of Multiple Checked Checkboxes</h2>
<form action="checkbox_value.php" method="post">
<label class="heading">Select Your Technical Exposure:</label><p>
<input type="checkbox" name="check_list[]"
value="C/C++"><label>C/C++ </label> <p>
<input type="checkbox" name="check_list[]" value="Java">
<label>Java</label> <p>
<input type="checkbox" name="check_list[]" value="PHP"><label>PHP</label><p>
<input type="checkbox" name="check_list[]"
value="HTML/CSS"><label>HTML/CSS</label><p>
<input type="checkbox" name="check_list[]"
value="UNIX/LINUX"><label>UNIX/LINUX</label><p>
<input type="submit" name="submit" Value="Submit"/>
<p>
</form>
</div>
</div>
</body>
</html>
checkbox_value.php file
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['check_list'])) {
// Counting number of checked checkboxes.
$checked_count = count($_POST['check_list']);
foreach($_POST['check_list'] as $selected) {
echo "<p>".$selected ."</p>";}
for ($x = 1; $x <= $checked_count; $x++) {
?>
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
</style>
</head>
<body>
<h2>Cell that spans two columns</h2>
<p>To make a cell span more than one column, use the colspan attribute.</p>
<table style="width:50%">
<tr>
<td>Technology</td>
<td><?php echo $selected; ?></td>
</tr>
</table>
<?php
}
}
else{
}
}
?>
</body>
</html>
The above code work. The problem is when I select Java and PHP, I get PHP displayed in both tables. When I select 3 options, the last option get displayed in all tables. What I need is when I select e.g. PHP, JAVA, and UNIX/LINUX, the 3 options (PHP, JAVA, UNIX/LINUX) be displayed on the tables separately - PHP on the first table, Java on the second table and UNIX/LINUX on the third table.
When I select only 2 (e.g. Java and PHP), I want Java on the first table and PHP on the second.
Please help.
You have to write the row logic inside the loop.
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
</style>
</head>
<body>
<h2>Cell that spans two columns</h2>
<p>To make a cell span more than one column, use the colspan attribute.</p>
<table style="width:50%">
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['check_list'])) {
// Counting number of checked checkboxes.
$checked_count = count($_POST['check_list']);
foreach($_POST['check_list'] as $selected) {
echo "<p>".$selected ."</p>";
?>
<tr>
<td>Technology</td>
<td><?php echo $selected; ?></td>
</tr>
<?php
}
}
else{
}
}
?>
</table>
</body>
</html>
Checkout the code below. You don't need to count the number of elements sepeartely as you are using foreach() loop already.
Also, just loop the table, not the complete HTML.
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['check_list'])) {
//Counting number of checked checkboxes.
//$checked_count = count($_POST['check_list']);
?>
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
</style>
</head>
<body>
<h2>Cell that spans two columns</h2>
<p>To make a cell span more than one column, use the colspan attribute.</p>
<?php
foreach($_POST['check_list'] as $selected)
{
?>
<table style="width:50%">
<tr>
<td>Technology</td>
<td><?php echo $selected; ?></td>
</tr>
</table>
<?php
}
}
}
?>
</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'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 } ?>
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>
Okay, so I am exporting the same information in two different ways...'html' and 'xlsx'. In order to get up and down arrows in my xlsx I did the following:
$upArrow = html_entity_decode('↑',ENT_QUOTES,'UTF-8');
$downArrow = html_entity_decode('↓',ENT_QUOTES,'UTF-8');
This works great. However, this does not work for the HTML export so i did the following:
$upArrow = ($_REQUEST['t'] == 'html') ? '↑' : html_entity_decode('↑',ENT_QUOTES,'UTF-8');
$downArrow = ($_REQUEST['t'] == 'html') ? '↓' : html_entity_decode('↓',ENT_QUOTES,'UTF-8');
However, now I am getting the literal string ↓ instead of ↓.
The HTML Writer will treat strings for worksheet cells as strings, not as html entities, because that's what it's expecting from PHPExcel.... use the UTF-8 up and down arrow characters, and that should work for all writers
EDIT
include 'PHPExcel.php';
$phpe = new PHPExcel();
$sheet = $phpe->getActiveSheet();
// Write a UTF-8 up-arrow character to cell A1
$sheet->setCellValue(
'A1',
html_entity_decode('↑',ENT_QUOTES,'UTF-8') . ' up arrow'
);
// Write a UTF-8 down-arrow character to cell A2
$sheet->setCellValue(
'A2',
html_entity_decode('↓',ENT_QUOTES,'UTF-8') . ' down arrow'
);
// Save as an OpenOfficeXML .xlsx file
$writer = new PHPExcel_Writer_Excel2007($phpe);
$writer->save('./test.xlsx');
// Save as a BIFF-8 .xls file
$writer = new PHPExcel_Writer_HTML($phpe);
$writer->save('./test.html');
// Save as an HTML file
$writer = new PHPExcel_Writer_Excel5($phpe);
$writer->save('./test.xls');
EDIT 2
The Markup generated is:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<!-- Generated by PHPExcel - http://www.phpexcel.net -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Spreadsheet</title>
<meta name="author" content="Unknown Creator" />
<meta name="title" content="Untitled Spreadsheet" />
<meta name="company" content="Microsoft Corporation" />
<style type="text/css">
html { font-family:Calibri, Arial, Helvetica, sans-serif; font-size:11pt; background-color:white }
table { border-collapse:collapse; page-break-after:always }
.gridlines td { border:1px dotted black }
.b { text-align:center }
.e { text-align:center }
.f { text-align:right }
.inlineStr { text-align:left }
.n { text-align:right }
.s { text-align:left }
td.style0 { vertical-align:bottom; border-bottom:none #000000; border-top:none #000000; border-left:none #000000; border-right:none #000000; color:#000000; font-family:'Calibri'; font-size:11pt; background-color:white }
table.sheet0 col.col0 { width:42pt }
table.sheet0 tr { height:15pt }
table.sheet0 tr.row0 { height:15pt }
table.sheet0 tr.row1 { height:15pt }
</style>
</head>
<body>
<style>
#page { left-margin: 0.7in; right-margin: 0.7in; top-margin: 0.75in; bottom-margin: 0.75in; }
body { left-margin: 0.7in; right-margin: 0.7in; top-margin: 0.75in; bottom-margin: 0.75in; }
</style>
<table border="0" cellpadding="0" cellspacing="0" id="sheet0" class="sheet0 gridlines">
<col class="col0">
<tbody>
<tr class="row0">
<td class="column0 style0 s">↑ up arrow</td>
</tr>
<tr class="row1">
<td class="column0 style0 s">↓ down arrow</td>
</tr>
</tbody>
</table>
</body>
</html>