用之前的结果定义变量,还有更好的方法吗?
Defining A Variable With the Result of the Previous, And Are There Better Ways To Go About It?
我正在编写一个网页,它生成一个博客帖子列表,以及相关数据(如时间戳和文章预览),并且我试图为每个博客帖子生成一个惟一的变量,该变量增量为1。
为了做到这一点,我在第59行创建了一个名为"name"的变量,它将接受字符串"dtartnum"并将其与for循环的整数I(最大为I)或变量$sqlindex(这是数据库中文章数量的索引)连接起来。
在第60行,我想用变量名的结果定义一个变量,并将$sqlindex的值赋给它(每次定义时,result_of_name只是name结果的占位符)。这样做的原因(基于我读过的关于Stack Overflow的其他文章)是PHP只在页面加载期间执行(大致)。
在这行逻辑中,这意味着PHP变量将变得不可访问,并且必须在PHP生成页面时生成过程并存储在本地。
稍后,我计划使用我发现的AJAX库将变量发布到一个页面(该页面尚未创建),该页面将充当MySQL数据库中行的"id"(这也是PRIMARY KEY)(因此我可以召回该页的行和相关信息)。在另一个页面上,我将有一个模板和一个Fill-In-The-Blank MySQL查询,等待我在上面粘贴的页面中的行"id"。
通过Stack Overflow,我在Bash中发现了类似的东西,然而,由于语言在语法和功能上的不同,我在JS中找不到太多的应用程序。我找不到这样的东西更接近JS。
总之,我的问题是:
我如何用变量名称的值定义"name"下的变量?即if name = "potato";那么下面的变量就是potato = $sqlindex;
和扩展名
有更好的方法来做这件事吗?在结构上有什么可以改进的吗?请让我知道,这样我可以在未来的项目中使用它。
所讨论的代码位于HEREDOC的底部,在被注释掉的代码块上方。
如果您需要我补充什么,请告诉我。
代码:
<?php
$conn = new mysqli($servername, $username, $password, $db);
//check Connection
if ($conn->connect_error){
die("ERROR: ". $conn->connect_error);
}
else{
//nothing, essentially
}
$rowcountsql= "SELECT id FROM writeups ORDER BY id;";
if ($result=$conn->query($rowcountsql))
{
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result);
}
for ($sqlindex = 0; $sqlindex <= $rowcount; $sqlindex++){
if ($result = $conn->query("SELECT * FROM writeups LIMIT ".$sqlindex.", 1;")){
if ($count = $result->num_rows){
// echo "The row count for this query is ",$count,"<br>";
while($row = $result->fetch_object()){
$titlecontent = $row->title;
$writeupcontent = $row->body;
$rowdateandtime = $row->dateandtime;
$article_number = $sqlindex + 1;
echo $str=<<<ARTICLE
<div class="article">
<div class="titleanddate">
<h2>$titlecontent</h2>
<h3 class="dtartnum">Written $rowdateandtime, Article Number $article_number</h3>
</div>
<div class="truncate">
<p class="writeup">
$writeupcontent
</p>
<a href="templatepage.php" onmouseenter="assignCookie()">Read More...</a>
<script>
function assignCookie(){
//total nr in list
window.alert(document.getElementsByClassName("article").length);
//pos in list and definition of variable per button
for (i=0; i<=sqlindex;i++){
var name = "dtartnum"+i;
var result_of_name = $sqlindex;
window.alert(result_of_name);//how do we get this to work?
}
/* This code will be developed later, I suppose... need to fix the above first
window.alert(dtartnum);
var pattern = /Article Number 'd{1,3}/;
if(pattern.test(document.getElementsByClassName(dtartnum).value)){
window.alert(pattern.test(document.getElementsByClassName(dtartnum).value));
}
else{
window.alert(getElementsByClassName(dtartnum).value);
}*/
}
</script>
</div>
</div>
ARTICLE;
}
}
}
}
?>
这个应该可以…
var name = "potato";
this[name] = $sqlindex;
alert(potato);
- 在JavaScript中拆分日期字符串的更好方法是什么
- 设置嵌套对象属性的更好方法
- 用Javascript重新格式化复杂文本日期字符串的更好方法
- 什么'这是从第三个函数上的async 1st函数获得结果的更好方法
- 将数组从javascript格式化为php的更好方法,反之亦然
- jquery:将动画绑定到滚动条位置的更好方法
- 使用较少代码隐藏和显示选择菜单内容的更好方法是什么?javascript
- 编写if-else语句的更好方法
- 传递promise回调方法的更好方法
- 在Knockoutjs中设置计算对象的observableArray的更好方法
- 从网页获取数据的更好方法,而不是使用DOM和HTML元素
- Backbone js代码气味-嵌入子视图的更好方法
- 从数组中删除项的更好方法
- 对多维数组进行分组的更好方法
- 在node.js和express中基于路径运行不同数据库查询的更好方法
- 动态更改测试中代码覆盖率的require语句的更好方法
- 设计具有数据持久性的web应用程序的更好方法
- 选择表行的更好方法
- 在nodejs服务器上提供文件的更好方法,而不是if/else
- Javascript;Ajax——填充对象的更好方法