count probleme

Secţiune dedicată începatorilor.

Moderatori: Moderatori, Start Moderator

betonic
Average Member
Mesaje: 112
Membru din: Lun Feb 12, 2007 8:45 pm
Contact:

count probleme

Mesajde betonic » Joi Aug 02, 2012 7:18 pm

Am o baza de date de genu :

Id nume localitate referent
1 vasile cluj 0
2 ion alba 1
3 alex suceava 1
4 ana ploiesti 2
5 mircea iasi 1
6 miihai bacau 3

As vrea sa imi afiseze rezultatele cam asa

Pozite Id Nume Localitate Nr ref
1 1 vasile cluj 3
2 2 ion alba 1
3 3 alex suceava 1

Practic sa le contorizeze dupa nr de ref.
Am ajuns la chestia asta.

$query = 'SELECT id, COUNT(referent) FROM `mail4` WHERE referent>0 GROUP BY referent ORDER BY COUNT(referent) DESC ';

Mi le ordoneaza dupa nr de ref dar celelate date (nume, localitatea, idul) nu mi le afiseaza.

Chiar daca nu-s nou pe acest forum nu folosesc php decat de cateva ori pe an, so daca imi dati un sfat raman dator. Mersi
[/b]



quadmachine
Senior Member
Mesaje: 807
Membru din: Sâm Iul 10, 2010 4:58 pm
Localitate: Ploiesti
Contact:

Mesajde quadmachine » Joi Aug 02, 2012 7:56 pm

Ia vezi asa functioneaza,eu am testat si imi functioneaza.Astept unr aspuns.

Cod: Selectaţi tot

SELECT id, nume, localitate, COUNT(referent) as `totalRef` FROM `table` WHERE `referent` > 0 GROUP BY COUNT(referent) DESC

Cod: Selectaţi tot

Nu confundati femeia de serviciu cu profesoara de Limba si Literatura Romana.

betonic
Average Member
Mesaje: 112
Membru din: Lun Feb 12, 2007 8:45 pm
Contact:

Mesajde betonic » Joi Aug 02, 2012 8:32 pm

"Invalid use of group function"

Asta imi zice.

betonic
Average Member
Mesaje: 112
Membru din: Lun Feb 12, 2007 8:45 pm
Contact:

Mesajde betonic » Joi Aug 02, 2012 8:33 pm

Cod: Selectaţi tot

<?php

require_once('config.php');

ini_set("display_errors", true);
error_reporting(E_ALL);

$sql = "SET NAMES 'utf8'";
mysql_query($sql, $conn);

$query = 'SELECT id, nume, localitate, COUNT(referent) as `totalRef` FROM `test` WHERE `referent` > 0 GROUP BY COUNT(referent) DESC';

$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result))


{
   

$total = ''. $row['COUNT(referent)'] .'';
   echo "   <tr>
    <td bgcolor=\"#FFFF99\" cellpadding=\"2\" >". $row['nume']." </td>
    <td bgcolor=\"#D1EFCF\" style=\"padding-top:3px; padding-bottom:3px;\" >". $total."</td>
</td>
<td bgcolor=\"#D1EFCF\" style=\"padding-top:3px; padding-bottom:3px;\" >". $row['id']."</td>
</td>
  </tr>";

}
?>

quadmachine
Senior Member
Mesaje: 807
Membru din: Sâm Iul 10, 2010 4:58 pm
Localitate: Ploiesti
Contact:

Mesajde quadmachine » Joi Aug 02, 2012 10:57 pm

Cod: Selectaţi tot

<?php
ini_set("display_errors", true);
error_reporting(E_ALL);

