how to reverse loop?
#1

How do i reverse a loop:
for(new i = 1; i < MAX_REPORTS; i++)

I dit try : for(new i = 1; i > MAX_REPORTS; i++) but that does not work.
Reply
#2

Try this and let me know if it doesn't work
pawn Код:
for(new i = MAX_REPORTS; i > 0; i --) {
    // your code here
}
And by the way - since the minimal index seems to be 1 - then the max id should be MAX_REPORTS.
Which means your "forward" loop should look like this:
pawn Код:
for(new i = 1; i <= MAX_REPORTS; i ++) {

}
Instead of this: (your current loop)
pawn Код:
for(new i = 1; i < MAX_REPORTS; i ++) {

}
Notice the "<=" instead of the "<". That is a "less or equal than" operator instead of your "less than" operator.

For more information about operators, check this out: https://sampwiki.blast.hk/wiki/Control_Structures#Operators

For more information about for loops, check this out: https://sampwiki.blast.hk/wiki/Control_S...res#for_.28.29
Reply
#3

I suspect your issue can more easily be solved by using another approach. Can you give us a little more context on what you're trying to do? Are the reports loaded from a database or somewhere else?
Reply
#4

Thank you RedFusion, it worked.
Reply
#5

@ Sithis, I have this now:

PHP код:
CMD:reports(playerid,params[])
{
    new 
str[1024], string2[1024], string[1024], szQuery[128], status[25], reportid[25];
    if(
AdminLevel[playerid] >=2)
    {
         for(new 
MAX_REPORTS0--)
         {
             new 
reportstatus[5];
             new 
DBResult:REPORTS_RESULT_FINAL;
             
format(szQuerysizeof(szQuery), "SELECT * FROM `REPORTS` WHERE `ID` = '%d'"i);
            
REPORTS_RESULT_FINAL db_query(LARP_DATABASEszQuery);
            
db_get_field_assoc(REPORTS_RESULT_FINAL"ID"reportidsizeof(reportid));
            
ReportInfo[i][ReportID] = strval(reportid);
            
db_get_field_assoc(REPORTS_RESULT_FINAL"Status"reportstatussizeof(reportstatus));
            
db_get_field_assoc(REPORTS_RESULT_FINAL"PlayerName"ReportInfo[i][ReportedName], 124);
            
db_get_field_assoc(REPORTS_RESULT_FINAL"Reported"ReportInfo[i][ReportedBy], 124);
            
db_get_field_assoc(REPORTS_RESULT_FINAL"Report"ReportInfo[i][ReportedReason], 124);
            
db_get_field_assoc(REPORTS_RESULT_FINAL"Date"ReportInfo[i][ReportedDate], 124);
            if(
ReportInfo[i][ReportID] != 0)
               {
                
ReportInfo[i][ReportStatus] = strval(reportstatus);
                if(
ReportInfo[i][ReportStatus] == 0) {status="{FF8000}Pending";}
                if(
ReportInfo[i][ReportStatus] == 1) {status="{00CC00}Open";}
                
format(str,sizeof(str),"%d\t%q\t%q\t%q\n"ReportInfo[i][ReportID], ReportInfo[i][ReportedName], ReportInfo[i][ReportedReason],status);
                
strcat(string2,str);
            }
            
db_free_result(REPORTS_RESULT_FINAL);
        }
        
format(str,sizeof(str),"Report ID\tReported Name\tReason\tStatus\n%s"string2);
           
strcat(string,str);
        
ShowPlayerDialog(playeridDIALOG_PENDINGREPORTS+1DIALOG_STYLE_TABLIST_HEADERS"Pending Reports"string"Select""Back");
    }
    return 
1;

Could it be done any better/ more efficient?

This is my table for reports:
Код:
CREATE TABLE `REPORTS` (`ID` INTEGER PRIMARY KEY AUTOINCREMENT, `Status` INTEGER NOT NULL, `PlayerName` TEXT NOT NULL, `Reporter` TEXT NOT NULL, `Report` TEXT NOT NULL, `Date` TEXT NOT NULL, 'Admin' TEXT NOT NULL DEFAULT 'None', 'Solution' TEXT NOT NULL DEFAULT 'None', 'DateClosed' TEXT NOT NULL DEFAULT 'NA')
Reply
#6

PHP код:
format(szQuerysizeof(szQuery), "SELECT * FROM `REPORTS` WHERE `ID` = '%d' ORDER DESC"i); 
if you want to select the reports in another order, if that's why you have a -- loop
Reply
#7

Quote:
Originally Posted by AndreiWow
Посмотреть сообщение
PHP код:
format(szQuerysizeof(szQuery), "SELECT * FROM `REPORTS` WHERE `ID` = '%d' ORDER DESC"i); 
if you want to select the reports in another order, if that's why you have a -- loop
I have used your method above, but that will not work inside a loop. because the loop delivers the ID to the query in which order they should load. And outside a loop it only did show 1 row from the db.
I do use this method in PHP, but it does not seem to work in PAWN.

PHP код:
$sql "SELECT * FROM REPORTS ORDER BY ID DESC LIMIT $record_index$limit"
 
 
$stmt $db->query($sql);
 while(
$row $stmt->fetchArray(SQLITE3_ASSOC) )
 {
        
$status $row['Status'];
        if(
$status==1)
        {
          echo 
"<tr class='success'>";
          echo 
"<td>"$row['ID'] . "</td>";
          echo 
"<td>Open</td>";
        }
        elseif(
$status==0
        {
          echo 
"<tr class='warning'>";
          echo 
"<td>"$row['ID'] . "</td>";
          echo 
"<td>Pending</td>";
        }
        elseif(
$status==2
        {
          echo 
"<tr class='danger'>";
          echo 
"<td>"$row['ID'] . "</td>";
          echo 
"<td>Closed</td>";
        }
       echo 
"<td>"$row['PlayerName'] . "</td>";
       echo 
"<td>"$row['Reporter'] . "</td>";
       echo 
"<td>"$row['Report'] . "</td>";
       echo 
"<td>"$row['Date'] . "</td>";
       echo 
"<td><a href='index.php?p=viewreport&id="$row['ID'] . "'><button class='btn btn-info'>View report</button></a>&nbsp;";
       echo 
"<a href='index.php?p=deletereport&id="$row['ID'] . "'><button class='btn btn-danger'>Delete</button></a></td>";
       echo 
"</tr>";
 } 
Reply
#8

You could just load 20 results for example by using an offset and a limit. Or just load them all at once. In ascending or descending order (forward / backwards order).
Then use a loop to browse through all of the results.

Here's an example:

pawn Код:
#define REPORT_PAGESIZE 20

// Put this part below in your function or command and modify it to your needs
new
    page = 0, // first page
    bool: is_descending = true, // descending date = "biggest" / most recent date first,
    DBResult: db_result,
    query[200+1],
    report_id,
    report_status,
    report_reportedname[MAX_PLAYER_NAME+1],
    report_byname[MAX_PLAYER_NAME+1],
    report_reason[124+1],
    report_date[19+1],
    output_row[100+1],
    output_full[(REPORT_PAGESIZE * 100)+1]
;

format(query, sizeof query, "SELECT * FROM `REPORTS` ORDER BY `Date` %q LIMIT %i OFFSET %i ", is_descending ? ("DESC") : ("ASC"), REPORT_PAGESIZE, page * REPORT_PAGESIZE);

db_result = db_query(LARP_DATABASE, query);

for(new row, rows = db_num_rows(db_result); row < rows; row ++) {
    report_id = db_get_field_assoc_int(db_result, "ID");
    report_status = db_get_field_assoc_int(db_result, "Status");
    db_get_field_assoc(db_result, "PlayerName", report_byname, sizeof report_byname);
    db_get_field_assoc(db_result, "Reported", report_reportedname, sizeof report_reportedname);
    db_get_field_assoc(db_result, "Report", report_reason, sizeof report_reason);
    db_get_field_assoc(db_result, "Date", report_date, sizeof report_date);
    if( report_id != 0 ) {
        format(output_row, sizeof output_row, "%d\t%s\t%s\t%s\n", report_id, report_reportedname, report_reason, report_status == 0 ? ("{FF8000}Pending") : ("{00CC00}Open"));
        strcat(output_full, output_row);
    }
    db_next_row(db_result);
}

db_free_result(db_result);
And i have a few suggestions for you aswell:
  • Integer values can be fetched directly as integer values by using db_get_field_assoc_int instead of fetching them as strings with db_get_field_assoc and then getting their value using strval.
  • Use ternary operators instead of creating new strings
    pawn Код:
    // New
    report_status == 0 ? ("{FF8000}Pending") : ("{00CC00}Open") // Ternary / Triadic operator

    // Old
    new report_status_str[15+1];
    if(report_status  == 0) {
        report_status_str = "{FF8000}Pending";
    } else {
        report_status_str = "{00CC00}Open";
    }
Sources:
db_get_field_assoc_int: https://sampwiki.blast.hk/wiki/Db_get_field_assoc_int
Ternary / Triadic Operator: https://sampwiki.blast.hk/wiki/Control_Structures#.3F:
Reply
#9

I was going to say the exact same. Just fetch the reports from the database and use ORDER BY ASCENDING/DESCENDING to get the data you want.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)