AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Broken table when supposed to work (https://forums.alliedmods.net/showthread.php?t=334497)

GasmoN 09-28-2021 13:58

Broken table when supposed to work
 
So I have this code that shows top15 players on the server, and while it works like a charm, I have issue with the HTML part of the code. Table's width is not 100%, and I have tried like 100 times to fix that and it simply won't work. Does anyone at least know what the issue here is and what should I try to do next?
PHP Code:

public mysql_top15(FailStateHandle:QueryError[], ErrcodeData[], DataSize)
{
    if(
FailState)
    {
        
log_amx("SQL Error: %s (%d)"ErrorErrcode);
        return 
PLUGIN_HANDLED;
    }
    
    new 
gMotd[2048];

    if(
SQL_NumResults(Query) > 1)
    {
        new 
szName[33];
        new 
iName;
        new 
iKills;
        new 
iDeaths;
        new 
iHeadshots;

        new 
0;
        new 
len 0;

        
len += formatex(gMotd[len], charsmax(gMotd) - len"<!DOCTYPE html><html>");
        
len += formatex(gMotd[len], charsmax(gMotd) - len"<head><meta name='viewport' content='width=device-width, initial-scale=1'>")
        
len += formatex(gMotd[len], charsmax(gMotd) - len"<meta http-equiv='X-UA-Compatible' content='IE=edge' />")
        
len += formatex(gMotd[len], charsmax(gMotd) - len"<style>table { border-collapse: collapse; width: 100%; border: 1px solid #ddd; } th, td { text-align: left; padding: 16px; }</style></head>");
        
len += formatex(gMotd[len], charsmax(gMotd) - len"<body><br><center><table><tr><th>Rank</th><th>Name</th><th>Kills</th><th>Deaths</th><th>Headshots</th></tr>");
        
        
iName SQL_FieldNameToNum(Query"name");
        
iKills SQL_FieldNameToNum(Query"kills");
        
iDeaths SQL_FieldNameToNum(Query"deaths");
        
iHeadshots SQL_FieldNameToNum(Query"headshots");
        
        
        while (
SQL_MoreResults(Query))
        {
            
len += formatex(gMotd[len], charsmax(gMotd) - len"<tr><td>%d", ++k);
            
            
SQL_ReadResult(QueryiNameszNamecharsmax(szName));
            
            
len += formatex(gMotd[len], charsmax(gMotd) - len"<td>%-22.22s"szName);
            
len += formatex(gMotd[len], charsmax(gMotd) - len"<td>%d"SQL_ReadResult(QueryiKills));
            
len += formatex(gMotd[len], charsmax(gMotd) - len"<td>%d"SQL_ReadResult(QueryiDeaths));
            
len += formatex(gMotd[len], charsmax(gMotd) - len"<td>%d"SQL_ReadResult(QueryiHeadshots));
    
            
SQL_NextRow(Query);
        }
    
        
len += formatex(gMotd[len], charsmax(gMotd) - len"</table></center></body></html>");
        
    }

    
show_motd(Data[0], gMotd"Top 15 Players");
    
    return 
PLUGIN_HANDLED;
    



Bugsy 09-28-2021 20:35

Re: Broken table when supposed to work
 
1. You have no </td>'s or </tr>'s
2. You can eliminate the SQL_FieldNameToNum() native calls by passing the column number directly to SQL_ReadResult().
3. You can replace formatex() with copy() on your first 5 formatex()'s.

Try this
PHP Code:

        while (SQL_MoreResults(Query))
        {
            
len += formatex(gMotd[len], charsmax(gMotd) - len"<tr><td>%d</td>", ++k);
            
            
SQL_ReadResult(QueryiName szNamecharsmax(szName));
            
            
len += formatex(gMotd[len], charsmax(gMotd) - len"<td>%-22.22s</td>"szName);
            
len += formatex(gMotd[len], charsmax(gMotd) - len"<td>%d</td>"SQL_ReadResult(QueryiKills ));
            
len += formatex(gMotd[len], charsmax(gMotd) - len"<td>%d</td>"SQL_ReadResult(QueryiDeaths ));
            
len += formatex(gMotd[len], charsmax(gMotd) - len"<td>%d</td></tr>"SQL_ReadResult(QueryiHeadshots ));
    
            
SQL_NextRow(Query);
        } 


fysiks 09-28-2021 20:48

Re: Broken table when supposed to work
 
I manually created the HTML that you're generating with your code and it renders perfectly in the Counter-Strike motd.txt. It takes up the whole width of the MOTD window. I tried it both with and without all the newline characters and it looked perfect either way.

HTML Code:

<!DOCTYPE html><html>
<head><meta name='viewport' content='width=device-width, initial-scale=1'>
<meta http-equiv='X-UA-Compatible' content='IE=edge' />
<style>table { border-collapse: collapse; width: 100%; border: 1px solid #ddd; } th, td { text-align: left; padding: 16px; }</style></head>
<body><br><center><table><tr><th>Rank</th><th>Name</th><th>Kills</th><th>Deaths</th><th>Headshots</th></tr>
<tr><td>1
<td>NameNameNameNameName!!
<td>10
<td>12
<td>14
<tr><td>2
<td>NameNameNameNameName!!
<td>10
<td>12
<td>14
<tr><td>3
<td>NameNameNameNameName!!
<td>10
<td>12
<td>14
<tr><td>4
<td>NameNameNameNameName!!
<td>10
<td>12
<td>14
<tr><td>5
<td>NameNameNameNameName!!
<td>10
<td>12
<td>14
<tr><td>6
<td>NameNameNameNameName!!
<td>10
<td>12
<td>14
<tr><td>7
<td>NameNameNameNameName!!
<td>10
<td>12
<td>14
<tr><td>8
<td>NameNameNameNameName!!
<td>10
<td>12
<td>14
<tr><td>9
<td>NameNameNameNameName!!
<td>10
<td>12
<td>14
<tr><td>10
<td>NameNameNameNameName!!
<td>10
<td>12
<td>14
<tr><td>11
<td>NameNameNameNameName!!
<td>10
<td>12
<td>14
<tr><td>12
<td>NameNameNameNameName!!
<td>10
<td>12
<td>14
<tr><td>13
<td>NameNameNameNameName!!
<td>10
<td>12
<td>14
<tr><td>14
<td>NameNameNameNameName!!
<td>10
<td>12
<td>14
<tr><td>15
<td>NameNameNameNameName!!
<td>10
<td>12
<td>14
</table></center></body></html>

Maybe try to print gMotd[] to a file so that you can verify and test the resulting HTML.

@Bugsy, it looks like it's able to properly render without all the closing tags as shown above. In fact, it looks like adding all the closing tags for tr and td will actually cause this to go over the 1536 character limit.

Also, note that the MOTD is limited to 1536 characters so you can declare gMotd[1537] instead of using 2048.

WATCH_DOGS UNITED 09-28-2021 21:06

Re: Broken table when supposed to work
 
In case this can help: https://smallseotools.com/minify-html/

Bugsy 09-28-2021 21:36

Re: Broken table when supposed to work
 
Quote:

Originally Posted by fysiks (Post 2759144)
@Bugsy, it looks like it's able to properly render without all the closing tags as shown above. In fact, it looks like adding all the closing tags for tr and td will actually cause this to go over the 1536 character limit.

Also, note that the MOTD is limited to 1536 characters so you can declare gMotd[1537] instead of using 2048.

Good to know, I never tested it without closing tags and am surprised it renders correctly. But that is awesome, being there's a shallow character limit.

fysiks 09-28-2021 22:38

Re: Broken table when supposed to work
 
Quote:

Originally Posted by WATCH_DOGS UNITED (Post 2759145)

That's nifty. And, it seems to remove all the closing tags for th, tr, td.

In this case, the meta tags go be removed also.

GasmoN 09-29-2021 13:04

Re: Broken table when supposed to work
 
For some weird reason, width: 100% in .was the problem. When motd was printed it was printed without % (meaning, only width: 100 was printed) The solution to this is to add two percentage signs, like this: width 100%% and now it works like a charm. I also added missing </td> and </tr>'s

Full working code:
PHP Code:

public mysql_top15(FailStateHandle:QueryError[], ErrcodeData[], DataSize)
{
    if(
FailState)
    {
        
log_amx("SQL Error: %s (%d)"ErrorErrcode);
        return 
PLUGIN_HANDLED;
    }
    
    new 
gMotd[2048];

    if(
SQL_NumResults(Query) > 1)
    {
        new 
szName[33];
        new 
iName;
        new 
iKills;
        new 
iDeaths;
        new 
iHeadshots;

        new 
0;
        new 
len 0;

        
len += formatex(gMotd[len], charsmax(gMotd) - len"<!DOCTYPE html><html>");
        
len += formatex(gMotd[len], charsmax(gMotd) - len"<head><meta name='viewport' content='width=device-width, initial-scale=1'>")
        
len += formatex(gMotd[len], charsmax(gMotd) - len"<meta http-equiv='X-UA-Compatible' content='IE=edge' />")
        
len += formatex(gMotd[len], charsmax(gMotd) - len"<style>table { border-collapse: collapse; width: 100%%; border: 1px solid #ddd; } th, td { text-align: left; padding: 16px; }</style></head>");
        
len += formatex(gMotd[len], charsmax(gMotd) - len"<body><br><center><table><tr><th>Rank</th><th>Name</th><th>Kills</th><th>Deaths</th><th>Headshots</th></tr>");
        
        
iName SQL_FieldNameToNum(Query"name");
        
iKills SQL_FieldNameToNum(Query"kills");
        
iDeaths SQL_FieldNameToNum(Query"deaths");
        
iHeadshots SQL_FieldNameToNum(Query"headshots");
        
        
        while (
SQL_MoreResults(Query))
        {
            
len += formatex(gMotd[len], charsmax(gMotd) - len"<tr><td>%d</td>", ++k);
            
            
SQL_ReadResult(QueryiNameszNamecharsmax(szName));
            
            
len += formatex(gMotd[len], charsmax(gMotd) - len"<td>%-22.22s</td>"szName);
            
len += formatex(gMotd[len], charsmax(gMotd) - len"<td>%d</td>"SQL_ReadResult(QueryiKills));
            
len += formatex(gMotd[len], charsmax(gMotd) - len"<td>%d</td>"SQL_ReadResult(QueryiDeaths));
            
len += formatex(gMotd[len], charsmax(gMotd) - len"<td>%d</td></tr>"SQL_ReadResult(QueryiHeadshots));
    
            
SQL_NextRow(Query);
        }
    
        
len += formatex(gMotd[len], charsmax(gMotd) - len"</table></center></body></html>");
        
    }

    
show_motd(Data[0], gMotd"Top 15 Players");
    
    return 
PLUGIN_HANDLED;
    


Thank you guys for your replies.

fysiks 09-29-2021 21:32

Re: Broken table when supposed to work
 
Ah, yeah, I totally forgot about that. Great job finding that.

With all the closing tags, are you not going over the 1536 limit? If yes, you can save some characters by removing the meta tags.

GasmoN 09-30-2021 13:01

Re: Broken table when supposed to work
 
First thing that I have changed is that I removed css style, and added link to the same style I uploaded to my website.
I wasn't going over that limit, because total characters number was 1220 (With all current nickanmes, kills and deaths included), BUT I have done some calculations and realised that if I have all 15 players with long nicknames (33 characters long), I would go over that limit.
So what I did, is checked output results again, and saw too many spaces in the html code. Then, after adding this
PHP Code:

while(replace(gMotdcharsmax(gMotd), "  </td>""</td>")) { } 

I reduced spaces in the output from 175 to 19. With the total characters count of 1064 this time. (With all current nickanmes, kills and deaths included)
So now, even if all players have nickanes of 33 characters (495 in total), and all of them have 99,999 kills and 99,999 deaths, the total number of characters would be 645. If we add 850 to that, which is characters count of table, without names, kills, and deaths, the total number will be 1,495. So problem solved.

Bugsy 09-30-2021 19:53

Re: Broken table when supposed to work
 
How is " </td>" (the 2 spaces preceding </td>) ending up in your HTML to begin with? Your formatex()'s do not have spaces like that. I recommend fixing the formatting instead of calling replace, if possible.


All times are GMT -4. The time now is 20:26.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.