0

I'm making a php script in which I want an output as follow. I have this code:

$query_man = query
$output_man = mysql_query($query_man);

if(mysql_num_rows($output_man) > 0){

while ($row = mysql_fetch_assoc($output_manutenzione)){

    echo "\t" . $row['hostname'];
    echo "\n\t\tTipo Manutenzione: " . $row['type'];
    echo "\n\t\tData inizio: " . $row['startdate'];
    echo "\n\t\tData fine: " . $row['enddate'];
    echo "\n\t\tUtente: " . $row['user'];
    echo "\n\t\tMotivo: " . $row['desc'];
    echo "\n\t\tStato: " . $row['State'] . "\n\n";
}

where type, startdate, enddate etc. are query fields.

In this way I get an outup like this (e.g.):

Hello1
   program
   2016-10-13 09:00
   2016-10-13 10:00
   kevin
   test
   open

Hello1
   program
   2016-10-13 13:00
   2016-10-13 15:30
   john
   test
   closed

Hello2
   program
   2016-10-12 11:00
   2016-10-13 11:30
   susan
   test
   closed

Hello3
   program
   2016-10-12 13:00
   2016-10-12 15:30
   clay
   test2
   open

Well, I don't understand how to get the following outup:

Hello1
   program
   2016-10-13 09:00
   2016-10-13 10:00
   kevin
   test
   open

   program
   2016-10-13 13:00
   2016-10-13 15:30
   john
   test
   closed

Hello2
   program
   2016-10-12 11:00
   2016-10-13 11:30
   susan
   test
   closed

Hello3
   program
   2016-10-12 13:00
   2016-10-12 15:30
   clay
   test2
   open

i.e. I'd like to group the info with the same hostname. How could I do?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Idro
  • 253
  • 1
  • 7

4 Answers4

4

If you remember the last hostname that you processed, you can test it against the current one and only if they are different output the hostname line

$query_man = query
$output_man = mysql_query($query_man);

if(mysql_num_rows($output_man) > 0){
    $last_host = NULL;
    while ($row = mysql_fetch_assoc($output_manutenzione)){

        if ($row['hostname'] != $last_host ) {
            echo "\t" . $row['hostname'];
            $last_host = $row['hostname'];
        }

        echo "\n\t\tTipo Manutenzione: " . $row['type'];
        echo "\n\t\tData inizio: " . $row['startdate'];
        echo "\n\t\tData fine: " . $row['enddate'];
        echo "\n\t\tUtente: " . $row['user'];
        echo "\n\t\tMotivo: " . $row['desc'];
        echo "\n\t\tStato: " . $row['State'] . "\n\n";
    }
}

This does assume that the query is sorted on hostname but your example output is sorted that way so I assume you already did that

But I have to add this

Every time you use the mysql_ database extension in new code a Kitten is strangled somewhere in the world it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the PDO or mysqli database extensions. Start here

Community
  • 1
  • 1
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • 2
    Should also add that the query will need to be sorted by `hostname` if this is going to work. – Mulan Oct 13 '16 at 09:23
  • @naomik Yup I was just thinking I should add that, thanks for the nudge – RiggsFolly Oct 13 '16 at 09:25
  • Your answer would have been even better if you yourself had used PDO in the code provided, but I still upvoted. :p – ChristianF Oct 13 '16 at 09:32
  • @ChristianF Yes I agree, but as it is unlikely that a jump from `mysql_` to `PDO` will be undertaken by the OP I deemed it a waste of my time and SO's disk space. – RiggsFolly Oct 13 '16 at 09:35
  • I'm a big proponent of dragging them, kicking and screaming if need be, into the present. Better to get it over with as soon as possible, before bad habits are established. ;) Anyway, just a minor nit-pick. As I said, your answer is just fine the way it is. You've covered everything relevant, and in a good manner. – ChristianF Oct 13 '16 at 09:38
1

You just need to check the previous hostname like so:

$prev_hostname = "";
while ($row = mysql_fetch_assoc($output_manutenzione)){
    if ($prev_hostname !== $row['hostname']) 
        echo "\t" . $row['hostname'];

    echo "\n\t\tTipo Manutenzione: " . $row['type'];
    echo "\n\t\tData inizio: " . $row['startdate'];
    echo "\n\t\tData fine: " . $row['enddate'];
    echo "\n\t\tUtente: " . $row['user'];
    echo "\n\t\tMotivo: " . $row['desc'];
    echo "\n\t\tStato: " . $row['State'] . "\n\n";

    $prev_hostname = $row['hostname'];
}
Rax Weber
  • 3,730
  • 19
  • 30
1

Memorize $row['hostname'] value and check if it is the same as previous line. If not, echo the value:

$query_man = query
$output_man = mysql_query($query_man);

if(mysql_num_rows($output_man) > 0){

    $prevHostname = '' ;
    while ($row = mysql_fetch_assoc($output_manutenzione)){
        if ($prevHostname != $row['hostname']) {
            echo "\t" . $row['hostname'];
            $prevHostname = $row['hostname'];
        }
        echo "\n\t\tTipo Manutenzione: " . $row['type'];
        echo "\n\t\tData inizio: " . $row['startdate'];
        echo "\n\t\tData fine: " . $row['enddate'];
        echo "\n\t\tUtente: " . $row['user'];
        echo "\n\t\tMotivo: " . $row['desc'];
        echo "\n\t\tStato: " . $row['State'] . "\n\n";
    }
}
Al Foиce ѫ
  • 4,195
  • 12
  • 39
  • 49
0

Try below code. you need to make some change in code as below with taking one another array

<?php $query_man = query
$output_man = mysql_query($query_man);

$array_result = array();
if(mysql_num_rows($output_man) > 0){

while ($row = mysql_fetch_assoc($output_manutenzione)){

    echo "\t" . $row['hostname'];
    echo "\n\t\tTipo Manutenzione: " . $row['type'];
    echo "\n\t\tData inizio: " . $row['startdate'];
    echo "\n\t\tData fine: " . $row['enddate'];
    echo "\n\t\tUtente: " . $row['user'];
    echo "\n\t\tMotivo: " . $row['desc'];
    echo "\n\t\tStato: " . $row['State'] . "\n\n";
    $array_result[$row['hostname']][] = $row;
}
print_r($array_result) ?>
Ashok Chandrapal
  • 1,020
  • 7
  • 27