简体   繁体   English

PHP 在SMARTY模板中准确循环显示数据

[英]PHP display while loop data accurately in SMARTY template

In the following code when I try to assign $viewAd value to the template file and display result it does not display the accurate results when assigned to the template.在下面的代码中,当我尝试将$viewAd值分配给模板文件并显示结果时,它在分配给模板时不会显示准确的结果。 However, it displays the accurate desired result on top of the page when I straight echo the $viewAd in the PHP page.但是,当我在 PHP 页面中直接回显$viewAd时,它会在页面顶部显示准确的所需结果。 I have given the screenshots below.我已经给出了下面的截图。

My PHP Structure goes like this:我的 PHP 结构如下:

$cat = $pdo->prepare("SELECT QUERY HERE");
$cat-> execute();

while($s = $cat->fetch()){
  $ads = $pdo->prepare("ANOTHER SELECT QUERY HERE");
  $ads-> execute();
  $ads_count = $ads->rowCount();

  if($ads_count > 0){
    $viewAd = "<h4>".$s['pcat_category']."</h4>"; // Echoing Category Name
    while($a = $ads->fetch()){
      if(isLoggedIn()){
        // If logged in display Ads relevant to members
        $viewAd .= 'SOME HTML DATA';
        foreach($membershipData as $mbs){
          $viewAd .= 'EXTENDED HTML DATA';
        }
        $viewAd .= 'CLOSING HTML DATA';
      }else{
        // If not logged in display ads relevant to outsiders
        $viewAd .= 'SOME HTML DATA';
        foreach($membershipData as $mbs){
          $viewAd .= 'EXTENDED HTML DATA';
        }
        $viewAd .= 'CLOSING HTML DATA';
      }
    }
    echo $viewAd; // RETURNS DESIRED RESULT ABOVE THE TEMPLATE ON TOP
  }
}
$smarty->assign('viewAds', $viewAd); // ASSIGNED TO TEMPLATE BUT DOES NOT RETURN DESIRED RESULT

Screenshot 1: Correct Result with straight PHP echo displays each category with ads in it屏幕截图 1:使用直接 PHP 回显的正确结果显示每个类别中都有广告

在此处输入图像描述

Screenshot 2: Incorrect Result when variable assigned to the template displays only the last category with the ads in it屏幕截图 2:分配给模板的变量仅显示包含广告的最后一个类别时的错误结果

在此处输入图像描述

Why am I not getting the same result when I assign the variable to the template?为什么将变量分配给模板时没有得到相同的结果? I have also tried the array method where I declared $viewAds = array() before the while loop and later I assigned $viewAds[] = $viewAd .我还尝试了在 while 循环之前声明$viewAds = array()的数组方法,后来我分配$viewAds[] = $viewAd Then in the tpl file I tried to echo the value using foreach loop like this {foreach $viewAds as $vas}{$vas}{/foreach} but that still didn't display each category and ads (the desired result like in the first screenshot).然后在 tpl 文件中,我尝试使用这样的 foreach 循环来回显该值{foreach $viewAds as $vas}{$vas}{/foreach}但仍然没有显示每个类别和广告(所需的结果就像第一张截图)。 Since, it did not work using array as well I removed it and tried to echo {$viewAds} straight way and with foreach too.因为,它使用数组也不起作用,我删除了它并尝试直接回{$viewAds}并使用 foreach 。 No luck, nothing is working.没有运气,没有任何工作。 All gives the result as in second screenshot. All 给出了第二个屏幕截图中的结果。 Displaying only the last category with ad in it.仅显示包含广告的最后一个类别。 However, since direct echo in PHP file is giving the correct result I am sure that my PHP logic is correct.但是,由于 PHP 文件中的直接回显给出了正确的结果,我确信我的 PHP 逻辑是正确的。 It's just that I am not being able to assign that result in the template file correctly and display it.只是我无法在模板文件中正确分配该结果并显示它。 What is the error I am making here?我在这里犯了什么错误? Am I missing out on something?我错过了什么吗?

You need to initialize $viewAd before starting the first loop, something like:您需要在开始第一个循环之前初始化$viewAd ,例如:

 $viewAd = ""; 

Then each category should be appended to $viewAd :然后每个类别都应该附加到$viewAd

 $viewAd .= "<h4>".$s['pcat_category']."</h4>";

This way all categories will be included in your final HTML.这样,所有类别都将包含在您的最终 HTML 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM