Could use some improvement

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

User avatar
BBoyd
Forum Commoner
Posts: 31
Joined: Sat Jan 21, 2012 8:24 pm
Location: Charleston, SC

Re: Could use some improvement

Post by BBoyd »

califdon wrote:I don't see anywhere that you assigned any value to $totals, but you are asking PHP to print out its value on nearly the last line that you showed. I can guess that you saw its use a few lines above there, where the line was commented out.
Just in case it wasn't clear, there is over 200 lines of code so I've not posted it every time. Also the variable you are referring to... "to print out its value on nearly the last line that you showed" actually works. The total appears on the report when it loads. It is the code that I commented out that doesn't work. That is what's confusing to me. The variable was declared 200 lines previously. I'm guessing that it has something to do where the code is concatenated within the HTML, but not experienced enough to know the reason why.

3 minutes let of this day. Gotta run before I miss it all. Good night!
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Could use some improvement

Post by califdon »

BBoyd wrote:
califdon wrote:I don't see anywhere that you assigned any value to $totals, but you are asking PHP to print out its value on nearly the last line that you showed. I can guess that you saw its use a few lines above there, where the line was commented out.
Just in case it wasn't clear, there is over 200 lines of code so I've not posted it every time. Also the variable you are referring to... "to print out its value on nearly the last line that you showed" actually works. The total appears on the report when it loads. It is the code that I commented out that doesn't work. That is what's confusing to me. The variable was declared 200 lines previously. I'm guessing that it has something to do where the code is concatenated within the HTML, but not experienced enough to know the reason why.
Sorry, I misinterpreted your earlier description. OK, you're right, if you wanted to remain within the <?php ... ?> tags, you probably need to form the concatenated string that you want to print/echo, assigning it to a variable, then print the variable. I might fall into that same trap, and I still don't see why you would have received an undeclared variable error, but it's just as easy to do it the other way, which you have found works.
User avatar
BBoyd
Forum Commoner
Posts: 31
Joined: Sat Jan 21, 2012 8:24 pm
Location: Charleston, SC

Re: Could use some improvement

Post by BBoyd »

califdon wrote:Of course, if you had a different situation where you couldn't predict what kind of data might appear in a particular column, you might want to format only numeric values. To do that, you would need to learn how to use the PHP function is_numeric() http://php.net/manual/en/function.is-numeric.php and the PHP ternary operator http://us.php.net/manual/en/language.op ... arison.php.

It is this sort of situation that makes it far more efficient to study PHP in an orderly succession by taking a class or reading a book or studying a tutorial, thus learning the pieces in the right sequence, because you could just simply write:

Code: Select all

...
foreach ($MyRows as $row) 
{
   explode($data, ',');
   foreach ($data as $data)
   {
      $printdata = is_numeric($data) ? number_format($data, 2) : $data);
      print "$printdata</td>";
   }
   print "</tr>";
}
...
and not worry about what column the numeric data is in.
OK. Now that I played around with this bit of code and it works, I cannot find how it can be right aligned. I would use number_format, but I'm using Windows so it does not work. I reviewed all the examples in the manual for number_format, but I did not see any clues for right align. I only want the cells that are numbers to be right aligned.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Could use some improvement

Post by Celauran »

A simple CSS rule will do the trick here.

Code: Select all

td.right
{
    text-align: right;
}

Code: Select all

print (is_numeric($data)) ? "<td class=\"right\">" : "<td>";
print $printdata . "</td>";
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Could use some improvement

Post by califdon »

Yes, it's important to make the distinction between the actual data (in this case, the formatted number) and how it's to be displayed, which is controlled by HTML and CSS. You can format the number with a PHP function (or a Javascript function), but the display, such as right justification, is part of HTML and/or CSS, although you may optionally send the HTML/CSS to the browser using PHP.
User avatar
BBoyd
Forum Commoner
Posts: 31
Joined: Sat Jan 21, 2012 8:24 pm
Location: Charleston, SC

Re: Could use some improvement

Post by BBoyd »

This makes perfect sense. I consider myself fairly knowledgeable in HTML and CSS, but not by incorporating it with loops within PHP. That is beyond the scope of my education and classes right now. My assignments are not dictating that I even go as far as I am, but I like to stretch out and see what the possibilities are. I'm about 3-4 weeks ahead in my PHP class so for the next 10 days or so I will need to concentrate on SQL and C++. I will be back.

Califdon, it has been a pleasure meeting you and I hope that we will talk again in the near future. You've helped me become more knowledgeable in PHP and programming logic and I thank you for that.

Brian
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Could use some improvement

Post by califdon »

Thanks for your kind words. We'll be glad to try to help you whenever you need it.

Just remember that PHP can be used to generate the HTML that is sent to the browser, so always think in terms of what the HTML must look like when it is read by the browser. If there is a repetitive block of HTML, such as a series of <option>s within a <select> element, it may make sense to generate it by a PHP loop with changing values. Similarly, if you need different HTML, depending on something known at the time the page is being assembled at the server, you can use PHP conditionals to send the appropriate HTML.

Good luck with the C++.
Post Reply