views:

133

answers:

4

User 2 offers to buy an item from User 1. User 1 can accept or reject. If User 1 accepts, then they will both be able to offer feedback about the transaction.

I have 2 blocks of IF statements. They both work and do the same thing but which is better coding practice?

IF BLOCK 1 checks if which user is there first and then checks if the transaction was accepted or if its still pending

 if ($_SESSION['user_id'] == $seller) {

    if ($row['status'] == 'P') {
        echo '<p>' . get_username_by_id($row['buyer']) . ' has made a bid of ' . $row['price'] . ' for your ' . $row['title'] . '
    <a href="transactions.php?id=' . $transactionid . '&action=accept">Accept</a> / <a href="transactions.php?id=' . $transactionid . '&action=reject">Reject</a><br />';
    } else if ($row['status'] == 'A') {
        echo '<p>' . get_username_by_id($row['buyer']) . ' paid ' . $row['price'] . ' for your ' . $row['title'] . '</p>';
        echo '<a href="feedback.php?id=' . $transactionid . '&action=givefeedback">Give Feedback</a></p>';
    }
} else if ($_SESSION['user_id'] == $buyer) {

    if ($row['status'] == 'P') {
        echo '<p> You have made a bid of ' . $row['price'] . ' for ' . $row['title'] . '</p>';
    } else if ($row['status'] == 'A') {
        echo '<p> You have paid ' . $row['price'] . ' for ' . $row['title'] . '</p>';
        echo '<a href="feedback.php?id=' . $transactionid . '&action=givefeedback">Give Feedback</a></p>';
    }
}

Or

IF BLOCK 2 has only 4 if statements and checks both user and status of transaction at same time

   if ($_SESSION['user_id'] == $seller && $row['status'] == 'P') {
    echo '<p>' . get_username_by_id($row['buyer']) . ' has made a bid of ' . $row['price'] . ' for your ' . $row['title'] . '
    <a href="transactions.php?id=' . $transactionid . '&action=accept">Accept</a> / <a href="transactions.php?id=' . $transactionid . '&action=reject">Reject</a><br />';
} else if ($_SESSION['user_id'] == $buyer && $row['status'] == 'P') {
    echo '<p> You have made a bid of ' . $row['price'] . ' for ' . $row['title'] . '</p>';
} else if ($_SESSION['user_id'] == $seller && $row['status'] == 'A') {
    echo '<p>' . get_username_by_id($row['buyer']) . ' paid ' . $row['price'] . ' for your ' . $row['title'] . '</p>';
    echo '<a href="feedback.php?id=' . $transactionid . '&action=givefeedback">Give Feedback</a></p>';
} else if ($_SESSION['user_id'] == $buyer && $row['status'] == 'A') {
    echo '<p> You have paid ' . $row['price'] . ' for ' . $row['title'] . '</p>';
    echo '<a href="feedback.php?id=' . $transactionid . '&action=givefeedback">Give Feedback</a></p>';
}
+3  A: 

This is subject to opinion, but I'm sure that most people will agree that the first one is cleaner because you're removing duplication (the check to see if they are buyer or seller). This will be even more apparent if you had more status types.

ryeguy
+1  A: 

I would say block 1 is better coding practice in general, since you do not duplicate information. Having said that, Block 2 more accurately describes the set of Strategy patterned objects which might arise in a different language environment and which could reduce the complexity of your code further.

Mike Burton
+4  A: 

The first one shows you the path: if the statuses were to increase in number, you could abstract the functionality into a function with little work. It looks and is cleaner.

I'd also prefer to separate the actual HTML from PHP. Instead of

echo '<p>' . get_username_by_id($row['buyer']) . ' has made a bid of ' 
. $row['price'] . ' for your ' . $row['title'] . '
<a href="transactions.php?id=' . $transactionid . '&action=accept">Accept</a> / 
<a href="transactions.php?id=' . $transactionid . '&action=reject">Reject</a><br />';

I'd prefer

<p>
  <?= get_username_by_id($row['buyer']) ?> has made a bid of 
  <?= $row['price'] ?> for your <?= $row['title'] ?> 
  <a href="transactions.php?id=<?= $transactionid ?>&action=accept">Accept</a> / 
  <a href="transactions.php?id=<?=$transactionid?>&action=reject">Reject</a>
</p>

But to each his own.

Adriano Varoli Piazza
Note: I used short tags to save space, I know they're not always active on every server. If that's the case, substitute `<?=` for `<?php echo `
Adriano Varoli Piazza
+1  A: 

As a personal choice I prefer the structure of option 1 because of the lower number of conditions. But I would change each else if to elseif to prevent errors because of omitted braces.

To let the code show some common data is used in the output, and the differences in the closing </p> tag of each choice, I would change it into something like this:

$buyerName = get_username_by_id($row['buyer']);
$price = $row['price'];
$title = $row['title'];

if ($_SESSION['user_id'] == $seller) {
    if ($row['status'] == 'P') {
        echo "<p>$buyerName has made a bid of $price for your $title"
           . " <a href='transactions.php?id=$transactionid&amp;action=accept'>Accept</a> /"
           . " <a href='transactions.php?id=$transactionid&amp;action=reject'>Reject</a><br />";
    } elseif ($row['status'] == 'A') {
        echo "<p>$buyerName paid $price for your $title</p>"
           . "<a href='feedback.php?id=$transactionid&amp;action=givefeedback'>Give Feedback</a></p>";
    }
} elseif ($_SESSION['user_id'] == $buyer) {
    if ($row['status'] == 'P') {
        echo "<p> You have made a bid of $price for $title</p>";
    } elseif ($row['status'] == 'A') {
        echo "<p> You have paid $price for $title</p>"
           . " <a href='feedback.php?id=$transactionid&amp;action=givefeedback'>Give Feedback</a></p>";
    }
}
Kwebble