我不退出如何从特定的键号回显两个项目(单独)。最后,我希望能够保留当前的随机函数,但是能够在需要时通过类似index.php?key=3
的网址从数组中获取一组特定的值 - 让我们假设当访问者随机化时“ rzU_fLcxIN0“并希望与某人分享,然后他会继续查看具有相应值的网址index.php?key=3
,并可以通过按钮随机化新值来继续。
也许我问了很多,但是一旦理解了要使用的部分(以及如何使用),感觉这应该是复杂的。我是新手,所以关于如何解决这个问题的所有帮助和指示对我都有用!
更新 我会尝试更好地解释随机问题和非随机问题。标准行为是向访问者显示随机内容,但访问者可以与他人共享特定帖子。 想象一下以下步骤,因为“访客”访问该网站;
index.php?key=3
。key=3
中的内容。的index.php
<a class="randomizerButton" data-href="data.php">Randomize</a>
<hr>
<div id="results">
<?php include('data.php'); ?>
</div>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('a.randomizerButton').click(function(){
scriptUrl = $(this).attr('data-href');
$.post(scriptUrl, function(response){
$('#results').html(response);
});
});
});
</script>
data.php
<?php
$var = array(
//1,2,3,4... etc. are unique IDs
1 => array("First", "0wLljngvrpw"),
2 => array("Second", "TINASKjjNfw"),
3 => array("Third", "rzU_fLcxIN0"),
4 => array("Fourth", "aZZ8UnCTVJA"),
);
$finalVar = $var[array_rand($var)];
echo $finalVar[0].'<br/>'.$finalVar[1];
echo ('<hr>');
// I've started out with things like the one below but I don't know how to output the values from a multidimensional array,
// Didn't manage to find a helpful resource on the problem, mainly since I don't know if this is the correct way to approach this.
$key='2';
echo $var[$key]; //outputs Array, need to go one level deeper I guess?
// Any pointers on how to load this specific value while maintaining the random button function as well, I'd be very grateful.
// $_GET["key"]...?
// loop array...?
?>
答案 0 :(得分:0)
为了更好地解释我的评论,对于固定与随机,这是一个逻辑条件,为此我们使用if语句。
考虑您有网址index.php?key=3
,我们只需在$_GET
if( isset( $_GET['key'] ) && isset( $var[$_GET['key']] ) ){
$finalVar = $var[$_GET['key']];
} else {
$finalVar = $var[array_rand($var)];
}
当有人随意输入输入时,只需执行
header('location: index.php');
exit();
清除网址。或者来自Javascript
window.location.href = 'index.php';
这将在if
上失败,并在条件的else
部分执行,这将使用随机密钥访问数组,否则如果key
在url查询中在数组$var
中,他们将获得该项目。