chrisyroid Posted December 27, 2015 Share Posted December 27, 2015 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. Quote Link to comment Share on other sites More sharing options...
requinix Posted December 27, 2015 Share Posted December 27, 2015 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); 1 Quote Link to comment Share on other sites More sharing options...
chrisyroid Posted December 27, 2015 Author Share Posted December 27, 2015 I don't quite understand what is supposed to be going on here. Also I just realized that I don't need "$mystring" don't know why that was still in there... Quote Link to comment Share on other sites More sharing options...
ginerjm Posted December 27, 2015 Share Posted December 27, 2015 Uh - what exactly don't you see? The code does what you tried to demo, but you will have ot make some changes to your form data names in order to use it. Quote Link to comment Share on other sites More sharing options...
chrisyroid Posted December 27, 2015 Author Share Posted December 27, 2015 I think I know where to change accounts[n] But I can't figure out where to change data[n][credits] And how do I use the above changed code to write to an html file? Quote Link to comment Share on other sites More sharing options...
requinix Posted December 28, 2015 Share Posted December 28, 2015 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 }); 1 Quote Link to comment Share on other sites More sharing options...
chrisyroid Posted December 28, 2015 Author Share Posted December 28, 2015 (edited) 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 December 28, 2015 by chrisyroid Quote Link to comment Share on other sites More sharing options...
chrisyroid Posted December 28, 2015 Author Share Posted December 28, 2015 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.