Raised This Month: $ Target: $400
 0% 

Solved Broken table when supposed to work


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
GasmoN
Member
Join Date: Jul 2014
Old 09-28-2021 , 13:58   Broken table when supposed to work
Reply With Quote #1

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;
    


Last edited by GasmoN; 09-29-2021 at 15:06. Reason: Solved
GasmoN is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 09-28-2021 , 20:35   Re: Broken table when supposed to work
Reply With Quote #2

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);
        } 
__________________

Last edited by Bugsy; 09-28-2021 at 20:45.
Bugsy is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 09-28-2021 , 20:48   Re: Broken table when supposed to work
Reply With Quote #3

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

Last edited by fysiks; 09-28-2021 at 20:52.
fysiks is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 09-28-2021 , 21:36   Re: Broken table when supposed to work
Reply With Quote #4

Quote:
Originally Posted by fysiks View Post
@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.
__________________
Bugsy is offline
WATCH_DOGS UNITED
BANNED
Join Date: Nov 2020
Old 09-28-2021 , 21:06   Re: Broken table when supposed to work
Reply With Quote #5

In case this can help: https://smallseotools.com/minify-html/
WATCH_DOGS UNITED is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 09-28-2021 , 22:38   Re: Broken table when supposed to work
Reply With Quote #6

Quote:
Originally Posted by WATCH_DOGS UNITED View Post
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.
__________________

Last edited by fysiks; 09-28-2021 at 22:45.
fysiks is offline
GasmoN
Member
Join Date: Jul 2014
Old 09-29-2021 , 13:04   Re: Broken table when supposed to work
Reply With Quote #7

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.

Last edited by GasmoN; 09-29-2021 at 13:05. Reason: [code] changed to [php]
GasmoN is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 09-29-2021 , 21:32   Re: Broken table when supposed to work
Reply With Quote #8

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.
__________________
fysiks is offline
GasmoN
Member
Join Date: Jul 2014
Old 09-30-2021 , 13:01   Re: Broken table when supposed to work
Reply With Quote #9

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.

Last edited by GasmoN; 09-30-2021 at 13:02.
GasmoN is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 09-30-2021 , 19:53   Re: Broken table when supposed to work
Reply With Quote #10

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.
__________________
Bugsy is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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