Joseph Bupe
Joseph Bupe

Reputation: 77

Code failing to pass query criteria to URL

My problem is that since I added the SWITCH function to the code for quering with cat_id the code can no longer pass the query criteria to the URL for paginating. cat_id is a categoryID which is saved as a foreign key in the child table.

Here is my code if you can help:

$criteria = array('ctitle', 'csubject', 'creference', 'cat_id', 'cmaterial', 'ctechnic', 'cartist', 'csource', 'cposture', 'stolen');
$likes = "";
$url_criteria = '';
foreach ( $criteria AS $criterion ) {
        if ( ! empty($_POST[$criterion]) ) {
                $value = ($_POST[$criterion]);
                $likes .= " AND `$criterion` LIKE '%$value%'";
    switch ($criterion) {
                    case 'cat_id':
                        $likes .= " AND `$criterion`='$value'";
                        break;
                    default:
                        $likes .= " AND `$criterion` LIKE '%$value%'";
                        break;
                $url_criteria .= '&'.$criterion.'='.htmlentities($_POST[$criterion]);
                }

        } elseif ( ! empty($_GET[$criterion]) ) {
                $value = mysql_real_escape_string($_GET[$criterion]);
                $likes .= " AND `$criterion` LIKE '%$value%'";
    switch ($criterion) {
                    case 'cat_id':
                        $likes .= " AND `$criterion`='$value'";
                        break;
                    default:
                        $likes .= " AND `$criterion` LIKE '%$value%'";
                        break;
                $url_criteria .= '&'.$criterion.'='.htmlentities($_GET[$criterion]);
        } //var_dump($likes);
        }
}
$sql = "SELECT * FROM collections WHERE c_id>0" . $likes . " ORDER BY c_id ASC";

Upvotes: 0

Views: 63

Answers (2)

nvanesch
nvanesch

Reputation: 2600

$url_criteria .= '&'.$criterion.'='.htmlentities($_POST[$criterion]);

this is after a

default: 
...
break;

so this is never executed.

Upvotes: 2

fullybaked
fullybaked

Reputation: 4127

You don't need to put the $url_criteria in your switch. Its not being handled and will never be executed by PHP

<?php
switch ($criterion) {
    case 'cat_id':
        $likes .= " AND `$criterion`='$value'";
        break;
    default:
        $likes .= " AND `$criterion` LIKE '%$value%'";
        break;
//removed from here 
}
// put it here
$url_criteria .= '&amp;'.$criterion.'='.htmlentities($_POST[$criterion]);

Upvotes: 2

Related Questions