Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 12

Discussione: problema nell'usort

  1. #1
    Utente di HTML.it
    Registrato dal
    Apr 2014
    Messaggi
    323

    problema nell'usort

    salve, ho questo codice recuperato :
    http://stackoverflow.com/questions/2...-file-with-php

    ed ho fatto qualche modifica ..ed il codice è questo:
    codice:
    <?phpdefine('MAX_PER_PAGE',10);
    // sanity checks for per-page and page index
    $numPosts = ctype_digit((string)$_GET['perpage']) ? $_GET['perpage'] : 5;
    $ostart = $start = max(1, ctype_digit((string)$_GET['page']) ? $_GET['page'] : 1) - 1;
    
    
    $mode = 0; 
    if ($mode == 0) { 
        $file = "photo/".$_GET["dir"]."/photo.txt";
    }
    
    
    // read the file into an array, strip newlines and ignore empty lines
    file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES | FILE_TEXT);
    
    
    // sort array (see custom function at bottom)
    usort($line, 'albumsort');
    
    
    $lines = count($line);
    
    
    // get total number of pages
    $numPages = ceil($lines / $numPosts);
    
    
    // additional sanity checks (also sets $ostart if it was invalid; used later)
    $numPosts = min(MAX_PER_PAGE, max(1, $numPosts));
    if ($start * $numPosts > $lines ) {
        $ostart = $start = max(0, $lines - $numPosts);
    }
    else {
        $start *= $numPosts;
    }
    
    
    // Only grab the part of the array we need
    $sliced = array_slice($line, $start, $numPosts);
    echo "<div id=\"photo\">\n";
    echo '<ul class="album">';
            
    // loop through posts, but break early if we run out
    for ($n = 0; $n < $numPosts && isset($sliced[$n]); $n++ ) {
        $photo = explode("|", $sliced[$n]); 
    
    
        if (isset($photo[0])) {     
    	?>
    	<li><a href="photo/<?php echo $_GET["dir"]; ?>/<?php $photo[0]; ?>"><img src="photo/thumb/<?php echo $_GET["dir"]; ?>/<?php $photo[0]; ?>"><p><?php echo $photo[1];?></p></li>
    	<?php	
        }
    }
    echo "</ul>\n\n";
    echo "</div>\n\n";
    // back link
    if ($ostart > 0) {
        echo "<a href=\"{$_SERVER['SCRIPT_NAME']}&perpage={$numPosts}&page={$ostart}\">&larr; Older</a>";
    }
    else {
        echo "None Older";
    }
    echo " || ";
    // forward link
    if ($ostart + 1 < $numPages) {
        $next = $ostart + 2;
        echo "<a href=\"{$_SERVER['SCRIPT_NAME']}&perpage={$numPosts}&page={$next}\">Newer &rarr;</a>";
    }
    else {
        echo "None Newer";
    }
    
    
    function albumsort($a, $b) {
        $dateA = strtotime(substr($a, 0, strpos($a, '|')));
        $dateB = strtotime(substr($b, 0, strpos($b, '|')));
    
    
        if ($dateA == $dateB) {
            return 0;
        }
        elseif ($dateA > $dateB) {
            return -1;
        }
        else {
            return 1;
        }
    }
    ?>
    gli errore sono questi:
    codice:
    Notice: Undefined index: perpage in D:\xampp\htdocs\gallery\index.php on line 4
    
    Notice: Undefined index: page in D:\xampp\htdocs\gallery\index.php on line 5
    
    Warning: usort() expects parameter 1 to be array, null given in D:\xampp\htdocs\gallery\index.php on line 16
    
    Warning: array_slice() expects parameter 1 to be array, null given in D:\xampp\htdocs\gallery\index.php on line 33
    
    
    
    None Older || None Newer
    come posso risolvere il problema ?

    avete idea?

    grazie mille e buona giornata.

  2. #2
    Utente di HTML.it L'avatar di clasku
    Registrato dal
    Aug 2006
    Messaggi
    3,197
    i primi due notice ti dicono che i due indici di $_GET non sono definiti
    il terzo che ad usort stai passando qualcosa di diverso da un array (tu passi $line, non mi pare di leggere questa variabile in altre parti del tuo codice)
    il quarto idem come sopra

  3. #3
    Utente di HTML.it
    Registrato dal
    Apr 2014
    Messaggi
    323
    ciao, io per la get faccio cosi:
    http://localhost/gallery/?dir=varie
    e doveva andare .. invece mi da quei errori..

    Non capisco cosa mi dici essendo fatto copia/incolla e modificato alcune cose che sapevo come fare..

    ti allego il file .txt


    se mi dici come fare ti sarei grato.

    buon 1 maggio.
    File allegati File allegati

  4. #4
    Utente di HTML.it L'avatar di clasku
    Registrato dal
    Aug 2006
    Messaggi
    3,197
    1. nell'url, oltre a dir, devi mettere due parametri di nome perpage e page, così
      localhost/?dir=varie&perpage=5&page=1 (page, ovviamente, varierà se non sei alla prima pagina)
    2. devi dare un nome all'array che si crea con file(), vedi il mio commento nel codice sotto


    Codice PHP:
    <?phpdefine('MAX_PER_PAGE',10);
    // sanity checks for per-page and page index
    $numPosts ctype_digit((string)$_GET['perpage']) ? $_GET['perpage'] : 5;
    $ostart $start max(1ctype_digit((string)$_GET['page']) ? $_GET['page'] : 1) - 1;


    $mode 0
    if (
    $mode == 0) { 
        
    $file "photo/".$_GET["dir"]."/photo.txt";
    }


    // read the file into an array, strip newlines and ignore empty lines

    // MIO COMMENTO
    // qui mancava il nome della variabile e dovrebbe risolvere il problema di usort e array_slice
    $line file($fileFILE_IGNORE_NEW_LINES FILE_SKIP_EMPTY_LINES FILE_TEXT);


    // sort array (see custom function at bottom)
    usort($line'albumsort');


    $lines count($line);


    // get total number of pages
    $numPages ceil($lines $numPosts);


    // additional sanity checks (also sets $ostart if it was invalid; used later)
    $numPosts min(MAX_PER_PAGEmax(1$numPosts));
    if (
    $start $numPosts $lines ) {
        
    $ostart $start max(0$lines $numPosts);
    }
    else {
        
    $start *= $numPosts;
    }


    // Only grab the part of the array we need
    $sliced array_slice($line$start$numPosts);
    echo 
    "<div id=\"photo\">\n";
    echo 
    '<ul class="album">';
            
    // loop through posts, but break early if we run out
    for ($n 0$n $numPosts && isset($sliced[$n]); $n++ ) {
        
    $photo explode("|"$sliced[$n]); 


        if (isset(
    $photo[0])) {     
        
    ?>
        <li><a href="photo/<?php echo $_GET["dir"]; ?>/<?php $photo[0]; ?>"><img src="photo/thumb/<?php echo $_GET["dir"]; ?>/<?php $photo[0]; ?>"><p><?php echo $photo[1];?></p></li>
        <?php   
        
    }
    }
    echo 
    "</ul>\n\n";
    echo 
    "</div>\n\n";
    // back link
    if ($ostart 0) {
        echo 
    "<a href=\"{$_SERVER['SCRIPT_NAME']}&perpage={$numPosts}&page={$ostart}\">&larr; Older</a>";
    }
    else {
        echo 
    "None Older";
    }
    echo 
    " || ";
    // forward link
    if ($ostart $numPages) {
        
    $next $ostart 2;
        echo 
    "<a href=\"{$_SERVER['SCRIPT_NAME']}&perpage={$numPosts}&page={$next}\">Newer &rarr;</a>";
    }
    else {
        echo 
    "None Newer";
    }


    function 
    albumsort($a$b) {
        
    $dateA strtotime(substr($a0strpos($a'|')));
        
    $dateB strtotime(substr($b0strpos($b'|')));


        if (
    $dateA == $dateB) {
            return 
    0;
        }
        elseif (
    $dateA $dateB) {
            return -
    1;
        }
        else {
            return 
    1;
        }
    }
    ?>

  5. #5
    Utente di HTML.it
    Registrato dal
    Apr 2014
    Messaggi
    323
    ti ringrazio moltissimo per avermelo fatto funzionare.. ora ho due domande.

    1) Per evitare che scrivo localhost/gallery/?dir=varie&perpage=20&page=1 come posso fare?
    2) Perché non va in ordine come è file txt dalla prima foto all'ultima..? invece me le mette a caso?

    grazie mille di nuovo.

  6. #6
    Utente di HTML.it L'avatar di clasku
    Registrato dal
    Aug 2006
    Messaggi
    3,197
    Spiega cosa vuoi ottenere esattamente

  7. #7
    Utente di HTML.it
    Registrato dal
    Apr 2014
    Messaggi
    323
    ok, ora ti spiego :

    Per problema 1: Vorrei fare che quando digito ?dir=varie senza i parametri della paginazione.
    Per problema 2: Vorrei andare in ordine come e il documento photo.txt (se lo ai aperto).

    idee?

    grazie mille.

  8. #8
    Utente di HTML.it L'avatar di clasku
    Registrato dal
    Aug 2006
    Messaggi
    3,197
    Senza i parametri di paginazione otterrai una pagina unica con tutte le foto: va bene questo?
    Per seguire l'ordine, basta che non usi usort

  9. #9
    Utente di HTML.it
    Registrato dal
    Apr 2014
    Messaggi
    323
    si, e come faccio?

    grazie mille.

    EDIT - secondo problema risolto.. commentando // usort.

    l'altro?
    Ultima modifica di LedGiallo; 01-05-2015 a 16:03

  10. #10
    Utente di HTML.it L'avatar di clasku
    Registrato dal
    Aug 2006
    Messaggi
    3,197
    prendi l'array $line che contiene tutte le righe, lo scorri con un foreach e per ogni riga sui explode come hai fatto prima
    in codice molto semplice
    Codice PHP:
    foreach($line as $l) {
      
    $foto explode("|"$l);
      
    // stampi a schermo i dati 


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.