1

以前に(いつものように)尋ねられた他の質問で解決策を探してみましたが、これに頭を悩ませているようには見えません。

ほら、1つのテーブルから(ランダムな順序で)多数の一意のIDを取得し、それらをエコーせずに配列に格納したいと思います。次に、その配列変数をループで使用して、パスごとにキーをインクリメントし、別の変数をその配列変数に設定できるようにします。紛らわしい?コードを見ると、より明確になると思います。

問題は、クエリした値を後でコードで使用するために配列に格納できないように見えることです。コメント/** /タグで示される問題のスポットを使用して、コードの適切な部分を貼り付けました。

どんな助けでも大歓迎です。

<?php

include ('parse_functions.php');

if ($fetch['use_rand']=='yes')

{ $loop = 5;    
$concept = $fetch['concept'];
$countRandom = "SELECT exID FROM examples WHERE concept='$concept' ORDER BY RAND()";
$askForRandom = mysql_query($countRandom) or die(mysql_error());
/* HERE I NEED TO STORE RANDOM KEYS (exID) INTO AN ARRAY */ }

else

{  if (!empty($fetch['ex5'])) { $loop = 5; }
 elseif (!empty($fetch['ex4'])) { $loop = 4; }
 elseif (!empty($fetch['ex3'])) { $loop = 3; }
 elseif (!empty($fetch['ex2'])) { $loop = 2; }
 elseif (!empty($fetch['ex1'])) { $loop = 1; }
 else { $loop = 0; }

    }

     if ($loop!==0)

      {

         echo '<div id="examples">' . "\n";
         echo '<table class="showExample" cellspacing="0" cellpadding="0" border="0" align="center">' . "\n";

         $turns = 1;

             do {

             if ($fetch['use_rand']=='no')

              { $exID = $fetch['ex'.$turns.'']; }

             else

              { $exID = /* THIS IS WHERE I WILL USE "RANDOM VARIABLE" */; }

             $askExamples = "SELECT * FROM examples WHERE exID='$exID'";
             $getExamples = mysql_query($askExamples) or die(mysql_error());
             $sortExamples = mysql_fetch_assoc($getExamples);

             echo '<tr>' . "\n";

// ...and so on
4

2 に答える 2

0

次のような行に沿って、最初から1つのクエリですべての適切な情報を取得してみませんか。

select 
    a.exID,
    examples.someField,
    examples.someOtherField
from 
    examples
        join
        (
            SELECT 
                exID 
            FROM 
                examples 
            WHERE 
                concept='$concept' 
            ORDER BY 
                RAND() 
            limit 5
        ) a
            on a.exID=examples.exID

次に、それらを配列(またはより適切にはオブジェクト)にポップするだけで、毎回1行にすべての関連情報が含まれます。

于 2012-08-05T00:54:56.473 に答える
0

情報を配列に格納するだけの場合は、簡単な方法があります。

/* code 
to open
db here
*/

/* assuming you have a value for $concept */

$countRandom = "SELECT exID FROM examples WHERE concept='$concept' ORDER BY RAND()";
$askForRandom = mysql_query($countRandom) or die(mysql_error());

$Element = 0; //Array elements start at ZERO. So this is to intialise it.
while ($Fields = mysql_fetch_array($askforRandom)) //As long as there are records, get them.
    {
    //Records are retrieved one at a time. So store each one's exID in the array element
    $Data[$Element] = $Fields["exID"];
    //Soon after storing one, increment the value of the $Element variable so it is ready for the next one.
    $Element++
    }

/* now you have the data in the array. So you can do what you like with it. */

それが役に立てば幸い

注:配列を読み取るときは、カウンターが$Elementよりも小さいことを確認する条件を設定してください。これは、最後のレコードを読み取った後、$Elementが1つインクリメントされるためです。これが明確であることを願っています。

于 2012-08-05T00:56:18.210 に答える