Note that there are some explanatory texts on larger screens.

plurals
  1. POEchoing a PHP String
    primarykey
    data
    text
    <p>Ok I've got PHP to successfully open and read the contents of a directory containing a number of images. I pass all of this data into an array, sort the results and finally iterate over all of the images so that it gives me a sting which I then echo onto the html page. In this string I set the css class for each div so that when the page renders each div is displayed correctly. </p> <p>My question is to do with the actual PHP setting the class of each div. Basically I have two classes that I apply to each div depending on what number they are. For example in the first horizontal row, the 1st to 3rd div gets the class of <code>.box</code> so as to set a <code>margin-right:10px;</code> and the 4th get's it's own class of <code>.box last</code> which sets <code>margin-right:0px;</code>.</p> <p>So what I wanted to ask was is there way of writing some logic to tell the PHP code to apply one class for the 1st to 3rd divs and a separate class for every 4th div for each row?Effectively the first three divs in the row get on class and the fourth div in each row gets another? </p> <pre><code>&lt;?php $dir = "images/"; $images = array(); $returnstr = ""; if ($handle = opendir($dir)) { while ( false !== ($entry = readdir($handle))) { if ($entry != "." &amp;&amp; $entry != ".."){ $images[] = $entry; } } closedir($handle); } natsort($images); $newArray = array_values($images); foreach ($newArray as $key =&gt; $value) { $returnstr .= '&lt;li class="box"&gt;'; $returnstr .= '&lt;a href="#" name="" class="thumb" id="' . $key . '"&gt;&lt;img src="'. $dir . '/' . $value . '"/&gt;&lt;/a&gt;'; $returnstr .= '&lt;/li&gt;'; } ?&gt; </code></pre> <p>This is the CSS for the 1st - 3rd divs... </p> <pre><code>.box { float: left; /*margin-right:10.5px;*/ margin-right:10px; /*width: 92px; height: 92px; */ width: 232.5px; height:150px; /*height: 232px; */ /*background-color: #999; border: 1px solid*/ margin-bottom: 10px; list-style: none; } </code></pre> <p>... and the CSS for every 4th div in the grid</p> <pre><code>.box.last { margin-right: 0px; } </code></pre> <p>This is the html I trying to atchieve.</p> <pre><code>&lt;li class="box"&gt; &lt;a href="#" name="cap1" class="thumb" id=""&gt;&lt;img src="images/1.jpg"/&gt;&lt;/a&gt; &lt;/li&gt; &lt;li class="box"&gt; &lt;a href="#" name="cap2" class="thumb" id=""&gt;&lt;img src="images/2.jpg"/&gt;&lt;/a&gt; &lt;/li&gt; &lt;li class="box"&gt; &lt;a href="#" name="cap3" class="thumb" id=""&gt;&lt;img src="images/3.jpg"/&gt;&lt;/a&gt; &lt;/li&gt; &lt;li class="box last"&gt; &lt;a href="#" name="cap4" class="thumb" id=""&gt;&lt;img src="images/4.jpg"/&gt;&lt;/a&gt; &lt;/li&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload