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

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

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

<title>Ban logs</title>

4.Print from Query(example)

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

<?php } } ?>


            $(document).ready(function () {
                    {  "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');

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.)

If the log files are generated from the mySQL database, then why not keep the data in the database and generate reports containing the applicable information for each log type based on a user’s access type (admin, normal user, etc…)?

If you still want to create separate text log files, then create simple text files with just data in rows and columns with zero html. Then you will need to have a database table containing three columns (a unique id, log file type, and an access type (admin, etc…). When you generate the log text file, you will give this file the same name of the unique id like “123456789.txt”.

When someone wants to view an old log file, you set up a page which based on a user’s access type, they could see a listing of only log files they have access to. Clicking on link would open import the text file data into a what ever format report you would to use for the specific log file type. This allows you to keep the log file data separate from the design of the logs, so you can change the look of the report without affecting the data and vice versa.

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.