如何将数组解析为变量(PHP)? [英] How to parse an array into variables (PHP)?

查看:60
本文介绍了如何将数组解析为变量(PHP)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一生中,我无法使它正常工作.我看过许多关于stackoverflow的文章,所以如果您能帮助的话,那太好了!我正在为客户提交表单.他们希望能够从一个下拉菜单中选择多个值,然后我将从数据库中提取多个值以获取他们的查询结果.

For the life of me I cannot get this to work. I've looked at many articles on stackoverflow so if you could help that would be wonderful! I am working on a form submission for a client. They want to be able to select multiple values from a dropdown, which in turn I will pull from a database to get their query results.

<form id="test" action="results.php" method="POST">
<select id="role" name="role[]" multiple>
<option value="Student">Student</option>
<option value="Faculty">Faculty</option>
<option value="Alumni">Alumni</option>
</select>

<?php
$query="SELECT City FROM Cities";
$result = mysqli_query($link, $query);
echo '<select name="city" id="city" multiple>';
while($r = mysqli_fetch_assoc($result)){
echo '<option value="'.$r['City'].'">'.$r['City'].'</option>'; }
?>
</select>
<input type="submit"/>
</form>

//results.php

//results.php

$results=array();

$results[] = $_POST['role'];

$results[]= $_POST['city'];

echo "<pre>";
print_r($results);
echo "</pre>";

**如何获取数组中的所有值并将其解析为单独的变量,以便可以在SQL语句中使用这些变量?这是我的输出:**

**How do I obtain all the values from the array and parse it into separate variables so I can use the variables in a SQL statement? Here is my output: **

Array
(
[0] => Array
    (
        [0] => Faculty
        [1] => Alumni
    )

[2] => Adams
)

非常感谢您的帮助! :)如果有更好的方法可以做到这一点,请告诉我.

Thanks so much for any help! :) And if there is a better way to do this, let me know.

推荐答案

:此代码对

: This code is wild open to SQL Injection , Please don't use it.

问题中已经有一个提交选项,我创建了一个 虚拟的城市提交选项,在其他文件中运行此代码, 而不是选择其他不同的选项,然后单击提交"按钮, 检查我们的查询是如何建立的 请先阅读注释,并确保您已阅读代码中的注释,因为它们比代码更重要

One submit options i already have in the question and one i created dummy submit options for city, run this code in the different file, than select different different options, and click on submit button, to check how our query is getting built Please read the note first and make sure you read the comment in the code, as they are more important than the code

注释1-> 简而言之,您要根据用户选择的选项来运行查询, 确保您阅读了注释以了解逻辑,注释比代码本身更重要,

Note 1-> in short you want to run the query, according to the selected options by the user, make sure you read the comment to understand the logic, comments are more important than the code it's self,

注释2-> 和其他我没有意识到的事情,您可能将值存储在不同的不同表中,如果是这种情况,代码将稍有变化,但基本的shell仍将保留一样

Note 2-> and more thing i did not realize, you may be storing your value in different different table, if that's the case, code will change little bit, but basic shell will remain the same

注释3-> 要实现想要实现的结果,基本上必须根据设置的选项创建查询,并且比使用IN关键字更合适,

Note 3-> To achieve the out come which you want to achieve, you basically have to create your query according to the set options, and than use IN keyword and you are good go,

注释4-> 我添加了echo语句,因此您可以逐步查看我们查询的发展方式,我添加了注释,如果您想只删除注释,则没有添加最后回显中的注释,以便您可以查看随时可以使用的查询字符串

Note 4-> I added echo statement, so you can see stage by stage how our query is developing, i added the comment, if you want see just remove the comment, I did not add the comment in the last echo so you can see the ready to use query string

再次说明-> ,我已经拥有一个提交选项,一个由我自己创建的选项,因此您可以看到正在发生的事情,然后它就会为您解决.

Note Again-> one submit options i already have, one i created by my self, so you can see what happening, and you it going to work out for you.

如您在评论中所说,您的表单中可能有12个字段,如果是这种情况,请使用此代码,因为假设您需要更改 将来会发生一些事情,您必须在十二个地方进行更改, 您会像错过某些东西一样犯错,或者使用错误的变量 或其他事情,使用此代码,您必须将其更改为一个位置, 它将适用于12或24个名额,但名额没有 问题

