patternphpMinor
Displaying recent buyers' feedback on games
Viewed 0 times
recentgamesdisplayingbuyersfeedback
Problem
I have the following code made to output the following:
Is there an efficient way to simplify it and not have to repeat the same
Is there an efficient way to simplify it and not have to repeat the same
if(!empty($st[$i]['feedback'])) { code block 2 times just to seperate 2 feedback lines with a echo '
';?$urlname,":platform"=>$platform));
echo '';
//Output the first 2 Feedback on line 1;
for($i = 0; $i
';
echo $st[$i]['feedback'].'
';
}
}
echo '';
//Output the last 2 Feedback on line 2;
for($i = 2; $i
';
echo $st[$i]['feedback'].'
';
}
}
echo '';
?>Solution
Simple Approach
The simplest thing to remove copy-pasted code is to extract the exact duplication to its own function. Any variables that are needed can be passed on:
Then use it:
Now the duplication is gone.
Further Improvements
Now, the simple approach did remove the direct duplication, but we still have two similar for loops, and we have a function that accepts arguments that do not have real meaning in that context.
Let's improve it further by adjusting our print feedback function:
Personally, I would create a real feedback object, but for this example I have kept it as an array, which may be fine for your purposes.
Now we can use this function in a more generic function:
And then use that function:
Misc
The simplest thing to remove copy-pasted code is to extract the exact duplication to its own function. Any variables that are needed can be passed on:
function printFeedback($st, $i) {
if(!empty($st[$i]['feedback'])) {
echo '
';
echo $st[$i]['feedback'].'
';
}
}Then use it:
$st = fetch([...]);
echo '';
//Output the first 2 Feedback on line 1;
for($i = 0; $i ';
//Output the last 2 Feedback on line 2;
for($i = 2; $i ';Now the duplication is gone.
Further Improvements
Now, the simple approach did remove the direct duplication, but we still have two similar for loops, and we have a function that accepts arguments that do not have real meaning in that context.
Let's improve it further by adjusting our print feedback function:
// echoes the given feedback array inside HTML.
function printFeedback($feedback) {
if($feedback['feedback'])) {
echo '
';
echo $feedback['feedback'].'
';
}
}Personally, I would create a real feedback object, but for this example I have kept it as an array, which may be fine for your purposes.
Now we can use this function in a more generic function:
// echoes the feedbacks in the feedback starting at position "from" and going to position "to" (exclusive).
function printFeedbackFromTo(Array $feedbacks, $from, $to) {
for($i = $from; $i < $to; $i++) {
printFeedback($feedbacks[$i]);
}
}And then use that function:
$st = fetch([...]);
echo '';
printFeedbackFromTo(0,2);
echo '';
printFeedbackFromTo(2,4);
echo '';Misc
- Feedback seems to be user-supplied, so you have to HTML encode it, to prevent XSS.
- Your comments are not needed. Just extract code into properly named functions, possibly with proper function comments (ideally using PHPDoc). In-code comments should not be used to structure your code - use functions for that - but to point out possible unclarities, problems, etc that could not be resolved otherwise.
- The functions you might want to consider would be
getFeedbacks($connection, $amountOfFeedbacks)andechoFeedbacks($feedbacks).
- Don't put your style info in a style attribute, but in an external CSS file
Code Snippets
function printFeedback($st, $i) {
if(!empty($st[$i]['feedback'])) {
echo '
<div class="col-lg-6 center">
<div class="col-sm-12 center" style="width: initial; background-color: ';
if($st[$i]['feedback_type'] == '+') { echo '#1AB394;"'; } else { echo 'red;"'; };
echo '>
<h2 style="font-size: 30px; color: #fff;">';
echo $st[$i]['feedback'].'
</h2>
</div>
</div>';
}
}$st = fetch([...]);
echo '<div class="row center">';
//Output the first 2 Feedback on line 1;
for($i = 0; $i < 2; $i++) {
printFeedback($st, $i);
}
echo '</div><br><div class="row center">';
//Output the last 2 Feedback on line 2;
for($i = 2; $i < 4; $i++) {
printFeedback($st, $i);
}
echo '</div>';// echoes the given feedback array inside HTML.
function printFeedback($feedback) {
if($feedback['feedback'])) {
echo '
<div class="col-lg-6 center">
<div class="col-sm-12 center" style="width: initial; background-color: ';
if($feedback['feedback_type'] == '+') { echo '#1AB394;"'; } else { echo 'red;"'; };
echo '>
<h2 style="font-size: 30px; color: #fff;">';
echo $feedback['feedback'].'
</h2>
</div>
</div>';
}
}// echoes the feedbacks in the feedback starting at position "from" and going to position "to" (exclusive).
function printFeedbackFromTo(Array $feedbacks, $from, $to) {
for($i = $from; $i < $to; $i++) {
printFeedback($feedbacks[$i]);
}
}$st = fetch([...]);
echo '<div class="row center">';
printFeedbackFromTo(0,2);
echo '</div><br><div class="row center">';
printFeedbackFromTo(2,4);
echo '</div>';Context
StackExchange Code Review Q#123226, answer score: 3
Revisions (0)
No revisions yet.