简体   繁体   中英

How can I add php variables to this Javascript?

I'm trying to build a simple news page with ajax filters depending on what category the user wants to see. The javascript below connects to a php file and outputs HTML with data from the mysql database. I need to know how to tell the JS to put the data from the "heading" column into a php variable so that it wraps it in the correct href link.

This is the php that creates the heading and the link

$data_fields = '`id`, `heading`, `date`, `copy`, `summary`, `article`, `press_release`, `video`';
$from = '`media`';

$news_result = $db->selectByStrings($data_fields, $from, $where_conditions, $order_by);
$news = $db->getAllRows($news_result);

foreach ($news as $new) {
    echo '<h2 class="news"><a class="news" href="'.$base_href.$new['id'].'">'.$new['heading'].'</a></h2>';
}

I somehow need to include this is the separate JS file, making sure it is only applied to data from the heading column.

Javascript

function makeTable(data) {
    var tbl_body = "";
    $.each(data, function () {
        var tbl_row = "";
        $.each(this, function (k, v) {
            tbl_row += "<div class='row'><div class='col-md-8 col-md-offset-2'>" + v + "   </div></div>";
        });
        tbl_body += "<div class='non-white'>" + tbl_row + "</div>";
    });
    return tbl_body;
}

function getEmployeeFilterOptions() {
    var opts = [];
    $checkboxes.each(function () {
        if (this.checked) {
            opts.push(this.name);
        }
    });
    return opts;
}

function updateEmployees(opts) {
    $.ajax({
        type: "POST",
        url: "filter3.php",
        dataType: 'json',
        cache: false,
        data: {filterOpts: opts},
        success: function (records) {
            $('#employees div').html(makeTable(records));
        }
    });
}

var $checkboxes = $("input:checkbox");
$checkboxes.on("change", function () {
    var opts = getEmployeeFilterOptions();
    updateEmployees(opts);
});
updateEmployees();

The php file the above connects to:

<?php
$pdo = new PDO('mysql:host=localhost;dbname=xxx', 'xxx', 'xxx');
$select = 'SELECT heading, summary, created_date';
$from = ' FROM media';
$where = ' WHERE TRUE';
$opts = isset($_POST['filterOpts']) ? $_POST['filterOpts'] : array('');
if (in_array("article", $opts)) {
    $where .= " AND article = 1";
}
if (in_array("press_release", $opts)) {
    $where .= " AND press_release = 1";
}
if (in_array("video", $opts)) {
    $where .= " AND video = 1";
}
$sql = $select.$from.$where;
$statement = $pdo->prepare($sql);
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($results);
echo($json);
?>

I think you are looking for something like this :

var base_href = 'http://www.stackoverflow.com/';
function makeTable(data) {
    var tbl_body = "";
    $.each(data, function (index, row) {
        var tbl_row = "";
        $.each(row, function (k, v) {
            if(k == 'heading' && 'id' in row) {
                v = '<a class="news" href="' + base_href + row.id +'">' + v + '</a>';
            }
            tbl_row += "<div class='row'><div class='col-md-8 col-md-offset-2'>" 
            + v + "   </div></div>";
        });
        tbl_body += "<div class='non-white'>" + tbl_row + "</div>";
    });
    return tbl_body;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM