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}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容