I'm a fan of a dry code. I'm not sure is doable but I'm trying to save me from writing a double query.
Here's my example:
$link = $_SERVER['PHP_SELF'];
$link_array = explode('/', $link);
$page = end($link_array);
In the first part of my code, I'm parsing my link.
if (strpos($link, 'something') !== false) {
$sql = $ee->db->query("SELECT * FROM `mytable` WHERE order_id = '$page'");
foreach ($sql->result() as $row) {
$tracking_code = $row->tracking_id;
$carrier_name = $row->carrier_name;
echo "The real carrier name is: {$carrier_name} .<br>";
echo "The real tracking code is: " . $tracking_code . ' .<br>';
} else {
$sql = $ee->db->query("SELECT * FROM `mytable` WHERE order_hash = '$page'");
foreach ($sql->result() as $row) {
$tracking_code = $row->tracking_id;
$carrier_name = $row->carrier_name;
echo "The real carrier name is: {$carrier_name} .<br>";
echo "The real tracking code is: " . $tracking_code . ' .<br>';
}
Then I run two queries: depends on the link that I'm parsing I'm gonna retrieve two different set of data.
I was wondering if it is possible to have a different approach, something in this line:
$sql = $ee->db->query("SELECT * FROM `mytable`");
right after my variables and then, inside the if statement:
if (strpos($link, 'something') !== false) {
$sql =. $ee->db->query("WHERE order_id = '$page'");
#code
} else {
$sql =. $ee->db->query("WHERE order_hash = '$page'");
#code
}
Or any other way to avoid to rewrite my code could be fine. In this case I'm not gonna save a lot, I'm just trying to understand what it could be the best approach.