Skip to content
Advertisement

Making an array out of substr_replace

Hi everyone newbie here,

I’m taking on the tedious task of changing a bunch of IDs for a game and turning them into a string. As you can see here I do this for ID 29 and turn it into Sharpshooter. Is there a more efficient way for me to do this? Or am i stuck writing about 100 more if cases?

    if ($rows[1] == 29)
    {
        $rows[1] = substr_replace("29","Sharpshooter",0); 
    }

Below is my full code and I have added a few more examples of what I’m talking about.

<?php

/* Link DB */
include_once 'config.php';

/* Initialize Connection */
$conn=sqlsrv_connect($serverName, $conn_array);
if ($conn){
}else{
    die; 
}

/* Prepare Statement Preparation */
$sql = "
SELECT TOP 25 G.CharacterName, G.JobCode, D.PVPWin, D.PVPLose, G.PvPExp, D.PVPGiveUp
FROM PvPRanking as G
INNER JOIN PVPScores as D
ON G.CharacterID = D.CharacterID
ORDER BY  RANK() OVER (ORDER BY TotalRank ASC ) 
";

/* Assign Parameter values. */
$param1 = 1;
$param2 = 2;

// Array requirement for prepare statement.
$procedure_params = array(
    array(&$param1, SQLSRV_PARAM_OUT),
    array(&$param2, SQLSRV_PARAM_OUT)
);

/* The ACTUAL Prepare Statement */
$stmt = sqlsrv_prepare( $conn, $sql, $procedure_params);

/*Execute*/
sqlsrv_execute($stmt);


/* Variables */
$autoincrement = 0;

echo

'
<tr>
<th>Rank</th>
<th>Name</th>
<th>Class</th>
<th>Wins</th>
<!-- <th>Losses</th>
<th>Experience</th>
<th>Quit</th> -->
</tr>';

// Rank # to increment itself. I.E 1,2,3,4,5,6 etc..
while ($rows=sqlsrv_fetch_array($stmt))
{
    $autoincrement++;
{
    if ($rows[1] == 29)
    {
        $rows[1] = substr_replace("29","Sharpshooter",0); 
    }
    if ($rows[1] == 30)
    {
        $rows[1] = substr_replace("30","Knight",0); 
    }
    if ($rows[1] == 29)
    {
        $rows[1] = substr_replace("31","Dragon Slayer",0); 
    }

}

// Echo will spit out the rows and the data.
echo 

'
<tr id=io>
<td> '.$autoincrement.' </td>
<b> <td style = "color:#AFA;"> '.$rows[0].' </td> </b>
<td> '.$rows[1].' </td>
<td> '.$rows[2].' </td>
<!--  <td> '.$rows[3].' </td>
<td> '.$rows[4].' </td>
<td> '.$rows[5].' </td> -->
';  


}
 ?>




</table>

Advertisement

Answer

Create your own translation array. If the value is not a key in the array, do not replace it.

$replacements = [
    29 => 'Sharpshooter',
];

$rows[1] = $replacements[$rows[1]] ?? $rows[1];

Assuming you are using a loop, the translation array should be written before the loop.

Alternatively, this could all be executed in the sql with a CASE block.


Ultimately, I would advise you to avoid performing this operation on every call of your query. You should simply add these translations as a new column in PvPRanking (or where most appropriate) and add them to the result set. In other words, whatever table has the JobCode values listed as unique values — that table should receive the new column.

If you DON’T have a table that contains these unique jobcodes, then you should make one and join that table to your existing query. This is the most professional and maintainable solution because when you need to update the list, you only need to update the table instead of potentially multuple files in your project.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement