<p>That query would basically tell MySQL to do this:</p>

<ol>
<li>Shuffle the blocks</li>
<li>Return the second one</li>
</ol>

<p>Since the shuffling happens <em>before</em> the offset/limit params get applied, they can’t be used to exclude the first block, since the first block will already be put in some random position by that point.</p>

<p>Instead you should get the ID of the first block, and exclude it using that:</p>

<pre class="lang-twig prettyprint-override"><code>{% set firstBlockId = city.cityBlocks.limit(1).ids()|first %}

{% if firstBlockId %}
    {% set randomQuotes = city.cityBlocks.id('not '~firstBlockId).order('RAND()') %}
    ...
{% else %}
    There were no blocks...
{% endif %}
</code></pre>

CC BY-SA 3.0
2014-09-02T20:50:35.673Z
2014-09-02T20:50:35.673Z
Brandon Kelly
<p>okay, shuffle seems to work:
first set these:</p>

<pre><code>{% set quotes = city.cityBlocks.offset(1) %}
{% set randomQuotes = shuffle(quotes) %}
</code></pre>

<p>then slice the the for loop like this</p>

<pre><code>{% for block in randomQuotes|slice(1,1) %}
...
{% endfor %}
</code></pre>

CC BY-SA 3.0
2014-09-02T20:36:25.837Z
2014-09-02T20:36:25.837Z
portnull
<p>I have quotes inside a global matrix which i want to output randomly on the homepage but without the first quote-block. this does not work:</p>

<pre><code>{% set quotes = city.cityBlocks.offset(1) %}
{% set randomQuotes = quotes.limit(1).order('RAND()') %}
</code></pre>

<p>the offset in 'quotes' works fine but the first block is still in randomQuotes anyway.</p>

CC BY-SA 3.0
2014-09-02T05:19:30.693Z
All that code would do is output the second block (which is all you'll ever get when offset=1 and limit=1). Have you tried removing the limit parameter?
Brandon Kelly
CC BY-SA 3.0
2014-09-02T20:28:47.820Z
thanks brandon, but that will just give me all the quotes.
portnull
CC BY-SA 3.0
2014-09-02T20:46:04.013Z
Sorry, I understand what you’re trying to do now.
Brandon Kelly
<matrix><random>
Output random blocks from a matrix but without the first
# Output random blocks from a matrix but without the first

(Asked by: username_0 on 2014-09-02)

I have quotes inside a global matrix which i want to output randomly on the homepage but without the first quote-block. this does not work:
{% set quotes = city.cityBlocks.offset(1) %}
{% set randomQuotes = quotes.limit(1).order('RAND()') %}

the offset in 'quotes' works fine but the first block is still in randomQuotes anyway.

**Tags:** matrix, random
> **Comments:**
> **username_1**: All that code would do is output the second block (which is all you'll ever get when offset=1 and limit=1). Have you tried removing the limit parameter?  
> **username_0**: thanks brandon, but that will just give me all the quotes.  
> **username_1**: Sorry, I understand what you’re trying to do now.  

---

## 2 answers

### Answer by username_1 on 2014-09-02. Score: 3 | Accepted answer

That query would basically tell MySQL to do this:

Shuffle the blocks
Return the second one

Since the shuffling happens before the offset/limit params get applied, they can’t be used to exclude the first block, since the first block will already be put in some random position by that point.
Instead you should get the ID of the first block, and exclude it using that:
{% set firstBlockId = city.cityBlocks.limit(1).ids()|first %}

{% if firstBlockId %}
    {% set randomQuotes = city.cityBlocks.id('not '~firstBlockId).order('RAND()') %}
    ...
{% else %}
    There were no blocks...
{% endif %}


---

### Answer by username_0 on 2014-09-02. Score: 2

okay, shuffle seems to work:
first set these:
{% set quotes = city.cityBlocks.offset(1) %}
{% set randomQuotes = shuffle(quotes) %}

then slice the the for loop like this
{% for block in randomQuotes|slice(1,1) %}
...
{% endfor %}


---

