CodeIgniter Forums
Adjust MySQL data represented on the HTML Page... - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Adjust MySQL data represented on the HTML Page... (/showthread.php?tid=51813)



Adjust MySQL data represented on the HTML Page... - El Forum - 05-18-2012

[eluser]BoyBlue[/eluser]
I've been looking for this answer for a little while and I'm sort of stumped...

I'm grabbing data (date and rating of a movie) from a mysql table to display on an HTML page. The way it has been input into the database is... Date: "date type' in mysql (1995-06-30)
Rating: 'g', 'pg', 'pg13', or 'r'

I want the HTML page to display them as (date) '1995' and (rating) 'G' or 'PG' or 'PG13' or 'R'

What would be the easiest way to accomplish this without changing how the data is inserted into the table?



thanks,
Ryan


Adjust MySQL data represented on the HTML Page... - El Forum - 05-18-2012

[eluser]TWP Marketing[/eluser]
Treat the date as a string array and read the first four characters as the year.
Code:
$someyear = $substr($date,0,3);
Use the strtoupper() function on your film rating value to force to upper case.


Adjust MySQL data represented on the HTML Page... - El Forum - 05-18-2012

[eluser]Unknown[/eluser]
Thanks for posting this. i really enjoyed reading this.

[url="http://www.focusbis.com.au/"]Quality management system[/url]


Adjust MySQL data represented on the HTML Page... - El Forum - 05-21-2012

[eluser]BoyBlue[/eluser]
Thanks so Much TWP Marketing!

Ok...so, here is the final code that works like a charm!

For the Year of Date only:
Quote:<? foreach ($movie_query->result() as $row) {

$movie_year=substr($row->release_date,0,4);

echo $movie_year; } ?>

For the Upper Case:
Code:
<? foreach ($movie_query->result() as $row) {
  
  $upper_rating=strtoupper($row->rated);
  
  echo $upper_rating; } ?>



Adjust MySQL data represented on the HTML Page... - El Forum - 05-21-2012

[eluser]boltsabre[/eluser]
You don't have to assign them to variables first, so to save on time and code, to increase page load speeds, reduce required server processing power, and reduce the risk of bugs, feel free to do it like this:

Code:
<? foreach ($movie_query->result() as $row) {
  echo substr($row->release_date,0,4);
  echo strtoupper($row->rated);
  } ?>



Adjust MySQL data represented on the HTML Page... - El Forum - 05-21-2012

[eluser]BoyBlue[/eluser]
Great suggestion boltsabre--I will do it!!