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">
<meta charset="utf-8">
<title>AJAX</title>
<meta name="viewport" content=
"width=device-width; initial-scale=1.0">
href="lib/css/stil.css" />
<script type="text/javascript"
src="lib/js/script.js"></script>
<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();
document.getElementById("frage").onclick =
function () {
resOb.open('get', 'lottozahlen1.php', true);
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

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

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

Solution:

``````\$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

Similar questions

Find the answer in similar questions on our website.

851

Solution:

``````\$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