Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: array include?
Forum PHP.pl > Forum > PHP
qoqo
Witam. Chcę w pewnym znanym CMS'ie wyświetlać reklamy między postami.

Mam taką modyfikację:



  1.  
  2. <?php
  3. /*
  4. Key = Position of the Ad
  5. Value = The AdSense Banner Code
  6. */
  7. $advertisements = array('3' => '<!-- adsense ad code 1 here -->',
  8. '6' => '<!-- adsense ad code 2 here -->',
  9. '9' => '<!-- adsense ad code 3 here -->');
  10.  
  11. $i = 1;
  12.  
  13. while (have_posts()) : the_post();
  14. ?>
  15.  
  16. <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
  17. <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
  18. <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
  19.  
  20. <div class="entry">
  21. <?php the_content('Read the rest of this entry &raquo;'); ?>
  22. </div>
  23.  
  24. <p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments ť', '1 Comment ť', '% Comments ť'); ?></p>
  25. </div>
  26.  
  27. <?php
  28. if(array_key_exists($i, $advertisements)) {
  29.  
  30. // you can add custom HTML code here
  31. echo $advertisements[$i];
  32. }
  33.  
  34. $i++;
  35.  
  36. endwhile;
  37. ?>
  38.  
  39.  
  40.  



Wyświetla się wszystko w porządku. Natomiast problemem jest, jeżeli chcę zaincludować plik z daną reklamą.

Czyli chodzi o to:

  1. $advertisements = array('3' => '<!-- adsense ad code 1 here -->',
  2. '6' => '<!-- adsense ad code 2 here -->',
  3. '9' => '<!-- adsense ad code 3 here -->');


W jaki sposób mogę w miejscu np.
'<!-- adsense ad code 1 here -->', includować jakiś plik?

Czy może będzie jakieś inne rozwiązanie zastępujące array?
Spawnm
Zmień echo na include i w array trzymaj nazwy lików z reklamami wink.gif
Puszy
Jeżeli chcesz includować dynamicznie generowane dane możesz skorzystać z serializacji:

  1. $data = array('a', 2, 'b');
  2. file_put_contents('dane.txt', serialize($data)); // dopliku dane.txt wrzucasz zserializowaną tablicę $data
  3.  
  4.  
  5. $dataFromFile = unserialize(file_get_contents('dane.txt')); // do zmiennej $dataFromFile przypisujesz tablicę $data
Pyton_000
wystarczy;
include.php
  1. <?php
  2. return array(1, 2, 3);


strona.php
  1. <?php
  2. $vars = include 'include.php';
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.