So i have the code to check for Prime numbers with value that is inputted. My question is, how can i turn my code so that the table can be dynamic in sets of 10 and the max is 100? So that the input can only take increments of 10 and the max value you can enter 100?
My current code right now is set to 10 columns and 4 rows so is there a way to make this change (dynamic i guess?) to change with the input increments of 10?
Here is my code:
<form method="post" action="">
<table border="2" width="1180px">
<thead>
<center>
Number Chart</center>
</thead>
<?php
error_reporting(0);
function isPrime($n)
{
if ($n == 1) return false;
if ($n == 2) return true;
if ($n % 2 == 0)
{
return false;
}
$i = 2;
for ($i = 2; $i < $n; $i++)
{
if ($n % $i == 0)
{
return false;
}
}
return true;
}
if (isset($_POST['value']) && $_POST['value'] != 0)
{
/* #var $start type */
$start = $_POST['value'];
}
else
{
$start = 1;
}
$n_cols = 10;
$n_rows = 5;
for ($i = 0; $i < $n_rows; $i++) //
{
$col = '';
for ($j = 0; $j < $n_cols; $j++)
{
$number = ($start - $i - ($j * $n_cols));
if (isPrime($number) == true)
{
//if prime color it red
$col.= '<th style="color:red">' . ($start - $j - ($i * $n_cols)) .
'</th>';
}
else
{
$col.= '<th>' . ($start - $j - ($i * $n_cols)) . '</th>';
}
}
$out.= '<tr>' . $col . $row . '</tr>';
}
echo $out;
?>
<tr>
<thead colspan=10>
<center>
<label for="input">Enter Limit:</label>
<input type="text" name="value" style="width: 60px">
<input type="submit" name="submit" value="Submit">
</center>
</thead>
</tr>
</table>
</form>
if (($_POST['value'] % 10) != 0 || $_POST['value'] >100 ){
echo 'you must enter a valid value';
}else{
//all the code you want to run only if the number posted is valid
}
the % is Modulo, see http://php.net/manual/en/language.operators.arithmetic.php for more details
Related
I don't want to create a big if list for all the story chapters of my website.
if $commentformname[$a] is equal to the number of the current chapter, then I make an array $commentonchapter1[$a] saying that a comment was inserted in the chapter 1, and chapter 2, and so on.
So if a comment was posted in the chapter 2, a array called commentonchapter2 will be created.
if ($sitecomments == true) {
$c = 0;
$i = 0;
$z = 0;
#defines the comment form name, name of who commented, the comment text and time (to be converted, example: 2020-04-09T15:53:07.580Za > 12:53 09/04/2020)
while ($c <= $commentsnumb) {
$commentformname[$z] = $comments[$i];
$i++;
$commenttername[$z] = $comments[$i];
$i++;
$commenttext[$z] = $comments[$i];
$i++;
$commenttime[$z] = $comments[$i];
$i++;
$c++;
$z++;
}
#shows the text of the comments tab
echo '<hr class="'.$sitehr2.'" />'."\n";
echo $divzoomanim.'<'.$n.'><p></p><br /><b>'.$tabnames[7].': '.$orangespan.$commentsnormalnumb.$spanc.' '.$icons[12].'</b><br /><br /><p></p></'.$n.'>'.$divc.'<hr class="'.$sitehr.'" />'."\n";
$i = 0;
$z = 0;
#converts the comment date and time
#example
#from 2020-04-09T15:53:07.580Za
#to 12:53 09/04/2020
while ($z <= $commentsnumb) {
$commenttime[$z] = substr($commenttime[$z], 0, -1);
$commenttime[$z] = date("H:i d/m/Y", strtotime($commenttime[$z]));
$z++;
}
$a = 0;
$z = 0;
$i = 0;
#styles the form name
#example
#from "pequenata-comment-7"
#to '<b>Commented on form "Pequenata-comment-7"</b>'
while ($a <= $commentsnumb) {
if ($lang == $langs[0] or $lang == $langs[1]) {
$commentformname[$i] = '<b>'.$commenttxt3.' '.$commenttxt5.' '.strtolower($formtxt).':</b> "'.ucwords($commentformname[$i]).'"';
}
if ($lang == $langs[2]) {
$commentformname[$i] = '<b>'.$commenttxt3.' '.$commenttxt4.' '.strtolower($formtxt).':</b> "'.ucwords($commentformname[$i]).'"';
}
$a++;
$z++;
$i++;
}
$a = 0;
$a2 = 1;
$i = 0;
$z = 0;
$c = 0;
$v = 0;
$b = 0;
$commentschapternumb = 0;
#Generates the array of the website comments
while ($c <= $commentsnumb) {
#Generates the comments of the website if the strlen of $commentformname[$a] is equal to 45, String: <b>Commented on form "Pequenata-comment-7"</b>
if (strlen($commentformname[$a]) == 45) {
$cmntsgeral[$b] = '<'.$m.' class="'.$textstyle2.'" style="text-align:left;border-width:3px;border-color:'.$bordercolor.';border-style:solid;"><div style="margin-left:5%;margin-right:5%;">'.'<br /><b>'.$a2.' - '.$commenttername[$i].'</b> - '.$commenttime[$i].'<br />'.$commentformname[$i].' '.'<hr class="'.$sitehr3.'" />'.$commenttext[$i].'<br /><br /><br /><br /><br />'.$divc.'</'.$m.'>'."<br />"."\n";
#Dispalyer disabled because of showing the wrong array (using $z, so it was tring to print a number on the array that didn't existed ($cmntschapter was incrementing on $z)
#echo $cmntsgeral[$z];
$a2++;
$b++;
}
#Generates the array of the chapter comments
if (strlen($commentformname[$a]) == 47) {
$cmntschapter[$v] = '<'.$m.' class="'.$textstyle2.'" style="text-align:left;border-width:3px;border-color:'.$bordercolor.';border-style:solid;"><div style="margin-left:5%;margin-right:5%;">'.'<br /><b>'.$a2.' - '.$commenttername[$i].'</b> - '.$commenttime[$i].'<br />'.$commentformname[$i].' '.'<hr class="'.$sitehr3.'" />'.$commenttext[$i].'<br /><br /><br /><br /><br />'.$divc.'</'.$m.'>'."<br />"."\n";
#Doesn't show the chapter comments because they shouldn't be showed in the website comments tab
#echo $cmntschapter[$z];
$v++;
$commentschapternumb++;
}
$a++;
$i++;
$z++;
$c++;
}
#Shows the website comments on the comments tab of the site
$z = 0;
while ($z <= $commentsnormalnumbtowrite) {
echo $cmntsgeral[$z];
$z++;
}
}
I am attempting to create a generater where the user inputs how many items to generate. For each time, a number from 1 to 100 is generated. Depending on the result, an item is randomly selected from one of three arrays. It should loop as needed and then display the results. I have partial code below as I tried to follow the logic.
<form action='random.php' method='POST' id='random'>
<table class='table table-responsive'>
<tr>
<td>Number of items: </td>
<td><input type='text' name='randitem'></td>
<td><button type="submit"Generate</button>
</td>
</tr>
</table>
<?php
$roll = rand(1, 100);
if $roll < 50
$commonItem = array(citem1, citem2, citem3);
if $roll => 50 and < 95
$uncommonItem = array(uitem1, uitem2, uitem3);
if $roll => 95
$rareItem = array(ritem1, ritem2, ritem3);
?>
Here's an idea, where you can randomly choose an item from each array based on which array to choose from (also random, per your sample code):
<?php
$commonItems = array('citem1', 'citem2', 'citem3');
$uncommonItems = array('uitem1', 'uitem2', 'uitem3');
$rareItems = array('ritem1', 'ritem2', 'ritem3');
$roll = rand(1,100);
if ($roll < 50) {
$size = count($commonItems) - 1;
$index = rand(0,$size);
$item = $commonItems[$index];
} elseif ($roll >= 50 && $roll < 95) {
$size = count($uncommonItems) - 1;
$index = rand(0,$size);
$item = $uncommonItems[$index];
} else {
$size = count($rareItems) - 1;
$index = rand(0,$size);
$item = $rareItems[$index];
}
echo $item;
?>
Here's a refactored version with a function that handles the repetitive functionality in the if statements:
<?php
$commonItems = array('citem1', 'citem2', 'citem3');
$uncommonItems = array('uitem1', 'uitem2', 'uitem3');
$rareItems = array('ritem1', 'ritem2', 'ritem3');
function getItem($itemArray) {
$size = count($itemArray) - 1;
$index = rand(0,$size);
return $itemArray[$index];
}
$roll = rand(1,100);
if ($roll < 50) {
$item = getItem[$commonItems];
} elseif ($roll >= 50 && $roll < 95) {
$item = getItem[$uncommonItems];
} else {
$item = getItem[$rareItems];
}
echo $item;
?>
EDIT: I missed the part about how you input a number of items, so here's that
// Check to see if form was submitted with something in randitem
if (isset($_POST['randitem'])) {
$commonItems = array('citem1', 'citem2', 'citem3');
$uncommonItems = array('uitem1', 'uitem2', 'uitem3');
$rareItems = array('ritem1', 'ritem2', 'ritem3');
function getItem($itemArray) {
$size = count($itemArray) - 1;
$index = rand(0,$size);
return $itemArray[$index];
}
// loop number of times set in form
foreach ($i = 0; $i < $_POST['randitem']; $i++) {
$roll = rand(1,100);
if ($roll < 50) {
$item = getItem[$commonItems];
} elseif ($roll >= 50 && $roll < 95) {
$item = getItem[$uncommonItems];
} else {
$item = getItem[$rareItems];
}
echo $item . "<br>"; // will print each item on its own line
}
}
?>
Note: this will do nothing if the form is submitted without a number, so consider changing the input type to "number" or doing some other validation to the form to ensure that only a number is sent to the PHP
i need help, i have code like this
<doctype html>
<html>
<table width="50%" cellpadding="5" cellspacing="0">
<?php
for($i = 1; $i <= 200; $i++) {
if (($i % 2) == 1) {
$tabel = "#8FBC8F";
}
else
$tabel = "#90EE90"; {
echo '<tr bgcolor="' . $tabel . '"><td> <font color="blue">' . $i . '</font></td></tr>';
}
}
?>
</html>
but i want to be like this:
table with 2 colors table rows, and 3 colors font, with loop iteration.
what should i do with code?
maybe I should make this font color into 3 conditions (blue, red, yellow). but I do not know how the code
Doing colours for lines and font:-
<doctype html>
<html>
<table width="50%" cellpadding="5" cellspacing="0">
<?php
for($i = 1; $i <= 200; $i++)
{
echo '<tr bgcolor="' . (($i % 2) ? "#8FBC8F" : "#90EE90") . '"><td> <font color="'.((($i % 3) == 0) ? 'blue' : ((($i % 3) == 1) ? 'yellow' : 'red' ) ).'">' . $i . '</font></td></tr>';
}
?>
</html>
Although you might be better trying to do this in CSS.
EDIT
Following your comment.
Below is using nested if statements. Quite verbose. Basically one outer if to check is $i is divisible by 2. Then within that checking whether it is divisible by 3, or what the remainder is
<doctype html>
<html>
<table width="50%" cellpadding="5" cellspacing="0">
<?php
for($i = 1; $i <= 200; $i++)
{
if (($i % 2) == 0)
{
if (($i % 3) == 0)
{
echo '<tr bgcolor="#90EE90"><td> <font color="blue">' . $i . '</font></td></tr>';
}
else
{
if (($i % 3) == 1)
{
echo '<tr bgcolor="#90EE90"><td> <font color="yellow">' . $i . '</font></td></tr>';
}
else
{
echo '<tr bgcolor="#90EE90"><td> <font color="red">' . $i . '</font></td></tr>';
}
}
}
else
{
if (($i % 3) == 0)
{
echo '<tr bgcolor="#8FBC8F"><td> <font color="blue">' . $i . '</font></td></tr>';
}
else
{
if (($i % 3) == 1)
{
echo '<tr bgcolor="#8FBC8F"><td> <font color="yellow">' . $i . '</font></td></tr>';
}
else
{
echo '<tr bgcolor="#8FBC8F"><td> <font color="red">' . $i . '</font></td></tr>';
}
}
}
}
?>
</html>
You could use non nested if statements, but then you may as well just use a switch:-
<doctype html>
<html>
<table width="50%" cellpadding="5" cellspacing="0">
<?php
for($i = 1; $i <= 200; $i++)
{
switch (true)
{
case ($i % 2) == 0 AND ($i % 3) == 0:
echo '<tr bgcolor="#90EE90"><td> <font color="blue">' . $i . '</font></td></tr>';
break;
case ($i % 2) == 0 AND ($i % 3) == 1:
echo '<tr bgcolor="#90EE90"><td> <font color="yellow">' . $i . '</font></td></tr>';
break;
case ($i % 2) == 0 AND ($i % 3) == 2:
echo '<tr bgcolor="#90EE90"><td> <font color="red">' . $i . '</font></td></tr>';
break;
case ($i % 2) == 1 AND ($i % 3) == 0:
echo '<tr bgcolor="#8FBC8F"><td> <font color="blue">' . $i . '</font></td></tr>';
break;
case ($i % 2) == 1 AND ($i % 3) == 1:
echo '<tr bgcolor="#8FBC8F"><td> <font color="yellow">' . $i . '</font></td></tr>';
break;
case ($i % 2) == 1 AND ($i % 3) == 2:
echo '<tr bgcolor="#8FBC8F"><td> <font color="red">' . $i . '</font></td></tr>';
break;
default:
// This should never happen
break;
}
}
?>
</html>
You could also save the need for any explicit if / switch statement and just set up a couple of arrays, and calculate the modulus to use as the subscript for the array. This has the advantage of being compact and also easily expanded:-
<doctype html>
<html>
<table width="50%" cellpadding="5" cellspacing="0">
<?php
$bgcolor_array(0=>"#90EE90", 1=>"#8FBC8F");
$font_array(0=>"#blue", 1=>"#yellow", 2=>"#red");
for($i = 1; $i <= 200; $i++)
{
echo '<tr bgcolor="'.$bgcolor_array[($i % 2)].'"><td> <font color="'.$font_array[($i % 3)].'">' . $i . '</font></td></tr>';
}
?>
</html>
Try this -
$j = 1;
for($i = 1; $i <= 200; $i++) {
if (($i % 2) == 1) {
$tabel = "#8FBC8F";
}
else
$tabel = "#90EE90";
if($j == 1) {
$col = "blue";
} else if ($j == 2) {
$col = "red";
} else {
$col = "yellow";
$j = 1;
}
echo '<tr bgcolor="' . $tabel . '"><td> <font color="'.$col.'">' . $i . '</font></td></tr>';
$j++;
}
You can do that easily way:
for($i = 1; $i <= 200; $i++) {
if (($i % 2) == 0)
$table = "#8FBC8F";
else
$table = "#90EE90";
echo '<tr bgcolor="'.$table.'"><td><font color="'.getFontColor($i).'">'.$i.'</font></td></tr>';
}
function getFontColor($i) {
switch( ($i%3) ) {
case 0:
return 'blue';
break;
case 1:
return 'red';
break;
case 2:
return 'yellow';
break;
}
}
Okay, what I want is to display retrieved data in a table form database, I want my table to be limit in 5 data per table. I need the tables to be display horizontally.
Like this:
while($row1 = sqlFetchArray($row))
{ $ctr = 0;
$ctr+1;
if($ctr=1 || $ctr<=5)
{
$html .='<table width="100px" style="float:left;"><tr><td>'.$row1['id'].'</td></tr></table>';
}
if($ctr=6 || $ctr<=10)
{
$html .='<table width="110px" style="float:left;"><tr><td>'.$row1['id'].'</td></tr></table>';
}
if($ctr=11 || $ctr<=15)
{
$html .='<table width="120px" style="float:left;"><tr><td>'.$row1['id'].'</td></tr></table>';
}
}
Output:
1 6
2 7
3 8
4 9
5 10
This should work for you:
(But the function sqlFetchArray as to work like mysql_fetch_array)
<?php
$limit = 15;
for($numberCounter = 0; $numberCounter < $numberCount = mysql_num_rows($row); $numberCounter++) {
$count = 0;
if($numberCounter >= mysql_num_rows($row))
break;
if ($count == 0 || $count % $limit == 0)
echo "<table width='100px' style='float:left;'>";
while ($row1 = sqlFetchArray($row) && $count < $limit) {
if($numberCounter >= mysql_num_rows($row))
break;
echo "<tr><td>" . $row1[$numberCounter] . "</td></tr>";
$count++;
$numberCounter++;
}
if($count == 0 || $count % $limit == 0)
echo "</table>";
}
?>
As an example:
<?php
$test = range(1, 43);
$limit = 15;
for($numberCounter = 0; $numberCounter < $numberCount = count($test); $numberCounter++) {
$count = 0;
if($numberCounter >= count($test))
break;
if ($count == 0 || $count % $limit == 0)
echo "<table width='100px' style='float:left;'>";
while ($count < $limit) {
if($numberCounter >= count($test))
break;
echo "<tr><td>" . $test[$numberCounter] . "</td></tr>";
$count++;
$numberCounter++;
}
if($count == 0 || $count % $limit == 0)
echo "</table>";
}
?>
My html document has the following form:
<form action="PrimeNumber.php" method="post">
Enter a number to determine if it is a prime number: <input type="text" name="numb" size="10">
<input type="submit" value="Check for Primeness">
</form>
Edit: Using a different code now but can't get it to echo a statement no matter what I do. Anyone know how I can make it echo is a prime number or is not.
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['numb'])) {
$num = $_POST['numb'];
function isPrime($num) {
if($num == 1) {
return false;
}
if($num == 2) {
return true;
}
if($num % 2 == 0) {
return false;
}
for($i = 3; $i <= ceil(sqrt($num)); $i = $i + 2) {
if($num % $i == 0)
return false;
}
return true;
}
}
}
?>
simple and easy to understand code, and working as well. do little changes according to your requirement, like assign input text value to $a variable and you good to go.
$a = 51;
for($i=2; $i < $a; $i++){
if($a % $i == 0){
echo "Numberis not prime.";
break;
}
else{
echo "Number is not prime.";
break;
}
}
A simple function to check is prime number:
function is_prime($p) {
return ($p > 1) && (($p%2 >= 1) && ($p%3 >= 1) && ($p%5 >= 1)) || in_array($p, [2,3,5]);
}
function checkPrime($num){
$isPrime = true;
for($i = 2; $i <= sqrt($num); $i++)
{
if($num % $i == 0)
{
$isPrime = false;
}
}
if($isPrime == true)
{
return "$num is prime number";
}else{
return "$num is not prime number";
}
}
echo checkPrime(29);
The output is
29 is prime number
For More Details
You may use the gmp package and correct your code because I got an output as 11 not being a prime number or use the following function as an alternative.
function isPrime($num) {
if($num == 1) {
return false;
}
if($num == 2) {
return true;
}
if($num % 2 == 0) {
return false;
}
for($i = 3; $i <= ceil(sqrt($num)); $i = $i + 2) {
if($num % $i == 0)
return false;
}
return true;
}
More information is available at the following page.
A simple function to help you find out if a number is a prime number
<?php
function fn_prime($number) {
$i = 2; $result = TRUE;
while($i < $number) {
if(!($number%$i)) {
$result = FALSE;
}
$i++;
}
return $result;
}
?>