Jump to content

Recommended Posts

I'm trying to shorten this piece of my code into a loop. I've tried to get the $param_name using:

$_POST as $param_name => $param_val

But that just gets me the last entry and not the whole thing. Here's the code I'm trying to shorten:

file_put_contents(
"content.html", $mystring .
"<strong> Updated last: " . $_POST['date'] . "</strong><br><br>" .
//Please add more entries depending on how many accounts you have configured
"<strong>Account 1</strong><br>(" . $_POST['accountname1'] . ")" . "<br>CREDITS<br>" . $_POST['credits1'] . "<br>-<br>" .
"<strong>Account 2</strong><br>(" . $_POST['accountname2'] . ")" . "<br>CREDITS<br>" . $_POST['credits2'] . "<br>-<br>" .
"<strong>Account 3</strong><br>(" . $_POST['accountname3'] . ")" . "<br>CREDITS<br>" . $_POST['credits3'] . "<br>-<br>" .
"<strong>Account 4</strong><br>(" . $_POST['accountname4'] . ")" . "<br>CREDITS<br>" . $_POST['credits4'] . "<br>-<br>" .
"<strong>Account 5</strong><br>(" . $_POST['accountname5'] . ")" . "<br>CREDITS<br>" . $_POST['credits5'] . "<br>-<br>" .
"<strong>Account 6</strong><br>(" . $_POST['accountname6'] . ")" . "<br>CREDITS<br>" . $_POST['credits6']); 

Also here's the Javascript code I'm posting with:

function bprs() {

    {
        var rowCount = $('#accountsTable tr').length;
        var accountsCount = rowCount -1;                       
        var accounts = [];
        for (var n = 1; n <= accountsCount; n++) {
            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;
            }, '');

I'd like my php page to generate the html code based on how many entries are in the post data. I know I'm close, I just don't know how to loop data like this properly.

Name your inputs either

a) accountName[n] and credits[n]

b) accounts[n][name] and data[n][credits]

 

I'd suggest the second. Then your PHP looks like

$contents = $mystring . "...";

$n = 1;
$accounts = array();
foreach ($_POST["accounts"] as $account) {
	$accounts[] = "<strong>Account {$n}</strong><br>({$account['name']})<br>CREDITS<br>{$account['credits']}";
}

$contents .= implode("<br>-<br>", $accounts);
  • Like 1

All the work happens in the bprs function. So that needs to change.

 

You're using jQuery, right? Your second loop, which puts everything together using &s and =s, can be replaced with a call to $.param(), as long as the first loop creates an object that looks like

{ data: [ { name: "name 1", credits: 1 }, { name: "name 2", credits: 2 }, ... ] }
So the first loop becomes like

var rowCount = $('#accountsTable tr').length;
var accountsCount = rowCount -1;                       
var accountsData = [];
for (var n = 1; n <= accountsCount; n++) {
	accountsData.push({                          
		name: $('#accountName' + n).text(),
		credits: $('#credits' + n).text()
	});
}
and the second loop goes away and becomes simply

var data = $.param({
	date: new Date(),
	data: accountsData
});
  • Like 1

Putting this in my php file writes "..." to my html file.

$contents = $mystring . "...";


$n = 1;
$accounts = array();
foreach ($_POST["accounts"] as $account) {
$accounts[] = "<strong>Account {$n}</strong><br>({$account['name']})<br>CREDITS<br>{$account['credits']}";
}


$contents .= implode("<br>-<br>", $accounts);


file_put_contents("content.html", $contents);

This is what I get now if I use var_dump$(_POST)

Object {readyState: 4, responseText: "↵array(2) {↵  ["date"]=>↵  string(58) "Sun Dec 27 …  ["credits"]=>↵      string(2) "72"↵    }↵  }↵}↵", status: 200, statusText: "OK"}abort: ( statusText )always: ()complete: ()done: ()error: ()fail: ()getAllResponseHeaders: ()getResponseHeader: ( key )overrideMimeType: ( type )pipe: ( /* fnDone, fnFail, fnProgress */ )progress: ()promise: ( obj )readyState: 4responseText: "↵array(2) {↵  ["date"]=>↵  string(58) "Sun Dec 27 2015 18:35:23 GMT-0700 (Mountain Standard Time)"↵  ["data"]=>↵  array(6) {↵    [0]=>↵    array(2) {↵      ["name"]=>↵      string(28) "****@gmail.com"↵      ["credits"]=>↵      string(3) "110"↵    }↵    [1]=>↵    array(2) {↵      ["name"]=>↵      string(33) "****@gmail.com"↵      ["credits"]=>↵      string(3) "110"↵    }↵    [2]=>↵    array(2) {↵      ["name"]=>↵      string(23) "****@gmail.com"↵      ["credits"]=>↵      string(3) "536"↵    }↵    [3]=>↵    array(2) {↵      ["name"]=>↵      string(32) "****@outlook.com"↵      ["credits"]=>↵      string(3) "333"↵    }↵    [4]=>↵    array(2) {↵      ["name"]=>↵      string(19) "****@gmail.com"↵      ["credits"]=>↵      string(3) "188"↵    }↵    [5]=>↵    array(2) {↵      ["name"]=>↵      string(20) "****@gmail.com"↵      ["credits"]=>↵      string(2) "72"↵    }↵  }↵}↵"setRequestHeader: ( name, value )state: ()status: 200statusCode: ( map )statusText: "OK"success: ()then: ( /* fnDone, fnFail, fnProgress */ )__proto__: Object
Edited by chrisyroid
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. :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.