IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

[Tableaux] Tri de tableaux par date


Sujet :

Langage PHP

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Octobre 2008
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2008
    Messages : 11
    Points : 17
    Points
    17
    Par défaut [Tableaux] Tri de tableaux par date
    Bonjour,

    J ai deux tableaux Mysql j'essaye des le trier par date mais je galère un peu.
    l erreur est que mon tableau n est pas considéré comme un tableau, alors il dit ceci:

    Warning: usort() [function.usort]: The argument should be an array
    ------------------------------------------------------------

    actuellement je teste donc differente synthaxe pour
    $tableau_cree_utilise .= array($avoir_cree[$i],$date_cree[$i]);

    apres je verrais pour le tri

    toute aide est la bienvenue.
    merci à tous
    et bonne soirée

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
     
    $tableau_cree_utilise = array();
    $avoir_cree = array();
    $date_cree = array();
    $avoir_utilisee = array();
    $date_utilise = array();
    $total=0;
    $i=0;
    $i2=0;
     
    //affiche les avoirs crees
    $avoir_query = tep_db_query( "SELECT * FROM `customers_balance_history` WHERE customers_id=".(int)$cID." order by date_customers_balance DESC LIMIT 0,100");	
    while ($avoir = tep_db_fetch_array($avoir_query)) {
    $i++;
    									$avoir_cree[$i]=number_format($avoir['amount_customer_balance'],2, ',', ' ');
    									$date_cree[$i]= tep_date_short($avoir['date_customers_balance']);
    									$tableau_cree_utilise .= array($avoir_cree[$i],$date_cree[$i]);
    }
     
    //fin affiche avoir cree/
    //avoir utilisé
    $customers_cli_query = tep_db_query( "select o.orders_id, o.customers_name, o.customers_id, o.payment_method, o.date_purchased, o.last_modified, o.currency, o.currency_value, s.orders_status_name,ot.class,ot.title, ot.text , ot.value as value_tot from " . TABLE_ORDERS . " o left join " . TABLE_ORDERS_TOTAL . " ot on (o.orders_id = ot.orders_id), " . TABLE_ORDERS_STATUS . " s where o.customers_id = '" . (int)$cID . "' and o.orders_status = s.orders_status_id and s.language_id = '" . (int)$languages_id . "'  order by date_purchased DESC");
    while ($customers_cli = tep_db_fetch_array($customers_cli_query)) {
    $i2++;
    								if(($customers_cli['class']=='ot_account_balance')&&($customers_cli['title']=='<br>Total des avoirs disponibles TTC :')){
     
    									$date_utilise=tep_date_short($customers_cli['date_purchased']);
    									$text = str_replace('&euro;','',$customers_cli['text']);
    									$text = str_replace('<br>','',$text);
    									 $avoir_utilise[$i2]=number_format($text,2, ',', ' ');
    									 $tableau_cree_utilise .= array($avoir_utilise[$i2],$date_utilise[$i2]);
     
    								}//end if
    }//fin while commande avoir utilisé
     
     
    // trier fonction
    function compare($a,$b)
    {return strcmp($a["date"],$b["date"]);}
     
    usort($tableau_cree_utilise,compare);
     
    // output results
    for($x = 0; $x < count($tableau_cree_utilise); $x++)
    {
        //test affiche tableau cree et utilisee trié
    	echo $tableau_cree_utilise[$x]."<br>";
    	 //test affiche tableau cree et utilisee
    	echo'<br><hr>';
    	print_r ($tableau_cree_utilise[$x])."<br>";
    }

  2. #2
    Membre éclairé Avatar de micetf
    Homme Profil pro
    Professeur des Ecoles
    Inscrit en
    Mai 2009
    Messages
    557
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ardèche (Rhône Alpes)

    Informations professionnelles :
    Activité : Professeur des Ecoles
    Secteur : Enseignement

    Informations forums :
    Inscription : Mai 2009
    Messages : 557
    Points : 831
    Points
    831
    Par défaut
    Bonsoir,
    Et quelque chose comme ça ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    $tableau_cree_utilise[] = array('avoir'=>$avoir_cree[$i],'date'=>$date_cree[$i]);
    Fred

  3. #3
    Membre à l'essai
    Profil pro
    Inscrit en
    Octobre 2008
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2008
    Messages : 11
    Points : 17
    Points
    17
    Par défaut
    merci, effectivement au moins le tableau est crée, reste à la trier.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    $tableau_cree_utilise = array();
    $avoir_cree = array();
    $date_cree = array();
    $avoir_utilisee = array();
    $date_utilise = array();
    $total=0;
    $i=0;
    $i2=0;
     
    //affiche les avoirs crees
    $avoir_query = tep_db_query( "SELECT * FROM `customers_balance_history` WHERE customers_id=".(int)$cID." order by date_customers_balance DESC LIMIT 0,100");	
    while ($avoir = tep_db_fetch_array($avoir_query)) {
    $i++;
     
    									$date_cree[$i]= tep_date_short($avoir['date_customers_balance']);
    									$avoir_cree[$i]=number_format($avoir['amount_customer_balance'],2, ',', ' ');
    									$tableau_cree_utilise[$i] = array('date'=>$date_cree[$i],'avoir'=>$avoir_cree[$i]);
    									//echo 'avoir creé:'.$avoir_cree[$i].' le:'.$date_cree[$i].'<br>';
     
    }
     
    //fin affiche avoir cree/
    //avoir utilisé
    $customers_cli_query = tep_db_query( "select o.orders_id, o.customers_name, o.customers_id, o.payment_method, o.date_purchased, o.last_modified, o.currency, o.currency_value, s.orders_status_name,ot.class,ot.title, ot.text , ot.value as value_tot from " . TABLE_ORDERS . " o left join " . TABLE_ORDERS_TOTAL . " ot on (o.orders_id = ot.orders_id), " . TABLE_ORDERS_STATUS . " s where o.customers_id = '" . (int)$cID . "' and o.orders_status = s.orders_status_id and s.language_id = '" . (int)$languages_id . "'  order by date_purchased DESC");
    while ($customers_cli = tep_db_fetch_array($customers_cli_query)) {
    $i2++;
    								if(($customers_cli['class']=='ot_account_balance')&&($customers_cli['title']=='<br>Total des avoirs disponibles TTC :')){
     
    									$date_utilise[$i2]=tep_date_short($customers_cli['date_purchased']);
    									$text = str_replace('&euro;','',$customers_cli['text']);
    									$text = str_replace('<br>','',$text);
    									$avoir_utilise[$i2]=number_format($text,2, ',', ' ');
    									$tableau_cree_utilise[$i2] = array('date'=>$date_utilise[$i2],'avoir'=>$avoir_utilise[$i2]);
    									//echo 'avoir utilisé:'.$avoir_utilise[$i2].' le:'.$date_utilise[$i2].'<br>';
    								}//end if
    }//fin while commande avoir utilisé
     
    array_multisort($tableau_cree_utilise, SORT_NUMERIC, SORT_DESC);
     
     
    for($x = 0; $x < 100; $x++)
    {
    	echo '<pre>';
    	print_r ($tableau_cree_utilise[$x]);
    	echo '</pre>';
    }
    la sortie donne:

    Array
    (
    [date] => 11/05/2009
    [avoir] => 63,00
    )

    Array
    (
    [date] => 18/04/2009
    [avoir] => 13,00
    )

    Array
    (
    [date] => 14/04/2009
    [avoir] => 13,80
    )

    Array
    (
    [date] => 23/04/2009
    [avoir] => 49,90
    )

    Array
    (
    [date] => 23/04/2009
    [avoir] => 13,80
    )

    Array
    (
    [date] => 28/05/2009
    [avoir] => 6,00
    )

    du coup je teste des variantes pour "array_multisort($tableau_cree_utilise, SORT_NUMERIC, SORT_DESC);"
    qui clairement ne trie pas mes colonnes comme je le souhaite.

    allez je retourne à mon investigation.

  4. #4
    Membre éclairé Avatar de micetf
    Homme Profil pro
    Professeur des Ecoles
    Inscrit en
    Mai 2009
    Messages
    557
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ardèche (Rhône Alpes)

    Informations professionnelles :
    Activité : Professeur des Ecoles
    Secteur : Enseignement

    Informations forums :
    Inscription : Mai 2009
    Messages : 557
    Points : 831
    Points
    831
    Par défaut
    Bonjour,
    Pour trier ton tableau de la date la plus récente à la plus ancienne,
    j'ai fait cela :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    function cmp($a,$b) {
    	$atime=explode('/',$a['date']);
    	$btime=explode('/',$b['date']);
    	echo $b['date'];
    	if (mktime(0,0,0,$atime[1],$atime[0],$atime[2])< mktime(0,0,0,$btime[1],$btime[0],$btime[2])) return 1;
    	else return -1;
    }
     
    uasort($tableau_cree_utilise,"cmp");
    Fred

  5. #5
    Membre à l'essai
    Profil pro
    Inscrit en
    Octobre 2008
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2008
    Messages : 11
    Points : 17
    Points
    17
    Par défaut
    merci pour etre fonction ca evolue bien,
    par contre le fait de creer le tableau avec deux boucles différentes a pour effet de classer par date la premiere boucle qui renseigne le tableau, puis ceux de la deuxieme boucle

    Le resultat ( en gras les données de la deuxieme boucle )
    Array
    (
    [date] => 28/05/2009
    [avoir] => 6,00
    )

    Array
    (
    [date] => 23/04/2009
    [avoir] => 13,80
    )

    Array
    (
    [date] => 23/04/2009
    [avoir] => 49,90
    )

    Array
    (
    [date] => 14/04/2009
    [avoir] => 13,80
    )

    Array
    (
    [date] => 11/05/2009
    [avoir] => 63,00
    )

    Array
    (
    [date] => 18/04/2009
    [avoir] => 13,00
    )

    je continu mes recherches et vous tiens au courant, encore merci pour votre aide.
    bonne soirée

  6. #6
    Membre à l'essai
    Profil pro
    Inscrit en
    Octobre 2008
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2008
    Messages : 11
    Points : 17
    Points
    17
    Par défaut
    les données etant renseignées chronologiquement, je ne pense pas que la fonction soit indispensable, car en testant la sortie
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    for($x = 0; $x < 100; $x++)
    {
    	echo '<pre>';
    	print_r ($tableau_cree_utilise[$x]);
    	echo '</pre>';
    }
    avant la fonction cmp le résultat est le meme.donc je vais verifier ma facon d'alimenter le tableau.

    je vous tiens au courant de l avancement du script,

    encore merci,
    developpez.net c'est

Discussions similaires

  1. [Tableaux] Tri de tableaux
    Par oneTime dans le forum Langage
    Réponses: 7
    Dernier message: 17/04/2008, 15h34
  2. [Tableaux] tri de tableaux
    Par yohann26 dans le forum Langage
    Réponses: 5
    Dernier message: 04/01/2008, 16h43
  3. Tri de fichier par date
    Par airod dans le forum Général Python
    Réponses: 1
    Dernier message: 27/06/2007, 13h48
  4. [Tableaux] Tri de tableaux associatifs à 2 dimensions
    Par max44410 dans le forum Langage
    Réponses: 4
    Dernier message: 18/05/2007, 03h54
  5. [Tableaux] tri sur tableaux
    Par pounie dans le forum Langage
    Réponses: 5
    Dernier message: 03/03/2006, 21h19

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo