gpt4 book ai didi

javascript - 每个函数中的条件是为所有记录输出相同的结果集

转载 作者:行者123 更新时间:2023-12-01 00:42:32 25 4
gpt4 key购买 nike

我正在执行 foreach 循环,然后发送该数据。然后在我的 AJAX 函数中,我在 success 函数中输出信息。这一切都运行良好。

但是,我只是调整了代码以包含新的数据属性。此数据属性保存$creator变量。可以在这里看到:

$html .= '<div class="projectCont" data-current="'.$category.'" data-creator="'.$project_creator.'">';

正在输出正确的数据。

我遇到的问题是,当数据属性 - data-creator 是客户时,将事件类添加到容器 - .projectCont 中。

现在看来只有最后一个循环对象正在被检查,然后无论这是什么,其余的数据都会被处理。

例如:我输出了大约 10 个循环对象。出于测试目的,我仅将其中之一(数据库中的最后一个)的创建者更改为“客户”。现在,当所有这些循环和输出时,每条记录都有根据我成功的条件添加的类。

有谁知道为什么会这样吗?我将此条件嵌套在每个函数中,认为它会检查并修改每个单独的记录。

有问题的条件(更多代码请参见 JS):

var projectCreator = $('.projectCont').data('creator');
if (projectCreator == 'Customer') {
$('.creatorIcon').addClass('active');
console.log("It should be showing");
} else {
$('.creatorIcon').removeClass('active');
}

JS:

success: function (data) {
//console.log(data);
if (data == null) {
alert("Unable to retrieve projects!");
alert(data);
} else {
var displayProjects = JSON.parse(data);
$wrapper.empty();
$(displayProjects).each(function() {
$wrapper.append(this.html);
//console.log(this.html);
var projectCreator = $('.projectCont').data('creator');
if (projectCreator == 'Customer') {
$('.creatorIcon').addClass('active');
console.log("It should be showing");
} else {
$('.creatorIcon').removeClass('active');
}
});
$wrapper.append(startBuilding);
}

PHP:

if ($projects_stmt = $con->prepare($projects_sql)) {
$projects_stmt->execute();
$project_rows = $projects_stmt->fetchAll(PDO::FETCH_ASSOC);
$proj_arr = array();
foreach ($project_rows as $project_row) {
$project_creator = $project_row['creator'];
$html = '';
$html .= '<div class="projectCont" data-current="'.$category.'" data-creator="'.$project_creator.'">';
$html .= '<div class="creatorIcon"><img src="/Projects/expand.png" alt="Customer Photo"></div>';
$html .= '</div>';
$data = array('id' => $project_row['id'], 'date' => $project_row['date_added'], 'html' => $html);
$proj_arr[] = $data;
}
}
echo json_encode($proj_arr);

更多 JS:

$('.categoryList').on('click', function (event) {
$('#projectsWrap').addClass('active'); //Once a category is selected the project wrap section will show
$wrapper = $('#projectGallery');
category = $(this).data('category');

//console.log(category);
$.ajax({
url: '/php/projectLoadTest.php',
type: 'POST',
data: {
'category': category
},
success: function (data) {
//console.log(data);
if (data == null) {
alert("Unable to retrieve projects!");
alert(data);
} else {
var displayProjects = JSON.parse(data);
$wrapper.empty();
$(displayProjects).each(function() {
$wrapper.append(this.html);
//console.log(this.html);
var projectCreator = $('.projectCont').data('creator');
if (projectCreator == 'Customer') {
$('.creatorIcon').addClass('active');
console.log("It should be showing");
} else {
$('.creatorIcon').removeClass('active');
}
});
$wrapper.append(startBuilding);
}
},
error: function (xhr, textStatus, errorThrown) {
alert(textStatus + " | " + errorThrown);
alert('There are currently no project images for this selection');
}
});
//was here
});

最佳答案

我认为在这种情况下你不应该搞乱 JS - 你可以在 PHP 中进行此类操作:

if ( $projects_stmt = $con->prepare( $projects_sql ) ) {

$projects_stmt->execute();

$project_rows = $projects_stmt->fetchAll( PDO::FETCH_ASSOC );

$proj_arr = array();

foreach ( $project_rows as $project_row ) {

$project_creator = $project_row[ 'creator' ];

$html = '';

$html .= '<div class="projectCont" data-current="' . $category . '" data-creator="' . $project_creator . '">';

// setting the active string - if Customer -> ' active'
$is_active = ( $project_creator == 'Customer' ) ? ' active' : '';

$html .= '<div class="creatorIcon' . $is_active . '"><img src="/Projects/expand.png" alt="Customer Photo"></div>';

$html .= '</div>';

$data = array( 'id' => $project_row[ 'id' ], 'date' => $project_row[ 'date_added' ], 'html' => $html );

$proj_arr[] = $data;

} // foreach

} // if

echo json_encode( $proj_arr );

关于javascript - 每个函数中的条件是为所有记录输出相同的结果集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57614102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com