How to check prime number in PHP with an input - php

I need help for my php program to find a prime number. This is my code:
<form method="post" action="">
<table border="1" width="180px">
<thead>
<tr bgcolor="yellow">
<th>#</th>
<th>Data 1</th>
<th>Data 2</th>
<th>Data 3</th>
</tr>
</thead>
<?php
error_reporting(0);
$start = 21;
$n_rows = 5;
$n_cols = 4;
for ($i = 0; $i < $n_rows; $i++) {
$row = '';
for ($j = 0; $j < $n_cols; $j++) {
$row .= '<td>'. ($start + $i + ($j * $n_rows)). '</td>';
}
$out .= '<tr>'. $row. '</tr>';
}
echo $out;
?>
<tr>
<td colspan=4>
<center>
<label for="input">Initial value:</label>
<input type="text" name="awal" style="width: 60px">
<input type="submit" name="submit" value="Send">
</center>
</td>
</tr>
</table>
</form>
The question is, how to check if we enter the initial value of the input to find prime numbers? If prime then the number will be red, and if it not prime will be black.
I need the code to make it, any response would be greatly appreciated.

Try:
<?php
error_reporting(0);
//this function will check whether the number passed to it is prime or not
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 initial value set, take that value or else begin from 1
if (isset($_POST['awal']) && $_POST['awal'] != 0)
{
$start = $_POST['awal'];
}
else
{
$start = 1;
}
$n_rows = 5;
$n_cols = 4;
for ($i = 0; $i < $n_rows; $i++)
{
$row = '';
for ($j = 0; $j < $n_cols; $j++)
{
$number = ($start + $i + ($j * $n_rows));
//checking if number prime
if (isPrime($number) == true)
{
//if prime color it red
$row.= '<td style="color:red">' . ($start + $i + ($j * $n_rows)) . '</td>';
}
else
{
$row.= '<td>' . ($start + $i + ($j * $n_rows)) . '</td>';
}
}
$out.= '<tr>' . $row . '</tr>';
}
echo $out;
?>

Related

Table with multiple buttons - need to get the value of the button that I click on in php.

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>

PHP: Show 3 td's of data per table row

