douou5933 2014-05-20 11:57
浏览 52
已采纳

如何将php while循环分配给smarty以获得所需的结果

i want to show featured ads in slider problem is there is two table have to use to get ads. First one is class_ads that have all info of ad like title, description, price etc and second table is class_ads_pictures from where i have to match id of each ad and get the name of folder and picture name from table ....

Please review my poor logic and teach me

  <?php

    require_once "include/include.php";

    global $db;
    global $lng;
    $smarty = new Smarty;
    $smarty = common($smarty);


    $featured_ads = mysql_query("SELECT * FROM class_ads where active=1 and featured=1 limit 50"); 
    while ($tmp = mysql_fetch_array($featured_ads)) { 
        $ad_id      = $temp['id'];
        $ad_title   = $temp['title'];

        //check for image if featured ad have image (Need to fetch only single image)
        $featured_ads_images = mysql_query("select from class_ads_pictures where ad_id=$ad_id order by order_no"); 
        $img = mysql_fetch_array($featured_ads_images);
        $img_id         = $img['id'];
        $ad_img         = $img['picture'];
        $img_folder     = $img['folder'];


        // Problem is how to assign and what have to assign, to get display in html file ...
    } 

    $smarty->assign('what', $what);

    $db->close();
    if($db->error!='') { $db_error = $db->getError(); $smarty->assign('db_error',$db_error); }

    $smarty->display('header_featured.html');
    close();
    ?>
  • 写回答

1条回答 默认 最新

  • dongzhong6675 2014-05-20 16:04
    关注

    Below code should work. However you should not use mysql but PDO or mysqli because mysql is deprecated.

    <?php
    
    require_once "include/include.php";
    
    global $db;
    global $lng;
    $smarty = new Smarty;
    $smarty = common($smarty);
    
    
    $ads = array();
    
    $featured_ads = mysql_query("SELECT * FROM class_ads where active=1 and featured=1 limit 50"); 
    while ($temp = mysql_fetch_assoc($featured_ads)) { 
        $record = array();
        $record['ad_id']      = $temp['id'];
        $record['ad_title']   = $temp['title'];
    
        //check for image if featured ad have image (Need to fetch only single image)
        $featured_ads_images = mysql_query("select * from class_ads_pictures where ad_id={$record['ad_id']} order by order_no"); 
        $img = mysql_fetch_assoc($featured_ads_images);
        $record['img_id']         = $img['id'];
        $record['ad_img']         = $img['picture'];
        $record['img_folder']     = $img['folder'];
    
        $ads[] = $record;
    } 
    
    $smarty->assign('ads', $ads);
    
    $db->close();
    if($db->error!='') { $db_error = $db->getError(); $smarty->assign('db_error',$db_error); }
    
    $smarty->display('header_featured.html');
    close();
    ?>
    

    In Smarty you can simple use it:

     {foreach item=item  from=$ads} 
      {$item.ad_id}
      {$item.ad_title}
      {$item.img_id}
      {$item.ad_img}
      {$item.img_folder}
     {/foreach}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建