How to break lines in the output of an Array?

Asked

Viewed 1,232 times

0

Good afternoon, I have an array in PHP that shows the date that was made the last backup of each store, and the information is shown as follows:

  LOJA01 - Bkp 01 teve o ultimo backup em1: 02-01-2017.
  LOJA01 - Bkp 02 teve o ultimo backup em3: 30-12-2016.
  LOJA01 - Bkp 03 teve o ultimo backup em3: 31-12-2016.
  LOJA03 - Bkp 01 teve o ultimo backup em1: 10-12-2016.
  LOJA03 - Bkp 02 teve o ultimo backup em1: 11-12-2016.
  LOJA03 - Bkp 03 teve o ultimo backup em1: 09-12-2016.
  LOJA05 - Bkp 01 teve o ultimo backup em1: 10-12-2016.
  LOJA05 - Bkp 02 teve o ultimo backup em1: 11-12-2016.
  LOJA05 - Bkp 03 teve o ultimo backup em1: 09-12-2016.
  LOJA06 - Bkp 01 teve o ultimo backup em1: 11-12-2016.
  LOJA06 - Bkp 02 teve o ultimo backup em1: 11-12-2016.
  LOJA06 - Bkp 03 teve o ultimo backup em1: 09-12-2016.

I wonder if there is any way for each different store to skip a line or separate, notice that each store has the bkp 01, 02 and 03...

The way out I’d like to be something like this:

  LOJA01 - Bkp 01 teve o ultimo backup em1: 02-01-2017.
  LOJA01 - Bkp 02 teve o ultimo backup em3: 30-12-2016.
  LOJA01 - Bkp 03 teve o ultimo backup em3: 31-12-2016.

  LOJA03 - Bkp 01 teve o ultimo backup em1: 10-12-2016.
  LOJA03 - Bkp 02 teve o ultimo backup em1: 11-12-2016.
  LOJA03 - Bkp 03 teve o ultimo backup em1: 09-12-2016.

  LOJA05 - Bkp 01 teve o ultimo backup em1: 10-12-2016.
  LOJA05 - Bkp 02 teve o ultimo backup em1: 11-12-2016.
  LOJA05 - Bkp 03 teve o ultimo backup em1: 09-12-2016.

  LOJA06 - Bkp 01 teve o ultimo backup em1: 11-12-2016.
  LOJA06 - Bkp 02 teve o ultimo backup em1: 11-12-2016.
  LOJA06 - Bkp 03 teve o ultimo backup em1: 09-12-2016.

My Array is as follows:

<?php
error_reporting(0);
ini_set(“display_errors”, 0 );
$lista_lojas = array(   
    "LOJA01 - Bkp 01"  => "servers/01/bk001/Mpcfg.ini",
    "LOJA01 - Bkp 02"  => "servers/01/bk002/Mpcfg.ini",
    "LOJA01 - Bkp 03"  => "servers/01/bk003/Mpcfg.ini",
    "LOJA03 - Bkp 01"  => "servers/03/bk001/Mpcfg.ini",
    "LOJA03 - Bkp 02"  => "servers/03/bk002/Mpcfg.ini",
    "LOJA03 - Bkp 03"  => "servers/03/bk003/Mpcfg.ini",
    "LOJA05 - Bkp 01"  => "servers/05/bk001/Mpcfg.ini",
    "LOJA05 - Bkp 02"  => "servers/05/bk002/Mpcfg.ini",
    "LOJA05 - Bkp 03"  => "servers/05/bk003/Mpcfg.ini",
    "LOJA06 - Bkp 01"  => "servers/06/bk001/Mpcfg.ini",
    "LOJA06 - Bkp 02"  => "servers/06/bk002/Mpcfg.ini",
    "LOJA06 - Bkp 03"  => "servers/06/bk003/Mpcfg.ini",
);
?>

