0

Afternoon all.

I'm trying to convert the following get_results query to allow the use of a variable dependent on which page link will be clicked for a team roster page.

I currently have 3 different sets of code, all would have to be updated should I need a change, so I want to parameterise it, so I can just call one bit of code.

Any advice on my bodged code or a better way to go about it would be great.

$table_name = "people";

$myrows = $wpdb->get_results( "SELECT *,                            
                              CASE `Position` 
                                 WHEN 'DL' THEN 'Defensive Lineman'
                                 WHEN 'LB' THEN 'Linebacker'
                                 WHEN 'DB' THEN 'Defensive Back'
                                 ELSE `Position`
                              END as `PositionLong`  FROM ".$table_name. 
                         " WHERE
                        (`Position`='DL' or
                          `Position`='LB' or
                          `Position`='DB') and 
                          `Season` = '2018'
                      ORDER BY `Number`");
foreach ($myrows as $details) {

I've read around, and my attempt was:

$rosterpage = 'Offense';
$selectcode =   'SELECT *,                          
                CASE `Position`
                    WHEN 'QB' THEN 'Quarterback'
                    WHEN 'RB' THEN 'Running Back'
                    WHEN 'WR' THEN 'Wide Receiver'
                    WHEN 'TE' THEN 'Tight End'
                    WHEN 'OL' THEN 'Offensive Lineman'
                    WHEN 'DL' THEN 'Defensive Lineman'
                    WHEN 'DL' THEN 'Defensive Lineman'
                    WHEN 'LB' THEN 'Linebacker'
                    WHEN 'DB' THEN 'Defensive Back'
                ELSE `Position`
                END as `PositionLong`  FROM '.$table_name.' ',
                'WHERE ';
$OffWhere =     '(  `Position`='QB' or
                `Position`='RB' or
                `Position`='WR' or
                `Position`='TE' or
                `Position`='OL'
            ) AND ';

$DefWhere =     '(  `Position`='DL' or
                `Position`='LB' or
                `Position`='DB'
            ) AND ';
$Season = '2018';

if($rosterpage!=="All"){                
    if($rosterpage=="Offense") {
        $prepared = $wpdb->prepare($selectcode. $OffWhere. "`Season` = ".$Season." ORDER BY `Number`", $search_query, $current_page, $rows_per_page)  ;
    } 
        else {
    if($rosterpage=="Defense") {
        $prepared = $wpdb->prepare($selectcode. $OffWhere. "`Season` = ".$Season." ORDER BY `Number`", $search_query, $current_page, $rows_per_page)  ;
    } 
}
else {
$prepared = $wpdb->prepare($selectcode. "`Season` = ".$Season." ORDER BY `Number`", $search_query, $current_page, $rows_per_page)  ;
    } 
$myrows = $wpdb->get_results($prepared);
foreach ($myrows as $details) {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.