dtpwra8456 2014-05-02 16:17
浏览 34
已采纳

PHP:包含tile的内容 - 保留$ _POST变量

I've got a script that sends out emails. It goes through a for loop, where it finds all subscribers, gets their name and sets a POST var with their name in it.

Next I've got a separate PHP file which contains the markup of the email and content. It has a $_POST['fname'] var which is the users name and needs to be updated dynamically.

The problem is I'm unsure how to grab the contents of my email file (template.php) and add it into the email processing file (bulk_send.php) so that the $_POST['fname'] var can be dynamically updated. Here's the code.

foreach ($emailList as &$value) {
   $getSubscriber = mysql_query("SELECT * FROM subscribers WHERE email = '$value' ");
   while($row = mysql_fetch_assoc($getSubscriber)){
      $_POST['fname'] = $row['fname'];
      $_POST['lname'] = $row['lname'];
   }
   //WHERE IM STUCK
   $bodyText = ***INCLUDE template.php .....***

   //Code that will send out the email with $bodyText as the body of the email

}

The contents of our template.php is something simple - lets just say something like:

 <div><?php echo $_POST['fname')." ".$_POST['lname]; ?></div>

How could I include template.php file appropriately in the Foreach loop above so that the POST vars are updated with each iteration?

  • 写回答

1条回答 默认 最新

  • dse55384 2014-05-02 16:38
    关注

    You can't quite directly set a variable equal to the contents of another file using include as suggested by Mr.coder in a comment.

    You can think of include as copying and pasting the contents of the included file. Based on your template.php, it would be as if your code were:

       while($row = mysql_fetch_assoc($getSubscriber)){
          $_POST['fname'] = $row['fname'];
          $_POST['lname'] = $row['lname'];
       }
    
       ?><div><?php echo $_POST['fname']." ".$_POST['lname']; ?></div><?php
    

    That would simply dump the div contents out to the browser, which is probably not what you want. Instead, I'd suggest building into your template a function which would create the message text you desire based on parameters fed to the function. Then you would include your template at the top of your page and call the function when needed. It would look more like this:

    include('template.php');
    
    foreach ($emailList as &$value) {
       $getSubscriber = mysql_query("SELECT * FROM subscribers WHERE email = '$value' ");
       while($row = mysql_fetch_assoc($getSubscriber)){
          $_POST['fname'] = $row['fname'];
          $_POST['lname'] = $row['lname'];
       }
       //WHERE IM STUCK
       $bodyText = makeBodyText($_POST['fname'], $_POST['lname']);
    
       //Code that will send out the email with $bodyText as the body of the email
    
    }
    

    Your template.php would then look something like this:

    function makeBodyText($fname, $lname)
    {
       return '<div>' . $fname .' '. $lname . '</div>';
    }
    

    Alternatively, because the included file does operate in the same scope as where it where was called, and because it does support the concept of a return value (thanks to user @kokx for that insight), you could make your template.php like this:

    return '<div>' . $_POST['fname'] .' '. $_POST['lname] . '</div>';
    

    And then you could use it as follows:

    $bodyText = include 'template.php';
    

    However, that significantly limits the flexibility of template.php. Also, it would potentially output bad information if called directly (from a browser, rather than as an include). So I would not recommend this method.

    Now all that aside, it seems odd to me that you are modifying the contents of $_POST. That strikes me (and others) as bad practice.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效