繁体   English   中英

我的代码被数组弄坏了,但是我无法弄清楚

[英]My code is broken with arrays, but I cannot figure it out

我不知道为什么$ promobill($ count_i)不打印任何内容。 我已经为此工作了几个小时,把头靠在墙上。 有任何想法吗?

$ promobill是否为数组? 应该是,但是我不确定它的格式是否正确。 这行代码是从原始数组中生成随机元素数组还是仅生成一个随机元素并将其分配给变量$ promobill?

$promo_bill = array_rand($site_promotions, $app_count);

这是我的完整代码:

    <?php

$site_promotions = array(
    'Free appetiser - orders over $3,000',
    '$100 off first time orders!',
    'testing...'
);

$appetisers = array(
    array('Pomegranate Shrimp','Jumbo shrimp sauteed with shitake mushrooms, herbs and pomegranate molasses.','#'),
    array('Hummus','Chick peas blended with tahini, garlic and lemon juice; topped with olive oil.','#'),
    array('Baba Ghannouj','Roasted eggplant blended with tahini, fresh garlic, parsley and lemon juice; topped with olive oil.','#'),
    array('Persian SaladPersian Salad','Freshly diced cucumbers, tomatoes and red onions tossed with lemon juice and a pinch of mint.','#'),
    array('Kash-Ke-Bademjan','Roasted eggplant purees with onion, garlic, mint and Kashk (aged, dried yogurt); topped with caramelized onions and kask.','#'),
    array('Must O Khiar','Homemade yogurt, freshly diced cucumbers and mint. Smooth and refreshing!','#'),
    array('Moosir','Persian Shallot finely sliced mixed with thich homemade yogurt. Try it, you\'ll like it.','#'),
    array('Olovieh','Persian potato salad with chopped chicken breast, pickles, peas, shredded carrots & mayonnaise.','#'),
    array('Caspian Eggplant','Mix of sweet charbroiled eggplant, tomato, onion & garlic topped with moosir.','#'),
    array('Pomegranate Mushrooms','Your choice of mushrooms flavored with exotic herbs, garlic, olive oil & pomegranate molasses.','#'),
    array('Dolmeh Felfel','Two bell peppers stuffed with basmati rice, cracked wheat, herbs & vegtables, baked in tomato saffron sauce.','#'),
    array('Dolmeh','Grape leaves stuffed with rice, tomato, onion, herbs & spices in tomato saffron sauce.','#'),
    array('Borani','Fresh spinach sautéed with onions and garlic in extra virgin olive oil and mixed with homemade yogurt.','#'),
    array('Spicy Pomegranate Wings','Charbroiled chicken wings with pomegranate glaze (7). Yummy, yummy, yummy!','#'),
    array('Falafel','Chickpeas seasoned and lightly fried served with tahini sauce.','#'),
    array('Persian Garden Salad','Crispy romaine, spring mix, red onions, cucumbers, cherry tomato, olives & Bulgarian cheese.','#')
);

$app_count = count($appetisers);
$promo_bill = array_rand($site_promotions, $app_count);

foreach($appetisers as $dish)
    {
        $count_i=key($dish);

        echo'
            <div class="sponsor" title="Click to flip">

                <div class="sponsorFlip">
                        <a class="group1" href="img/appetisers.jpg" title="'.$dish[0].'">       
                        <img class="sponsorIMG" src="img/appetisers_t.jpg" alt="Donya Catering menu" />
                    </a>    
                    <div class="dish_name">'.$dish[0].'</div>                   
                </div>

                <div class="sponsorData">
                    <div class="sponsorDescription">'.$dish[1].'</div>
                    <div class="sponsorURL">
                        <a class="sponsorLINK" href="promos/.'$promobill($count_i)'.">'.$promo_bill($count_i).'</a>
                    </div>
                </div>
            </div>
        ';
    }
?>

这是我想出的。 JSFiddle上看到它的实际应用 ...

<?php

$promotions = array();
$promos = array();
$promotions1 = array();

foreach (explode("\n", file_get_contents('http://donyacatering.com/st/wp-content/themes/DonyaCatering/data/promotions.txt')) as $promotions1) {
    $promotions[] = explode(",", $promotions1);
}

$appetisers = array(
    array('Pomegranate Shrimp','Jumbo shrimp sauteed with shitake mushrooms, herbs and pomegranate molasses.','http://donyacatering.com/'),
    array('Hummus','Chick peas blended with tahini, garlic and lemon juice; topped with olive oil.','http://donyacatering.com/'),
    array('Baba Ghannouj','Roasted eggplant blended with tahini, fresh garlic, parsley and lemon juice; topped with olive oil.','http://donyacatering.com/'),
    array('Persian SaladPersian Salad','Freshly diced cucumbers, tomatoes and red onions tossed with lemon juice and a pinch of mint.','http://donyacatering.com/')
);

                foreach($appetisers as $id=>$dish)
                {
                    $r=rand(0, count($promotions) - 1);
                    $promos[$id][0] = $promotions[$r][0];
                    $promos[$id][1] = $promotions[$r][1];
                    echo'
                    <div class="sponsor" title="Click to flip">     
                        <div class="sponsorFlip">
                            <a class="group1" href="http://donyacatering.com/st/wp-content/themes/DonyaCatering/img/appetisers.jpg" title="'.$dish[0].'<br>'.$dish[1].'">       
                            <img class="sponsorIMG" src="http://donyacatering.com/st/wp-content/themes/DonyaCatering/img/appetisers_t.jpg" alt="Donya Catering menu" />
                            </a>    
                            <div class="dish_name">'.$dish[0].'</div>                   
                        </div>

                        <div class="sponsorData">
                            <div class="sponsorURL">
                                <a class="sponsorLINK" href="http://donyacatering.com/st/wp-content/themes/DonyaCatering/promos/'.$promos[$id][1].'" title="'.$promos[$id][1].'">'.$promos[$id][0].'</a>
                            </div>
                        </div>
                    </div>
                    ';
                }

            ?>

与其尝试为每道菜建立一系列促销,不如简单地为每个菜品争取一个促销?

foreach ($appetisers as $dish) {
    $promotion = $site_promotions[array_rand($site_promotions, 1)];
    // use $promotion
}

您似乎在计算数组的开胃菜,然后尝试将该计数用于site_promotions,而应该这样做:

$app_count = count($site_promotions);
$promo_bill = array_rand($site_promotions, $app_count);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM