Good time of day, how to implement such a thing ?.

while($ser_row = $adb->fetch_array($sql)) { $serviceid = $ser_row['serviceid']; $servicename = $ser_row['servicename']; $autouserid = $ser_row['autouserid']; $carnomer = $ser_row['serialnumber']; $avtograf = $ser_row['avtograf']; $rastoplivo = $ser_row['rastoplivo']; $allmkad = $ser_row['allmkad']; $peregruztn = $ser_row['peregruztn']; 

From the beginning, you need to remove all repetitions from $ ser_row ['serialnumber']; and then find them all $ ser_row ['allmkad']; and fold them.

  • one
    why not make service [$ ser_row ['serialnumber']] = $ ser_row; and then foreach through the service array? - zb '
  • Thank you, but do not tell me how (foreach)? And then I have some kind of crap out .. in the query, your search and output in the table does not fit there, but you need to add everything for the total. - machanics 7:44 pm
  • what is not clear ? $ sum = 0; foreach ($ service as $ ser) $ sum + = (int) $ ser ['allmkad']; echo $ sum; - zb '
  • Emm so he adds the total amount for all serialnumber. The fact is that I need what he considered the total for each of the serialnumber - machanics
  • Uncle Thanks for the help) everything turned out just like I wanted! - machanics

3 answers 3

If you need to weed out the repetition of lines and perform an operation on the values ​​of the others, it is better to do it in sql using GROUP BY. PRESS

  • This option is not suitable because you need to pull everything from the beginning into the table and then calculate the results - machanics
  • 2
    Refine @qpaycm SELECT serviceid, servicename, autouserid, serialnumber, avtograf, rastoplivo, peregruztn, SUMM (allmkad) as sumallmkad FROM tablename GROUP BY serviceid; As a result, all the data will be in place, and the sum of all allmkad will be here - $ ser_row ['sumallmkad']; - dekameron

To "automate" the process of declaring variables, it is better to use the following line foreach ($ ser_row as $ key => $ value) $$ key = $ value;

    Just in your loop with fetch

     $summs[$set_row['serialnumber']]+=(int)$ser_row['allmkad']; 

    or so (if other data is needed)

     if (!isset($all[$set_row['serialnumber'])) $all[$set_row['serialnumber']=array(); $all[$set_row['serialnumber']]['summs']+=(int)$ser_row['allmkad']; $all[$set_row['serialnumber']]['rows'][]=$ser_row; 
    • Tell me whether it is possible to do with foreach so that he would show the result first and then list what it came out of, for example, on what dates. if (! isset ($ all [$ ser_row ['serialnumber']])) $ all [$ ser_row ['serialnumber']] = array (); $ all [$ ser_row ['serialnumber']] ['summs'] + = (int) $ ser_row ['allmkad']; $ all [$ ser_row ['serialnumber']] ['dates'] = $ ser_row ['cf_672']; foreach ($ all as $ key => $ ser) {if ($ key == '') {} else {$ chartauto. = "{state: '$ key', km: $ ser [summs], peregruz: $ ser [peregruz], prostoy: $ ser [prostoy]}, "; - machanics