Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I suggest rewriting (at least) that part of the code as I believe (database) logic and output should be separated.</p> <p>However, in your code it is not separated, so why even separate the div from it? If you wouldn't, the database logic inside the function can easily be used to achieve what you want.</p> <pre><code>// new function parameters: // $maxCols maximum number of divs default=NULL // $colSize how many rows in one col default=10 if ($maxCols !== NULL) $limit = ','.$maxCols * $colSize; else $limit = ''; $query = "SELECT * FROM sampleTable ORDER BY Data1, Data2 LIMIT ".$startAt.$limit; if ($result = mysqli_query($link, $query)) { $currentRow = 1; echo '&lt;div class="down_col"&gt;'; while ($row = mysqli_fetch_row($result)) { if ($currentRow % $colSize == 0) echo '&lt;/div&gt;&lt;div class="down_col"&gt;'; $data1Data2 = $row[1] . ' - ' . $row[2]; echo '&lt;p&gt;&lt;a href="sample/sample/' . $data1Data2 . '.pdf" target="_self"&gt;' . $data1Data2 . '&lt;/a&gt;&lt;/p&gt;'; $currentRow++; } echo '&lt;/div&gt;'; } </code></pre> <p>Now you can simply call the function once like this</p> <pre><code>$startAt = empty($_GET['startAt']) ? 0 : intval($_GET['startAt']); // version 1: print all rows in cols of size 10 printDownTrans($startAt); // version 2: print maximum of 3 cols of size 10 printDownTrans($startAt, 3); // version 3: print maximum of 5 cols of size 8 printDownTrans($startAt, 5, 8); </code></pre> <p>You can never trust user input! Especially when you are working with a database, this is a great vulnerability. When set, <code>$_GET['startAt']</code> will be used in the query with no escaping whatsoever (maybe in the parts you didn't post?) - <code>intval</code> is the easy solution here, since you always expect an integer.</p> <p>Since you are new to PHP, you might not know the <a href="http://php.net/manual/language.operators.arithmetic.php" rel="nofollow">modulo operator <code>%</code></a>. I hope this example helps:</p> <pre><code>(1 % 10 == 0) -&gt; (1 == 0) -&gt; false (4 % 10 == 0) -&gt; (4 == 0) -&gt; false (10 % 10 == 0) -&gt; (0 == 0) -&gt; TRUE (11 % 10 == 0) -&gt; (1 == 0) -&gt; false (20 % 10 == 0) -&gt; (0 == 0) -&gt; TRUE </code></pre>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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