how do i generate series of numbers? - php

i need help to generate series of number and it should be generate series of number when
form is submitted i have code but it not working properly?
For Example I Want Like This
00500
00501
00502
00503 and so on...
Here My Code
$random = rand(500,999);
$new_val = $random+1;
for($i=1;$i<=$new_val;$i++)
{
if( strlen($i)==1 )
{
$say='0000'.$i;
}
elseif( strlen($i)==2 )
{
$say='000'.$i;
}
elseif( strlen($i)==3 )
{
$say='00'.$i;
}
elseif( strlen($i)==4 )
{
$say='0'.$i;
}
elseif( strlen($i)==5 )
{
$say=$i;
}
}
<form method="post">
<input class="input_field" readonly="readonly" type="text"
name="sno" id="sno" value="<?=$say?>" >
<input type="submit" name="test" value="submit" />
</form>

Use sprintf to format it:
<?php
$start = rand(500, 999);
for ($i = 1; $i <= $start; $i++) {
$num = sprintf("%05d", $i);
echo $num;
}
?>

As Blender says, use sprintf to format your numbers
$start = rand(500, 999);
for ($i = $start; $i <= 999; $i++) {
$num = sprintf("%1$05d", $i);
echo $num . '<br>';
}
Note that the form must contain an "action" attribute

Related

PHP basic: why my variable showing wrong value?

I am new to PHP. I am trying to create a simple form where user will submit there name and mark they got in the exam.
<form action="" method="post">
<input type="text" name="name" id="" placeholder="Your name">
<input type="number" name="mark" id="" placeholder="Your Mark">
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST["submit"])){
$name = $_POST['name'] ;
$mark = $_POST['mark'];
if( $mark > 80 ){
$result = "A+";
}elseif($mark > 70 && $mark < 80){
$result = "A";
}
elseif($mark > 60 && $mark < 70){
$result = "A-";
}
elseif($mark > 40 && $mark < 60){
$result = "B";
}else{
$result = "Fail";
}
echo $result;
}
?>
But if I echo the result variable it is always showing 'Fail'. What is I am doing wrong? thanks
As mentioned in the comments, when comparing ranges, you need to consider the corner cases:
$mark >= 70 && $mark < 80
Also there are few ways to simplify your code: If you check for marsk >= 80 in the first IF, there is no need to check for that in the next elseif:
if( $mark >= 80 )
{
$result = "A+";
}
elseif($mark >= 70)
{
$result = "A";
}
elseif($mark >= 60)
{
$result = "A-";
}
elseif($mark >= 40)
{
$result = "B";
}
else
{
$result = "Fail";
}
echo $result;

