douxin2011 2013-07-13 15:21
浏览 27
已采纳

PHP随机文本一次

I've been looking for the solution of my problem for ages and I still haven't found it so i desided to create a stackoverflow account to ask the question myself.

this is what I created so far:

<? //Chooses a random number $num = Rand (1,2);
switch ($num){
case 1: $retrieved_data = "test"; break;
case 2: $retrieved_data = "test1"; break;
} ?> 

And this is the place where I want the text to appear;

<form enctype="multipart/form-data" action="upload.php" method="POST">
<input name="<?php echo $retrieved_data; ?>" type="file" />
<input name="<?php echo $retrieved_data; ?>" type="file" />
<input type="submit" value="Upload" /></form>

My problem is I want to have the "test1" and "test" randomly displayed on these places but I want them to be different from eachother every time. So if the first input type is "test" I want the second input type to be 'test1" but I can't seem to get this working

Does anyone know what I'm doing wrong or a code to make this possible?

  • 写回答

3条回答 默认 最新

  • dqpu4988 2013-07-13 15:35
    关注

    If you have limited number of possibilities, put them int an array, shuffle it and shift/pop elements from it:

    $retrieved_data = array('test', 'test1');
    shuffle($retrieved_data);
    
    $random1 = array_shift($retrieved_data);
    $random2 = array_shift($retrieved_data);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路