dpx49470 2013-09-10 14:41
浏览 31
已采纳

php while while循环里面循环

Having some problems with while loop inside another while loop

first while loop is fetching main images of the product from My SQL DB table while the second while loop is fetching other screenshots of the main product.

Here is my problem that I cannot figure out, the script is working as it should when I have only one result from DB, but if there is some multiple main products than it is failing to fetch main image of the product and its additional screenshots.

Here is my script:

<?
if ($free_pr_module_2 == 1) {
print<<<END
<div class="bonus">
<div class="specs-top mult">Featured $mult_pr_title</div>

END;

// multiple product image
 $mult_query = 'SELECT * FROM Files WHERE pageID = :id';
 $res = $db->prepare($mult_query);
 $res->execute(array(':id' => $pid));
while ($info = $res -> fetch()){
    $path_to_images = $image_path.$info['ImagePath'].DS;
    $mult_image_link = '<a href="'.$path_to_images.$info['templImage_lg'].'" rel="prettyPhoto[mu]" title="'.$info['templateTitle'].'"><img src="'.$path_to_images.$info['templateImage'].'" alt="'.$info['templateTitle'].' Screenshot'.'" width="'.$info['templimgwidth'].'" height="'.$info['templimgheight'].'" title="'.$info['templateTitle'].'"></a>';
    $mult_templTitle = '<h3>'.$info['templateTitle'].'</h3>';
    $mult_templText = $info['add_product_description'];
    $mult_templVersion = '<span class="tech"><b>VERSION</b> '.$info['templateVersion'].'</span>';
    $mult_templLicense = '<span class="tech"><b>LICENSE</b> '.$info['templLicense'].'</span>';
    $mult_download_but_path = SITE_DOMAIN.DS.DIR_FILES.DS;
    $mult_file_urlid = $info['urlid'];
    $mult_xls_button = ($info['xls'] == 1) ? '<div class="bb">
 <div class="bbl">Excel 2003(XP)<br><span>File: XLS</span></div>
 <div class="bb_button"><a class="bb_xls" href="'.$mult_download_but_path.'xls.php?urlid='.$mult_file_urlid.'" rel="nofollow" onClick="_gaq.push([\'_trackEvent\', \'Downloads\', \'XLS\', this.href.substr(29)]); user_notice(this,\'\'); return false;">Download</a></div>
</div>' : ('');
    $mult_xlsx_button = ($info['xlsx'] == 1) ? '<div class="bb">
 <div class="bbl">Excel 2007 +<br><span>File: XLSX</span></div>
 <div class="bb_button"><a class="bb_xlsx" href="'.$mult_download_but_path.'xlsx.php?urlid='.$mult_file_urlid.'" rel="nofollow" onClick="_gaq.push([\'_trackEvent\', \'Downloads\', \'XLSX\', this.href.substr(29)]); user_notice(this,\'\'); return false;">Download</a></div>
</div>' : ('');
    $mult_ots_button = ($info['ots'] == 1) ? '<div class="bb">
 <div class="bbl">OpenOffice<br><span>File: OTS</span></div>
 <div class="bb_button"><a class="bb_ots" href="'.$mult_download_but_path.'ots.php?urlid='.$mult_file_urlid.'" rel="nofollow" onClick="_gaq.push([\'_trackEvent\', \'Downloads\', \'OTS\', this.href.substr(29)]); user_notice(this,\'\'); return false;">Download</a></div>
</div>' : ('');
    $mult_gdocs_button = ($info['gdocs'] == NULL) ? '' : '<div class="bb">
 <div class="bbl">Google Docs<br></div>
 <div class="bb_button"><a class="bb_gdt" href="'.$info['gdocs'].'" rel="nofollow" target="_blank" onClick="_gaq.push([\'_trackEvent\', \'Downloads\', \'GDT\', this.href.substr(40)]);">Preview</a></div>
</div>';
    $mult_numbers_button = ($info['numbers'] == 1) ? '<div class="bb">
 <div class="bbl">Mac Numbers<br><span>File: NUMBERS</span></div>
 <div class="bb_button"><a class="bb_mac" href="'.$mult_download_but_path.'num.php?urlid='.$mult_file_urlid.'" rel="nofollow" onClick="_gaq.push([\'_trackEvent\', \'Downloads\', \'MCN\', this.href.substr(40)]); user_notice(this,\'\'); return false;">Download</a></div>
</div>' : ('');


print<<<END

<div class="mult_cont">
<div class="ss_left">

$mult_image_link

<div class="screnshot">
<span class="enlarge">1</span>
END;

// aditional images
$mult_main_image = "SELECT * FROM simages WHERE parentpageID = :id and main_img_show = '2'";
$res = $db->prepare($mult_main_image);
$res->execute(array(':id' => $pid));

$mult_num = 2;
while ($info = $res -> fetch()){
    $mult_post_img_link = $path_to_images.$info['main_img'];
    $mult_prphoto_id = $info['prphoto_id'];
    $mult_post_img_title = $info['main_img_title'];
    $mult_link_l = ($mult_num++);

print<<<END
<a href="$mult_post_img_link" class="enlarge" rel="prettyPhoto[$mult_prphoto_id]" title="$mult_post_img_title">$mult_link_l</a> 

END;
}

print<<<END
<div class="clear"></div>
</div>
</div>

$mult_templTitle
$mult_templText
<div class="clear"></div>
</div>
<div class="download">
<div class="mult_spec">
$mult_templVersion
$mult_templLicense
</div>

$mult_xls_button

$mult_xlsx_button

$mult_ots_button

$mult_gdocs_button

$mult_numbers_button

<div class="clear"></div>
</div>
END;
}

print<<<END
</div>
END;

}else
echo '';

?>

Please be patients with me since I am only learning PHP/My SQL and all this is practically my first attempt to create something useful for my site.

Your help is highly appriciated

  • 写回答

1条回答 默认 最新

  • duanshan1977 2013-09-10 14:43
    关注

    You are overwriting you variables. Change $res and $info from inner loop to something else, eg. $res2.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)