Page that displays the array data:

    <?php

        date_default_timezone_set('America/Sao_Paulo');

        // Inclui o arquivo com os caminhos dos PLUEAN.BIN 
        include_once('includes/array_servers.php');

        foreach($lista_lojas as $l_nome => $l_caminho) 
        { ?>

        <?php 

            $today = date('d-m-Y'); //recebe a data atual
            $seconds = strtotime($today); //converte para segundos
            $diff_date = date("d-m-Y",($seconds - 345600));
            $data_antiga = "31-12-1969";
            // echo $diff_date;
            $data_mod = date ("d-m-Y", filemtime($l_caminho));
            //echo $data_obsoleta;


                    if (($data_mod <= $diff_date) and ($lojaAt == $lojaAt))

                    { ?>
                        <img src="img/down.png" width="15" height="15"/>&nbsp;
                            <?php echo "$l_nome teve o ultimo backup em1: " . date ("d-m-Y.", filemtime($l_caminho));
                             echo $lojaAt; echo "<br>"  ;
                    } elseif (($data_mod == $data_antiga) and ($lojaAt == $lojaAt))
                    {?>
                        <img src="img/down.png" width="15" height="15"/>&nbsp;
                            <?php echo "$l_nome teve o ultimo backup em2: " . date ("d-m-Y.", filemtime($l_caminho));
                            echo $lojaAt; echo "<br>"   ;

                    } elseif (($data_mod >= $diff_date) and ($lojaAt == $lojaAt)) 
                    {?>
                        <img src="img/up.png" width="15" height="15"/>&nbsp;
                            <?php echo "$l_nome teve o ultimo backup em3: " . date ("d-m-Y.", filemtime($l_caminho));
                            echo $lojaAt; echo "<br>"   ;

                    } else

                    ?> 
<?php   } ?>
  • And the code where you show the items?

  • It’s up there!

1 answer

2

A solution would be to treat at the time of printing the value. + - So:

<?php


$lista_lojas = array(   
    "LOJA01 - Bkp 01"  => "servers/01/bk001/Mpcfg.ini",
    "LOJA01 - Bkp 02"  => "servers/01/bk002/Mpcfg.ini",
    "LOJA01 - Bkp 03"  => "servers/01/bk003/Mpcfg.ini",
    "LOJA03 - Bkp 01"  => "servers/03/bk001/Mpcfg.ini",
    "LOJA03 - Bkp 02"  => "servers/03/bk002/Mpcfg.ini",
    "LOJA03 - Bkp 03"  => "servers/03/bk003/Mpcfg.ini",
    "LOJA05 - Bkp 01"  => "servers/05/bk001/Mpcfg.ini",
    "LOJA05 - Bkp 02"  => "servers/05/bk002/Mpcfg.ini",
    "LOJA05 - Bkp 03"  => "servers/05/bk003/Mpcfg.ini",
    "LOJA06 - Bkp 01"  => "servers/06/bk001/Mpcfg.ini",
    "LOJA06 - Bkp 02"  => "servers/06/bk002/Mpcfg.ini",
    "LOJA06 - Bkp 03"  => "servers/06/bk003/Mpcfg.ini",
);

$lojaAnt = '';
foreach($lista_lojas as $key => $item) {

    $lojaAt = explode(' ',$key)[0];                         ;

    if ($lojaAt<>$lojaAnt){
        echo "<br>";
    }

    echo $key."  ".$item."<br>";    
    $lojaAnt=$lojaAt;
}

/*Saída:

LOJA01 - Bkp 01 servers/01/bk001/Mpcfg.ini
LOJA01 - Bkp 02 servers/01/bk002/Mpcfg.ini
LOJA01 - Bkp 03 servers/01/bk003/Mpcfg.ini

LOJA03 - Bkp 01 servers/03/bk001/Mpcfg.ini
LOJA03 - Bkp 02 servers/03/bk002/Mpcfg.ini
LOJA03 - Bkp 03 servers/03/bk003/Mpcfg.ini

LOJA05 - Bkp 01 servers/05/bk001/Mpcfg.ini
LOJA05 - Bkp 02 servers/05/bk002/Mpcfg.ini
LOJA05 - Bkp 03 servers/05/bk003/Mpcfg.ini

LOJA06 - Bkp 01 servers/06/bk001/Mpcfg.ini
LOJA06 - Bkp 02 servers/06/bk002/Mpcfg.ini
LOJA06 - Bkp 03 servers/06/bk003/Mpcfg.ini */

