I need help with my loop. In each div, I want to show two sets of FirstName and LastName instead of just one set, but I don’t know how can I do that because of the loop. Also, the point of setting different font sizes is to create a visual look which is a funnel-like shape. My questions are: how can I add another set of names in each div and is there a better way to code this? How can I make my code more efficient?
Edit: Well, I’m mainly trying to figure out how to add another set of names into the div or I can just use another loop. What I mean when I say add another set of name into the div, I mean I want to add another row of data to the div; I want to have the first two rows of data fetched from MySQL in one div.
$state = 1;
$fontcount = 25;
while ($row = mysql_fetch_assoc($result)) {
if( $fontcount == 25 ) { $fontsize = "250%";
} elseif( $fontcount < 25 && $fontcount >= 22 ) { $fontsize = "210%";
} elseif( $fontcount < 22 && $fontcount >= 19 ) { $fontsize = "170%";
} elseif( $fontcount < 19 && $fontcount >= 16 ) { $fontsize = "150%";
} elseif( $fontcount < 16 && $fontcount >= 13 ) { $fontsize = "130%";
} else { $fontsize = "110%";
}
if( $state%2 == 0 ) {
echo "<div style='background-color: #black; font-size: " . $fontsize . "; text-transform:uppercase; text-align:center;'>";
} else {
echo "<div style='background-color: #blue; font-size: " . $fontsize . "; text-transform:uppercase; text-align:center;'>";
}
echo $row['FirstName'] . " " . ' <span style="font-size: 15px;">$' . $row['LastName'] . "</span>";
echo "</div>";
$state++;
$fontcount--;
}
If I’m understanding your question correctly, you want to display two full names in each div.
To do that, you should only open the div tag when the record is odd, then close it when the row is even or is the last record in the set. There are a few ways to achieve this. I would suggest you keep an even/odd counter much like you’re doing now with $state. Open the div tag on an odd record; close it on an even record. (Add a check after the loop to close the div if the final record was odd.) You would then have to adjust your $state variable to only increment when the record was odd, so that the divs got alternating styling.
As tster pointed out, your code here isn’t really inefficient in any serious way. You could probably nit-pick a few minor things, but the only real efficiency problems you will see in most applications lies in nested loops. Since you’re writing output here, I assume your code is not deeply nested, so I wouldn’t sweat it.