douyueju2912 2014-08-26 12:13
浏览 44
已采纳

从mysql中获取图像

i am new in smarty and i have a little problem that i cant fetch images for my posts. I store the images path in table ( images ) and the posts in table (posts):

my php code is

    $get_new_jobs=$mysqli->query("select id,numview,title,cat1,details,country,active from ads where country='".$country_en."' and active='1' order by id desc limit 4");
    $nums=$get_new_jobs->num_rows;
    while($row=$get_new_jobs->fetch_assoc()){

        $mm[]=$row;

    }

$smarty->assign('posts',$mm);
$smarty->assign('image',$the_images_name);
$smarty->display('posts.tpl');

and my images code is

$sql=mysqli->query("select adid,image,p from images where adid='".$row['id']."'");
$fet = $sql->fetch_assoc();
$the_images_name = $fet['image'];

and the tpl code is

<table width="100%" border="0" cellpadding="5">
{foreach from=$posts item=item}
<tr valign="top">
<td>        
{$image}
<br />
{$item.title}
</td>
</tr>        
{/foreach}
</table>

so how can i get each image for each post.

regards

  • 写回答

1条回答 默认 最新

  • doulai8405 2014-08-26 12:35
    关注

    Basically you can fetch the image inside the loop and then can assign the image url to the index of the same array you are sending the post data

    The Php part

    $get_new_jobs=$mysqli->query("select id,numview,title,cat1,details,country,active from ads where country='".$country_en."' and active='1' order by id desc limit 4");
        $nums=$get_new_jobs->num_rows;
        while($row=$get_new_jobs->fetch_assoc())
        {
    
            $sql=mysqli->query("select adid,image,p from images where adid='".$row['id']."'");
            $fet = $sql->fetch_assoc();
            $the_images_name = $fet['image'];
            $row['image_url']= $the_images_name;
            $mm[]=$row;
         }
    
    $smarty->assign('posts',$mm);
    $smarty->display('posts.tpl'); 
    

    The tpl part

    <table width="100%" border="0" cellpadding="5">
    {foreach from=$posts item=item}
    <tr valign="top">
    <td>        
    <img src="{$item.image_url}">
    <br />
    {$item.title}
    </td>
    </tr>        
    {/foreach}
    </table>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序