Download Source Code -PHP – Odd Numbers Generator

Loading...

PHP – Odd Numbers Generator

Submitted by: 
razormist
Language: 
PHP
Visitors have accessed this post 8 times.

In this tutorial we will create a Odd Numbers Generator using PHP. This code will instantly separate all the possible odd numbers in the web page. This code use a PHP POST method to generate an odd number dynamically in an array of number using an algebraic conditional statement .This is a user-friendly kind of program feel free to modify it.

We will be using PHP as a scripting language that manage a database server to handle a bulk of data per transaction. It describe as an advance technology that manage both server and control-block of your machine.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here’s the link for XAMPP server https://www.apachefriends.org/index.html.

And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Odd Numbers Generator</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-4">
  19. <form method="POST">
  20. <div class="form-inline">
  21. <label>Enter a number</label>
  22. <input type="number" class="form-control" max="100" name="number" required="required" min="1"/>
  23. <button name="generate" class="btn btn-primary">Generate</button>
  24. </div>
  25. </form>
  26. <br />
  27. <?php
  28. if(ISSET($_POST['generate'])){
  29. $array = [];
  30. $number = $_POST['number'];
  31. for($i=1; $i<=$number; $i++){
  32. array_push($array, $i);
  33. }
  34.  
  35. echo implode(", ", $array);
  36. }
  37. ?>
  38. </div>
  39. <br />
  40. <div class="col-md-8">
  41. <?php include 'generate.php'?>
  42. </div>
  43. </div>
  44. </body>
  45. </html>

Creating the Main Function

This code contains the main function of the application. This code will generate an odd number when the button is clicked. To do that just copy and write this block of codes inside the text editor, then save it as generate.php.

  1. <?php
  2. if(ISSET($_POST['generate'])){
  3. $array = [];
  4. $number = $_POST['number'];
  5. for($i=1; $i<=$number; $i++){
  6. array_push($array, $i);
  7. if ($i % 2 != 0){
  8. $odd[]=$i;
  9. }
  10. }
  11. ?>
  12. <center><h4 class="text-warning">Odd Numbers</h4></center>
  13. <?php
  14. echo implode(", ", $odd);
  15. ?>
  16. </div>
  17.  
  18. <?php
  19. }
  20. ?>

There you have it we successfully created Odd Numbers Generator using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Tags: 
php
Bootstrap
odd
Converter

Add new comment

(Visited 1 times, 1 visits today)
Loading...