如何将implode变量添加到各种文本框值

时间:2015-08-14 20:33:38

标签: php arrays string forms implode

我有以下三页代码。第一页询问用户他们喜欢哪种音乐类型(最终将有20多种类型)。第二页要求用户对他们选择的类型进行排名,第三页排序并显示他们的排名。

第一页要求用户选择他们喜欢的类型:

<form id="genre" name="genre" method="post" action="musicsell.php">
  <input type="checkbox" name="genre[]" id="Rap" value="Rap"/>Rap<br />
  <input type="checkbox" name="genre[]" id="HipHop" value="HipHop"/>HipHop<br />
  <input type="checkbox" name="genre[]" id="RnB" value="RnB"/>RnB<br />
  <input type="checkbox" name="genre[]" id="Rock" value="Rock"/>Rock<br />
  <input type="checkbox" name="genre[]" id="Jazz"value="Jazz"/>Jazz<br />

  <p>
    <input type="submit" value="Next">
    <br />
  </p>
</form>

第二个要求他们对他们选择的类型进行排名(优先排序),其中1是最好的:

<form id="form1" name="form1" method="post" action="musicresults.php">
<?php
$name = $_POST['genre'];

if(isset($_POST['genre'])) {
foreach ($name as $genre){

?>

<input type="number" required="required" id="<?php echo $genre ?>" name="music[<?php echo $genre ?>]" max="3" min="1" /><?php echo $genre ?><br /> 

<?php
    }
} 
?>

<input type="submit" name="button" id="button" value="Submit" /></form>

第三页和最后一页对结果进行排序和回声:

<?php
//Get the form results (which has been converted to an associative array) from the $_POST super global
$musicgenres = $_POST['music'];

//Sort the values by rank and keep the key associations.
asort($musicgenres, SORT_NUMERIC );

//Loop over the array in rank order to print out the values.
foreach($musicgenres as $music => $rank)
{
   echo "$music is your $rank choice";
   echo "<br>";
}
?>

结果的一个例子:

说唱是你的第一选择 HipHop是您的2选择
RnB是您的第3选择

但是,如何将一组单独的代码放到最后一页,我可以回显一个变量(例如echo "musicstring = $musicstring";),它会显示如下结果:

  

musicstring = Rap,HipHop,RnB

我认为需要内爆功能,但我不确定。

2 个答案:

答案 0 :(得分:2)

是的, public void ProcessOrders(){ synchronized (this) { if(IS_Process_RUNNING){ logger.error("ProcessOrders() is already running."); return; } IS_Process_RUNNING = TRUE; } try{ DoSomeWork(); } catch(Exception e){ LogFatalError.logFatalError("Error processing orders.....", e); } finally{ IS_Process_RUNNING = FALSE; } 就是您所需要的:

implode

答案 1 :(得分:0)

Edited: Yes, implode function will work for you.

The implode() function returns a string from the elements of an array.

Syntax: implode(separator,array)

find implode() documentation here: http://www.w3schools.com/php/func_string_implode.asp

update the foreach function like this:

foreach($musicgenres as $music => $rank)
{
   array_push($musicstring, $music);   
   echo "$music is your $rank choice";
   echo "<br>";
}

and then, You may have to add following code in last:

 $musicstring = implode(", ", $musicgenres);
 echo $musicstring;

Hope this helps!