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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?