as you said in the comment you may have 12 field, in your form, if that's the case, use this code, because lets say if you have to change some thing in the future, and you have to change at tweleve places, you will make mistake like miss some thing, or use the wrong variable or some thing else, with this code, you have to change it one place, and it will get apply to 12 or 24 places, number of places does not matter,

还有一件事,如果将此php代码包装在函数中会更好,原因是让我们说您在其他页面上有表单,而您只需要拥有相同功能的东西要执行此操作,只需调用函数,以后如果您更改了某些内容,只需更改函数代码

and one more thing, it will better if you wrap this php code inside the function, the reason is lets say you have form on some other page, and you need same functionality only thing you have to do than, just call the function, and in the future if you have change some thing, just change the function code

我正在为您提供代码示例,为什么将它包装在函数中更好,假设您的表名与表单中给定的所选名称不同,或者您决定对值进行钻孔在不同的不同表中,您将不得不更改代码,如果您编写了十二次或每种形式,并且您不得不更改它,那么麻烦就大了,但是如果您将此代码用作不同形式的函数,您只需在功能上或此处进行一些更改,并将其应用到所有地方,一会儿您搞砸了一些东西,而这并不是他们的全部,因此希望能对您有所帮助

I am giving you example on your code why it is better to wrap this in a function, lets say your table name are different than the given selected name in your form or you decided to hole values in different different table, than you have to change the code, if you wrote this twelve times or each form, and than you have to change it, than you are in big trouble, but if you use this code as function for different different form, you just have to do some changes in function or in here, and will get applied everywhere, in short chances of you screwing up some thing is just not their, so hope fully this will help you

SideNote-我想说的一件事是,此解决方案看起来很大的原因是由于注释,形式和注释,如果您计算php代码行,则没有最后的回声声明,实际上它只有10行php代码,所以不要害怕,因为它看起来很大

SideNote -- one more thing i want to say, the reason this solution look big, is because of note, form and comment, if you count the php code line, with out the last echo statement, it actually only 10 lines of php code, so dont get afraid, becuase it's look big

    <form id="test" action="" method="POST">
<select id="role" name="role[]" multiple>
<option value="Student">Student</option>
<option value="Faculty">Faculty</option>
<option value="Alumni">Alumni</option>
</select>

<select id="city" name="city[]" multiple>
<option value="London">London</option>
<option value="Paris">Paris</option>
<option value="New York">New York</option>
</select>
    <input type="submit">
</form>
<?php
//creating variable and saying all the post request is equal to this variable
$selected_options=$_POST;
foreach($selected_options as $key=>$option){
    $countValue = count($option);
    for($i=0; $i<$countValue; $i++){
        /*
         * start adding the value seperated by coma, remember again it going to
         *  be on extra coma so we have to remove it.
         */

        $queryString_start_with_coma .= ",$option[$i]";
    }
    /*
     * come out of loop, and now remove that extra coma
     */
    $queryString_remove_extra_come= preg_replace("/,/", "", $queryString_start_with_coma, 1);
    /*
     * start building your query, use variable $key, just check the line below,
     * you will understand where and why i am using variable $key.
     */
    $query_string_with_and .= " AND $key IN($queryString_remove_extra_come)"; 

    /*
     * now unset the variable, this line is very important, so please also check
     * your out come without this line, 
     * what i am simply  doing is emptying the variable, if you dont 
     * do it, it will add the value in the existing value, which i dont want, what 
     * i want when the loop run for the second selected options, i want my variable
     * to be empty, so i can create new string
     * you will understand more if you remove this line and compare your two outcome
     * Note: you dont have to unset if you dont want to, but you have empty the 
     * variable, you can also do by creating a empty string, do what ever you want
     * to do, just make sure the variable is empty for the second loop
     */
    unset($queryString_start);
}

$query_string_second_part_ready = preg_replace("/AND/", "", $query_string_with_and, 1);
//echo "$query_string_second_part_ready<br>";
$query_string= "SELECT * FROM table_name WHERE ".$query_string_second_part_ready;
//see how your query look like
echo $query_string;

这篇关于如何将数组解析为变量(PHP)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