I am facing an issue where the database has incorrect naming conventions for rows. To avoid affecting website functionality, I cannot correct these conventions within the query itself. Thus, I have decided to use JavaScript to rename them correctly for display in the view.
In order to achieve this, I created a query that generates a fictional array like this:
Original Array
['Tree_Domestic', Rabbit, Unicorn, Cheetah_Domestic, Shark, Whale_Domestic]
The goal is to identify entries in the array that do not contain "_domestic" or "_international" and replace them with "_international". For instance, [Rabbit,Unaicorn,Shark
] should be transformed into:
[Rabbit_International,Unicorn_International,Shark_International]
Although I successfully accomplished this task, I encountered one final hurdle,
The modified array rearranged alphabetically, which is undesirable. The desired array order should be:
['Tree_Domestic', Rabbit_International, Unicorn_International, Cheetah_Domestic, Shark_International, Whale_Domestic]
This specific order is essential because it aligns with the counting procedure applied on popular rows. If I modify the array before conducting the count, the results will not correspond to the original order of items in the array.
Below is my query:
$sql = 'SELECT animals,
COUNT(*)
FROM fictional_signup
WHERE usergroup NOT IN (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
GROUP BY popular
ORDER BY COUNT(*) DESC';
Javascript
var renameLocations = [];
dataLabel = <?php echo json_encode($locations) ?>;
dataCount = <?php echo json_encode($count) ?>;
for(t = 0; t < dataLabel.length; t++){
if(dataLabel[t].includes('_Domestic') || dataLabel[t].includes('_International')){
renameLocations.push(dataLabel[t]);
}
}
for(z = 0; z < dataLabel.length; z++){
if(!dataLabel[z].includes('_Domestic') && !dataLabel[z].includes('_International')){
renameLocations.push(dataLabel[z] + "_International");
}
}
// Returns the correct naming conventions but the order is incorrect with the count.
console.log(renameLocations);