Updating:

Inserted the solution into the code you provided:

 <?php

date_default_timezone_set('America/Sao_Paulo');

// Inclui o arquivo com os caminhos dos PLUEAN.BIN 
include_once('includes/array_servers.php');
$lojaAnt = '';
foreach ($lista_lojas as $l_nome => $l_caminho) {
?>

        <?php

    $today       = date('d-m-Y'); //recebe a data atual
    $seconds     = strtotime($today); //converte para segundos
    $diff_date   = date("d-m-Y", ($seconds - 345600));
    $data_antiga = "31-12-1969";
    // echo $diff_date;
    $data_mod    = date("d-m-Y", filemtime($l_caminho));
    //echo $data_obsoleta;

    //$lojaAt = explode(' ',$l_nome)[0];  


    if ($lojaAt <> $lojaAnt) {
        echo "<br>"; //Colocar aqui o código para quebrar a linha
    }

    $lojaAnt = $lojaAt;


    if (($data_mod <= $diff_date) and ($lojaAt == $lojaAt)) {
?>
                      <img src="img/down.png" width="15" height="15"/>&nbsp;
                            <?php
        echo "$l_nome teve o ultimo backup em1: " . date("d-m-Y.", filemtime($l_caminho));
        echo $lojaAt;
        echo "<br>";
    } elseif (($data_mod == $data_antiga) and ($lojaAt == $lojaAt)) {
?>
                      <img src="img/down.png" width="15" height="15"/>&nbsp;
                            <?php
        echo "$l_nome teve o ultimo backup em2: " . date("d-m-Y.", filemtime($l_caminho));
        echo $lojaAt;
        echo "<br>";

    } elseif (($data_mod >= $diff_date) and ($lojaAt == $lojaAt)) {
?>
                      <img src="img/up.png" width="15" height="15"/>&nbsp;
                            <?php
        echo "$l_nome teve o ultimo backup em3: " . date("d-m-Y.", filemtime($l_caminho));
        echo $lojaAt;
        echo "<br>";

    } else
?> 
<?php
}
?> 
  • Thanks, bad mood... gave me a light, but the problem I bumped is that I use an if structure to calculate some dates there... and when I put your script to this structure it does not work,... upstairs I posted my output file in case you can give me a help! a big hug.

  • Okay, I’ll take a look.

  • Did I not understand the difference of the display in each IF? "em1", "em2" and "em3" differs in something? or did you just put it to test? Why the display is the same for everyone. In this case it would be better to use the OR operator or || and use an IF only.

  • It was for testing, just to find out what he was getting into!

  • I can’t test your code because it has include and use of functions not provided. That last Else? Will ever run?

  • no... he is not executed!

  • So, what’s the problem? Don’t just put the line break after that if? I’ll edit the answer then you test.

  • Edited response. Give a look.

  • Bad mood, I pasted the code the way you posted it, but it didn’t work it came out underneath each other without breaking line, I think it might be something related to IF structure, since without IF it works...

  • Check if he’s printing the <br> corresponding? it may be that you need to use another feature for spacing, since apparently you are using image. tries something like <div style="margin-bottom:20px"></div> in place of <br>.

  • now it just increased the spacing! will these IF’s getting in the way? I’ll try using <font color> instead of images

  • But you said the spacing doesn’t work?

  • Without a code I can test, it’s hard to help you.

  • Buddy, I posted some answers in the chat... if I take the if and put in its structure works good! but when I start using if to use the images it stops working... I will post the code in the chat

Show 10 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.