Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Problem z wdrożeniem instrukcji if w kodzie.
dominick
post 16.01.2015, 11:52:40
Post #1





Grupa: Zarejestrowani
Postów: 88
Pomógł: 0
Dołączył: 30.05.2014

Ostrzeżenie: (0%)
-----


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>
Go to the top of the page
+Quote Post
markuz
post 16.01.2015, 12:05:33
Post #2





Grupa: Zarejestrowani
Postów: 1 240
Pomógł: 278
Dołączył: 11.03.2008

Ostrzeżenie: (0%)
-----


Wyświetlanie 2 pierwszych tytułów:

  1. foreach($documents as $i => $d) {
  2. if($i > 2) break;
  3. echo $d->document->document_title;
  4. }


Ten post edytował markuz 16.01.2015, 12:07:58


--------------------
Go to the top of the page
+Quote Post
dominick
post 16.01.2015, 12:13:00
Post #3





Grupa: Zarejestrowani
Postów: 88
Pomógł: 0
Dołączył: 30.05.2014

Ostrzeżenie: (0%)
-----


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>
Go to the top of the page
+Quote Post
markuz
post 16.01.2015, 12:19:35
Post #4





Grupa: Zarejestrowani
Postów: 1 240
Pomógł: 278
Dołączył: 11.03.2008

Ostrzeżenie: (0%)
-----


  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?

Ten post edytował markuz 16.01.2015, 12:23:09


--------------------
Go to the top of the page
+Quote Post
dominick
post 16.01.2015, 12:26:54
Post #5





Grupa: Zarejestrowani
Postów: 88
Pomógł: 0
Dołączył: 30.05.2014

Ostrzeżenie: (0%)
-----


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
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 18.07.2025 - 02:23