Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it L'avatar di diaz
    Registrato dal
    Apr 2000
    residenza
    Berlin
    Messaggi
    379

    while loop stile wordpress

    sto realizzando un sito in PHP indipendente da Wordpress
    ma mi piacerebbe adottare lo stile che questo CMS usa

    Codice PHP:
    <?php while (gallery_get_previews(10)) : ?>

      <div>
        <?php image_title(); ?>

        [img]<?php image_url(); ?>[/img]
      </div>
    <?php endwhile; ?>
    cio che non riesco a fare e' fare il fetch e muovere il pointer ogni volta che il ciclo while torna a capo

    la funzione che ho realizzato e' la seguente
    inizialmente la funzione era piu' semplice, poi ho iniziato a usare mysql_data_seek() sperando che potesse fare il lavoro
    Codice PHP:
    function gallery_get_previews($quantity)

    {

      

      global 
    $NSbDB$row;

      

      
    $sql "SELECT * FROM images";

      
    $sql .= " ORDER BY id_image ASC ";

      if(
    $quantity$sql .= " LIMIT ".$quantity;

      

      
    $result mysql_query($sql$NSbDB);

      

      

      for (
    $i mysql_num_rows($result) - 1$i >= 0$i--) {

          if (!
    mysql_data_seek($result$i)) {

              return 
    false;

          }

          

          elseif (!(
    $row mysql_fetch_assoc($result))) {

              return 
    false;

          }

          return 
    $result;

      }
      


    E da quel giorno, se andavo da qualche parte, io ci andavo correndo!

    Tweeting @apphancer | Views my own – except when I say things I don’t really think.

  2. #2
    Per come hai strutturato il codice del loop stile wordpress, la funzioe gallery_get_previews() NON deve eseguire la query. DEVE solo eseguire un singolo fetch o, in alternativa, eseguire la query SOLO la prima volta che viene richiamata, ed eseguire il fetch tutte le altre.

    Altrimenti introduci un inutile overhead che riduce sensibilmente le performance.

  3. #3
    Utente di HTML.it L'avatar di diaz
    Registrato dal
    Apr 2000
    residenza
    Berlin
    Messaggi
    379
    grazie Filippo

    Invio il codice per chi fosse interessato o se qualcuno ha consigli su come migliorarlo

    Codice PHP:
            <?php while (gallery_get_previews('random'6)) : ?>
              <div>
                [url="<?php gallery_image_link() ?>"][img]<?php gallery_image_url('thumb'); ?>[/img][/url]
                <?php gallery_image_title(); ?>
              </div>
            <?php endwhile; ?>
    la funzione
    Codice PHP:
    function gallery_get_previews($method$quantity)
    {
      
      
    // the following variable is the resource of the query, it must be unique to this function
      
    global $gallery_get_previews_result;
      
      global 
    $conn$row;
      
      
    // prepare query statement
      
    $sql "SELECT * FROM images";
      
    $sql .= " ORDER BY id_image ASC ";
      if(
    $quantity$sql .= " LIMIT ".$quantity;
      
      if(!
    $gallery_get_previews_result)
      {
        
    $gallery_get_previews_result mysql_query($sql$conn);
      }
      
      
    $row mysql_fetch_object($gallery_get_previews_result);
      
      return 
    $row;
      
      
    // free the result
      
    mysql_free_result($gallery_get_previews_result);

    E da quel giorno, se andavo da qualche parte, io ci andavo correndo!

    Tweeting @apphancer | Views my own – except when I say things I don’t really think.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.