Pagina 1 di 4 1 2 3 ... ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 39
  1. #1
    Utente di HTML.it
    Registrato dal
    Nov 2007
    Messaggi
    33

    Scelta random campo SQL

    Ciao, ho un DB che contiene delle immagini a cui sono associati dei dati, uno dei quali è la categoria di appartenenza. Ora, vorrei che la query scegliesse una delle tre categorie (paesaggi,ritratti,macro), poi scegliesse casualmente due immagini appartenenti alla categoria scelta. Ho provato diverse soluzioni, ma niente. Posto il codice, che tra le altre cose non mi visualizza due immagini, ma solo una. Se mi potete aiutare ve ne sarei grato.
    <?
    @include 'config.php';
    $catList = ('Paesaggi', 'Ritratti');
    $i = rand(0,2);
    $catSel = $catList[$i];
    $sql = "SELECT foto_id,foto_nome,foto_size,foto_type, foto, foto_cat,foto_naz FROM img WHERE foto_cat = ".$catSel." ORDER BY RAND() limit 2";
    $result = @mysql_query($sql) or die(mysql_error ());
    while($row = @mysql_fetch_array($result)){
    $id_img = $row['foto_id'];
    $type = $row['foto_type'];
    $img = $row['foto'];
    if (!$id_img)
    {
    echo "Id sconosciuto";
    }else{
    @header ("Content-type: ".$type);
    echo $img;
    echo $img;
    }
    }
    ?>

  2. #2
    Innanzitutto scriviamo il codice con i tag giusti:
    Codice PHP:
    <?
    @include 'config.php';
    $catList = ('Paesaggi''Ritratti');
    $i rand(0,2);
    $catSel $catList[$i];
    $sql "SELECT foto_id,foto_nome,foto_size,foto_type, foto, foto_cat,foto_naz FROM img WHERE foto_cat = ".$catSel." ORDER BY RAND() limit 2";
    $result = @mysql_query($sql) or die(mysql_error ());
    while(
    $row = @mysql_fetch_array($result)){
    $id_img $row['foto_id'];
    $type $row['foto_type'];
    $img $row['foto'];
    if (!
    $id_img)
    {
    echo 
    "Id sconosciuto";
    }else{
    @
    header ("Content-type: ".$type);
    echo 
    $img;
    echo 
    $img;

    }
    ?>
    e già così diventa più chiaro.
    Togli tutti gli at (@) perché se ci dovessero essere errori non li vedresti e tanti cari saluti alla soluzione.
    Poi per la scelta casuale della categoria puoi randomizzare l'array (che nel tuo caso non è) ed estrarre il primo valore:
    Codice PHP:
    $catList = array('Paesaggi''Ritratti');
    $catList=array_rand($catList);
    $catSel=$catList[0]; 
    Per visualizzare le immagini sarà meglio riguardare il codice:
    Codice PHP:
    while($row mysql_fetch_array($result)){ 
    $id_img $row['foto_id']; 
    $type $row['foto_type']; 
    $img $row['foto']; 
    if (!
    $id_img

    echo 
    "Id sconosciuto"
    }else{ 
    echo 
    $img
    }  

    Prova così.
    Ce l'ho fatta! - ItalianPixel -

  3. #3
    Utente di HTML.it
    Registrato dal
    Nov 2007
    Messaggi
    33
    codice:
    <? 
    include 'config.php'; 
    $catList = array('Paesaggi', 'Ritratti'); 
    $catList=array_rand($catList); 
    $catSel=$catList[0];
    $sql = "SELECT foto_id,foto_nome,foto_size,foto_type, foto, foto_cat,foto_naz FROM img WHERE foto_cat = ".$catSel."  ORDER BY RAND() limit 2"; 
    $result = @mysql_query($sql) or die(mysql_error ()); 
    while($row = @mysql_fetch_array($result)){ 
    $id_img = $row['foto_id']; 
    $type = $row['foto_type']; 
    $img = $row['foto']; 
    if (!$id_img) 
    { 
    echo "Id sconosciuto"; 
    }else{
    header ("Content-type: ".$type);
    echo $img; 
    }  
    } 
    ?>
    Ho provato, ma non funziona ancora. L'errore è: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY RAND() limit 2' at line 1. Poi ho provato ad eliminare
    codice:
    header ("Content-type: ".$type);
    ma così facendo, si vede del codice anziché lìimmagine. Ho provato alcune modifiche in un file base (dove è data una sola categoria), ma ancora si visualizza solo una foto. Altre idee.

  4. #4
    prova aggiungendo apici singoli a $catSel:

    Codice PHP:
    $sql "SELECT foto_id,foto_nome,foto_size,foto_type, foto, foto_cat,foto_naz FROM img WHERE foto_cat = '".$catSel."'  ORDER BY RAND() LIMIT 2"
    e lascia header ("Content-type: ".$type);

    p.s. i tag per il php sono PHP non CODE
    Ce l'ho fatta! - ItalianPixel -

  5. #5
    Utente di HTML.it
    Registrato dal
    Nov 2007
    Messaggi
    33
    Niente più errori di sintassi, ma le due immagini non si vedono. Non si vede niente. Che sia la query che non ricava alcun risultato?

  6. #6
    In teoria per l'immagine sarebbe meglio usare:
    Codice PHP:
    echo '[img]'.$img.'[/img]'
    stando attento ad inserire il percorso corretto nel caso si trovino dentro ad una cartella.
    Ce l'ho fatta! - ItalianPixel -

  7. #7
    Utente di HTML.it
    Registrato dal
    Nov 2007
    Messaggi
    33
    Le immagini non sono in una cartella, ma nel DB. So che non è la scelta migliore, ma ho voluto fare cosi. Comunque, per ricapitolare, ho due file:
    1)contiene il codice base (che non sceglie casualmente fra più categorie), funziona ma mostra solo una immagine anziché due affiancate. Se però eseguo la query in phpMyAdmin la query funziona e restituisce due righe casuali. Il Codice:
    Codice PHP:
    <?
    @include 'config.php';
      
    $sql "SELECT foto_id,foto_nome,foto_size,foto_type, foto, foto_cat,foto_naz FROM img WHERE foto_cat='Paesaggi' ORDER BY RAND() limit 2";
      
    $result = @mysql_query($sql) or die(mysql_error ());
      
    $row = @mysql_fetch_array($result);
      
    $id_img $row['foto_id'];
      
    $type $row['foto_type'];
      
    $img $row['foto'];
        @
    header ("Content-type: ".$type);
        echo 
    $img;
    ?>
    2)Il file con il codice più completo, ma che rimane bianco. Ti posto il codice aggiornato:
    Codice PHP:
    <? 
    include 'config.php'
    $catList = array('Paesaggi''Ritratti'); 
    $catList=array_rand($catList); 
    $catSel=$catList[0];
    $sql "SELECT foto_id,foto_nome,foto_size,foto_type, foto, foto_cat,foto_naz FROM img WHERE foto_cat = '".$catSel."'  ORDER BY RAND() LIMIT 2";
    $result mysql_query($sql) or die(mysql_error ()); 
    while(
    $row mysql_fetch_array($result)){ 
    $id_img $row['foto_id']; 
    $type $row['foto_type']; 
    $img $row['foto']; 
    if (!
    $id_img

    echo 
    "Id sconosciuto"
    }else{
    header ("Content-type: ".$type);
    echo 
    $img;
    echo 
    '[img]'.$img.'[/img]';
    }  

    ?>

  8. #8
    Codice PHP:
    <? 
    include 'config.php'
    $catList = array('Paesaggi''Ritratti');  
    $catList=array_rand($catList);  
    $catSel=$catList[0]; 
    $result=mysql_query("SELECT * FROM img WHERE foto_cat='$catSel' ORDER BY RAND() LIMIT 2") or die ("Errore query"); 
    while(
    $row=mysql_fetch_array($result)){
         
    $id_img $row['foto_id']; 
         
    $type $row['foto_type']; 
         
    $img $row['foto']; 
         
    header ("Content-type: ".$type); 
         echo 
    $img
    }
    ?>
    Prova questo e vedi se ti da errori
    Ce l'ho fatta! - ItalianPixel -

  9. #9
    Utente di HTML.it
    Registrato dal
    Nov 2007
    Messaggi
    33
    Niente errori, ma rimane bianco come l'altro. C'è un modo per vedere se una query con variabili funziona?

  10. #10
    Prova a togliere header ("Content-type: ".$type); e dimmi cosa ti stampa.
    O prova a mettere header ("Content-type: ".$type); fuori dal ciclo while, in cima al codice.
    Ce l'ho fatta! - ItalianPixel -

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 © 2024 vBulletin Solutions, Inc. All rights reserved.