I was milling over some old PHP code today, funny, because my friend Norm linked me to David Hansson’s blog post…it looks like PHP is a Friday thing!
After looking at my old code, I thought to myself, hmm, what better time to showcase the beauty of Rails. I made a post on a message board I frequent, and there was quite a bit of discussion. Anyways, take a look at this!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | header("Content-type: text/html; charset=utf-8"); include_once('inc.functions.php'); require('inc.drawrating.php'); $uid = $_GET['uid']; $conn= mysql_connect("localhost","db","dbpassword") or die (mysql_error()); mysql_select_db(ave_development) or die(mysql_error()); $q = "SELECT views FROM stores WHERE uid=$uid"; $result = mysql_query($q,$conn); $tmp = mysql_fetch_object($result); $views = $tmp->views; $views++; $lview = date("Y-m-d H:i:s"); $q = "UPDATE stores SET views=$views, last_view='$lview' WHERE uid=$uid"; mysql_query($q, $conn); $q = "select * from stores where uid = $uid"; $result = mysql_query($q,$conn); $row = mysql_fetch_object($result); $name = $row->name; $phone = $row->phone; if ($phone == 0) { $phone = ""; } else { $phone = " - " . format_phone($phone); } $description = $row->description; $description_lb = str_replace("\n", "<br>", $row->description); $address = $row->address1; |
This is the code I would use if I were to do the same thing in Rails.
1 2 3 4 5 | # the controller class StoresController < ApplicationController @store = Store.find_by_id(params[:id]) @store.increment!("views") end |
1 2 3 | # I'd place this in my 'show' view <%= number_to_phone(@store.phone) if @store.phone.exists? -%> <%= simple_format(@store.description) -%> |