Jump to content
impekt

Need help for PHP script

Recommended Posts

Hello, I created a small simple script that generates a sequence of numbers and I would like to put a (hidden) list of numbers as the starting script, and I would like the script to detect if there is the same sequence of numbers than in the list, and if so, the script reset until it’s good
It’s for my school, and I’m having trouble :)

Share this post


Link to post
Share on other sites

We are not going to do your assignment for you but, if you show us what you have so far, we can point you in the right direction.

Share this post


Link to post
Share on other sites

Yep, i have that :

 

<?php
 

$regularNumbers = 5;
$bonusNumbers = 1;
$regularNumbersMin = 1;
$regularNumbersMax = 69;
$bonusNumbersMin = 1;
$bonusNumbersMax = 26;
$bonusUniqueFromRegular = false;
$results = array(
    'regular_nums' => array(),
    'bonus_nums' => array()
);
 

foreach(range(1, $regularNumbers) as $i){
    $randNum = mt_rand($regularNumbersMin, $regularNumbersMax);
    while(in_array($randNum, $results['regular_nums'])){
        $randNum = mt_rand($regularNumbersMin, $regularNumbersMax);
    }
    array_push($results['regular_nums'], $randNum);
}
 

foreach(range(1, $bonusNumbers) as $i){
    $randNum = mt_rand($bonusNumbersMin, $bonusNumbersMax);
    while(in_array($randNum, $results['bonus_nums']) || ($bonusUniqueFromRegular && in_array($randNum, $results['regular_nums']))){
        $randNum = mt_rand($bonusNumbersMin, $bonusNumbersMax);
    }
    array_push($results['bonus_nums'], $randNum);
}
 

sort($results['regular_nums']);
sort($results['bonus_nums']);
 

echo 'Results: <br>';
echo implode(", ", $results['regular_nums']);
echo '<br>';
echo 'Bonus: <br>';
echo implode(", ", $results['bonus_nums']);

Share this post


Link to post
Share on other sites

and what is the 'trouble'??

 

BTW - as a newbie you may not be aware but PHP is a case-sensitive language.  That means that if you use a cap letter in a var name but then reference that var later on without the proper cap, you will not be referencing the same var.  That can be a real problem with a long script when you can't figure out why something isn't quite right.  I highly suggest that you NOT use mixed case names to avoid this problem.  All lower case is so much easier to work with, IMHO.  That and some underscores to break up longer var names.  In your case I would use "bonus_numbers_min" and "bonus_numbers_max".

Share this post


Link to post
Share on other sites

OK, guessing this is a "lottery". Use array_intersect() to find matching numbers

$results = [ 4, 36, 44, 55, 57 ];

$user    = [ 3, 21, 44, 45, 55 ];

$matching = array_intersect($results, $user);

Check

echo '<pre>', print_r($matching, 1), '</pre>';

Array
(
    [2] => 44
    [3] => 55
)

 

Share this post


Link to post
Share on other sites

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.