+ Reply to Thread
Results 1 to 5 of 5

Thread: Which ways best

  1. #1
    Join Date
    Aug 2007
    Location
    South West
    Posts
    13

    Question Which ways best

    Hello everyone, i would very much appreciate some advice regarding how im writing php.

    Now, i notice a few different ways. the following are two examples of how i have come accross different ways to write it.

    Example 1

    PHP Code:
    $sql mysql_query("SELECT * FROM future ORDER BY id ASC") or die (mysql_error());
    while (
    $row mysql_fetch_array($sql)) {
       
    $title $row["exhibition_name"];
    $artists $row["artists"];
    $date $row["date"];
    $description $row["description"];
            
            
    echo 
    '
    <p>'
    .$title.'</p>
    <p>'
    .$artists.'</p>
    <p>'
    .$date.'</p>        
    <p>'
    .$description.'</p>
    '
    ;
    }         
    ?> 
    Example 2

    PHP Code:
    $sql "SELECT * FROM future ORDER BY id ASC;";
    $result mysql_qyery($sql);
    $row mysql_fetch_assoc($result);

    echo
    " <p>".$row['exhibition_name']."</p>";
    echo
    " <p>".$row['artists']."</p>";
    echo
    " <p>".$row['date']."</p>";
    echo
    " <p>".$row['description']."</p>";

    ?> 
    Now, i find example 1 a breeze, and visually its more organised and easy to read. I get a bit confused with example 2 and find that i always hav more errors this way, as its easier to miss off commas etc.

    What is the best way, is one more professional than the other???

    Many Thanks,
    Sam

  2. #2
    Join Date
    Feb 2006
    Location
    North of Boston, MA
    Posts
    1,471

    Default

    Without looking too closely at the code, there is no "best" way to write this. Unless you plan to share this code with others and a universal format is desired, go with what works for you. Over time, you will learn what makes the most sense. Just my $0.02...
    redsox9 - Go Red Sox!!! 2004 and 2007 World Series Champions!
    Visit FenwayFanatics.com, home to Boston Red Sox baseball fans everywhere

  3. #3
    Join Date
    Feb 2006
    Location
    Somewhere where I don't know where I am
    Posts
    2,155

    Default

    IMO, this is what I'd do.

    PHP Code:
    $sql "SELECT exhibition_name, artists, date, description FROM future ORDER BY id ASC;";
    $result mysql_qyery($sql);
    $row mysql_fetch_array($result);

    echo 
    '<p>' $row[0] . '</p>';
    echo 
    '<p>' $row[1] . '</p>';
    echo 
    '<p>' $row[2] . '</p>';
    echo 
    '<p>' $row[3] . '</p>'
    You could also use the row names
    PHP Code:
    echo '<p>' $row['exhibition_name'] . '</p>';
    echo 
    '<p>' $row['artists'] . '</p>';
    echo 
    '<p>' $row['date'] . '</p>';
    echo 
    '<p>' $row['description'] . '</p>'
    But as redsox said, there really isn't necessarily a "best" way.
    Sign Up Now!
    Unlimited Storage, Unlimited Transfer, Host Unlimited domain names, 1 Free Domain Name
    BlueHost Features | BlueHost Help Desk | Become a BlueHost Affiliate | BlueHost CEO Blog
    (888) 401-4678 | Create a support ticket

  4. #4

    Default

    Quote Originally Posted by areidmtm View Post
    IMO, this is what I'd do.

    PHP Code:
    $sql "SELECT exhibition_name, artists, date, description FROM future ORDER BY id ASC;";
    $result mysql_qyery($sql);
    $row mysql_fetch_array($result);

    echo 
    '<p>' $row[0] . '</p>';
    echo 
    '<p>' $row[1] . '</p>';
    echo 
    '<p>' $row[2] . '</p>';
    echo 
    '<p>' $row[3] . '</p>'
    You could also use the row names
    PHP Code:
    echo '<p>' $row['exhibition_name'] . '</p>';
    echo 
    '<p>' $row['artists'] . '</p>';
    echo 
    '<p>' $row['date'] . '</p>';
    echo 
    '<p>' $row['description'] . '</p>'
    But as redsox said, there really isn't necessarily a "best" way.
    i myself personally always use the names. the reason is, it helps you know what that variable is. $row[3] is not very descriptive, and it is harder to know exactly what data is saved in it. but $row['exhibition_name'] is very very clear to exactly what the variable is storing. good programming uses variable names that are as descriptive as possible

  5. #5
    Join Date
    Feb 2006
    Location
    Somewhere where I don't know where I am
    Posts
    2,155

    Default

    Quote Originally Posted by wallaceb View Post
    good programming uses variable names that are as descriptive as possible
    I've been programming for over 10 years and I consider myself a good programmer with good programming skills. I understand your point that it's easier to know what the variable is. That might be good if you're reselling your code to someone that might make changes or edit the code your wrote.

    The code that I have coded were for people that we're(can't) going to edit them. Copyrights and contracts prohibits that. When I look at my code and see $row[0], I know exactly what that is because I wrote it. I always found it simpiler to write $row[0] rather than $row['variable_name'] again since I already wrote it once in the select statement.

    Just my option, either way works and neither is better then the other.

    Sometimes obscurity is security.
    Sign Up Now!
    Unlimited Storage, Unlimited Transfer, Host Unlimited domain names, 1 Free Domain Name
    BlueHost Features | BlueHost Help Desk | Become a BlueHost Affiliate | BlueHost CEO Blog
    (888) 401-4678 | Create a support ticket

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts