Charles
Charles

Reputation: 189

PHP / HTML select and unselect links to change URL

I have this code where i show different status names:

$request = $_GET;
$array = Statuses('Porting', $behavior='');
foreach($array["results"] as $ret) {
    $sql="SELECT * from orders_porting where status = '".$ret["name"]."' ";
    $rs=mysql_query($sql,$conn);

    if($_GET[$ret["name"]] == '1') {
        unset($request[$ret["name"]]);
        $query = http_build_query($request);
        echo '<a href="?'.$query.'">';
    } else {
        echo '<a href="?'.$query.'&'.$ret["name"].'=1">';
    }
    echo '<div class="TicketsMenuTabs""';
    if($_GET[$ret["name"]] == '1') {
        echo ' id="active"';
    }
    echo '>'.$ret["name"].' ('.mysql_num_rows($rs).')</div></a>';
}

it shows 'buttons' that can be clicked and i want to be able to un click them too.

i have added the unset and http_build_query in the if statement, but if 2 buttons are selected and i click on one of them again to un selected it, they all become un selected

Upvotes: 1

Views: 108

Answers (2)

Ste B&#228;chler
Ste B&#228;chler

Reputation: 478

In your loop, if every link is selected, you will have unset everything when you reach the end (of your loop).

try this:

  • after unseting the link in your $request variable, do the output and reset the $request variable with the content from $_GET.
  • don't forget to set the $query variable (see the anwer from Mickael)

so it should look something like this:

$request = $_GET;
$array = Statuses('Porting', $behavior='');
foreach($array["results"] as $ret) {
    $sql="SELECT * from orders_porting where status = '".$ret["name"]."' ";
    $rs=mysql_query($sql,$conn);

    if($_GET[$ret["name"]] == '1') {
        unset($request[$ret["name"]]);
        $query = http_build_query($request);
        $request = $_GET;
        echo '<a href="?'.$query.'">';
    } else {
        $query = http_build_query($request);
        echo '<a href="?'.$query.'&'.$ret["name"].'=1">';
    }
    echo '<div class="TicketsMenuTabs""';
    if($_GET[$ret["name"]] == '1') {
        echo ' id="active"';
    }
    echo '>'.$ret["name"].' ('.mysql_num_rows($rs).')</div></a>';
}

also - try to check if the key in $_GET exists before reading it - like this:

$request = $_GET;
$array = Statuses('Porting', $behavior='');
foreach($array["results"] as $ret) {
    $sql="SELECT * from orders_porting where status = '".$ret["name"]."' ";
    $rs=mysql_query($sql,$conn);

    if(isset($_GET[$ret["name"]]) && $_GET[$ret["name"]] == '1') {
        unset($request[$ret["name"]]);
        $query = http_build_query($request);
        $request = $_GET;
        echo '<a href="?'.$query.'">';
    } else {
        $query = http_build_query($request);
        echo '<a href="?'.$query.'&'.$ret["name"].'=1">';
    }
    echo '<div class="TicketsMenuTabs""';
    if(isset($_GET[$ret["name"]]) && $_GET[$ret["name"]] == '1') {
        echo ' id="active"';
    }
    echo '>'.$ret["name"].' ('.mysql_num_rows($rs).')</div></a>';
}

and last but not least - i'll have to agree with krzysiej's comment - don't use the mysql_* functions - they are depricated - use mysqli or pdo-mysql instead (sorry - can't post more that two links because of my rep - you'll find the link to pdo-mysql if you click the "depricated" link)

Upvotes: 3

magon
magon

Reputation: 81

In the else section of your condition if($_GET[$ret["name"]] == '1') the variable $query isn't set because you set it here :

if($_GET[$ret["name"]] == '1') {
    unset($request[$ret["name"]]);
    $query = http_build_query($request);
    echo '<a href="?'.$query.'">';
}

Try this :

$query = http_build_query($request);

if($_GET[$ret["name"]] == '1') {
    unset($request[$ret["name"]]);
    echo '<a href="?'.$query.'">';
} else {
    echo '<a href="?'.$query.'&'.$ret["name"].'=1">';
}

Upvotes: 0

Related Questions