简体   繁体   English

从选择创建项目列表/数组

[英]creating a list/array of items from select

Background: I am using HTML/PHP/MySQL/ and Javascript if needed 背景:如果需要,我正在使用HTML / PHP / MySQL /和Javascript

What I have is a list of approximately 1000 people - I want to let the user select people, and then use the selected people to add them to a database. 我所拥有的大约是1000个人的列表-我想让用户选择个人,然后使用选择的个人将其添加到数据库中。

I was thinking that each row of the table would have a unique link, and the link would either add that person to an array - or to some kind of list - and then at the end I can take that list and input it into the database.. 我以为表格的每一行都会有一个唯一的链接,并且该链接会将那个人添加到数组(或某种列表)中,然后最后我可以获取该列表并将其输入数据库..

I really want to have it that when somebody clicks a person's name - it automatically gets added to a list on lets say the left hand side - then once they are done, they can submit the list. 我真的很想拥有这样的功能:当某人单击某人的名字时-它会自动添加到左侧的列表中-假设完成后,他们便可以提交该列表。

let me know if clarification is needed. 让我知道是否需要澄清。

Thanks for your help! 谢谢你的帮助!

I would go with: 我会去:

  • Create page with space for smaller list on the left and list of people on the right. 创建页面,在页面左侧留有较小的空间,在右侧留有人员的空间。

Display people, with pagination, in table using something like this: 使用类似以下内容的表格在页面上显示人物:

<table id="people">
    <tr>
        <td id="<?php echo $person['id']; ?>"><?php echo $person['name']; ?></td>
    </tr>
</table>

When someone clicks on tr javascript code is executed to send post request with id the person. 当某人单击tr时,将执行javascript代码以发送具有id的发帖请求。 PHP file responsible for handling this stores the id in the $_SESSION['selectedPeople'] array or removes it from there if it is present. 负责处理此问题的PHP文件将ID存储在$_SESSION['selectedPeople']数组中,如果存在,则将其从那里删除。 Also, when you click on td, .selected class is toggled and list of selected people on the left side is updated(for example, via getting JSON from PHP file and processing it to display in the table). 同样,当您单击td时,将切换.selected类,并更新左侧的选定人员列表(例如,通过从PHP文件获取JSON并将其处理以显示在表中)。

  • you can press submit at any time. 您可以随时按提交。 It would: 它会:

a) take you to the other page and submit the list(you have $_SESSION['selectedPeople'] variable) b) send an ajax call to page which will process $_SESSION['selectedPeople'] a)带您到另一页并提交列表(您有$_SESSION['selectedPeople']变量)b)将ajax调用发送到将处理$_SESSION['selectedPeople']

Some code: 一些代码:

selectHandler.php: selectHandler.php:

<?php
if($_POST && isset($_POST['id'])) {
    $id = (int)$_POST['id'];
    $action = '';
    session_start();
    if(!isset($_SESSION['selectedPeople'])) {
        $_SESSION['selectedPeople'] = array($id);
        $action = 'added';
    } else {
        if(in_array($id, $_SESSION['selectedPeople'])) {
            $_SESSION['selectedPeople'] = array_diff($my_array, array($id));
            $action = 'removed';
        } else {
            $_SESSION['selectedPeople'][] = $id;
            $action = 'added';
        }
    }
    echo json_encode(array('action' => $action));
}
?>

Javascript(+ JQuery ): Javascript(+ JQuery ):

$(function () {
    $('#people tr').click(function () {
        var id = $(this).children('td[id]').attr('id');
        $.post("selectHandler.php", { id: id })
            .done(function( data ) {
                if(data.action == "added") {
                    $(this).addClass('selected');
                } else if(data.action == "removed") {
                    $(this).removeClass('selected');
                } else {
                    alert('Some kind of error.');
                }
            }, "json");
        refreshSelectedList();
    });
});

I hope you get the idea. 希望您能明白。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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