PHP Program to print the numbers in reverse order with form and database
PHP Program to print the numbers in reverse order with form and database
In this tutorial, we will learn about the followings;
- PHP Program to print the numbers in reverse order
- PHP Program to print the numbers in reverse order with the formĀ
- PHP Program to print the numbers in reverse order with database
PHP Program to print the numbers in reverse order.
In this example, programmer stores the values in the variables. However, if you interested to see the program about taking values in the form, please see example 2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $number=123; $orignal_num=$number; $rs=0; while($number>0) { $rs = $rs * 10 + $number % 10; $number = (int)($number / 10); } print_r("Result:$orignal_num<br>Revrse of Number is= $rs"); ?> |
PHP Program to print the numbers in reverse order with the formĀ
In this example, we will take the inputs from the user and store them in variables.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<html> <head> <title></title> </head> <body> <div class="nukr"><h2 class="col_insert" >Reverse Number</h2></div> <form action="" method="post" name="insert_form" id="insert_form" class="comment-form"> <table class="tableI1" align="left" border="0" bordercolor="#FFFFFF" > <tr><td ><b><i>Enter Number</b></i></td> <td><input id="Charge" name="Charge" type="text" size="30" /> <tr><td></td><td><input type="submit" id="btnsubmit" name="btnsubmit" value="Calculate" size="30"/></td></tr> <tr><td></td><td> </td></tr> </table> </form> </body> </html> <?php if(isset($_POST['btnsubmit'])) { $number=$_POST['Charge']; $orignal_num=$number; $rs=0; while($number>0) { $rs = $rs * 10 + $number % 10; $number = (int)($number / 10); } print_r("Result:$orignal_num<br>Revrse of Number is= $rs"); } ?> |
PHP Program to print the numbers in reverse order with the database.
In this example, we will take the inputs from the user and temporarily store them in variables and then finally stores them permanently in the database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<html> <head> <title></title> </head> <?php include 'dbconction.php'; ?> <body> <div class="nukr"
><h2 class="col_insert" >Reverse Number</h2></div> <form action="" method="post" name="insert_form" id="insert_form" class="comment-form"> <table class="tableI1" align="left" border="0" bordercolor="#FFFFFF" > <tr><td ><b><i>Enter Number</b></i></td> <td><input id="Charge" name="Charge" type="text" size="30" /> <tr><td></td><td><input type="submit" id="btnsubmit" name="btnsubmit" value="Calculate" size="30"/></td></tr> <tr><td></td><td> </td></tr> </table> </form> </body> </html> <?php if(isset($_POST['btnsubmit'])) { $number=$_POST['Charge']; $orignal_num=$number; $rs=0; while($number>0) { $rs = $rs * 10 + $number % 10; $number = (int)($number / 10); } $sql= "insert into reves(Number, Revers) Value ('$orignal_num','$rs')"; if($conn->query($sql)=== TRUE) { echo"<font color=#FF00FF> New Record Inserted<br></font>"; } else { echo"Error:".$sql."<br>".$conn->error; } print_r("Youer Input=$orignal_num<br>Revrse of Number is= $rs"); } ?> |
dbconnection.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php error_reporting(0); $servername = "localhost"; $username = "root"; $password = ""; $dbname = "fyp"; // Create connection $conn = new mysqli($servername, $username, $password,$dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } else { } //echo "Connected successfully"; mysql_query("set character_set_results='utf8'"); ?> |
Database toĀ print the numbers in reverse order