javascript - Ordered list with random numbers between 1 and 49

912

I have some problems to Code an simple PHP ordered list that print out a random number list between 1 and 49.

The Main Code

<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8">
<title>AJAX</title>
<meta name="viewport" content=
"width=device-width; initial-scale=1.0">
<link rel="stylesheet" type="text/css"
href="lib/css/stil.css" />
<script type="text/javascript"
src="lib/js/script.js"></script>
</head>
<body>
<h1>Ziehung der Lottozahlen</h1>
<button id="frage">Starte die Ziehung</button>
<div id="antwort">
</div>
</body>
</html>

Here the external PHP Code to randomize the numbers

<?php
$zahlen = range (1, 49);
shuffle($zahlen);
for ($i = 0; $i < 6; $i++) {
echo $zahlen[$i] . " ";
 }
?>

And here the Java script

var resOb = new XMLHttpRequest();
window.onload = function() {
document.getElementById("frage").onclick = 
function () {
    resOb.open('get', 'lottozahlen1.php', true);
    resOb.onreadystatechange = function () {
        if (resOb.readyState == 4) {
            document.getElementById("antwort").innerHTML = 
            resOb.responseText;
}
    };
    resOb.send(null);
};
};

T Now my question...how i can let Show the numbers in a ordered list?

386

Answer

Solution:

use PHP code below way

$zahlen = range (1, 49);
shuffle($zahlen);
$arr = array();
for ($i = 0; $i < 6; $i++) {
$arr[$i] =$zahlen[$i] . " ";
 }
 sort($arr,1);
echo implode(' ',$arr);
757

Answer

Solution:

You could split the string, map the values as number and sort the array. Then generate the wanted list items and put it to the wanted unordered list.

var lotto = '22 34 14 10 99',
    list = lotto
        .split(' ')
//        .map(Number)
//        .sort(function (a, b) {
//            return a - b;
//        })
        .reduce(function (list, a) {
            var li = document.createElement('li');
            li.innerHTML = a;
            list.appendChild(li);
            return list;
        }, document.createElement('ol'));

document.body.appendChild(list);



851
votes

Answer

Solution:

Extending Kool-Mind's answer:

$zahlen = range (1, 49);
shuffle($zahlen);
$arr = array();
for ($i = 0; $i < 6; $i++) {
  $arr[$i] =$zahlen[$i] . " ";
}
sort($arr,1);
echo implode(' ',$arr);

Try adding the code below(to generate the list):

<script>
  $('#button').click(function(){
    $('#newDiv').append('<ol>');
    $('#newDiv').append('<?php foreach($arr as $a){echo"<li>".$a."";}?>');
    $('#newDiv').append('</ol>');
  })
</script>

Hope it helps =)

EDIT

This should be working fine now, hope it helps =)

<button id="button">Button</button>
<div id="newDiv"></div>

<script>
  var clicked = false;
  $('#button').click(function(){
    if (!clicked){
      <?php
      $arr = array();
      for ($i = 0; $i < 6; $i++) {
        $arr[$i] = rand(1,49);
      }
      sort($arr,1);
    ?>
    $('#newDiv').append('<ol>');
    $('#newDiv').append('<?php foreach($arr as $a){echo"<li>".$a."";}?>');
    $('#newDiv').append('</ol>');
    clicked = true;
   }
  })
</script>



People are also looking for solutions of the problem: the metadata storage is not up to date, please run the sync-metadata-storage command to fix this issue.
Source

Share


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.


Similar questions

Find the answer in similar questions on our website.

151 javascript - Ordered list with random numbers between 1 and 49
89 PHP attempting to make JavaScript friendly timestamp, failing, returns 1.34899651119E+12
70 php - File not found localhost/phpmyadmin macos sierra 10.12.15
127 javascript - Download links and bookmark in browser action with 1 click
577 javascript - PHP / JQuery - Load data only if clicked in an user on the list
161 php - Python Cryptography: Cannot sign with RSA private key using PKCS1v15 padding
50 Write JavaScript code inside JavaScript and all in PHP?
217 php - Delete all records after 10 mins
162 javascript - insert data to database using ajax and show modal if success with codeigniter
907 php - Laravel 5.1 blade template not working
851

Answer

Solution:

Extending Kool-Mind's answer:

$zahlen = range (1, 49);
shuffle($zahlen);
$arr = array();
for ($i = 0; $i < 6; $i++) {
  $arr[$i] =$zahlen[$i] . " ";
}
sort($arr,1);
echo implode(' ',$arr);

Try adding the code below(to generate the list):

<script>
  $('#button').click(function(){
    $('#newDiv').append('<ol>');
    $('#newDiv').append('<?php foreach($arr as $a){echo"<li>".$a."";}?>');
    $('#newDiv').append('</ol>');
  })
</script>

Hope it helps =)

EDIT

This should be working fine now, hope it helps =)

<button id="button">Button</button>
<div id="newDiv"></div>

<script>
  var clicked = false;
  $('#button').click(function(){
    if (!clicked){
      <?php
      $arr = array();
      for ($i = 0; $i < 6; $i++) {
        $arr[$i] = rand(1,49);
      }
      sort($arr,1);
    ?>
    $('#newDiv').append('<ol>');
    $('#newDiv').append('<?php foreach($arr as $a){echo"<li>".$a."";}?>');
    $('#newDiv').append('</ol>');
    clicked = true;
   }
  })
</script>

People are also looking for solutions to the problem: php - Getting value from query builder select count result in Laravel

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.