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.

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!