duandu8202
2014-12-29 21:22 阅读 24
已采纳

PHP半随机字生成器[关闭]

The following code is working. But I'd like to improve it. Basically, this page displays a random str per each categories and reloads a page when a user pushes a button. Can it be different so I could manage the data easier(I want to update the words library frequently). I also wonder if this is short enough for quick loading. Thank you so much for your help in advance.

<!-- begin -->
<html>
<head>
</head>

<body>


<?php 
#hash = random url generated from 128 chars


$words = array('apple', 'orange', 'banana'); 
$rword = rand(0,2);

$syms = array('chair', 'sofa', 'table', 'coffee table');
$rsym = rand(0,3); 

$value1 = $words[$rword];
$final1 = strtoupper($value1);

$value2 = $syms[$rsym];
$final2 = strtoupper($value2);

?>


<h2>Fruit</h2><p><?php echo $final1 = strtolower($value1); ?></p>
<h2>Furniture</h2><p><?php echo $final2 = strtolower($value2); ?></p>


<FORM METHOD="LINK" ACTION="random.php">
<INPUT TYPE="submit" VALUE="generate again">
</FORM>



</body>
</html>

<!-- end -->
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dqmdlo9674 dqmdlo9674 2014-12-29 22:01

    I would create a Database with 2 tables.

    Table 1 would be Categories: This would contain Fruit,Furniture,Rivers,etc.

    Table 2 would be Items: This would contain chair,apple,sofa,orange,etc. all associated to the correct Categories id.

    You could then query for ALL categories and return that into a full array. You could then foreach your way through the array of categories and do another query set to an array of items. Once you do that you can get the length of items and do much the same as you are doing.

    This is a descriptive answer to give you an idea that you should move to. Without knowledge of your exact idea and structure set I do not want to risk writing faulty code.

    点赞 评论 复制链接分享

相关推荐