Pregunta

Tengo el siguiente código:

<?php $i = 0; ?>

    <?php foreach ($this->getMyCollection() as $faqItem): ?>
        <a class="anchor" href="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB) ?>faq#<?php echo $i ?>"><?php echo $this->htmlEscape($faqItem->getQuestion()) ?></a><br>
    <?php
        $i++;
        if($i>2)break;
    ?>
    <?php endforeach; ?>

Pero lo que quiero mostrar es tres filas diferentes cada vez que se actualiza una página. ¿Cómo puedo hacer eso?

¿Fue útil?

Solución

Prueba esto:

Con array_rand Puede pasar una matriz y definir la cantidad de resultados que desea. Devolverá el número dado de claves que desea usar.

<?php $i = 0;
$items = $this->getMyCollection();
$keys = array_rand($items, 3);
foreach ($keys as $key): 
{

    $faqItem = $items[$key];
?>

    <a class="anchor" href="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB) ?>faq#<?php echo $i ?>"><?php echo $this->htmlEscape($faqItem->getQuestion()) ?></a><br>
<?php
    $i++;
} ?>

U opción 2, use el Barajar función:

<?php foreach (shuffle($this->getMyCollection()) as $faqItem): ?>
    <a class="anchor" href="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB) ?>faq#<?php echo $i ?>"><?php echo $this->htmlEscape($faqItem->getQuestion()) ?></a><br>
<?php
    $i++;
    if($i>2)break;
?>
<?php endforeach; ?>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top