ループに関する多くの解決策を確認しましたが、私の問題には適用できません。ただし、ループについてはまだ混乱しています。乱数を生成し、生成された数値がテーブル列の内容と等しいかどうかを確認したいのですが、同じ値がなくなるまで再度生成されます。
皆さんが私を助けてくれることを願っています。
「リスト」と呼ばれるデータベースにテーブルがあり、idとimgの構造があり、 値が含まれています
リスト表:
id | img
1 | 1
2 | 2
3 | 3
4 | 4
number.php
$new_array = array();
$query = "SELECT img FROM list";
$query = mysql_query($query);
while ($query_get = mysql_fetch_array($query)) {
$new_array[] = $query_get[0]; //Store img values into an array
}
$rand = rand(1,5); //Generates random number 1 to 5
$search_r = array_search($rand, $new_array); //Checks if the random number has the same value to the array
while($search == "") { //Until random number gets number 5 it would stop looping
$rand = rand(1,5);
}
echo $rand; //and echo the number 5 here.
それでも結果は私のブラウザでノンストップの読み込み画面です
さて、乱数と配列が同じ値を持つかどうかを判断するためのこのコードがあります。しかし、それは1つのループでしか得意ではありません。
if (array_search($rand,$new_array)) {
echo "random number has the same value inside the array" . $rand;
} else {
echo "random number does not have the same value inside an array" . $rand;
}
簡単に言うと、テーブルimg の値は 1、2、3、および 4で、1 から 5 までの数字を生成しています。生成された数値が 5 を出力するまで、それは echo になります。生成された数値が1 から 4 になると、 5 になるまで再び生成されます。