2 回答
TA贡献1802条经验 获得超5个赞
你需要从某个地方得到那个故事。有几种方法可以到达那里。
如果要从文本文件加载它:
// Here we sanitize the story ID to avoid getting hacked:
$story_id = preg_replace('#[^a-zA-Z0-9_ -]#', '', $_POST['story']);
// Then we load the text file containing the story:
$path = 'stories/' . $story_id . '.txt';
$story_text = is_file($path) ? file_get_contents($path) : 'No such story!';
如果你想从数据库加载它:
$conn = new mysqli($servername, $username, $password, $dbname);
$stmt = $conn->prepare("SELECT * FROM stories WHERE story_id = ? LIMIT 1");
$stmt->bind_param("s", $_POST['story']);
$stmt->execute();
$result = $stmt->get_result();
$story = $result->fetch_assoc();
$story_text = !empty($story['story_text']) ? $story['story_text'] : 'No such story!';
stories这假设您有一个名为字段story_id和的表story_text。
你也可以有一个单独的文件,将你所有的故事分配到变量中(假设你真的只有几个,加载未使用的故事对性能的影响是最小的):
$stories['Seaside'] = <<<EOL
Here is the seaside story.
EOL;
$stories['Mountain'] = <<<EOL
Here is the mountain story.
EOL;
然后在你的故事文件中,你“加载”它:
$story_text = !empty($stories[$_POST['story']) ? $stories[$_POST['story']] : 'No such story!';
然后(使用上述任何选项),只需:
<?php echo $story_text; ?>
在上述选项中,如果您正在寻找简单且易于维护的东西,我会选择文本文件加载来获取您的故事文本。祝你讲故事好运。:)
假设您想在故事中使用表单变量。您需要使用标记,例如{{ place }},并在输出故事文本之前替换它们:
$story_text = str_replace('{{ name }}', $_POST['name'], $story_text);
$story_text = str_replace('{{ place }}', $_POST['place'], $story_text);
这会将“曾几何时有 {{ name }} 探索 {{ place }}...”变成“曾几何时有 Светослав 探索堪察加半岛...”等。
TA贡献1854条经验 获得超8个赞
...instead of <?php echo $_POST["story"]; ?> I want to print 200+ words story relating to what User has chosen.
使用条件if()或switch语句查看您的 $_POST['story'] 是否已设置并等于您的故事选项之一。
//--> ON story_get.php
//--> (Provided the $_POST variable in fact has the values assigned to the global array)
//--> use var_dump($_POST) on story_get.php to check if the global $_POST array has
//--> key/value pairs coming from your index.php page
// set variables that have your story information.
$seaside = //--> Your story about the sea side
$mountain = //--> Your story about the mountain
$output = null; //--> Empty variable to hold display info from conditional
//--> Now to see if the form element that selects story is set using isset
if(isset($_POST['story']){
//--> Now that we know the input in the form that holds the value for story isset
//--> Check to see if it is set and then declare a variable and assign it to that variable
$story = $_POST['story'];
if($story === 'sea'){
$output = $seaside;
}elseif($story === 'mount'){
$output = $mountain;
}else{
$output = //--> Set a default setting that displays output here if neither story is selected.
}
}
$output在 html 文档中回显您希望显示故事内容的变量
<div>
<?=$output?>
<!--// OR //-->
<?php echo $output; ?>
</div>
- 2 回答
- 0 关注
- 99 浏览
添加回答
举报