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 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法