javascript - 用前面的结果定义一个变量,有更好的方法吗?

标签 javascript php mysql

我正在编写一个生成博客文章列表以及相关数据(例如时间戳和文章预览)的网页,并且我正在尝试为每个博客文章生成一个唯一的变量,该变量递增一。

为了做到这一点,我在第 59 行创建了一个名为“name”的变量,它将获取字符串“dtartnum”并将其与 for 循环的整数 i 连接起来,最大为 i,或变量 $sqlindex(这是数据库中文章数量的索引)。

在第 60 行,我有一个变量,我想用变量名的结果来定义它,并将 $sqlindex 的值赋给它(result_of_name 只是每次定义时 name 结果的占位符)。这样做的原因(基于我读过的关于 Stack Overflow 的其他文章)是 PHP 仅在页面加载期间(大致)执行。

在这一行逻辑中,这意味着 PHP 变量将变得不可访问,并且必须在 PHP 生成页面时以过程方式生成并存储在本地。

稍后,我计划使用我发现的 AJAX 库将变量发布到页面(尚未创建),该页面将充当“id”(也是主键)对于我拥有的 MySQL 数据库中的行(这样我就可以记忆起页面的行和相关信息)。在另一页上,我有一个模板和一个 Fill-In-The-Blank MySQL 查询,等待我在上面粘贴的页面中的行的“id”。

通过 Stack Overflow,我在 Bash 中发现了类似的东西,但是,由于这些语言在语法和功能上如此不同,我在 JS 中找不到太多的应用程序。对于更接近 JS 的任何东西,我找不到这样的东西。

所以,总而言之,我的问题是:

如何用变量名的值定义“name”下的变量?即如果名称=“马铃薯”;那么下面的变量就是 potato = $sqlindex;

而且,我猜在扩展

有没有更好的方法来解决这个问题?结构上有什么可以改进的吗?请让我知道,以便我可以将其用于 future 的项目。

有问题的代码位于 HEREDOC 的底部,注释 block 上方。

如果您需要我添加任何内容,请告诉我。

代码:

     <?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 - 用前面的结果定义一个变量,有更好的方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31593400/

相关文章:

javascript - 如何从 dojo 商店的项目数​​组中删除对象

javascript - 从 JavaScript 调用 Solidity 函数得到了 [对象 promise ]?

php - 是否可以阻止 Zend Studio 像这样格式化嵌入式 PHP?

选择语句中的 PHP/MySql if 运算符

mysql - 多对多关系与级联或设置空删除?

php - 使用新值更新联结表

javascript - jQuery 无法获得良好格式的 Id

javascript - 将Mysql时间戳转换为本地国家时间戳

javascript - Laravel 5.5 中使用 AJAX 提交表单

mysql - 查询sql合并多个表