Jump to content

not expected html output


dave25
Go to solution Solved by dave25,

Recommended Posts

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>';

?>
Link to comment
Share on other sites

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');
Link to comment
Share on other sites

 

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>
Link to comment
Share on other sites

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>
Link to comment
Share on other sites

  • Solution

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 by dave25
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.