Responding to the query about rps, and also addressing the OP's question, I would suggest steering clear of utilizing a switch
statement and opting for an array instead. Given that row.currency
will consistently hold values of 1, 2, or 3, you can easily map these to corresponding currency symbols within an array assigned to currency
:
var currency = ['£','£','$','€'];//default value is 0
//string concatenation:
'<td>' + currency[+(row.currency) || 0] + '<inp...';
The logic here relies on the assumption that coercing row.currency
into a number will always result in a value less than or equal to 3. If it exceeds this range, you risk appending undefined
to the string. To mitigate this, consider the following adjustment:
'<td>' + currency[+(row.currency)] || currency[0] + '<inp...';
This ensures that the expression defaults to '£'
. However, overall, I find your code structure rather peculiar. It appears that you are constructing HTML within JavaScript, likely intended for client-side execution. In scenarios involving PHP (as implied by your title), generating HTML server-side is typically more advantageous compared to your current approach.
If hypothetically querying a database with PDO
:
$currency = array(1 => '£', 2 => '$', 3 => '€');
while($row = $stmt->fetch(PDO::FETCH_OBJ))
{
$row->currency = isset($currency[$row->currency]) ? $row->currency : 1;//default 1
echo '<tr><td>'. $currency[$row->currency].'</td>';
}
In light of this, without clearer details regarding the context and purpose of your posted code, determining the optimal strategy remains uncertain.