How could I optimize this PHP JS code?

Asked

Viewed 50 times

0

I have this code in PHP and JS that searches in BD the visits that the site X received in the current month and plays on a chart of Google Charts.

Despite being functional and serving me well until then, I am bothered by the appearance and size of this and I would love to know if there is any better way to perform this same task with a much cleaner and smarter code.

Thanks in advance for any contribution.

Below is an excerpt in PHP:

<?php
$meses = array(
    '01'=>'Janeiro',
    '02'=>'Fevereiro',
    '03'=>'Março',
    '04'=>'Abril',
    '05'=>'Maio',
    '06'=>'Junho',
    '07'=>'Julho',
    '08'=>'Agosto',
    '09'=>'Setembro',
    '10'=>'Outubro',
    '11'=>'Novembro',
    '12'=>'Dezembro'
);

$mes_atual_nome = $meses[date('m')];

$mes_atual = date('m');
$ano_atual = date('Y');

$ReadViewsDia_1 = ExeRead("pageviews WHERE pageview_dia = '01' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
$numViewsDia_1 = mysqli_num_rows($ReadViewsDia_1);
if ($numViewsDia_1 > 0): $TotalViewsdia_1 = $numViewsDia_1; else: $TotalViewsdia_1 = '0'; endif;

$ReadViewsDia_2 = ExeRead("pageviews WHERE pageview_dia = '02' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
$numViewsDia_2 = mysqli_num_rows($ReadViewsDia_2);
if ($numViewsDia_2 > 0): $TotalViewsdia_2 = $numViewsDia_2; else: $TotalViewsdia_2 = '0'; endif;

$ReadViewsDia_3 = ExeRead("pageviews WHERE pageview_dia = '03' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
$numViewsDia_3 = mysqli_num_rows($ReadViewsDia_3);
if ($numViewsDia_3 > 0): $TotalViewsdia_3 = $numViewsDia_3; else: $TotalViewsdia_3 = '0'; endif;

$ReadViewsDia_4 = ExeRead("pageviews WHERE pageview_dia = '04' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
$numViewsDia_4 = mysqli_num_rows($ReadViewsDia_4);
if ($numViewsDia_4 > 0): $TotalViewsdia_4 = $numViewsDia_4; else: $TotalViewsdia_4 = '0'; endif;

$ReadViewsDia_5 = ExeRead("pageviews WHERE pageview_dia = '05' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
$numViewsDia_5 = mysqli_num_rows($ReadViewsDia_5);
if ($numViewsDia_5 > 0): $TotalViewsdia_5 = $numViewsDia_5; else: $TotalViewsdia_5 = '0'; endif;

$ReadViewsDia_6 = ExeRead("pageviews WHERE pageview_dia = '06' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
$numViewsDia_6 = mysqli_num_rows($ReadViewsDia_6);
if ($numViewsDia_6 > 0): $TotalViewsdia_6 = $numViewsDia_6; else: $TotalViewsdia_6 = '0'; endif;

$ReadViewsDia_7 = ExeRead("pageviews WHERE pageview_dia = '07' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
$numViewsDia_7 = mysqli_num_rows($ReadViewsDia_7);
if ($numViewsDia_7 > 0): $TotalViewsdia_7 = $numViewsDia_7; else: $TotalViewsdia_7 = '0'; endif;

$ReadViewsDia_8 = ExeRead("pageviews WHERE pageview_dia = '08' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
$numViewsDia_8 = mysqli_num_rows($ReadViewsDia_8);
if ($numViewsDia_8 > 0): $TotalViewsdia_8 = $numViewsDia_8; else: $TotalViewsdia_8 = '0'; endif;

$ReadViewsDia_9 = ExeRead("pageviews WHERE pageview_dia = '09' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
$numViewsDia_9 = mysqli_num_rows($ReadViewsDia_9);
if ($numViewsDia_9 > 0): $TotalViewsdia_9 = $numViewsDia_9; else: $TotalViewsdia_9 = '0'; endif;

$ReadViewsDia_10 = ExeRead("pageviews WHERE pageview_dia = '10' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
$numViewsDia_10 = mysqli_num_rows($ReadViewsDia_10);
if ($numViewsDia_10 > 0): $TotalViewsdia_10 = $numViewsDia_10; else: $TotalViewsdia_10 = '0'; endif;

$ReadViewsDia_11 = ExeRead("pageviews WHERE pageview_dia = '11' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
$numViewsDia_11 = mysqli_num_rows($ReadViewsDia_11);
if ($numViewsDia_11 > 0): $TotalViewsdia_11 = $numViewsDia_11; else: $TotalViewsdia_11 = '0'; endif;

$ReadViewsDia_12 = ExeRead("pageviews WHERE pageview_dia = '12' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
$numViewsDia_12 = mysqli_num_rows($ReadViewsDia_12);
if ($numViewsDia_12 > 0): $TotalViewsdia_12 = $numViewsDia_12; else: $TotalViewsdia_12 = '0'; endif;

$ReadViewsDia_13 = ExeRead("pageviews WHERE pageview_dia = '13' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
$numViewsDia_13 = mysqli_num_rows($ReadViewsDia_13);
if ($numViewsDia_13 > 0): $TotalViewsdia_13 = $numViewsDia_13; else: $TotalViewsdia_13 = '0'; endif;

$ReadViewsDia_14 = ExeRead("pageviews WHERE pageview_dia = '14' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
$numViewsDia_14 = mysqli_num_rows($ReadViewsDia_14);
if ($numViewsDia_14 > 0): $TotalViewsdia_14 = $numViewsDia_14; else: $TotalViewsdia_14 = '0'; endif;

$ReadViewsDia_15 = ExeRead("pageviews WHERE pageview_dia = '15' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
$numViewsDia_15 = mysqli_num_rows($ReadViewsDia_15);
if ($numViewsDia_15 > 0): $TotalViewsdia_15 = $numViewsDia_15; else: $TotalViewsdia_15 = '0'; endif;

$ReadViewsDia_16 = ExeRead("pageviews WHERE pageview_dia = '16' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
$numViewsDia_16 = mysqli_num_rows($ReadViewsDia_16);
if ($numViewsDia_16 > 0): $TotalViewsdia_16 = $numViewsDia_16; else: $TotalViewsdia_16 = '0'; endif;

$ReadViewsDia_17 = ExeRead("pageviews WHERE pageview_dia = '17' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
$numViewsDia_17 = mysqli_num_rows($ReadViewsDia_17);
if ($numViewsDia_17 > 0): $TotalViewsdia_17 = $numViewsDia_17; else: $TotalViewsdia_17 = '0'; endif;

$ReadViewsDia_18 = ExeRead("pageviews WHERE pageview_dia = '18' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
$numViewsDia_18 = mysqli_num_rows($ReadViewsDia_18);
if ($numViewsDia_18 > 0): $TotalViewsdia_18 = $numViewsDia_18; else: $TotalViewsdia_18 = '0'; endif;

$ReadViewsDia_19 = ExeRead("pageviews WHERE pageview_dia = '19' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
$numViewsDia_19 = mysqli_num_rows($ReadViewsDia_19);
if ($numViewsDia_19 > 0): $TotalViewsdia_19 = $numViewsDia_19; else: $TotalViewsdia_19 = '0'; endif;

$ReadViewsDia_20 = ExeRead("pageviews WHERE pageview_dia = '20' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
$numViewsDia_20 = mysqli_num_rows($ReadViewsDia_20);
if ($numViewsDia_20 > 0): $TotalViewsdia_20 = $numViewsDia_20; else: $TotalViewsdia_20 = '0'; endif;

$ReadViewsDia_21 = ExeRead("pageviews WHERE pageview_dia = '21' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
$numViewsDia_21 = mysqli_num_rows($ReadViewsDia_21);
if ($numViewsDia_21 > 0): $TotalViewsdia_21 = $numViewsDia_21; else: $TotalViewsdia_21 = '0'; endif;

$ReadViewsDia_22 = ExeRead("pageviews WHERE pageview_dia = '22' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
$numViewsDia_22 = mysqli_num_rows($ReadViewsDia_22);
if ($numViewsDia_22 > 0): $TotalViewsdia_22 = $numViewsDia_22; else: $TotalViewsdia_22 = '0'; endif;

$ReadViewsDia_23 = ExeRead("pageviews WHERE pageview_dia = '23' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
$numViewsDia_23 = mysqli_num_rows($ReadViewsDia_23);
if ($numViewsDia_23 > 0): $TotalViewsdia_23 = $numViewsDia_23; else: $TotalViewsdia_23 = '0'; endif;

$ReadViewsDia_24 = ExeRead("pageviews WHERE pageview_dia = '24' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
$numViewsDia_24 = mysqli_num_rows($ReadViewsDia_24);
if ($numViewsDia_24 > 0): $TotalViewsdia_24 = $numViewsDia_24; else: $TotalViewsdia_24 = '0'; endif;

$ReadViewsDia_25 = ExeRead("pageviews WHERE pageview_dia = '25' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
$numViewsDia_25 = mysqli_num_rows($ReadViewsDia_25);
if ($numViewsDia_25 > 0): $TotalViewsdia_25 = $numViewsDia_25; else: $TotalViewsdia_25 = '0'; endif;

$ReadViewsDia_26 = ExeRead("pageviews WHERE pageview_dia = '26' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
$numViewsDia_26 = mysqli_num_rows($ReadViewsDia_26);
if ($numViewsDia_26 > 0): $TotalViewsdia_26 = $numViewsDia_26; else: $TotalViewsdia_26 = '0'; endif;

$ReadViewsDia_27 = ExeRead("pageviews WHERE pageview_dia = '27' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
$numViewsDia_27 = mysqli_num_rows($ReadViewsDia_27);
if ($numViewsDia_27 > 0): $TotalViewsdia_27 = $numViewsDia_27; else: $TotalViewsdia_27 = '0'; endif;

$ReadViewsDia_28 = ExeRead("pageviews WHERE pageview_dia = '28' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
$numViewsDia_28 = mysqli_num_rows($ReadViewsDia_28);
if ($numViewsDia_28 > 0): $TotalViewsdia_28 = $numViewsDia_28; else: $TotalViewsdia_28 = '0'; endif;

$ReadViewsDia_29 = ExeRead("pageviews WHERE pageview_dia = '29' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
$numViewsDia_29 = mysqli_num_rows($ReadViewsDia_29);
if ($numViewsDia_29 > 0): $TotalViewsdia_29 = $numViewsDia_29; else: $TotalViewsdia_29 = '0'; endif;

$ReadViewsDia_30 = ExeRead("pageviews WHERE pageview_dia = '30' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
$numViewsDia_30 = mysqli_num_rows($ReadViewsDia_30);
if ($numViewsDia_30 > 0): $TotalViewsdia_30 = $numViewsDia_30; else: $TotalViewsdia_30 = '0'; endif;

$ReadViewsDia_31 = ExeRead("pageviews WHERE pageview_dia = '31' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
$numViewsDia_31 = mysqli_num_rows($ReadViewsDia_31);
if ($numViewsDia_31 > 0): $TotalViewsdia_31 = $numViewsDia_31; else: $TotalViewsdia_31 = '0'; endif;

Below the section in JS:

<script type="text/javascript">
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);

function drawChart() {
    var data = google.visualization.arrayToDataTable([
        ['Dias', {type: 'string', role: 'annotation'}, 'Todas as visitas', 'Visitas únicas ao site'],
        ['Dia 1', '', <?=$TotalViewsdia_1 ?>,      0],
        ['Dia 2', '',  <?=$TotalViewsdia_2 ?>,      0],
        ['Dia 3', '',  <?=$TotalViewsdia_3 ?>,       0],
        ['Dia 4', '',  <?=$TotalViewsdia_4 ?>,      0],
        ['Dia 5', '',  <?=$TotalViewsdia_5 ?>,      0],
        ['Dia 6', '',  <?=$TotalViewsdia_6 ?>,      0],
        ['Dia 7', '',  <?=$TotalViewsdia_7 ?>,      0],
        ['Dia 8', '',  <?=$TotalViewsdia_8 ?>,      0],
        ['Dia 9', '',  <?=$TotalViewsdia_9 ?>,      0],
        ['Dia 10', '',  <?=$TotalViewsdia_10 ?>,      0],
        ['Dia 11', '',  <?=$TotalViewsdia_11 ?>,      0],
        ['Dia 12', '',  <?=$TotalViewsdia_12 ?>,      0],
        ['Dia 13', '',  <?=$TotalViewsdia_13 ?>,      0],
        ['Dia 14', '',  <?=$TotalViewsdia_14 ?>,      0],
        ['Dia 15', '',  <?=$TotalViewsdia_15 ?>,      0],
        ['Dia 16', '',  <?=$TotalViewsdia_16 ?>,      0],
        ['Dia 17', '',  <?=$TotalViewsdia_17 ?>,      0],
        ['Dia 18', '',  <?=$TotalViewsdia_18 ?>,      0],
        ['Dia 19', '',  <?=$TotalViewsdia_19 ?>,      0],
        ['Dia 20', '',  <?=$TotalViewsdia_20 ?>,      0],
        ['Dia 21', '',  <?=$TotalViewsdia_21 ?>,      0],
        ['Dia 22', '',  <?=$TotalViewsdia_22 ?>,      0],
        ['Dia 23', '',  <?=$TotalViewsdia_23 ?>,      0],
        ['Dia 24', '',  <?=$TotalViewsdia_24 ?>,      0],
        ['Dia 25', '',  <?=$TotalViewsdia_25 ?>,      0],
        ['Dia 26', '',  <?=$TotalViewsdia_26 ?>,      0],
        ['Dia 27', '',  <?=$TotalViewsdia_27 ?>,      0],
        ['Dia 28', '',  <?=$TotalViewsdia_28 ?>,      0],
        ['Dia 29', '',  <?=$TotalViewsdia_29 ?>,      0],
        ['Dia 30', '',  <?=$TotalViewsdia_30 ?>,      0],
        ['Dia 31', '',  <?=$TotalViewsdia_31 ?>,      0]
    ]);

    var options = {
        chartArea : { top:20 },
        vAxis: {minValue: 0},
        curveType: 'function',
        backgroundColor: 'none',
        legend: { position: 'bottom' },
        annotations: { style: 'line' },
        colors: ['#15A0C8', '#dc3912'],
        pointSize: 7,
        series: {
            0: { pointShape: 'circle' },
            1: { pointShape: 'triangle' },
            2: { pointShape: 'square' },
            3: { pointShape: 'diamond' },
            4: { pointShape: 'star' },
            5: { pointShape: 'polygon' }
        }
    };

    var chart = new google.visualization.AreaChart(document.getElementById('curve_chart'));
    chart.draw(data, options);
}
</script>

1 answer

1


you can make an array of $TotalViewsdia:

<?php
$meses = array(
    '01' => 'Janeiro',
    '02' => 'Fevereiro',
    '03' => 'Março',
    '04' => 'Abril',
    '05' => 'Maio',
    '06' => 'Junho',
    '07' => 'Julho',
    '08' => 'Agosto',
    '09' => 'Setembro',
    '10' => 'Outubro',
    '11' => 'Novembro',
    '12' => 'Dezembro'
);

$mes_atual_nome = $meses[date('m')];

$mes_atual = date('m');
$ano_atual = date('Y');

$TotalViewsdia = [];
for ($i=1; $i < 32; $i++) { 
    $ReadViewsDia = ExeRead("pageviews WHERE pageview_dia = '$i' AND pageview_mes = '$mes_atual' AND pageview_ano = '$ano_atual'");
    $numViewsDia = mysqli_num_rows($ReadViewsDia);

    $TotalViewsdia[$i] = ($numViewsDia > 0) ? $numViewsDia : 0;
}

In the script you can run foreach listing the array values:

google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);

function drawChart() {
var data = google.visualization.arrayToDataTable([
['Dias', {type: 'string', role: 'annotation'}, 'Todas as visitas', 'Visitas únicas ao site'],

<?php
foreach ($TotalViewsdia as $key => $value) {
?>
    ['Dia <?=$key?>', '', <?= $value ?>, 0],
<?php
}
?>
    ]);
var options = {
chartArea : { top:20 },
vAxis: {minValue: 0},
curveType: 'function',
backgroundColor: 'none',
legend: { position: 'bottom' },
annotations: { style: 'line' },
colors: ['#15A0C8', '#dc3912'],
pointSize: 7,
series: {
0: { pointShape: 'circle' },
1: { pointShape: 'triangle' },
2: { pointShape: 'square' },
3: { pointShape: 'diamond' },
4: { pointShape: 'star' },
5: { pointShape: 'polygon' }
}
};

var chart = new google.visualization.AreaChart(document.getElementById('curve_chart'));
chart.draw(data, options);
}
  • Fantastic your code friend, only has one however, the graph is generated but the values appear all as zero "0", giving a "var_dump" realized that really the returned values are of type "INT" and all as zero "0";

  • I noticed here and could not find anything that was causing this error (a comma out of place or something etc)... Could you tell me if there’s any little mistake in your solution?

  • Thanks in advance for your huge contribution.

  • I am new to PHP and foreach is something that still complicates me, your code will definitely help me understand it a little better.

  • 1

    Hello buddy, you really made a mistake and this line $TotalViewsdia[$i] = ($numViewsDia > 0) ? $TotalViewsdia : 0; the correct would be $TotalViewsdia[$i] = ($numViewsDia > 0) ? $numViewsDia : 0;

  • 1

    I also edited in response to correct error

  • Show!!!... now it’s over!

  • Thank you very much, I’m here a while looking at your code to learn more from this practical example, you helped me a lot.

Show 3 more comments

Browser other questions tagged

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