WEB let's compare PHP next to Rails

Discussion in 'OT Technology' started by biawokauns, Apr 4, 2008.

  1. biawokauns

    biawokauns New Member

    Joined:
    Sep 18, 2001
    Messages:
    19,893
    Likes Received:
    0
    Location:
    Republic of Kalifornia
    I'm looking at an old app I wrote in PHP, it's messy as fuck, but I want ot use it to demonstrate why rails is FTW!

    ps: is there a PHP tag in OT to do syntax highlighting?

    the below code basically does the following, connects to the DB, finds a store by the UID, and does some formatting
    Code:
    <?
    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;
    
    
    ?>
    
    

    here's how I would do it in rails, (mind you, I am writing this right now)
    Code:
    class StoresController < ApplicationController
     @store = Store.find_by_id(params[:id])
     @store.increment!("views")
    end
    
    then in the view, the only code I'd need would be
    Code:
    <%= number_to_phone(@store.phone) if @store.phone.exists? -%>
    <%= simple_format(@store.description) -%>
    
    yeah , I know I could move that code to a helper, or to the controller, but this is just for illustration :p
     
  2. samxx

    samxx self made man

    Joined:
    Jan 2, 2006
    Messages:
    1,366
    Likes Received:
    0
    Location:
    San Francisco, CA (no homo)
    duh. php ftl
     
  3. Jesse

    Jesse PSN: iamajesse; XBL: Inhale My Rod; G8 GT crew; Ne OT Supporter

    Joined:
    Jan 12, 2005
    Messages:
    25,649
    Likes Received:
    0
    Location:
    California :: (925)
    And where do you specify the DB info for ROR?
     
  4. biawokauns

    biawokauns New Member

    Joined:
    Sep 18, 2001
    Messages:
    19,893
    Likes Received:
    0
    Location:
    Republic of Kalifornia
    in a config/database.yml. this would be the development db, you would use production and tests too

    Code:
    development:
      adapter: mysql
      database: db_name
      username: root
      password: 
      socket: /var/lib/mysql/mysql.sock
    
    
     
  5. Vailripper

    Vailripper Daywalkers have feelings too.

    Joined:
    Jan 25, 2006
    Messages:
    6,170
    Likes Received:
    0
    Location:
    Bellingham, WA
    .

    All Rails is doing is adding anther layer of abstraction. It's equivalent to creating an N-tier application. I do the same thing when I'm writing .NET, but it's built on a much more robust framework.
     
  6. littleman

    littleman I took an AMEX survey and all I got was this crapp

    Joined:
    Sep 9, 2004
    Messages:
    2,597
    Likes Received:
    0
    Location:
    NY
    i want to learn rails... wheres the best place to start?
     
  7. biawokauns

    biawokauns New Member

    Joined:
    Sep 18, 2001
    Messages:
    19,893
    Likes Received:
    0
    Location:
    Republic of Kalifornia
    read my first tutorial, the browse the wiki on rubyonrails.org...lots of the shit is out of date, i hope to put out a bunch of updated tutorials very soon
     
  8. Ricky

    Ricky █▄ █▄█ █▄ ▀█▄

    Joined:
    Jun 17, 2005
    Messages:
    38,767
    Likes Received:
    6
    Is it really that easy ? :o
     
  9. littleman

    littleman I took an AMEX survey and all I got was this crapp

    Joined:
    Sep 9, 2004
    Messages:
    2,597
    Likes Received:
    0
    Location:
    NY
    your wordpress crashed :(
     
  10. biawokauns

    biawokauns New Member

    Joined:
    Sep 18, 2001
    Messages:
    19,893
    Likes Received:
    0
    Location:
    Republic of Kalifornia
    upgrading to 2.5 son
     
  11. Jesse

    Jesse PSN: iamajesse; XBL: Inhale My Rod; G8 GT crew; Ne OT Supporter

    Joined:
    Jan 12, 2005
    Messages:
    25,649
    Likes Received:
    0
    Location:
    California :: (925)
    It's almost too easy. :mamoru:

    It might be the first programming language I pick up.. oh wait, I'm too fucking lazy to learn any.
     
  12. Logik

    Logik Livin la vida broka

    Joined:
    Jun 30, 2000
    Messages:
    20,667
    Likes Received:
    1
    Location:
    The Steel City
    interesting...
     
  13. samxx

    samxx self made man

    Joined:
    Jan 2, 2006
    Messages:
    1,366
    Likes Received:
    0
    Location:
    San Francisco, CA (no homo)
    its nice because it moves the development cycle up significantly, allowing you to pump out more projects faster and to respond to the client, faster.

    in short, more money.

    occasionally you run into some bullshit and need to get creative, but that's why you don't know JUST rails.
     
  14. biawokauns

    biawokauns New Member

    Joined:
    Sep 18, 2001
    Messages:
    19,893
    Likes Received:
    0
    Location:
    Republic of Kalifornia
    like when .count breaks :wtc:
     
  15. MrBrotato

    MrBrotato New Member

    Joined:
    Sep 7, 2005
    Messages:
    5,558
    Likes Received:
    0
    I sort of know a little PHP and I've heard a lot of talk about how Ruby is just sort of a fad and can't really do all the things PHP can do. Is this true? Typically I just do simple includes for header/footer files, is Ruby suitable for this type of stuff or no?
     
  16. kingtoad

    kingtoad OT Supporter

    Joined:
    Sep 2, 2003
    Messages:
    55,918
    Likes Received:
    10
    Location:
    Los Angeles
    Okay, let's see this.

    Yes, stand-alone PHP code can and does look a bit ugly and tedious. Let's be fair though... Ruby on Rails is using a solid framework. The PHP script you are displaying does not. PHP can use just as little code using a framework to accomplish those exact needs. There's a lot of bad PHP code on the net and the reason for that is because it's shipped without a framework, straight out of the box, unlike ruby on rails is. I don't know too much about Rails so if I'm talking out of my ass about RoR, let me know, but some of that information is what I've picked up on.

    Granted the Rails code looks a lot easier, the PHP code can look just as easy using a solid framework. :o
     
  17. samxx

    samxx self made man

    Joined:
    Jan 2, 2006
    Messages:
    1,366
    Likes Received:
    0
    Location:
    San Francisco, CA (no homo)
    true, cakePHP (for example) is a pretty decent rails competitor.

    I just feel that rails is a bit more.. baked.

    that said, its important to know more than just a few.
     
  18. biawokauns

    biawokauns New Member

    Joined:
    Sep 18, 2001
    Messages:
    19,893
    Likes Received:
    0
    Location:
    Republic of Kalifornia
    except the beauty of Rails is ActiveRecord :o
     
  19. biawokauns

    biawokauns New Member

    Joined:
    Sep 18, 2001
    Messages:
    19,893
    Likes Received:
    0
    Location:
    Republic of Kalifornia
    no doubt, PHP is powerful, I learned PHP first. The script you see is crap, but I wanted to use it, because lots of scriptst look like that :o

    One of the things about Rails is it stresses convention over configuration, look at my tutorial with SQLite, you don't even configure the fucker :p You can look at rails code and say, fuck, I don't know Ruby, but I know what the hell is going on here. THAT is the true beauty of rails.
     
  20. biawokauns

    biawokauns New Member

    Joined:
    Sep 18, 2001
    Messages:
    19,893
    Likes Received:
    0
    Location:
    Republic of Kalifornia
    Rails goes back to using the MVC, model/view/controller, it separates web requests from database interaction from display/views.

    with your example you would create a basic layout, which you could fuck with. for example

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
           "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
      <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
      <title>Temps: <%= controller.action_name %></title>
      <%= stylesheet_link_tag 'scaffold' %>
    </head>
    <body>
    
    <p style="color: green"><%= flash[:notice] %></p>
    
    <%= yield  %>
    
    </body>
    </html>
    
    now you could include headeres and footers there, but this is how i woul do it:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
           "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <%= render :partial => 'layout/header' %>
    <body>
    
    <p style="color: green"><%= flash[:notice] %></p>
    
    <%= yield  %>
    
    <%= render :partial => 'layout/footer' %>
    
    </body>
    </html>
    
    
     
  21. Supergeek

    Supergeek New Member

    Joined:
    Jan 23, 2007
    Messages:
    1,855
    Likes Received:
    0
    Location:
    Colorado
    Totally agree.
     
  22. biawokauns

    biawokauns New Member

    Joined:
    Sep 18, 2001
    Messages:
    19,893
    Likes Received:
    0
    Location:
    Republic of Kalifornia
    It might be a fad, but it can definitely do what PHP can do.
     
  23. biawokauns

    biawokauns New Member

    Joined:
    Sep 18, 2001
    Messages:
    19,893
    Likes Received:
    0
    Location:
    Republic of Kalifornia
    a framework is exactly that, a framework. no doubt cakephp offers a solid framework, but it's still trying to emulate something that it's not...the Ruby language, which in itself, was created to be simplistic
     
  24. Supergeek

    Supergeek New Member

    Joined:
    Jan 23, 2007
    Messages:
    1,855
    Likes Received:
    0
    Location:
    Colorado
    Actually you're talking about the Rails framework, not the Ruby language.
     
  25. Supergeek

    Supergeek New Member

    Joined:
    Jan 23, 2007
    Messages:
    1,855
    Likes Received:
    0
    Location:
    Colorado
    And I've tried to learn Rails. It has some stupid arbitrary shit that pisses me off, like creating your table names for you, and hope to god you chose a field name it recognizes so it can pluralize it. And arbitrary uppercase/lowercase restrictions on certain things.

    Other languages make sense, but frameworks, and particularly Rails, have some arbitrary shit that just doesn't make sense, you just have to do it their way.

    I really would like to be able to develop more rapidly, though. It's just so much stuff already uses PHP... I don't think PHP is hard at all, but it does have a tendancy to get messy if you don't plan your project well.
     

Share This Page