197. PHP Program to read ‘n’ names and print it in ascending order.
How does this program work?
- In this program you are going to learn about how to read ‘n’ names and print it in ascending order using PHP.
- By using given code we can arrange the names in ascending order.
Here is the code
<?php
if(isset($_POST['submit']))
{
$size = array();
$size = $_POST['Fnum'];
echo "The array of names :"."<br>" ;
foreach ($size as $value)
{
echo($value)."<br>";
}
sort($size);
echo "Ascending order : "."<br>" ;
//To arrange ascending order depends on its length
$len = count($size);
for($i=0; $i< $len; $i++)
{
echo $size[$i];
echo"<br>";
}
return 0;
}
?>
<html>
<head>
<title>PHP Program To read ‘n’ names and print it in ascending order</title>
</head>
<body>
<form method="post">
<table border="0">
<tr>
<td> <input type="text" id="num1" name="num1" value="" placeholder="Enter array size"/> </td>
</tr>
<div id="screens">
</div>
<tr>
<td> <input type="submit" name="submit" value="Submit"/> </td>
</tr>
</table>
</form>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
function create(param)//To print no.of textbox dynamically
{
'use strict';
var i;
$("#screens").empty();
for (i = 0; i < param; i += 1)
{
$(' #screens').append('<input type="text" name="Fnum[]" placeholder="Enter name">');
}
}
$( '#num1').change(function ()
{
create( $(this).val());
});
</script>
</html>