Вопрос

I have a table with the following structure:

CREATE TABLE IF NOT EXISTS `data` (
  `id` int(16) NOT NULL AUTO_INCREMENT,
  `name` varchar(40) NOT NULL,
  `email` varchar(40) NOT NULL,
  `photo` varchar(150) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

In this table there are more than 60 records. I want to preview the records into four boxes - for each box 15 records. Basically, doing a pagination is easy, but I want it like that to be in a slide show. My code right now is:

$mysql_employers = "SELECT * FROM data LIMIT ".$page.",".$limiter."";
$query_employers = $dblink->query($mysql_employers) or die("employers failed!");
$employers_link = array();
while($employers = $query_employers->fetch(PDO::FETCH_ASSOC)){
$employers_link[]=$employers;
}
$employers = new Smarty;
$employers->caching = false;
$employers->assign("employers",$employers_link);
$employers->display("default/employers.tpl");

$page is the page number and $limiter is the limit per page, which is 15. The smarty template code is

<div class="employers-box">
{$foreach $employers as $emp}
<div class="employers">
    <div class="employerphoto"><img src="{$emp.photo}"></div>
    <div class="employername"><a href="viewemplyer.php?employer={$emp.id}">{$emp.name}</a></div>
</div>
{/foreach}
</div>

What can I change in the code to make it shown in the page as four boxes (with 15 employers for each box) instead of being in pages?

Это было полезно?

Решение

Not the most elegant way, but you could just use if statements to check when the threshold is reached to close one div and open the next.

In your example, your array is numeric so we can use the index key as a counter. Read up about that on the smarty page here.

You can also use modulo to have just one simple if statement.

<div class="employers-box">
{foreach key=$counter item=$emp from=$employers}
  {if $counter % 15 === 0}
    <div id="employers-box-{$counter / 15}" class="employers">
  {/if}
  <div class="employer">
    <div class="employerphoto"><img src="{$emp.photo}"></div>
    <div class="employername"><a href="viewemplyer.php?employer={$emp.id}">{$emp.name}</a>  
  </div>
  {if $counter % 15 === 0}
    </div>
  {/if}
{/foreach}
</div>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top