doukun1450 2018-12-14 16:35
浏览 117

如何从.txt文件中显示随机生成的文本和图像?

Here is my code that generates random texts from the random.txt file inside a input field.

<?php
 //Path of file
 $myFile = "random.txt";
 //Read file from array
 $lines = file($myFile);
 //Get number line of file
 $lineTotal = count($lines);
 //Remove 1 line (start from 0)
 $count = $lineTotal-1;
 //Get casual number
 $number_casual = rand(0,$count);
 //Print line 2 ([0] = 1 ; [1] = 2 ; ...)
?>
<input name="accesspin" style="width: 300px" value="<?php echo htmlentities( $lines[$number_casual] ); ?>" size="36">

Here is the random.txt file:

USA
UK
Canada

I want to show random pictures above the input field:

<img src="A random pic"alt="Flags" height="42"width="42">

<input name="accesspin" style="width: 300px" value="<?php echo htmlentities( $lines[$number_casual] ); ?>" size="36">

And the random.txt will look like this:

us_flag.jpg
USA
uk_flag.jpg
UK
ca_flag.jpg
Canada

I want this PHP code to randomly show a flag picture above the input field and the country name under the flag picture's file name in the txt file to show inside the input field.

  • 写回答

1条回答 默认 最新

  • dtc66318 2018-12-14 17:49
    关注
    <?php
     srand(time(null));
     //Path of file
     $myFile = "random.txt";
     //Read file from array
     $lines = file($myFile);
     //Get number line of file
     $lineTotal = count($lines);
     //Remove 1 line (start from 0)
     $count = $lineTotal-1;
     //Get casual number
     $number_casual = rand(0,$count);
     if($number_casual%2==0){
        $value = htmlentities( $lines[$number_casual+1] );
        $image = htmlentities( $lines[$number_casual] );
     }else{
        $value = htmlentities( $lines[$number_casual] );
        $image = htmlentities( $lines[$number_casual-1] );
     }
     //Print line 2 ([0] = 1 ; [1] = 2 ; ...)
    ?>
    <input name="accesspin" style="width: 300px" value="<?php echo $value; ?>" size="36">
    <img src="<?php echo $image; ?>"alt="Flags" height="42"width="42">
    
    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染