简体   繁体   English

php 中的选择选项

[英]Selection option in php

I am working on a PHP quiz game and at the moment i am struggling a bit mixing php and html together as i have more previous experience in html. I am working on a PHP quiz game and at the moment i am struggling a bit mixing php and html together as i have more previous experience in html. I have the following select option for quiz selection:我有以下 select 选项用于测验选择:

<select style="width: 300px" name="quizes" size="10">
  <option onClick="window.location = 'quiz1.php'" >Quiz 1</option>
  <option>Quiz 2</option>
  <option>Quiz 3</option>
  <option>Quiz 4</option>
  <option>Quiz 5</option>
</select>

I was told that this can be made with php code so that the new quizes are added onto it when created.有人告诉我,这可以用 php 代码制作,以便在创建时将新测验添加到上面。 Can somebody help me out with this, i dont quite understand how this would work.有人可以帮我解决这个问题,我不太明白这是如何工作的。

I am not using database for my quizes, each quiz is/will be on one page, with the questions stored in array and the processing of the questions is on the same page.我没有为我的测验使用数据库,每个测验都在/将在一页上,问题存储在数组中,问题的处理在同一页上。

Try this, added fire event in <select>试试这个,在<select>中添加火灾事件

<select style="width: 300px" name="quizes" size="10" onChange="window.location = 'quiz1.php?val='+this.value;">
  <option value='Quiz 1'>Quiz 1</option>
  <option value='Quiz 2'>Quiz 2</option>
  <option  value='Quiz 3' >Quiz 3</option>
  <option  value='Quiz 4'>Quiz 4</option>
  <option  value='Quiz 5'>Quiz 5</option>
</select>

Try this.尝试这个。 It presumes that the page where the selection is made, is in the same directory as your quiz php pages.它假定进行选择的页面与您的测验 php 页面位于同一目录中。

<?php
$dir    = getcwd();
$files = scandir($dir);

foreach($files as $file)
{
    if (strpos($file,'quiz') !== false)
    {
        $fileName = ucfirst(str_replace('.php', '', $file));
        $quizPages[] = array('name'=>$fileName,'file'=>$file);
    } 
}
?>

<select style="width: 300px" name="quizes" size="10">
<?php
foreach($quizPages as $quiz)
{
    echo '<option name="' . $quiz["name"] . '" onClick="window.location = \'' . $quiz["file"] . '\'" >' . $quiz["name"] . '</option>';
}
?>
</select>

Update for comment.更新评论。

This is likely due to the fact you have a file called Quizes in that directory also, which having the word quiz in its being caught in the foreach loop.这可能是因为您在该目录中还有一个名为 Quizes 的文件,其中包含单词 quiz 被 foreach 循环捕获。 What I would suggest is to change this line我的建议是改变这条线

if (strpos($file,'quiz') !== false)

To this对此

if (strpos($file,'quiz') !== false && strpos($file,'quizes') === false)

Further update.进一步更新。 If you want to have a custom name for the quiz I would suggest you use the following naming convention quiz_desired_name.php.如果您想为测验自定义名称,我建议您使用以下命名约定 quiz_desired_name.php。 So for example If I have a quiz called what animal are you.例如,如果我有一个测验叫你是什么动物。 Then the filename should be quiz_what_animal_are_you.php therefore separating words with underscores.那么文件名应该是 quiz_what_animal_are_you.php 因此用下划线分隔单词。

Then just replace this line然后只需替换此行

$fileName = ucfirst(str_replace('.php', '', $file));

with this有了这个

$fileName = ucfirst(str_replace(array('.php','quiz_','_'), array('','',' '), $file));

if you want to use html tags inside php,add an如果您想在 php 中使用 html 标签,请添加

eg:例如:

 echo"<select>";

before every html tag在每个 html 标签之前

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM