【问题标题】:Need to cycle through a sequence of mysql query results需要循环遍历一系列mysql查询结果
【发布时间】:2013-11-27 01:24:59
【问题描述】:

我需要不断循环浏览来自 Kiosk 的 MySQL 表的 14 个查询结果

显示每个结果 10 秒

PHP 或 JS 中的任何示例都会受到赞赏

现在我有 14 个 php 文件,我使用这个 meta http-equiv=\"refresh\" content=\"10; url=ccml2.php\" 循环浏览每个文件的页面都会更改以转到下一页,最后指向第一个。 但我认为必须有一种方法可以使用单个 php 文件来执行此操作。 每个查询都会更改 where on select 以获取需要显示的值。 例如从字段 3 = 值的表中选择字段 1、字段 2。 进行查询,显示结果 10 秒,更改为下一个值,重复。 值可以是 1 到 14

这是第一次尝试,但它没有做我认为应该做的事情

$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if (mysqli_connect_errno()) {
  printf("Connect failed: %s\n", mysqli_connect_error());
  exit();
}
$query = sprintf("SELECT ClassName, ClassId FROM class");
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
while($row = $result->fetch_assoc())
  {
  $ICName[]=$row['ClassName'];  //gets 14 Class Names
  $IC_Id[] = $row['ClassId']; // gets matching ID 1 to 14
  }
for($x=0;$x<$result->num_rows;$x++)
  {
    echo "<html>";
    echo "<body>";
    echo "<head>";
    echo "<Title>Cactus Combat Match League</Title>";
    echo "<link type=\"text/css\" rel=\"stylesheet\" href=\"ccml.css\" media=\"all\" />";
    echo "</head>";
    echo "<img width=\"1280\" height=\"190\" src=\"images/Banner.png\"></img>";
    echo "<p></p>";
    echo "<p></p>";
    echo "<h2>Thursday Night Match Scores 11/14/2013</h2>";
    echo "<table border=\"1\" width=\"1200\" align=\"center\">";
    echo "<tr>";
    echo "<th class=\"th1\" colspan = \"8\">".$ICName[$x]."</th>"; // Display Class Name
    echo "</tr>";
    echo "<tr>";
    echo "<th class=\"th2\">#</th>";
    echo "<th class=\"th2\">Name</th>";
    echo "<th class=\"th2\">Stage 1</th>";
    echo "<th class=\"th2\">Stage 2</th>";
    echo "<th class=\"th2\">Stage 3</th>";
    echo "<th class=\"th2\">Stage 4</th>";
    echo "<th class=\"th2\">Stage 5</th>";
    echo "<th class=\"th2\">TOTAL</th>";
    echo "</tr>";  // following gets results for this Class
    $query1 = sprintf("SELECT mid, SName, Stage1, Stage2, Stage3, Stage4, Stage5, Total FROM shooter WHERE cid=".$IC_Id[$x]." AND mdate='2013-11-14' ORDER BY Total");
    $result1 = $mysqli->query($query1) or die($mysqli->error.__LINE__);
    while ($row1 = $result1->fetch_assoc()) // Display Results
      {
      echo "<tr>";
      echo "<td class=\"t1\">".$row1['mid']."</td>";
      echo "<td class=\"t3\">".$row1['SName']."</td>";
      echo "<td class=\"t2\">".number_format((float)$row1['Stage1'], 2, '.', '')."</td>";
      echo "<td class=\"t2\">".number_format((float)$row1['Stage2'], 2, '.', '')."</td>";
      echo "<td class=\"t2\">".number_format((float)$row1['Stage3'], 2, '.', '')."</td>";
      echo "<td class=\"t2\">".number_format((float)$row1['Stage4'], 2, '.', '')."</td>";
      echo "<td class=\"t2\">".number_format((float)$row1['Stage5'], 2, '.', '')."</td>";
      echo "<td class=\"t4\">".number_format((float)$row1['Total'], 2, '.', '')."</td>";
      echo "</tr>";
      }
    echo "</table>";
    echo "</body>";
    echo"</html>";
    sleep(1);
  }
mysqli_close($mysqli);
?>

【问题讨论】:

  • 请给我们更多细节..
  • 分享你现有的代码会有帮助
  • 回复的那两个怎么回答?
  • 现在我有 14 个 php 文件,我用这个 页面更改each 转到下一页,最后一个指向第一个。但我认为必须有一种方法可以使用单个 php 文件来执行此操作。每个查询都会更改 where on select 以获取需要显示的值。例如从字段 3 = 值的表中选择字段 1、字段 2。进行查询,显示结果 10 秒,更改为下一个值,重复。值可以是 1 到 14。
  • 希望这有助于让它停止

标签: javascript php mysql


【解决方案1】:

我想我找到了一个更优雅的解决方案。

问题:射击比赛的顺序显示结果(由25个班级的结果组成),其次是按比赛时间顺序显示的射手数量,然后是比赛的每个阶段的前三名射手。结果存储在 MySQL 数据库中)。

每个结果都按比赛日期过滤。

用每个班级的开始、排名、部分排名、阶段和比赛日期初始化一个表格

如果该类别中有射手,则首先显示每个类别 5 秒,如果没有,则显示 1 秒。 然后一次显示射手 12 的排名,直到完成。 然后显示每个阶段的前三名射手。

我将只显示第一个的代码(显示每类射手的结果)

