使用foreach进行查询优化?

时间:2016-09-23 10:55:54

标签: php html mysql

我为IF做了3个不同的查询($ rs1,$ rs2,$ rs3)。如果指定的产品分配了任何图像,它会在数据库中查找。

事情是我不知道如何仅使用1个查询和1个foreach来优化所有这些。

如果有人可以帮助我,请输入以下代码:

<?php use_helper("StaticUrl");?>
<?php
$rs1  = Doctrine_Query::create()->from("Product")
                          ->select("photo1")
                          ->where("title = ?", $product)
                          ->fetchOne(array(), Doctrine_Core::HYDRATE_ARRAY);

$rs2  = Doctrine_Query::create()->from("Product")
                          ->select("photo2")
                          ->where("title = ?", $product)
                          ->fetchOne(array(), Doctrine_Core::HYDRATE_ARRAY);

$rs3  = Doctrine_Query::create()->from("Product")
                          ->select("photo3")
                          ->where("title = ?", $product)
                          ->fetchOne(array(), Doctrine_Core::HYDRATE_ARRAY);

                          ?>


<div class="product_image">
    <?php 
      $href = url_for($product->getRouteUrl(ESC_RAW));
      if ($sf_context->getActionName() == 'view')
    {
      $href = static_url_for($product->generatePhotoPath('large',ESC_RAW));
    }
    ?>

  <a <?php echo $sf_context->getActionName() == 'view' ? 'class="lightbox"' : ''; ?> href="<?php echo $href;?>" title="<?php echo $product; ?>">
        <img src="<?php echo  static_url_for($product->generatePhotoPath(ESC_RAW)); ?>" alt="<?php echo $product; ?>" data-zoom-image="<?php echo  static_url_for($product->generatePhotoPath('large',ESC_RAW)); ?>" id="zoomea<?php echo SITE_ID; ?>" />
  </a>


  <div id="gallery_01" style="position:absolute;">
    <?php if(!empty($rs1['photo1'])){ ?>

  <a href="#" data-image="<?php echo  static_url_for($product->generatePhotoPath("medium", 1, ESC_RAW)); ?>" data-zoom-image="<?php echo  static_url_for($product->generatePhotoPath("large", 1, ESC_RAW)); ?>" >
    <img class="zoomab" src="<?php echo  static_url_for($product->generatePhotoPath("small", 1, ESC_RAW)); ?>" width="50" height="60"/>
  </a>
    <?php } 

        if(!empty($rs2['photo2'])){
    ?>
  <a href="#" data-image="<?php echo  static_url_for($product->generatePhotoPath("medium", 2, ESC_RAW)); ?>" data-zoom-image="<?php echo  static_url_for($product->generatePhotoPath("large", 2, ESC_RAW)); ?>" >
    <img class="zoomab" src="<?php echo  static_url_for($product->generatePhotoPath("small", 2, ESC_RAW)); ?>" width="50" height="60" />
  </a>

    <?php } 

        if(!empty($rs3['photo3'])){
    ?>

  <a href="#" data-image="<?php echo  static_url_for($product->generatePhotoPath("medium", 3, ESC_RAW)); ?>" data-zoom-image="<?php echo  static_url_for($product->generatePhotoPath("large", 3, ESC_RAW)); ?>"  >
    <img class="zoomab" src="<?php echo  static_url_for($product->generatePhotoPath("small", 3, ESC_RAW)); ?>"  width="50" height="60" />
  </a>
<?php } ?>

</div>

</div>

<script src="/js/elevatezoom/jquery.elevatezoom.js" type="text/javascript"></script>

1 个答案:

答案 0 :(得分:0)

<?php use_helper("StaticUrl");?>
<?php
$rs1  = Doctrine_Query::create()->from("Product")
                          ->select("photo1,photo2,photo3")
                          ->where("title = ?", $product)
                          ->fetchOne(array(), Doctrine_Core::HYDRATE_ARRAY);
                          ?>


<div class="product_image">
    <?php 
      $href = url_for($product->getRouteUrl(ESC_RAW));
      if ($sf_context->getActionName() == 'view')
    {
      $href = static_url_for($product->generatePhotoPath('large',ESC_RAW));
    }
    ?>

  <a <?php echo $sf_context->getActionName() == 'view' ? 'class="lightbox"' : ''; ?> href="<?php echo $href;?>" title="<?php echo $product; ?>">
        <img src="<?php echo  static_url_for($product->generatePhotoPath(ESC_RAW)); ?>" alt="<?php echo $product; ?>" data-zoom-image="<?php echo  static_url_for($product->generatePhotoPath('large',ESC_RAW)); ?>" id="zoomea<?php echo SITE_ID; ?>" />
  </a>


  <div id="gallery_01" style="position:absolute;">
    <?php if(!empty($rs1['photo1'])){ ?>

  <a href="#" data-image="<?php echo  static_url_for($product->generatePhotoPath("medium", 1, ESC_RAW)); ?>" data-zoom-image="<?php echo  static_url_for($product->generatePhotoPath("large", 1, ESC_RAW)); ?>" >
    <img class="zoomab" src="<?php echo  static_url_for($product->generatePhotoPath("small", 1, ESC_RAW)); ?>" width="50" height="60"/>
  </a>
    <?php } 

        if(!empty($rs1['photo2'])){
    ?>
  <a href="#" data-image="<?php echo  static_url_for($product->generatePhotoPath("medium", 2, ESC_RAW)); ?>" data-zoom-image="<?php echo  static_url_for($product->generatePhotoPath("large", 2, ESC_RAW)); ?>" >
    <img class="zoomab" src="<?php echo  static_url_for($product->generatePhotoPath("small", 2, ESC_RAW)); ?>" width="50" height="60" />
  </a>

    <?php } 

        if(!empty($rs1['photo3'])){
    ?>

  <a href="#" data-image="<?php echo  static_url_for($product->generatePhotoPath("medium", 3, ESC_RAW)); ?>" data-zoom-image="<?php echo  static_url_for($product->generatePhotoPath("large", 3, ESC_RAW)); ?>"  >
    <img class="zoomab" src="<?php echo  static_url_for($product->generatePhotoPath("small", 3, ESC_RAW)); ?>"  width="50" height="60" />
  </a>
<?php } ?>

</div>

</div>

<script src="/js/elevatezoom/jquery.elevatezoom.js" type="text/javascript"></script>