i want to write a php script to find if a given number is prime or not [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 4 years ago.
I have tried to create a code which is
<html>
<body>
<form method = "post">
<?php
Number : <input type = "text" name = "prime">;
<input type = "submit" value ="Submit">;
$num = $_REQUEST["prime"];
$flag = 0;
for($i = 2; $i <= $num/2; $i++)
{
if( $num % $i == 0)
{
$flag = 1;
break;
}
}
if($flag == 0)
echo "$num is a prime number";
else
echo "$num is not a prime number";
?>
</form>
</body>
</html>
Whenever i try to run it, i get the error
Parse error: syntax error, unexpected '<', expecting end of file in
C:\xampp\htdocs\pc.php on line 5
Any help would be appreciated
Your code is wrong. There is no statement in line 5. Corrected code:
<html>
<body>
<?php
if($_SERVER['REQUEST_METHOD']=='post'){
$num = $_POST["prime"];
$flag = 0;
for($i = 2; $i <= $num/2; $i++)
{
if( $num % $i == 0)
{
$flag = 1;
break;
}
}
if($flag == 0)
echo $num." is a prime number";
else
echo $num." is not a prime number";
}
?>
<form method = "post" action="#">
Number : <input type = "text" name = "prime">
<input type = "submit" value ="Submit">
</form>
</body>
</html>
This will check current request method. If is is pot
You can follow the below procedure for solving your problem
<html>
<body>
<?php
if(isset($_POST['prime']))
{
$num = $_POST["prime"];
$flag = 0;
for($i = 2; $i <= $num/2; $i++)
{
if( $num % $i == 0)
{
$flag = 1;
break;
}
}
if($flag == 0)
echo "$num is a prime number";
else
echo "$num is not a prime number";
}
?>
<form method = "post">
<input type = "text" name="prime">
<input type = "submit" value ="Submit">
</form>
</body>
</html>

How to check prime number with an input set as LIMIT

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

Set Shuffle, No Repeating

I have an array for flash cards, and using shuffle I am outputting 15 unique cards, 3 each for 5 different categories.
What I want to do is create these card sets for about a dozen people on the same web page, but the part I can't figure out is how to make it so each complete set is unique and doesn't repeat from a set given to any other user.
A short code sample with a brief explanation would be the most helpful to me.
Here is the code I modified to my needs. Not much changed really.
<?php
/* original source:
* 3d10-engine.php
* by Duane Brien
*/
if (empty($_POST)) {
for ($i = 1; $i < 16; $i++) {
$numbers['ALL'][] = $i;
}
$picks = array();
$letters = array ('ALL');
foreach ($letters as $letter) {
for ($i = 0;$i < 10;$i++) {
shuffle($numbers[$letter]);
$chunks = array_chunk($numbers[$letter], 5);
$cards[$i][$letter] = $chunks[0];
if ($letter == 'N') {
$cards[$i][$letter][2] = ' '; // Free Space
}
}
foreach ($numbers[$letter] as $number) {
$balls[] = $letter.$number;
}
shuffle($balls);
}
$cardsstr = serialize($cards);
$ballsstr = serialize($balls);
$picksstr = serialize($picks);
} else {
$cards = unserialize($_POST['cardsstr']);
$balls = unserialize($_POST['ballsstr']);
$picks = unserialize($_POST['picksstr']);
array_unshift($picks, array_shift($balls));
echo "<h1>Just Picked: " . $picks[0] . "</h1>";
$cardsstr = serialize($cards);
$ballsstr = serialize($balls);
$picksstr = serialize($picks);
}
?>
Picks : <?php echo implode(',', $picks) ?>
<form method='post'>
<input type='hidden' name='cardsstr' value='<?php echo $cardsstr ?>' />
<input type='hidden' name='ballsstr' value='<?php echo $ballsstr ?>' />
<input type='hidden' name='picksstr' value='<?php echo $picksstr ?>' />
<input type='submit' name='cards' value='next number' />
</form>
Start Over
<?php
foreach ($cards as $card) {
echo "<table border='1'>";
echo "<tr><td>A</td><td>B</td><td>C</td><td>D</td><td>E</td></tr>";
for ($i = 0; $i < 5; $i++) {
echo "<tr><td>" . $card['B'][$i] . "</td><td>" .$card['I'][$i] . "</td><td>" . $card['N'][$i] . "</td>";
echo "<td>" . $card['G'][$i] . "</td><td>" . $card['O'][$i] . "</td></tr>";
}
echo "</table>";
}
?>
Since you have more options in each set, random pick is enough to achieve unique final result.
I mean don't make this thing more complex.
Try this sample
<?php
//Initialize your 5 sets here
$numbers['B'] = range(1,15);
$numbers['I'] = range(16,30);
$numbers['N'] = range(31,45);
$numbers['G'] = range(45,60);
$numbers['O'] = range(61,75);
//My Assumption is you to pick 3 from each
while(TRUE){
$rand = rand(0,5);
if(count($numbers_B) < 3 && !in_array($numbers['B'][$rand]){
$numbers_B[] = $numbers['B'][$rand];
}
$rand = rand(0,5);
if(count($numbers_I) < 3 && !in_array($numbers['I'][$rand]){
$numbers_I[] = $numbers['I'][$rand];
}
$rand = rand(0,5);
if(count($numbers_N) < 3 && !in_array($numbers['N'][$rand]){
$numbers_N[] = $numbers['N'][$rand];
}
$rand = rand(0,5);
if(count($numbers_G) < 3 && !in_array($numbers['G'][$rand]){
$numbers_G[] = $numbers['G'][$rand];
}
$rand = rand(0,5);
if(count($numbers_O) < 3 && !in_array($numbers['O'][$rand]){
$numbers_O[] = $numbers['O'][$rand];
}
if( count($numbers_B) == 3 && count($numbers_I) == 3 && count($numbers_N) == 3 &&
count($numbers_G) == 3 && count($numbers_O) == 3 ){
break;
}
}
$result = $numbers_B + $numbers_I + $numbers_N + $numbers_G + $numbers_O; ?>
Here $result value should be unique, And I consider number of sets is constant. If it is dynamic, then try the same logic with two dimensional array.
Just store the prepared sets in an array and then check each shuffle if it exists in the array using the already (in_array function) or not, if it does then shuffle again.

Using PHP trying to convert str to int

I really don't get why this isn't working, so please help. I'm trying to convert a str to an int and do if statements with it, but I can't for some reason. The code jumps right over the if statement like it's not even there???
<?php
$cost = $_REQUEST['cost'];
$cost = (int) $cost;
if($cost < 2){
header('Location: page.php?say=numerror');
}
?>
<input name="cost" id="cost" type="text" class="tfield" />
I have a suspicion you need:
if ($cost < 2) {
exit(header('Location: page.php?say=numerror'));
}
why do you need a conversion just use this:
<?php
$cost = $_REQUEST['cost'];
if($cost < 2 or !is_numeric($cost)){
header('Location: page.php?say=numerror');
}
?>
<input name="cost" id="cost" type="text" class="tfield" />
Try this:
<?php
$cost = $_REQUEST['cost'];
$cost = intval($cost);
if($cost < 2){
header('Location: page.php?say=numerror');
}
?>
// HTML
<input name="cost" id="cost" type="text" class="tfield" />
Here is more about intval() function at intval() PHP reference manual. I hope, that will be helpful.
If this not help you. Here is PHP function where you from string can separate integers.
<?php
function str2int($string, $concat = true) {
$length = strlen($string);
for ($i = 0, $int = '', $concat_flag = true; $i < $length; $i++) {
if (is_numeric($string[$i]) && $concat_flag) {
$int .= $string[$i];
} elseif(!$concat && $concat_flag && strlen($int) > 0) {
$concat_flag = false;
}
}
return (int) $int;
}
// Callings
echo var_dump(str2int('sh12apen11')); // int(12)
echo var_dump(str2int('sh12apen11', false)); // int(1211)
echo var_dump(str2int('shap99en')); // int(99)
echo var_dump(intval('shap99en')); // int(0)
?>
P.S Function copied from link above. Isn't mine.

Categories