error occur in databases.php page

16 views
Skip to first unread message

AFA

unread,
Mar 10, 2023, 2:15:15 AM3/10/23
to SubjectsPlus
Dear team,

I am trying to add the dropdown feature in the databases.php file to filter the result by source and for this, I have added a function named getDBbySourceBoxes() that generates a dropdown list of sources (databases) to be displayed on a databases.php page. The function takes two optional parameters: $selected_source and $show_formats.

The function executes an SQL query to retrieve the titles of all the databases along with their sources and then loops through the results to generate HTML code for the dropdown list. For each source, an <option> element is created with the source name as its label and a URL to the page that displays the database list for that source. 

The $selected_source parameter is used to mark the currently selected source in the dropdown list. If the parameter matches the source name for an option, the selected attribute is added to the <option> element. 

The $show_formats parameter is a boolean that indicates whether to show the dropdown list or not. If set to true, the function will wrap the dropdown list with an <select> element and return the HTML code as a string. If set to false, the function will only generate the options and return them as a string.

Here is the function code that I added in the functions.php file :-

function getDBbySourceBoxes($selected_source = "", $show_formats = true) {
    $sources_option_boxes = "";
    $alphabet = "";
    global $db;

    // Execute the SQL query
    $results = $db->query("
SELECT title, source
        FROM title
        JOIN rank ON rank.subject_id = subject_id AND rank.title_id = title.title_id
        JOIN source ON source.source_id = rank.source_id
        ORDER BY source.rs ASC, source.source, rank.rank ASC, title.title;
    ");

    if ($results) {
        // Loop through the results and generate the selected options
        foreach ($results as $result) {
            $source = $result['source'];
            $new_source = ucwords(preg_replace('/_/', ' ', $source));

            $sources_option_boxes .= "<option value=\"databases.php?letter=bysource&amp;source=$source\"";

            if ($selected_source == $source) {
                $sources_option_boxes .= " selected=\"selected\"";
            }

            $sources_option_boxes .= ">" . _($new_source) . "</option>";
        }
    }

    if ($show_formats == true) {
        $alphabet .= " <select name=\"browser\" id=\"select_source\" onChange=\"window.location=this.options[selectedIndex].value\" title=\"Databases by Source\">
            <option value=\"databases.php?letter=bysource\">" . _("List All Sources") . "</option>
            $sources_option_boxes
        </select>";
    }

    return $alphabet;
}

function getLetters( $table, $selected = "A", $numbers = 1, $show_formats = true, $show_free = true ) {

$selected = scrubData( $selected );

$selected_subject = "";
if ( isset( $_GET["subject_id"] ) ) {
$selected_subject = intval( $_GET["subject_id"] );
}

$selected_type = "";
if ( isset( $_GET["type"] ) ) {
$selected_type = $_GET["type"];
}

$selected_source = "";
if ( isset( $_GET["source"] ) ) {
$selected_source = $_GET["source"];
}

$showsearch = 0;
$abc_link   = "";

// If it's an array, just plunk that stuff in //

if ( is_array( $table ) ) {
$letterz    = $table;
$showsearch = 0;
$azRange    = $table;
} else {

$shownew = 1;
$extras  = "";

switch ( $table ) {
case "databases":
$lq       = "SELECT distinct UCASE(left(title,1)) AS initial
                    FROM location l, location_title lt, title t
                    WHERE l.location_id = lt.location_id AND lt.title_id = t.title_id
                    AND eres_display = 'Y'
                    AND left(title,1) REGEXP '[A-Z]'
                    ORDER BY initial";
$abc_link = "databases.php";
$shownew  = 0;
break;
}

//print $lq;
$db = new Querier;
$lr = $db->query( $lq );

foreach ( $lr as $mylets ) {
$letterz[] = $mylets[0][0];
}

// let's init an array of all letters
$azRange = range( 'A', 'Z' );

if ( $numbers == 1 ) {
$letterz[] = "Num";
$azRange[] = "Num";
}

$letterz[] = "All";
$azRange[] = "All";

if ($show_free == true) {
$letterz[] = "Free";
$azRange[] = "Free";
}


if ( ! $selected ) {
$selected = "ALL";
}
}


$alphabet = "<div id=\"letterhead\" align=\"center\">";

foreach ( $azRange as $char ) {
if ( in_array( $char, $letterz ) ) {
if ( $char == $selected ) {
$alphabet .= "<span id=\"selected_letter\">$char</span> ";
} else {
$alphabet .= "<a href=\"$abc_link?letter=$char\">$char</a> ";
}
} else {
$alphabet .= "<span class=\"inactive\">$char</span> ";
}
}

/*
  foreach ($letterz as $value) {
if ($value == $selected) {
  $alphabet .= "<span id=\"selected_letter\">$value</span> ";
} else {
  $alphabet .= "<a href=\"$abc_link?letter=$value\">$value</a>";
}
  }
*/
if ( $table == "databases" ) {
$alphabet .= getDBbyTypeBoxes( $selected_type, $show_formats );
$alphabet .= getDBbySubBoxes( $selected_subject );
$alphabet .= getDBbySourceBoxes( $selected_source, $show_formats );
}

if ( $showsearch != 0 ) {
$alphabet .= "<input type=\"text\" id=\"letterhead_suggest\" size=\"30\"  />";
}


$alphabet .= "</div>";

return $alphabet;
}

then I added some code in databases.php file too :-
if ( $_GET["letter"] == "bysource" ) {
    $page_title = _( "Database List By Source" );
    if ( $clean_id == "" ) {
        $_GET["source_id"] = "";
        $show_sources = true;
    } else {
        $show_sources = false;
        // add source name to title
        $connection = $db->getConnection();
        $statement  = $connection->prepare( "SELECT source FROM source WHERE source_id = :id AND active = '1'" );
        $statement->bindParam( ":id", $clean_id );
        $statement->execute();
        $myrow = $statement->fetchAll();
        $page_title .= ": " . $myrow[0][0];
    }
} else {
    $_GET["source_id"] = "";
    $show_sources = false;
}


but that is not working. Is something I am missing? I am attaching the screenshot of the user interface database.php page. the result is not displaying when I select from the dropdown and the dropdown value is showing more than one not showing unique value in the dropdown.


databases.php.png
Reply all
Reply to author
Forward
0 new messages