Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> array include?
qoqo
post
Post #1





Grupa: Zarejestrowani
Postów: 53
Pomógł: 0
Dołączył: 5.06.2014

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


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?
Go to the top of the page
+Quote Post
Spawnm
post
Post #2





Grupa: Moderatorzy
Postów: 4 069
Pomógł: 497
Dołączył: 11.05.2007
Skąd: Warszawa




Zmień echo na include i w array trzymaj nazwy lików z reklamami (IMG:style_emoticons/default/wink.gif)
Go to the top of the page
+Quote Post
Puszy
post
Post #3





Grupa: Zarejestrowani
Postów: 279
Pomógł: 42
Dołączył: 10.10.2011

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


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





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


wystarczy;
include.php
  1. <?php
  2. return array(1, 2, 3);


strona.php
  1. <?php
  2. $vars = include 'include.php';
Go to the top of the page
+Quote Post

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

 



RSS Aktualny czas: 23.08.2025 - 16:04