Jump to content

Novice - Lottery Ticket Generator


Nelalen
Go to solution Solved by QuickOldCar,

Recommended Posts

Struggling with creating the following:
 
a. The authorized user can select one or both of the two available lottery services (in form of checkboxes) provided by your website.
 
Generate six sets of LOTTO 6/49 numbers (sorted in ascending order) when option one,  labeled as “LOTTO 6/49”,  is checked. One set of  “LOTTO 6/49” numbers consists of 6 unique numbers between 1 and 49 inclusively.
 
Generate six sets of LOTTO MAX numbers (sorted in ascending order) when option two, labeled as “LOTTO MAX”,  is checked. One set of “LOTTO MAX” numbers consists of 7 unique numbers between 1 and 49 inclusively.
 
b. When the authorized user submits the form with one or both of the two available services checked, your web server will generate six sets of lottery numbers for each type accordingly and display these numbers of each type of lotteries to the user.
 
Code so far:
 
<?php
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors','1');
 
if(!isset($_SESSION)){
    session_start();
}
 
    
?>
<html>
    <head>
        <title> Choose Tickets</title>
    </head>
    <body>
        <h2> Choose your tickets</h2>
        
<?php
 
 if (filter_input(INPUT_POST,'form_tickets')){
     if(!empty($_SESSION["tickets"])){
         $tickets = array_unique(
                 
                    array_merge(unserialize($_SESSION["tickets"]),
                    filter_input(INPUT_POST, 'form_tickets')));
         $_SESSION["tickets"] = serialize($tickets);
} else {
        $_SESSION["tickets"] = serialize(filter_input(INPUT_POST, 'form_tickets'));
    }
    echo "<p> Your tickets have been selected!</p>";
 }
 
?>
  
  <form method="POST" action="">
  <p><strong>Select your ticket(s):</strong><br>
      
      Lotto 6/49: <input type="checkbox" value="Lotto 6/49"><br />
      Lotto Max: <input type="checkbox" value="Lotto Max"><br />
     
  
  <p><input type="submit" value="submit"/></p>
  
  </form>
  
      
      
    </body>
</html>
 
I honestly don't really know what I'm doing..
This class isn't specifically php so he's going through it too quick for me to grasp/learn. I'm sort of going off examples he's given us but I can't seem to fit the pieces together to make this thing.
Again, any help is greatly appreciated I have a vague idea of how it's all supposed to work but not sure of the commands to do it or how to structure it. 

 

Link to comment
Share on other sites

  • Solution

We don't usually help with school work.

 

To me this seems a bit complex being is not a normal php class.

 

I created a simple form and a function to handle the number sets for you.

You can use it, integrate whatever else need.

<form method="POST" action="">
  <p><strong>Select your ticket(s):</strong><br>
      <label for="Lotto6">Six sets of LOTTO 6/49 numbers</label>
      <input type="checkbox" id="Lotto6" name="six" value="Lotto 6/49"><br />
      <label for="Lotto7">Six sets of LOTTO MAX (7/49)</label>
      <input type="checkbox" id="Lotto7" name="seven" value="Lotto Max"><br />

  <p><input type="submit" value="submit"/></p>
 
</form>

<?php
function lotteryNumbers($min_number, $max_number, $amount_of_numbers)
{
    $range = range($min_number, $max_number);
    shuffle($range);
    $numbers_array = array_slice($range, 0, $amount_of_numbers);
    natsort($numbers_array);
    return array_values($numbers_array);
}

if (isset($_POST['six'])) {
        echo "<h2>LOTTO 6/49</h2>";
  $number_set = array();
        for ($i = 1; $i <= 6; $i++) {
            $number_set = lotteryNumbers(1, 49, 6);
            echo implode(",", $number_set) . "<br />";
        }
}

if (isset($_POST['seven'])) {
        echo "<h2>LOTTO MAX</h2>";
  $number_set = array();
        for ($i = 1; $i <= 6; $i++) {
            $number_set = lotteryNumbers(1, 49, 7);
            echo implode(",", $number_set) . "<br />";
        }
}

?>
Edited by QuickOldCar
Link to comment
Share on other sites

I totally understand. I'm struggling since we've only spent 2 wks on php and he doesn't entirely seem to understand it well himself. The number stuff me and a partner had somewhat figured out now but your code adds on the couple pieces we were missing. Does this seem like the kind of project we should be doing so soon? We're thinking this course is expecting too much  and we were going to bring it up with the prof. (it's his first time teaching this specific course). A lot of others seem to be lost as well. 

 

Thank you for the code; it really helps! The main issue we're trying to figure out is how the info we put into the checkboxes is processed. I'd think we would output the checkbox data to another php file containing the generator and then taking the output from that and echo-ing the answers with fancy html/css. Thing is we aren't sure how to access the information to process it or store the data to output on another page. Overall, PHP is unbelievably confusing right now..

Thank you for the help though it is much appreciated :]

Link to comment
Share on other sites

The form values are held in $_POST which is an associative array.

 

to see these values:

print_r($_POST);

The example code only determines if was checkmarked and not the actual values.

 

You can style it in the same script, add some css and style whatever need to.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.