可能有一种更优雅的方法可以做到这一点,但这很有效。 其他两个大致相同,只是数据和表格不同。 代码开始

// What Class to Deal with and What Match Date to Display
$query = sprintf("SELECT class_cnt, thur FROM CCnt");
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_row($result))
      {
      $Class_Id = $row[0];              // Set the Class ID from the database
      $rdate=row[1];            // Set the Date of the Match to Display
      }
mysqli_free_result($result);

// Format Display Date in M-D-Y format
$pieces = explode("-", $rdate);
$ddate = $pieces[1]."/".$pieces[2]."/".$pieces[0];

// Get the Name of the Class
$query = sprintf("SELECT ClassName FROM class WHERE ClassId=%d", $Class_Id);
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_row($result))
      {
      $ICName = $row[0];
      }
mysqli_free_result($result);

// Select Data to Display
$query = sprintf("SELECT mdate, cid, mid, SName, Stage1, Stage2, Stage3, Stage4, Stage5, Total FROM shooter WHERE cid=%d AND mdate='%s' ORDER BY Total", $Class_Id, $rdate);
$result = mysqli_query($link, $query);
// Adjust refresh time based on if there are Results
$row_cnt = mysqli_num_rows($result);
if ($row_cnt > 0)
    $dtime = 5;    // 5 deconds if results
else
    $dtime = 1;    // 1 second if none

// Start of HTML Output
echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>";
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">";
echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">";
echo "<head>";
printf("<Title>Cactus Combat %s Results</Title>", $ICName );                           // Title the page
echo "<link type=\"text/css\" rel=\"stylesheet\" href=\"ccml.css\" media=\"all\" />";  // Get Style file
// Check to see what Class to display
switch ($Class_Id)
  {
  case 1:
  case 2:
  case 3:
  case 4:
  case 5:
  case 6:
  case 7:
  case 8:
  case 9:
  case 10:
  case 11:
  case 12:
  case 13:
  case 14:
  case 15:
  case 16:
  case 17:
  case 18:
  case 19:
  case 20:
  case 21:
  case 22:
  case 23:
  case 24:
  printf("<meta http-equiv=\"refresh\" content=\"%d; url=ccml1.php\">", $dtime);       // Class ID = 1 - 24 Refresh to same page
  break;
  case 25:
  printf("<meta http-equiv=\"refresh\" content=\"%d; url=ccml2.php\">", $dtime);       // Class ID = 25 Set to Next Page
  break;
  }
$Class_Id++;                                 // Increment the Class ID
echo "</head>";
echo "<body>";
echo "<img width=\"1280\" height=\"190\" src=\"images/Banner.png\"></img>";            // Display the Banner
echo "<p></p>";
echo "<p></p>";
echo "<h2>Thursday Night Match Scores ".$ddate."</h2>";// Display the Date of the Match
echo "<table>";
echo "<tr>";
echo "<th class=\"th1\" colspan = \"8\">".$ICName."</th>";                             // Display Which Class these Results are
echo "</tr>";
echo "<tr>";                                                                           // Output Table Headers
echo "<th class=\"th2\">#</th>";
echo "<th class=\"th3\">Name</th>";
echo "<th class=\"th4\">Stage 1</th>";
echo "<th class=\"th4\">Stage 2</th>";
echo "<th class=\"th4\">Stage 3</th>";
echo "<th class=\"th4\">Stage 4</th>";
echo "<th class=\"th4\">Stage 5</th>";
echo "<th class=\"th4\">TOTAL</th>";
echo "</tr>";

// Put Data into HTML Table Rows
while ($row = mysqli_fetch_row($result))
  {
  echo "<tr>";                                                                         // Output Data Row
  printf("<td class=\"t1\">&nbsp;%d</td>",$row[2]);                //Match ID
  printf("<td class=\"t3\">&nbsp;%s</td>",$row[3]);                // Name
  printf("<td class=\"t2\"> %6.2f &nbsp;</td>",$row[4]);           //Stage 1 Time
  printf("<td class=\"t2\"> %6.2f &nbsp;</td>",$row[5]);           //Stage 2 Time
  printf("<td class=\"t2\"> %6.2f &nbsp;</td>",$row[6]);           //Stage 3 Time
  printf("<td class=\"t2\"> %6.2f &nbsp;</td>",$row[7]);           //Stage 4 Time
  printf("<td class=\"t2\"> %6.2f &nbsp;</td>",$row[8]);           //Stage 5 Time
  printf("<td class=\"t4\"> %6.2f &nbsp;</td>",$row[9]);           //Stage Total Time
  echo "</tr>";
  }
echo "</table>";
echo "</body>";
echo"</html>";
mysqli_free_result($result);

// Update Class to Display in database
if ($Class_Id > 25)     // Check to see if we are done IF Yes reset the Class ID
  $Class_Id = 1;
$UpdQuery = sprintf("UPDATE CCnt SET class_cnt = %d", $Class_Id);// Update Class ID
mysqli_query($link, $UpdQuery);

mysqli_close($link);
?>

【讨论】:

    猜你喜欢
    • 2021-02-15
    • 1970-01-01
    • 2015-05-09
    • 1970-01-01
    • 1970-01-01
    • 2015-10-05
    • 2011-12-02
    • 2020-04-13
    • 1970-01-01
    相关资源
    最近更新 更多