douming4359 2016-08-15 15:47
浏览 25
已采纳

创建一个函数来输出3个变量的所有迭代

I am working on a web app which queries a mySQL db and outputs results in a table based on 3 different variable arrays. Right now I have every iteration as a separate call and I would like to find a way to roll it into a single function if possible.

Here are the parameters I am working with.

$gender = array('male', 'female');
$age = array('adult', 'child', 'senior');
$hairColor = array('black', 'brown', 'blond', 'red', 'bald');

What I am looking to do is output each iteration as a row in a table.

male     adult     black    data_one    data_two
male    adult     brown     data_one    data_two
male     adult     blond     data_one    data_two
female     child     red     data_one    data_two
(etc)...

Here is the code I have:

$result = mysqli_query($connection, "SELECT * FROM profile_table WHERE `gender`='male' AND `age`='adult' AND `hair_color`= 'black' AND `data_one`='$data_one' AND `data_two`='$data_two'");

echo '<tr><td>Male</td>';
echo '<td>Adult</td>';
echo '<td>Black</td>';

formVariables($data_one, $data_two);

How would I convert the currently static data into variables that would output all iterations?

Thanks for any help you can provide.

  • 写回答

1条回答 默认 最新

  • dongtuanzi1080 2016-08-15 16:01
    关注

    I don't understand the point of this question, anyway here's a code that will mix these for you

    function mix ($gender, $age, $hairColor) {
        foreach( $gender as $g ) {
            foreach( $age as $a ) {
                foreach( $hairColor as $h ) {
                    echo "$g\t$a\t$h" . PHP_EOL;
                }
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容