I am wanting to use 8 images from my database and load them into a HTML Table. I would like to display 4 images per table row, but however when i run my code below, i seem to get all images in one table row. Any help would be great. Thank you in advance.
$count = $get->rowCount();
if ($count > 0)
{
echo '<table id="" class="uiGrid _51mz _1m6c" cellpadding="2" cellspacing="0">';
$i = 0;
while ($r = $get->fetch(\PDO::FETCH_OBJ))
{
$globals = new \Libraries\Helpers\Views\Globals;
if ($i == 0)
{
echo '<tr class="_51mx">';
}
echo '
<td class="_51m-">
<a href="/e/a/'.$r->data_id.'">
<div class="uiScaledImageContainer _f-u2" style="width:74px;height:74px;">
<img class="scaledImageFitWidth img" src="https://gstatic.acfee.org/akamaihd/i/'.$globals->data_image_name($r->data_id).'">
<div class="_3s6x">
<div class="_50f3"></div>
</div>
</div>
</a>
</td>
';
if ($i > 4)
{
$i = 0;
echo '</tr>';
};
$i++;
echo '
<script type="text/javascript">
$("#favs-preloader").hide();
</script>
';
}
echo '</table>';
put this code directly it will works...
echo '<table id="" class="uiGrid _51mz _1m6c" cellpadding="2" cellspacing="0">';
$i = 0;
while ($r = $get->fetch(\PDO::FETCH_OBJ))
{
$globals = new \Libraries\Helpers\Views\Globals;
$i++;
if ($i == 1)
{
echo '<tr class="_51mx">';
}
echo '
<td class="_51m-">
<a href="/e/a/'.$r->data_id.'">
<div class="uiScaledImageContainer _f-u2" style="width:74px;height:74px;">
<img class="scaledImageFitWidth img" src="https://gstatic.acfee.org/akamaihd/i/'.$globals->data_image_name($r->data_id).'">
<div class="_3s6x">
<div class="_50f3"></div>
</div>
</div>
</a>
</td>
';
if ($i >= 4)
{
echo '</tr>';
$i = 0;
};
echo '
<script type="text/javascript">
$("#favs-preloader").hide();
</script>
';
}
echo '</table>';
In your current code, you're verifying if the $i variable is == 0 in order to add a row. But you're always increasing it's value at the end, even after you set it to zero in the if($i > 4)
Change these lines
if ($i > 4)
{
$i = 0;
echo '</tr>';
};
$i++;
To this:
if ($i > 4)
{
$i = 0;
echo '</tr>';
}
else
$i++;

I just want to print pyramid only using for loop in php as like as the give image

My code is
<center>
<?php
for($i = 0; $i < 7 ; $i++) {
for($j = 0; $j < $i; $j++) {
echo "*";
}
echo "<br/>";
}
?>
<center>
But I want is pyramid with useing <center></center> html tags.
You're looking for something like this:
<div class="text-center"><!-- or however you choose to center this -->
<?php
$w = 7;
for ($n = 0; $n < $w; $n++) {
echo str_repeat("-", $n) . "<br>";
}
?>
</div>
<?php
int k =0;
for($i=1;$i<=20;$i++){
for(a=1;a<=20;$a++){
echo " ";
}
while(k!=(2*i-1){
echo "* ";
k++;
}
k=0;
echo "\n";
}
?>

Creating a table that multiplies height by weight

How would I create a table that multiples height by weight?
I have no idea how to figure it out!
I am trying to create a BMI calculator.
// collect values from a form sent with method=get
$min_weight = mysql_real_escape_string($_GET["min_weight"]);
$max_weight = mysql_real_escape_string($_GET["max_weight"]);
$min_height = mysql_real_escape_string($_GET["min_height"]);
$max_height = mysql_real_escape_string($_GET["max_height"]);
?>
<table>
<tr>
<td>Key:</td>
<td class="good show">Healthy (20-25)</td>
<td cla ss="warning show">Overweight (25-30)</td>
<td class="bad show">Unhealthy ( -20 or 30+)</td>
</tr>
</table>
<table border="1">
<?php
echo "<tr><td>Height →<br>Weight ↓</td>";
for ( $i = $min_height; $i <= $max_height; $i+=5 )
{
echo "<td>{$i}</td>";
}
echo "</tr>";
for ( $j = $min_weight; $j <= $max_weight; $j+=5 )
{
echo "<tr>";
echo "<td>{$j}</td>";
echo "</tr>";
}
This should do the job, i've also put some isset()'s:
<?php
// collect values from a form sent with method=get
$min_weight = isset($_GET["min_weight"]) ? mysql_real_escape_string($_GET["min_weight"]):0;
$max_weight = isset($_GET["max_weight"]) ? mysql_real_escape_string($_GET["max_weight"]):0;
$min_height = isset($_GET["min_height"]) ? mysql_real_escape_string($_GET["min_height"]):0;
$max_height = isset($_GET["max_height"]) ? mysql_real_escape_string($_GET["max_height"]):0;
?>
<table>
<tr>
<td>Key:</td>
<td class="good show">Healthy (20-25)</td>
<td class="warning show">Overweight (25-30)</td>
<td class="bad show">Unhealthy ( -20 or 30+)</td>
</tr>
</table>
<?php
$table = '<table border="1">';
$table .= '<tr><td>Height →<br>Weight ↓</td>';
for ($i = $min_height;$i <= $max_height;$i += 5){
$table .= "<td>$i</td>";
}
$table .= "</tr>";
for($j = $min_weight; $j <= $max_weight;$j += 5){
$table .= "<tr><td>$j</td>";
for ($i = $min_height;$i <= $max_height;$i += 5){
$table .= '<td>'. $i * $j .'</td>';
}
$table .= '</tr>';
}
$table .= '</table>';
echo $table;
?>
Even though this works is it wrong to have the table border outside of the php tags?
<table>
<tr>
<td>Key:</td>
<td class="good show">Healthy (20-25)</td>
<td class="warning show">Overweight (25-30)</td>
<td class="bad show">Unhealthy ( -20 or 30+)</td>
</tr>
</table>
<table border="1">
<?php
echo "<tr><td>Height →<br>Weight ↓</td>";
for ( $j = $min_height; $j <= $max_height; $j+=5 )
{
echo "<td>{$j}</td>";
}
for ( $i = $min_weight; $i <= $max_weight; $i+=5 )
{
echo "<tr><td>{$i}</td>";
for ( $j = $min_height; $j <= $max_height; $j+=5 )
{
$var = number_format(($i)/(($j/100)*($j/100)),'2','.','');
{
echo "<td class='warning'>";
}
echo $var;
echo "</td>";
}
echo "</tr>";
}
?>
</table>

PHP 2-dimensional array -> how to convert to HTML table syntax?

I need to read a bi-dimensional array and convert the values into an HTML table. For example:
$grid = array( array(0,0,0,0),array(0,0,0,0),array(0,0,0,0),array(0,0,0,0),array(0,0,0,0));
$grid[0][0]=4;$grid[0][1]=4;$grid[0][2]=4;$grid[0][3]=4;
$grid[1][0]=3;$grid[1][1]=3;$grid[1][2]=5;$grid[1][3]=5;
$grid[2][0]=3;$grid[2][1]=3;$grid[2][2]=5;$grid[2][3]=5;
$grid[3][0]=0;$grid[3][1]=0;$grid[3][2]=5;$grid[3][3]=5;
Reading this array, I want to dynamically write the <TABLE> syntax where the cells with the same number in the grid are merged horizontally or vertically:
That is, I should be able to create the following syntax from the $grid above:
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td colspan="4">4</td>
</tr><tr>
<td colspan="2">3</td>
<td colspan="2" rowspan="2">5</td>
</tr><tr>
<td> </td>
<td> </td>
</tr></table>
I have been able to process the colspan correctly, but am having trouble with the rowspan issue. Any ideas? basically I want to be able to create custom homepages with custom content from the array "format" established by the $grid.
Any help greatly appreciated!
Code:
<?php
$grid = array(
array(0,0,0,0),
array(0,0,0,0),
array(0,0,0,0),
array(0,0,0,0),
);
$grid[0][0]=4;$grid[0][1]=4;$grid[0][2]=4;$grid[0][3]=4;
$grid[1][0]=3;$grid[1][1]=3;$grid[1][2]=5;$grid[1][3]=5;
$grid[2][0]=3;$grid[2][1]=3;$grid[2][2]=5;$grid[2][3]=5;
$grid[3][0]=0;$grid[3][1]=0;$grid[3][2]=5;$grid[3][3]=5;
$w = count($grid[0]);
$h = count($grid);
for ($y = 0; $y < $h; ++$y) {
echo "<tr>\n";
for ($x = 0; $x < $w; ++$x) {
$value = $grid[$y][$x];
if ($value === null) {
continue;
}
$colspan = 1;
while ($x + $colspan < $w && $grid[$y][$x + $colspan] === $value) {
$grid[$y][$x + $colspan++] = null;
}
$rowspan = 1;
$rowMatches = true;
while ($rowMatches && $y + $rowspan < $h) {
for ($i = 0; $i < $colspan; ++$i) {
if ($grid[$y + $rowspan][$x + $i] !== $value) {
$rowMatches = false;
break;
}
}
if ($rowMatches) {
for ($i = 0; $i < $colspan; ++$i) {
$grid[$y + $rowspan][$x + $i] = null;
}
++$rowspan;
}
}
$rowspan = $rowspan > 1 ? " rowspan=\"$rowspan\"" : "";
$colspan = $colspan > 1 ? " colspan=\"$colspan\"" : "";
echo " <td$rowspan$colspan>$value</td>\n";
}
echo "</tr>\n";
}
?>
Output:
<tr>
<td colspan="4">4</td>
</tr>
<tr>
<td rowspan="2" colspan="2">3</td>
<td rowspan="3" colspan="2">5</td>
</tr>
<tr>
</tr>
<tr>
<td colspan="2">0</td>
</tr>
<?php
$grid = array( array(0,0,0,0),array(0,0,0,0),array(0,0,0,0),array(0,0,0,0),array(0,0,0,0));
foreach($grid AS $rowkey => $row)
{
echo "<tr>\n";
foreach($grid[$rowkey] AS $columnKey => $field)
{
echo "<td>{$field}</td>\n";
}
echo "</tr>\n";
}
?>

Categories