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.

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

报告相同问题?

悬赏问题

  • ¥15 C++ 菜单窗口独立出来,可以随意移动放大缩小。
  • ¥15 java代码写在记事本上后在cmd上运行时无报错但又没生成文件
  • ¥15 关于#python#的问题:在跑ldsc数据整理的时候一直抱这种错误,要么--out识别不了参数,要么--merge-alleles识别不了参数(操作系统-linux)
  • ¥15 PPOCRLabel
  • ¥15 网友们我该怎么办啊,急
  • ¥15 混合键合键合机对准标识
  • ¥100 现在不懂的是如何将当前的相机中的照片,作为纹理贴图,映射到扫描出的模型上
  • ¥15 目标跟踪,计算机视觉
  • ¥15 魔霸ROG7 pro,win11.息屏后会显示黑屏,如图,如何解决?(关键词-重新启动)
  • ¥15 有没有人知道这是哪里出了问题啊?要怎么改呀?