Need help simplifying repeating entries in my script

Hi. I Need help simplifying repeating entries in my script. I have lots of repeating entries which are a pain to configure for the user. I rather have this script do this task automatically. I tried to use loops to solve this but I ended up settling with this method.

[php]<?php header('Access-Control-Allow-Origin: http://mysite.com'); ?>

<?php if(sizeof($_POST)) { file_put_contents; } else { echo "ERROR! Must be activated from script!"; exit(); } file_put_contents( "content.html", $mystring . " Updated last: " . $_POST['date'] . "

" . //Please add more entries depending on how many accounts you have configured "Account 1
(" . $_POST['accountname1'] . ")" . "
CREDITS
" . $_POST['credits1'] . "
-
" . "Account 2
(" . $_POST['accountname2'] . ")" . "
CREDITS
" . $_POST['credits2'] . "
-
" . "Account 3
(" . $_POST['accountname3'] . ")" . "
CREDITS
" . $_POST['credits3'] . "
-
" . "Account 4
(" . $_POST['accountname4'] . ")" . "
CREDITS
" . $_POST['credits4'] . "
-
" . "Account 5
(" . $_POST['accountname5'] . ")" . "
CREDITS
" . $_POST['credits5'] . "
-
" . "Account 6
(" . $_POST['accountname6'] . ")" . "
CREDITS
" . $_POST['credits6']); ?>[/php]

I’m not sure where your post information is coming from, but you may be better off using post arrays?
This way you can loop through each field for example:
[php]
foreach ($_POST[‘accountname’] as $key => $value) {
// Use the key to match the variables
$_POST[‘accountname’][$key]
$_POST[‘credits’][$key]
}
[/php]

If you want to continue the way you are going then I would suggest looping through until you can’t find any more info.
[php]

<?php header('Access-Control-Allow-Origin: http://mysite.com'); if(sizeof($_POST)) { file_put_contents("content.html", buildText($mystring)); } else { echo "ERROR! Must be activated from script!"; exit(); } function buildScript($mystring) { $entry = 1; $text = $mystring . ' Updated last: ' . $_POST['date'] . '

'; while ($has_entries) { if (!isset($_POST['accountname' . $entry])) { break; } else { $text = 'Account ' . $entry . '
(' . $_POST['accountname' . $entry] . ')' . '
CREDITS
' . $_POST['credits' . $entry] . '
-
'; } $entry++; } return $text; } [/php]

All I’m getting is the date. My data comes from a Tampermonkey script (javascript) which posts data from the browser to the server. I get a 500 (Internal Server Error) when I use “buildText” When I change it to “buildScript” I get the date and nothing else. Here’s the script I use to post the data:

[code]function bprs() {

{
    var rowCount = $('#accountsTable tr').length;
    var accountsCount = rowCount -1;                       
    var accounts = [];
    for (var n = 1; n <= accountsCount; n++) {
        // Using objects literals to group your data is a great way to make your code clearer
        accounts[n] = {                          
            name: $('#accountName' + n).text(),
            credits: $('#credits' + n).text()
        };
    }

    var date = new Date();
    var data = "date=" + date + 
        accounts.reduce(function (prev, account, n) {
            return prev + "&accountname" + n + "=" + account.name +
                "&credits" + n + "=" + account.credits;
        }, '');

    $.ajaxPrefilter(function( options, originalOptions, jqXHR ) {
        options.async = true;
    });

    $.ajax({
        url: 'http://webz4life01.x10host.com/submit.php', // point to the php file here
        async:false,
        type: "POST",
        dataType: "json",
        data: data,
        success: function (result) {
            JSON.parse(result);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            console.log(xhr);
        }
    });[/code]

Also I just realized that I don’t need “$mystring” Don’t know why that was still in there…

Sorry a few errors in the syntax. Haven’t tested it.
[php]
header(‘Access-Control-Allow-Origin: http://mysite.com’);

if(sizeof($_POST)) {
file_put_contents(“content.html”, buildScript($mystring));
} else {
echo “ERROR! Must be activated from script!”;
exit();
}

function buildScript($mystring) {
$entry = 1;
$text = $mystring . ’ Updated last: ’ . $_POST[‘date’] . ‘

’;
while ($entry > 1) {
if (!isset($_POST[‘accountname’ . $entry])) {
break;
} else {
$text .= 'Account ’ . $entry . ‘
(’ . $_POST[‘accountname’ . $entry] . ‘)’ . ‘
CREDITS
’ . $_POST[‘credits’ . $entry] . ‘
-
’;
}

    $entry++;
}

return $text;

}
[/php]

Still getting only the time. :frowning: Everything else seems to be working fine though.

Make sure you are sending through the appropriate data “accountname1” etc. Check network tab in your browser to make sure the post data is being sent. If it is you can var_dump($_POST) to see exactly what post data is being sent. You’ll need to look in your networking tab again to see the response.

var_dump shows in the console:

" array(13) { ["date"]=> string(58) "Sun Dec 27 2015 11:28:53 GMT-0700 (Mountain Standard Time)" ["accountname1"]=> string(28) "*****@gmail.com" ["credits1"]=> string(3) "110" ["accountname2"]=> string(33) "*****@gmail.com" ["credits2"]=> string(3) "109" ["accountname3"]=> string(23) "*****@gmail.com" ["credits3"]=> string(3) "536" ["accountname4"]=> string(32) "*****@outlook.com" ["credits4"]=> string(3) "333" ["accountname5"]=> string(19) "*****@gmail.com" ["credits5"]=> string(3) "188" ["accountname6"]=> string(20) "*****@gmail.com" ["credits6"]=> string(2) "72" } "

I had to use:

foreach( $_POST as $key => $value ) { }

To output the data properly.
I debugged this method using:

foreach( $_POST as $key => $value ) { $add = "$key.$value"; $f = fopen("test.txt", "w"); fwrite($f, $add); fclose($f); }

But now when I use:

[code] foreach( $_POST as $key => $value ) {
$entry = 1;
$text = $mystring . ’ Updated last: ’ . $_POST[‘date’] . ‘

’;
while ($entry > 1) {
if (!isset($_POST[‘accountname’ . $entry])) {
break;
} else {
$text .= 'Account ’ . $entry . ‘
(’ . $_POST[‘accountname’ . $entry] . ‘)’ . ‘
CREDITS
’ . $_POST[‘credits’ . $entry] . ‘
-
’;
}

   $entry++;

}

file_put_contents(“test.txt”, $text);
}[/code]

I get “ERR_CONNECTION_TIMED_OUT” when I post.

After endless google searches I came up with a solution which includes a bit of your last few posts.

I changed my Tampermonkey script to count the account tables and send that to my PHP.

"&accountsnumber=" + accountsCount

Then I modified my PHP to accept that value and used that number to figure out how many entries should be written to my html file.

$accountnumber = $_POST['accountsnumber']; $str = '<strong>Updated last: ' . $_POST['date'] . '</strong><br /><br />'; for ($i = 0; $i < $accountnumber; $i++) { $identer = $i + 1; $str.= '<strong>Account ' . $identer . '</strong><br />(' . $_POST['accountname' . $identer] . ')' . '<br />CREDITS<br />' . $_POST['credits' . $identer] . '<br />-<br />'; } file_put_contents("content.html", rtrim($str, '<br />-<br />'));

Thank you so much for all your help. :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service