Re: JSON and PHP
[email protected] (The Doctor)
| Newsgroups | comp.lang.php |
|---|---|
| Organization | NetKnow News |
| Message-ID | <[email protected]> |
In article <[email protected]>, J.O. Aho <[email protected]> wrote: >On 28/12/2022 01.34, The Doctor wrote: >> Trying to implement a JSON array in PHP >> Using > ><too much code so removed, use a code repository and link to it> > >> Just wonder how to supress repeats. > >Don't insert the same data multiple times into an array. > >Here is a short code demonstrating what you are doing >--- example.php --- ><?PHP >for($i = 0; $i < 3; $i++) { // this replaces the foreach in the original >code >$MyObj[] = array("same each time", "this from round $i"); >var_dump($MyObj); >} >?> >--- eof --- > >You can see how the array grows each iteration of the loop. > Just that we are converting from php to php/json. Further, the foreach is due to a line that is coming is from a previous session being passed on. >Some tips: >Separate display and logic into two files, one with only the logic and >one with only showing the resulting data, this makes it far cleaner and >simpler to update the design of the page without affecting the logic. > >Do not have variable with the same name but different case, it makes >your code far more difficult to follow and more mistakes can sneak in > >Validate input data, always go with whitelisting of what is allowed to >be in a input, this way you always know the data is safe to use. Regular >expression can make this simpler. > >Always calculate the total, do not trust that from a post or else >customers gets all the items for free. Also item prices should always be >fetched from the stored price list, not from a post value. > >Store data that you don't want to be manipulated but you need no the >next page in the session, as this data can't be manipulated by the user >and you don't need to revalidate the data on each page. > >-- > //Aho -- Member - Liberal International This is [email protected] Ici [email protected] Yahweh, King & country!Never Satan President Republic!Beware AntiChrist rising! Look at Psalms 14 and 53 on Atheism https://www.empire.kred/ROOTNK?t=94a1f39b Happy Christmas 2022 and Merry New Year 2023 Beware https://mindspring.com