require_once('config.php');

                           $sql = "SET NAMES 'utf8'";
                           mysql_query($sql, $conn);

                           $query = mysql_query("SELECT id, nume, localitate, COUNT(referent) as `totalRef`
                           FROM `test` WHERE `referent` > 0 GROUP BY COUNT(referent) DESC") or die(mysql_error());
                           
                                if(mysql_num_rorws($query) == 0){
                             
                                       print 'Fara rezultate,posibil ca tabela safie goala.';
                             
                                } else {

                                  while($row = mysql_fetch_array($result)){

                                  // totalRef nu l-am pus de nebun acolo,chiar daca ii face count il ia ca "totalRef".

                                  print "<tr>
                                  <td bgcolor=\"#FFFF99\" cellpadding=\"2\" >". $row['nume']." </td>
                                  <td bgcolor=\"#D1EFCF\" style=\"padding-top:3px; padding-bottom:3px;\" >". $row['totalRef']."</td>
                                  </td>
                                  <td bgcolor=\"#D1EFCF\" style=\"padding-top:3px; padding-bottom:3px;\" >". $row['id']."</td>
                                  </td>
                                  </tr>";
                                  }
?>

Cod: Selectaţi tot

Nu confundati femeia de serviciu cu profesoara de Limba si Literatura Romana.

betonic
Average Member
Mesaje: 112
Membru din: Lun Feb 12, 2007 8:45 pm
Contact:

Mesajde betonic » Vin Aug 03, 2012 12:47 am

tie iti merge?

Mie nu-mi face nimic chiar daca am corectat mica gresala de la :
if(mysql_num_rows($query) == 0)

quadmachine
Senior Member
Mesaje: 807
Membru din: Sâm Iul 10, 2010 4:58 pm
Localitate: Ploiesti
Contact:

Mesajde quadmachine » Vin Aug 03, 2012 2:27 am

Cod: Selectaţi tot

SELECT id, nume, localitate, COUNT(referent) as `totalRef`
FROM `test` GROUP BY `referent` HAVING COUNT(referent) > 0


Asta sigur functioneaza.

Cod: Selectaţi tot

Nu confundati femeia de serviciu cu profesoara de Limba si Literatura Romana.

betonic
Average Member
Mesaje: 112
Membru din: Lun Feb 12, 2007 8:45 pm
Contact:

Mesajde betonic » Vin Aug 03, 2012 5:30 pm

nu merge

R3dC0d3
PHPRomania Supporter
Mesaje: 11
Membru din: Lun Aug 15, 2011 10:05 pm

Mesajde R3dC0d3 » Dum Aug 05, 2012 2:06 pm

Nu inteleg:
Tu ai pus asa:
Pozite Id Nume Localitate Nr ref
1 1 vasile cluj 3
2 2 ion alba 1
3 3 alex suceava 1

Daca ma uit bine in tabelul tau cred ca ai vrut sa pui :
Pozite Id Nume Localitate Nr ref
1 1 mihai bacau 3
2 2 ana ploiesti 2
3 3 alex suceava 1
...

Ce ai scris tu nu are niciun sens vrei sai dai lui vasile valoarea lui mihai

betonic
Average Member
Mesaje: 112
Membru din: Lun Feb 12, 2007 8:45 pm
Contact:

Mesajde betonic » Dum Aug 05, 2012 6:12 pm

nu.

Eu am facut un concurs si fiecare la inregistrare primeste o pagina.
De exemplu ion are idul 50. Dupa ce s-a inregistrat ii dau linku asta care il distribuie.

http://aaa.com/referent.php?referent=50

Asata o da mai departe cine se inscrie din pagina asta are la referent nr 50
(corespunzator cu id lui ion).

Apoi le grupez si stiu cati oameni a adus ion.

Ce am scris eu era grupare dupa nr de ref.

R3dC0d3
PHPRomania Supporter
Mesaje: 11
Membru din: Lun Aug 15, 2011 10:05 pm

Mesajde R3dC0d3 » Dum Aug 05, 2012 6:49 pm

Incerca asta:
$query = 'SELECT id, nume, localitate, COUNT(referent) as `totalRef` FROM `test` WHERE `referent` > 0 GROUP BY `totalRef` DESC';

betonic
Average Member
Mesaje: 112
Membru din: Lun Feb 12, 2007 8:45 pm
Contact:

Mesajde betonic » Lun Aug 06, 2012 12:59 pm

$query = 'SELECT id, nume, localitate, COUNT(referent) as `totalRef` FROM `test` WHERE `referent` > 0 GROUP BY `referent` ORDER BY `totalRef` DESC';

asta merge, ii grupeaza dupa nr de referinte , doar $row['nume'] afisat nu numele celui care a adus referintele ci e numele primul inregistrat.

adica daca ion care are idul nr 5 a inregistrat pe vasile, mihai si gheorghe , atunci el idul 5 are 3 referinte , interogarea de mai sus le grupeaza pe cele 3 inregistrari insa cand printe $row['nume'] imi afiseaza numele primului inregistrat , cazu de fata vasile si eu as vrea sa imi zica ca inregistrarile is facute de idul nr 5 adica ion :)) complicat

Reventon
Average Member
Mesaje: 68
Membru din: Lun Ian 31, 2011 5:07 pm

Mesajde Reventon » Lun Aug 06, 2012 2:02 pm

ti-am simulat un query pe serverul meu.
ai de dat o bere :)
acuma iti arata cati referali are fiecare, cat si numele parintelui

-----------

SELECT tabel1.id, tabel1.nume, tabel1.localitate, tabel1.referent, COUNT(tabel2.nume) AS nr_referenzi, tabel3.nume AS parinte
FROM utilizator AS tabel1

LEFT JOIN utilizator AS tabel2
ON tabel2.referent = tabel1.id

LEFT JOIN utilizator AS tabel3
ON tabel1.referent = tabel3.id

GROUP BY tabel1.id
ORDER BY tabel1.id

-----------------
asa arata tabelul rezultat:

id nume localitate referent nr_referenzi parinte
1 vasile cluj 0 3 NULL
2 ion alba 1 1 vasile
3 alex suceava 1 1 vasile
4 ana ploiesti 2 0 ion
5 mircea iasi 1 0 vasile
6 mihai bacau 3 0 alex

betonic
Average Member
Mesaje: 112
Membru din: Lun Feb 12, 2007 8:45 pm
Contact:

Mesajde betonic » Lun Aug 06, 2012 3:34 pm

de asta aveam nevoie . mersi. cum facem cu berea aia? :) daca ai un cont la brd iti dau de 10 beri . serios vb

Reventon
Average Member
Mesaje: 68
Membru din: Lun Ian 31, 2011 5:07 pm

Mesajde Reventon » Mar Aug 07, 2012 2:07 pm

ma bucur ca te-am putut ajuta.
am cont la brd :)


Înapoi la “PHP Incepători”

Cine este conectat

Utilizatori ce ce navighează pe acest forum: Niciun utilizator înregistrat și 47 vizitatori