0

我正在为网站http://mytempsite.net/gotie/mixandmatch构建定制衬衫制造商

我的设置是第一步,他们将从 12 种不同的衬衫中选择衬衫颜色,然后进入下一步,他们将能够选择领带。我需要能够将一个变量传递给下一页,告诉它只提取带有领带的图像,例如红色衬衫。

我这样做的想法是通过在图像 alt 或标题标记处拥有该属性,然后从正在显示的当前图像中获取该属性。

我需要知道的是如何?

我尝试使用此代码作为开始

<?php
    $url=$this->helper('core/url')->getCurrentUrl();;

    $html = file_get_contents($url);

    $doc = new DOMDocument();
    @$doc->loadHTML($html);

    $divs = $doc->getElementByID('loadarea');

    foreach ($divs as $div) {
           echo "Found the loadarea div <br />";
    }
?>

但它没有用,而且它也导致我的页面加载非常缓慢。

以防万一这里是缩略图的代码

<?php 
    $products = Mage::getModel('catalog/product')->getCollection();

    foreach($products as $prod) {
        $product = Mage::getModel('catalog/product')->load($prod->getId());
        $pro_title = $product->getName();
        $img = $product->getImageUrl();

        echo "<a href='".$img."' title='".$pro_title."' rel='enlargeimage' rev='targetdiv:loadarea,enabletitle:no,trigger:click,preload:none,fx:fade'><img src='".$img."' width='100px'/></a>";

}?>

我希望我正确地措辞了这个问题,以免过于本地化。如果是这样,我会改写它。

4

2 回答 2

0

我相信 $someVar = get_field('image file') 应该返回图像中所有标记数据的数组。然后,您可以访问数组中的数据(即 $someVar['alt'])。

这是一个更简单的解决方案,不需要您将图像变成对象。

$html=file_get_contents("URL OF YOUR SITE");
$doc = new DOMDocument();
@$doc -> loadHTML($html);
    // Add a class to your big image to identify it (ex. selected)
$tags = getElementsByClassName($doc, 'selected');
foreach ($tags as $tag) {
    echo $tag->getAttribute('alt');
}


    // Not my function but its very useful.  I'll track down where I got it
    // and add source later.    
function getElementsByClassName(DOMDocument $DOMDocument, $ClassName) {
    $Elements = $DOMDocument -> getElementsByTagName("*");
    $Matched = array();

    foreach($Elements as $node)
    {
        if( ! $node -> hasAttributes())
            continue;

        $classAttribute = $node -> attributes -> getNamedItem('class');

        if( ! $classAttribute)
                continue;

        $classes = explode(' ', $classAttribute -> nodeValue);

        if(in_array($ClassName, $classes))
            $Matched[] = $node;
    }

    return $Matched;
}
于 2013-05-01T19:08:25.207 回答
0

我最终做的是,当单击缩略图时,隐藏的文本框会获取该衬衫的 product_id,然后当您转到下一页时,它会自动拉出该产品的产品图像。我已经为我的整个表单插入了代码,以防将来有人需要这样做:)

快乐编码!

         <form id="GoTie_Builder" method="POST" action="/gotie/mixandmatch/tie">
                                <script>
                                function changeInput(pro_id)
                                {
                                    var my_form = document.getElementById('GoTie_Builder');
                                    my_form.shirt_color.value = pro_id;
                                }

                                function changePattern(pattern)
                                {
                                    var my_div = document.getElementById('shirt_zoom');
                                    my_div.innerHTML = '<img src="http://www.tuxedojunction.com/Content/Products/Vests/LegacyBlueVelvet_s_1.jpg" />';

                                }

                                </script>
                                <div id="shirt_zoom" style="width:300px; height:100px;">
                                <img src="http://www.tuxedojunction.com/Content/Products/Vests/LegacyBlueVelvet_s_1.jpg" />
                                </div>

                                <div class="Builder_thumbnails" style="float:left;">

                            <?php 
                                $cat_id = 8;
                                $products = Mage::getModel('catalog/category')->load($cat_id)->getProductCollection();
                                echo '<input id="shirt_color" type="text" name="shirt_color" value="0">';
                                foreach($products as $prod) {
                                    $product = Mage::getModel('catalog/product')->load($prod->getId());

                                    $pattern = $this->helper('catalog/image')->init($product, 'thumbnail');

                                    //echo "<img onclick='changeInput($pro_id)' class='product_thumbnail' src='".$pattern."' alt='".$pro_id."' width='100px'/>";


                                    $pro_id = $product->getId();
                                    $pro_title = $product->getName();
                                    $img = $product->getImageUrl();


                                    $input_id = "shirt_color";
                                    echo "<a href='".$img."' title='".$pro_title."' rel='enlargeimage' rev='targetdiv:loadarea,enabletitle:no,trigger:click,preload:none,fx:fade'><img onclick='changeInput($pro_id)' class='product_thumbnail' src='".$this->helper('catalog/image')->init($product, 'thumbnail')."' alt='".$pro_id."' width='100px' height='100px'/></a>";

         }?>

  </div>      


         <div id="loadarea" style="width:300px;top: 0px;right: 0px;float: right;position: relative;"><img src="http://cdn4.blackenterprise.com/wp-content/blogs.dir/1/files/2011/07/White-Shirt-620x480.jpg" width="500px;"/>

         </div>
         <input type="submit" value="Choose a Tie" />
         </form>
于 2013-05-02T15:35:14.033 回答