dave25 Posted May 17, 2015 Share Posted May 17, 2015 Problem with $reverse and $arrow. Always same value. <?php $get = array( 'sort' => NULL, 'order' => NULL, 'search' => NULL ); if(!empty($_GET)){ $defs = array( 'sort' => FILTER_SANITIZE_STRING, 'order' => FILTER_SANITIZE_STRING, 'search' => FILTER_SANITIZE_STRING ); $get = filter_input_array(INPUT_GET, $defs); } $sort = in_array($get['sort'], array('id', 'country_code', 'country_name')) ? $get['sort'] : 'id'; $order = in_array($get['order'], array('ASC', 'DESC')) ? $get['order'] : 'ASC'; $reverse = ($order == 'DESC' ? 'ASC' : 'DESC'); $arrow = ($sort == $get['sort'] and $order == 'ASC') ? 'files/images/table/sort_asc.png' : ($sort == $get['sort'] and $order == 'DESC') ? 'files/images/table/sort_desc.png' : 'files/images/table/sort_both.png'; $query = 'SELECT * FROM country WHERE CONCAT_WS("|", id ,country_code, country_name) LIKE :search ORDER BY '.$sort.' '.$order.''; $select = $db->prepare($query); $select->bindValue(':search', '%'.$get['search'].'%', PDO::PARAM_STR); $select->execute(); print ' <table width="500px" border="1"> <tr> <td><a href="index.php?do=test2&sort=id&order='.$reverse.'">ID<img src="'.$arrow.'"></a></td> <td><a href="index.php?do=test2&sort=country_code&order='.$reverse.'">Country Code<img src="'.$arrow.'"></a></td> <td><a href="index.php?do=test2&sort=country_name&order='.$reverse.'">Country Name<img src="'.$arrow.'"></a></td> </tr>'; while($row = $select->fetch(PDO::FETCH_ASSOC)){ print ' <tr> <td>'.$row['id'].'</td> <td>'.$row['country_code'].'</td> <td>'.$row['country_name'].'</td> </tr>'; } print ' </table>'; ?> Quote Link to comment Share on other sites More sharing options...
fastsol Posted May 17, 2015 Share Posted May 17, 2015 You're missing a closing ) for the reverse var. you can't use 'and' in a if() expression, change that to && for $arrow Quote Link to comment Share on other sites More sharing options...
dave25 Posted May 17, 2015 Author Share Posted May 17, 2015 You're missing a closing ) for the reverse var. you can't use 'and' in a if() expression, change that to && for $arrow Changed that but no help. Quote Link to comment Share on other sites More sharing options...
fastsol Posted May 17, 2015 Share Posted May 17, 2015 It was hard for me to read the code from my phone earlier. You weren't missing the closing ) on reverse, it was in the wrong place, you had it at the end of the line. Also, in order to use a ternary with an elseif ternary, the elseif must be enclosed in it's own set of (). $reverse = ($order == 'DESC') ? 'ASC' : 'DESC'; $arrow = ($sort == $get['sort'] && $order == 'ASC') ? 'files/images/table/sort_asc.png' : (($sort == $get['sort'] && $order == 'DESC') ? 'files/images/table/sort_desc.png' : 'files/images/table/sort_both.png'); Quote Link to comment Share on other sites More sharing options...
dave25 Posted May 17, 2015 Author Share Posted May 17, 2015  It was hard for me to read the code from my phone earlier. You weren't missing the closing ) on reverse, it was in the wrong place, you had it at the end of the line. Also, in order to use a ternary with an elseif ternary, the elseif must be enclosed in it's own set of (). $reverse = ($order == 'DESC') ? 'ASC' : 'DESC'; $arrow = ($sort == $get['sort'] && $order == 'ASC') ? 'files/images/table/sort_asc.png' : (($sort == $get['sort'] && $order == 'DESC') ? 'files/images/table/sort_desc.png' : 'files/images/table/sort_both.png'); Basically its working. But still some issues.  Without Sort with ID clicked. Everything is okey. <tr>    <td><a href="index.php?do=test2&sort=id&order=ASC">ID<img src="files/images/table/sort_both.png"></a></td>     <td><a href="index.php?do=test2&sort=country_code&order=ASC">Country Code<img src="files/images/table/sort_both.png"></a></td>    <td><a href="index.php?do=test2&sort=country_name&order=ASC">Country Name<img src="files/images/table/sort_both.png"></a></td> </tr> Cliked ID. Not expected html output. <tr>    <td><a href="index.php?do=test2&sort=id&order=DESC">ID<img src="files/images/table/sort_asc.png"></a></td>     <td><a href="index.php?do=test2&sort=country_code&order=DESC">Country Code<img src="files/images/table/sort_asc.png"></a></td>    <td><a href="index.php?do=test2&sort=country_name&order=DESC">Country Name<img src="files/images/table/sort_asc.png"></a></td> </tr> Should be. <tr>    <td><a href="index.php?do=test2&sort=id&order=DESC">ID<img src="files/images/table/sort_asc.png"></a></td>     <td><a href="index.php?do=test2&sort=country_code&order=ASC">Country Code<img src="files/images/table/sort_both.png"></a></td>    <td><a href="index.php?do=test2&sort=country_name&order=ASC">Country Name<img src="files/images/table/sort_both.png"></a></td> </tr> Quote Link to comment Share on other sites More sharing options...
fastsol Posted May 17, 2015 Share Posted May 17, 2015 Well of course it's not going to have different values for each link, you're using the same var for each link. <td><a href="index.php?do=test2&sort=id&order='.$reverse.'">ID<img src="'.$arrow.'"></a></td> <td><a href="index.php?do=test2&sort=country_code&order='.$reverse.'">Country Code<img src="'.$arrow.'"></a></td> <td><a href="index.php?do=test2&sort=country_name&order='.$reverse.'">Country Name<img src="'.$arrow.'"></a></td> It's a little confusing as to what you are trying to do but I think it should be like this. //First link maybe should be $order or $sort <td><a href="index.php?do=test2&sort=id&order='.$order.'">ID<img src="'.$arrow.'"></a></td> <td><a href="index.php?do=test2&sort=country_code&order='.$reverse.'">Country Code<img src="'.$arrow.'"></a></td> <td><a href="index.php?do=test2&sort=country_name&order='.$reverse.'">Country Name<img src="'.$arrow.'"></a></td> Quote Link to comment Share on other sites More sharing options...
Solution dave25 Posted May 17, 2015 Author Solution Share Posted May 17, 2015 (edited) After thinking a lot i came up with this working solution. Removed $arrow and reverse. Replaced with.. function sortReverse($sort, $match, $order){ if($sort == $match){ if($order == 'DESC') return 'ASC'; else return 'DESC'; }else{ return 'ASC'; } } function sortArrow($sort, $match, $order){ if($sort == $match && $order == 'ASC') return 'files/images/table/sort_asc.png'; else if($sort == $match && $order == 'DESC') return 'files/images/table/sort_desc.png'; else return 'files/images/table/sort_both.png'; } And table code print ' <table width="500px" border="1"> <tr> <td><a href="index.php?do=test2&sort=id&order='.sortReverse($sort, 'id', $order).'">ID<img src="'.sortArrow($sort, 'id', $order).'"></a></td> <td><a href="index.php?do=test2&sort=country_code&order='.sortReverse($sort, 'country_code',$order).'">Country Code<img src="'.sortArrow($sort, 'country_code', $order).'"></a></td> <td><a href="index.php?do=test2&sort=country_name&order='.sortReverse($sort, 'country_name',$order).'">Country Name<img src="'.sortArrow($sort, 'country_name', $order).'"></a></td> </tr>'; ..... Edited May 17, 2015 by dave25 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.