Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 18
  1. #1

    Ordinamento per nome in tabella amicizie

    Ciao ragazzi! Ho un problema che non riesco a capire come risolvere...Magari per voi è una cosa stupidissima..
    Ho una tabella amicizie composta da questi campi

    id_amicizia (contiene l'id univoco dell'amicizia)
    from_friend (id dell'utente che richiede l'amicizia)
    to_friend (id dell'utente a cui viene richiesta l'amicizia)
    request_date (data in cui è stata richiesta)
    request_status (stato della richiesta 0=in attesa 1=accettata 2=rifiutata)


    solo che mi si pone questo problema: nella query dove estraggo le mie amicizie vorrei estrarle in ordine alfabetico del nome dell'utente il cui nome però è dentro la tabella utenti...

    La query è questa:

    Codice PHP:

            $query_limit 
    mysql_query("SELECT * FROM amicizie 
        WHERE
        ((from_friend=
    $id_utente)OR(to_friend=$id_utente))
        AND request_status=1
        AND friend_blocker=1    
        ORDER BY ...?  "
    ); 
    Dove logicamente $id_utente sarebbe l'id dell'utente loggato...

    Come posso fare??

  2. #2
    ... devo per caso fare una innerjoin?

  3. #3
    Utente di HTML.it L'avatar di nicola75ss
    Registrato dal
    Nov 2004
    Messaggi
    12,923
    Sicuramente devi fare un join.

    Se ti è possibile postare un dump delle due tabelle con alcuni record fittizi è più facile aiutarti.

  4. #4
    allora la tabella amicizie è questa:

    Codice PHP:
    CREATE TABLE IF NOT EXISTS `amicizie` (
      `
    id_amiciziaint(10NOT NULL AUTO_INCREMENT,
      `
    from_friendint(10unsigned NOT NULL,
      `
    to_friendint(10unsigned NOT NULL,
      `
    request_datetimestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      `
    request_statusint(1NOT NULL DEFAULT '0',
      `
    friend_blockerint(1NOT NULL DEFAULT '1',
      `
    id_friend_blockerint(10unsigned DEFAULT NULL,
      
    PRIMARY KEY (`id_amicizia`),
      
    KEY `from_friend` (`from_friend`),
      
    KEY `to_friend` (`to_friend`)
    ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=
    La tabella utenti è questa:

    Codice PHP:
    -- Struttura della tabella `utenti`
    --

    CREATE TABLE IF NOT EXISTS `utenti` (
      `
    idint(10unsigned NOT NULL AUTO_INCREMENT,
      `
    usernamevarchar(30NOT NULL,
      `
    passwordchar(32NOT NULL,
      `
    emailvarchar(100NOT NULL,
      `
    sessovarchar(1NOT NULL,
      `
    anno_nascitayear(4NOT NULL,
      `
    sit_sentimentalevarchar(100NOT NULL,
      `
    provinciavarchar(100NOT NULL,
      `
    cittavarchar(100) DEFAULT NULL,
      `
    tempset('0','1'NOT NULL,
      `
    regdatevarchar(11NOT NULL,
      `
    uidvarchar(32NOT NULL,
      `
    permessiint(11NOT NULL DEFAULT '0',
      
    PRIMARY KEY (`id`),
      
    KEY `username` (`username`,`password`)
    ENGINE=InnoDB  DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='Tabella con le iscrizioni degli utenti' AUTO_INCREMENT=328 

    Spero vada vene..

    il fatto è che non so come utilizzarla questa join per estrarre solo gli amici..

  5. #5
    Utente di HTML.it L'avatar di nicola75ss
    Registrato dal
    Nov 2004
    Messaggi
    12,923
    Se posti anche qualche record rimuovendo eventuali dati sensibili giusto per popolare le due tabelle è meglio.

  6. #6
    Utente di HTML.it L'avatar di nicola75ss
    Registrato dal
    Nov 2004
    Messaggi
    12,923
    Non avendo un'idea chiarissima della situazione posso solo provare a darti qualche suggerimento.

    Per ricavare solo i nomi degli amici potresti fare così

    codice:
    select u.username from utenti as u 
    inner join (
    select distinct if(from_friend=$id_utente,to_friend,from_friend) as amici
    from amicizie
    where (from_friend=$id_utente or to_friend=$id_utente) and request_status=1 and friend_blocker=1) as tab
    on u.id = tab.amici
    order by u.username
    mentre per avere pure tutte le informazioni della tabella amicizie

    codice:
    select a.*,u.username as nome from amicizie as a
    inner join utenti as u on if(from_friend=$id_utente,to_friend,from_friend) = u.id
    where from_friend=$id_utente and request_status=1 and friend_blocker=1
    union
    select a.*,u.username from amicizie as a
    inner join utenti as u on if(from_friend=$id_utente,to_friend,from_friend) = u.id
    where to_friend=$id_utente and request_status=1 and friend_blocker=1
    order by nome
    ma in questo caso non so se sia contemplata la possibilità di reciprocità (tizio e caio rispettivamente from e to e viceversa) e come debba essere gestita, con l'esclusione o meno del doppione.
    Spero di esserti stato comunque utile.

  7. #7
    Grazie per la risposta!!!

    allora ho messo la tua query in questo modo:

    Codice PHP:

    $query_limit 
    mysql_query("SELECT u.username FROM utenti as u 
        INNER JOIN (SELECT DISTINCT if(from_friend=
    $id_utente,to_friend,from_friend) as amici
        FROM amicizie
        WHERE (from_friend=
    $id_utente or to_friend=$id_utente) and request_status=1 and friend_blocker=1) AS tab ON u.id = tab.amici
        ORDER BY u.username"
    );


        while(
    $results mysql_fetch_array($query_limit)) 
        {  
    // apro parentesi del while iniziale che mi estrae tutti i messaggi ricevuti
          
    $id_amicizia=$results['id_amicizia'];
          
    $from_friend=$results['from_friend'];
          
    $to_friend=$results['to_friend'];
          
    $request_date=$results['request_date'];
          
    $request_status=$results['request_status']; 
    Solo che non so se la seconda parte dove estraggo i risultati è giusto perchè mi da i seguenti errori:

    Notice: Undefined index: id_amicizia in ...

    Notice: Undefined index: from_friend in ...

    Notice: Undefined index: to_friend in ...

    Notice: Undefined index: request_date in ...

    Notice: Undefined index: request_status in ...

  8. #8
    Utente di HTML.it L'avatar di nicola75ss
    Registrato dal
    Nov 2004
    Messaggi
    12,923
    Forse sarebbe il caso di provare le query e vedere cosa restituiscono prima di incollarle nel codice php.

    Come ti ho scritto la prima query restituisce SOLO l'elenco dei nominativi, quindi un unico campo. Ovvio che tutte le altre informazioni non siano presenti.

    Provale entrambe e vedi un pò. Una volta chiarito ciò che ti serve penserai a come integrale nel tuo script.

  9. #9
    ho incollato la tua query in phpmyadmin e l'ho eseguita...effettivamente mi tira fuori gli utenti di cui l'utente loggato è amico... a me sembrerebbe andare...

  10. #10
    Utente di HTML.it L'avatar di nicola75ss
    Registrato dal
    Nov 2004
    Messaggi
    12,923
    Prova anche l'altra e vedi quale si adatta meglio alle tue esigenze.

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.