How to add the output of a php-function to a list
所以我正在尝试编写代码来做以下非常基本的事情:
*从 html 输入表单中取一个数字
*取一个计数器,从0循环到输入数字
*在 php 输出中将数字作为列表项获取,除非它可以被 3 或 5 整除,它们分别获取 ping 和 pong 作为输出值。
目前,可悲的是,这不起作用,我得到的只是tite
- PHP 101:+ 用于数学加法。 .。用于连接。 $x = ‘a’ + ‘b’ 将最终成为 0。而且您的 php 代码似乎与 javascript 混合在一起,这是完全非法的语法。
-
有用的提示:您说您收到一条错误消息 – 告诉我们它是什么!
-
@lolcat 请也给答案投票
第一个问题:- form 方法是 POST 并且您使用 $_GET 来检索第二页中的值。更改代码如下:-
$inputNumber = $_GET[“inputNumber”]; 到 $inputNumber = $_POST[“inputNumber”];
第二个是:- $outputMessage =”
<MMKG1>
“+ $outputMessage +”
<MMKGX1>
“; 必须是 $outputMessage =”
<MMKG1>
“.$outputMessage.”
<MMKGX1>
“;
第三个是:- “#outputList”.append ($outputMessage) ; 似乎您正在将 javascript 代码与 php 代码混合。不要那样做。
所以您的第二页代码必须如下所示:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | <?php
$inputNumber = $_POST["inputNumber"];
function colorFate($inputNumber) { $new_array = array(); $outputMessage =""; if ($inputNumber < 100){ for ( $counter = 1; $counter <= $inputNumber; $counter++) { if ($counter % 5 === 0 && $counter % 3 === 0){ $new_array[‘Ping-Pong’][$counter] = $counter; }else if ($counter % 3 === 0){ $new_array[‘Ping’][$counter] = $counter; }else if ($counter % 5 === 0){ $new_array[‘Pong’][$counter] = $counter; }else if ($counter % 5 != 0){ $new_array[‘nothing’][$counter] = $counter; } } return $new_array; } else{ alert("Please follow the guidelines on what to enter!"); } } ?> <!DOCTYPE html> <html> <head> PHPingPong </head> <body> Read and Weep! <?php $result = colorFate($inputNumber);?> <?php foreach($result as $key=>$value){ echo" <ul> <span>$key numbers are:-".implode(‘,’,$value)." </ul> "; }?> </body> </html> |
- 我将项目添加到列表中的糟糕尝试确实被类似的 javascript 项目蚕食了。我如何仍然在 php 中使用列表结构?
- 如果给定数字 10,您能说出您想要的结果吗,因为我添加了它,并且根据您的情况,它给出了正确的值 PongPong。或者你用任何数字告诉任何结果。
- 我这样做了
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/268489.html