Page 2 of 2

Re: Could use some improvement

Posted: Sat Feb 11, 2012 10:57 pm
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!

Re: Could use some improvement

Posted: Sun Feb 12, 2012 11:19 am
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.

Re: Could use some improvement

Posted: Mon Feb 13, 2012 11:50 am
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.

Re: Could use some improvement

Posted: Mon Feb 13, 2012 12:24 pm
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>";

Re: Could use some improvement

Posted: Mon Feb 13, 2012 1:03 pm
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.

Re: Could use some improvement

Posted: Mon Feb 13, 2012 9:18 pm
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

Re: Could use some improvement

Posted: Tue Feb 14, 2012 12:19 pm
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++.