My Code Optimization

Hi guys,
I’m make website which is functional and I’m make all what I wanted.All work just like I wanted.But now I want to improve my code optimization.

I have page where admin can see log’s(For example connection logs,ban logs,etc)
For every logs I’m make new .php file

/admin/logs/connection.php
/admin/logs/ban.php
And I have 20-25 same files which have 90% similar code.I want to make it in only one file ( > /admin/logs.php?ID=1
/admin/logs.php?ID=2
etc.

Code which is not same in all files
1.Who have permit to see it log

if($user['Admin'] == "3" )
{
//If admin = 3 ,he can see it logs,other users get : You don't have permission to view this log
}

2.MySQL Query

SELECT * FROM logs WHERE Category = 'BanLogs'
//example,so fory every log there is different Category,of course

3.Tittle
<title>Ban logs</title>

4.Print from Query(example)

<?php if (!empty($logs)) { foreach ($logs as $log) { ?>
<tr>
    <td><?php echo $log['ID']; ?></td>
    <td><?php echo $log['Username']; ?></td>
    <td><?php echo $log['Country']; ?></td>
    <td><?php echo $log['Date']; ?></td>
</tr>

<?php } } ?>

5.JavaScript

        <script>
            $(document).ready(function () {
                $('#dataTables-connection').dataTable(
                    {  "order": [[ 0, "desc" ]],  
                    "aoColumnDefs": [{ "bVisible": false, "aTargets": [0] }],
                    "pageLength": 50,
                    "lengthMenu": [[50, 200, 500, 1000, -1], [50, 200, 500, 1000,"All"]],
                    "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
                    if ( aData[4] > 8)
                    {
                        $('td', nRow).css('background-color', 'Red');
                    }
                     }
                    
                });
            });
    </script>

So my question is how to make this in one file? (logs?ID=1 ) etc…What is best way for it…
I don’t know where I can read more about this.Tutorials or something like that.(I don’t know for what to search :slight_smile: )

Thank you very much

Putting everything into one file is not always a good thing. For eg. what happens if you want to change the underlying database? It is best to keep the sql separate so you can plug and play it in your project. Or what happens if you want to translate your strings and make them work in different languages? you will need them in separate files then as well.
Just my two cents.

But what will happend if I want to change my desing of page or something like that…I will needed to edit 20+ files for any changes in my website.
I don’t think that anyone will create 20 same files for anything( x.php y.php z.php and all are very similar.)

You shouldn’t mix the code with the logs. Create a ‘template’ of the log files and then use it to generate the rest.
That is, write code to generate the log files from a template combined with the specific log data.