Alex _TNT
Alex _TNT

Reputation: 319

Data taken from mysql display division if the date by day is changed

Currently I have this code that takes data from mysql and displays it as text and tried to make a underline like bar if the date by day is changed, currently with no luck

The first picture shows what this code actually does and the second one what I'm trying to do. I tried to check if the current date is different from the new one with no luck.

How do I detect the date change with the current code, so that I can make a break between the same day for easy viewing? enter image description here

<?php
chdir(__DIR__);

error_reporting(E_STRICT | E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);


require_once 'config.php';

$mysqli = new mysqli($server, $db_user, $db_password, $database);

if ($mysqli->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}   



$sql_list = "SELECT date, name, quantity, 0 as price, 'Download' as Source FROM downloads union all SELECT date, name, quantity, price, 'Sale' as Source FROM sales ORDER BY(`Date`) DESC";


$result_list = $mysqli->query($sql_list);

$ans_lists = array();
while($row = $result_list->fetch_assoc()){
        $source =$row["Source"];
        $name = $row["name"];

        if (stripos($source, "Sale") !== false) {
            $price = $row["price"] ."$";
        }else{
            $price = "";
        }

        $date_str = strtotime($row["date"]);
        $year = date('Y', $date_str);
        $month = date('m', $date_str);
        $day = date('d', $date_str);
        $hour = date('H', $date_str);
        $minute = date('i', $date_str);




        $ans_list = "" . sprintf('%s-%s-%s %s:%s',$year,$month,$day,$hour,$minute) . ", " .  $name . " - " .  $source . " Added " . $price . "";
        //echo $ans_list;
        $ans_lists[] = $ans_list;
}



$count = 0;
$max_lines = 1000;    

foreach($ans_lists as $f){

    if ($count >= $max_lines){
        break;
    }

    if (stripos($f, "Sale") !== false) {
        $class = "sales";
        $text = "text-sales";

        if (preg_match("/(CRCV2)/i", $f)) {
                $squaresales = "crcv2-sales";
        }else if(preg_match("/(CRC12)/i", $f)){
                $squaresales = "crc12-sales";
        }

    }
    else{
        $class = "row";
        $square = "general";
        $text = "text-general";
        $squaresales = "";

    }

    if (preg_match("/(CRCV2|CRCPV2-01|CRCPV2-02|CRCPV2-03|CRCPV2-04|CRCPV2-05|CRCPV2-06|CRCPV2-07|CRCPV2-08|CRCPV2-09|CRCPV2-10|CRCPV2-11|CRCPV2-12)/i", $f)) {
            $square = "crcv2";
    }else if(preg_match("/(CRC12|CRCP01|CRCP02|CRCP03|CRCP04|CRCP05|CRCP06|CRCP07|CRCP08|CRCP09|CRCP10|CRCP11|CRCP12|CRCP13|CRCP14)/i", $f)){
            $square = "crc12";
    }

$count++;
    echo "<div class='".$class." scale'><a class='".$square."'></a><a class='".$squaresales."'></a><a class='".$text."'>".$f."</a></div>\r\n";
}

$mysqli->close();

enter image description here

Upvotes: 0

Views: 58

Answers (1)

rozklad
rozklad

Reputation: 443

Try changing your $ans_lists[] = $ans_list to something like this:

// Here youll get something like 20150708
$ymd_date = date( 'Ymd', strtotime($row["date"]) );

// Prepare array for results by day
if ( !isset( $ans_lists[$ymd_date] ) ) {
    $ans_lists[$ymd_date] = array();
}

// Add result to specific date
$ans_lists[$ymd_date][] = $ans_list;

And then change foreach($ans_lists as $f){ iterator to

foreach($ans_lists as $ymd_date => $array_of_entries){
    foreach($array_of_entries as $f){
         // ... here stays your printing logic
         // ...
    }
    // Here ends one specific day
    // so you can output breakline here or something
    echo '<hr>';
}

Upvotes: 1

Related Questions