Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Problem z wdrożeniem instrukcji if w kodzie.
Forum PHP.pl > Forum > PHP
dominick
Problem chyba bardzo prosty ale jakieś błędy robie. Nie mogę zrobić aby w 1 menu wyświetliło 2 pozycje i w 2 menu też 2 pozycje.
oto kod:
  1. <ul class="footer_nav">
  2.  
  3. <?php $i = 1; ?>
  4. <?php if($i <= 2):?>
  5. <?php foreach ($documents as $d): ?>
  6. <li <?php if ($i == count($documents)): ?>class="last"<?php endif ?> ><a href="<?php echo Route::url('site_documents/frontend/documents/show', array('url' => $d->document->document_url)) ?>"><?php echo $d->document->document_title ?></a></li>
  7. <?php $i++ ?>
  8. <?php endforeach ?>
  9. <?php endif ?>
  10. </ul>
  11. <ul class="footer_nav">
  12. <?php $i = 1; ?>
  13. <?php if($i > 2):?>
  14. <?php foreach ($documents as $d): ?>
  15. <li <?php if ($i == count($documents)): ?>class="last"<?php endif ?> ><a href="<?php echo Route::url('site_documents/frontend/documents/show', array('url' => $d->document->document_url)) ?>"><?php echo $d->document->document_title ?></a></li>
  16. <?php $i++ ?>
  17. <?php endforeach ?>
  18. <?php endif ?>
  19. </ul>
markuz
Wyświetlanie 2 pierwszych tytułów:

  1. foreach($documents as $i => $d) {
  2. if($i > 2) break;
  3. echo $d->document->document_title;
  4. }
dominick
Już chyba przemęczenie materiału po całej nocce... zlepiłem coś takiego i wywala błąd
  1. <ul class="footer_nav">
  2.  
  3. <?php $i = 1; ?>
  4. <?php foreach ($documents as $i => $d): ?>
  5. <?php if($i > 2):?>
  6. <?php break;?>
  7.  
  8. <li <?php if ($i == count($documents)): ?>class="last"<?php endif ?> ><a href="<?php echo Route::url('site_documents/frontend/documents/show', array('url' => $d->document->document_url)) ?>"><?php echo $d->document->document_title ?></a></li>
  9. <?php $i++ ?>
  10. <?php endforeach ?>
  11. <?php endif ?> <?endif?>
  12. </ul>
markuz
  1. $output = '<ul class="footer_nav">';
  2. foreach($documents as $i => $d) {
  3. $class = $i == count($documents) - 1 ? 'last' : '';
  4. $output .= '<li class="'.$class.'"><a href="'.Route::url('site_documents/frontend/documents/show', array('url' => $d->document->document_url)).'">'.$d->document->document_title.'</a></li>';
  5. }
  6. $output .= '</ul>';
  7. echo $output;


Jeszcze nie jestem pewny co będzie pod $i bo to klucz tabeli (domyślnie od 0 do count($tabela) - 1 aczkolwiek dobrze jakbyś to sprawdził).
Ładniej?
dominick
Owiele ładniej dzięki za pomoc.
poniżej działający kod, który wyświetla od 2 i powyżej 2 pozycji
  1. <?php
  2. $output = '<ul class="footer_nav">';
  3. foreach($documents as $i => $d) {
  4. if($i <= 1) {
  5. $class = $i - 1 == count($documents) ? 'last' : '';
  6. $output .= '<li class="'.$class.'"><a href="'.Route::url('site_documents/frontend/documents/show', array('url' => $d->document->document_url)).'">'.$d->document->document_title.'</a></li>';
  7.  
  8. }}
  9. $output .= '</ul>';
  10. echo $output;
  11.  
  12. ?>
  13. <?php
  14. $output = '<ul class="footer_nav">';
  15. foreach($documents as $i => $d) {
  16. if($i > 1) {
  17. $class = $i - 1 == count($documents) ? 'last' : '';
  18. $output .= '<li class="'.$class.'"><a href="'.Route::url('site_documents/frontend/documents/show', array('url' => $d->document->document_url)).'">'.$d->document->document_title.'</a></li>';
  19.  
  20. }}
  21. $output .= '</ul>';
  22. echo $output;
  23.  
  24. ?>


wciskam "pomógł" smile.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.