PHP 在另一个 foreach() 循环中只显示 for() 循环中的第一个值 [英] PHP displays only the first value in a for() loop insinde another foreach() loop

查看:84
本文介绍了PHP 在另一个 foreach() 循环中只显示 for() 循环中的第一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 for() 循环的小问题,该循环以动态方式显示另一个 foreach() 循环中的值.

代码的小背景:

  1. 输出表是根据从名为categories"的表中的 1 列(名称为coloane")的一个 mysql 查询 SELECT(这是第一个选择)获得的值动态创建的;

  2. 存储在coloane"列中的值实际上是另一个名为articles"的表中某些列的名称

  3. 第一个 mysql 选择是从coloane"列中获取值,以便我可以创建第二个.在第二个表返回行 (num_rows() > 0) 后,我开始创建表.

  4. 该表有 2 个始终存在的静态列,其余的列根据不同的条件动态添加.

剧情:

  • 我分解从第一次选择中获得的值 - 来自coloane"列 - 为表格标题制作 [th] ;
  • 这个爆炸我将它存储在变量$rows_th"下;
  • 为了制作表头的 [th],我使用了 foreach() 循环(这里我也删除了所有下划线);
  • 之后,我继续使用另一个 foreach() 创建 [tbody] 和 [td],只是这个 foreach 是为第二个选择的值完成的;
  • 在这个 foreach() 中,我创建了另一个循环,在变量$rows_th"上使用 for() 函数,因此表具有相同的长度;
  • 这里的值是这样组合的:

     '<td id="'.$i.'">'.$values_td[$rows_th[$i]].'</td>';

主要问题是,使用上面列出的格式,它只会显示第一个值

当我尝试使用 is_string() 函数验证$values_td[$rows_th[$i]]"时,我发现第一个值是一个字符串,之后所有其他值都不是一个字符串...

执行的测试:

 if (is_string($values_td[$rows_th[$i]]))打印 '<td id="'.$i.'">找到值!!!</td>';} 别的 {打印 '<td id="'.$i.'">运气不好...</td>';}

对可能出现的问题有任何想法吗?

我知道我遗漏了一些东西,但我似乎无法弄清楚:-/

此处列出了完整代码:http://pastebin.com/4MFifh92

解决方案

你就不能这样做:

SELECT * FROM results_query_header_by_cat WHERE `id` = 3;

那么:

SELECT * FROM results_query_content_by_cat WHERE `id_category` = 3;

然后是结果:

echo '[thead]';foreach ( $results as $result ){foreach( $result['coloane'] 作为 $header ){echo '[th]'.$header.'[/th]';}}echo '[/thead][tbody]';foreach ( $records as $record ){回声 '[tr][td]' .$result['生产者'] .'[/td][td]' .$result['model'] .'[/td][td]' .$result['imei'] .'[/td][td]' .$result['locatie'] .'[/td][/tr]';}echo '[/tbody]';

这就是你要找的吗?

e:我没有试过这个,但我想应该是这样的.只是:

  • 通过类别 ID 获取标题

  • 按类别 ID 获取记录.

  • 将第一个结果集回显为带有循环的标题,

  • 将第二个结果集作为表行与循环中的列进行回显.

I have small issue with a for() loop that displays values inside another foreach() loop in a dynamic way.

A small background of the code:

  1. The output table is created dynamically based on the values obtain from one mysql query SELECT (which is the 1st select) from 1 column (that has name "coloane") in a table called "categories";

  2. The values stored in the column "coloane" are actually the name of some columns from another table called "articles"

  3. The first mysql select is to obtain the values from the column "coloane" so I can create the second. After the second table returns rows (num_rows() > 0) I start to create the table.

  4. The table has 2 static columns that are always present, and the rest of the columns are added dynamically based on different criteria.

The plot:

  • I explode the values obtained from the first select - from the column "coloane" - to make the [th]'s for the table header;
  • this explode I store it under the variable "$rows_th";
  • to make the [th]'s for the table header, i use a foreach() loop (and here I also remove any underscores);
  • after that I proceed to create the [tbody] and the [td]'s using another foreach(), only that this foreach is done for the values of the second select;
  • inside this foreach() I create another loop, using the for() function on the variable "$rows_th" so the table has the same length;
  • here the values are combined like this:

     '<td id="'.$i.'">'.$values_td[$rows_th[$i]].'</td>';
    

The main issue is that, using the format listed above, it will only display the first value!

When I tried to verify the "$values_td[$rows_th[$i]]" using the is_string() function, I discovered that the first value is a string, and after that, every other value is not a string...

The test performed:

     if (is_string($values_td[$rows_th[$i]]))
       print '<td id="'.$i.'">Value found!!!</td>';
     } else {
       print '<td id="'.$i.'">No luck...</td>';
     }

Any ideas on what might be going wrong ?

I know I'm missing something, but I just can't seem to figure it out :-/

The entire code is listed here: http://pastebin.com/4MFifh92

解决方案

Can't you just do something like:

SELECT * FROM results_query_header_by_cat WHERE `id` = 3;

Then:

SELECT * FROM results_query_content_by_cat WHERE `id_category` = 3;

Then with the results:

echo '[thead]';
foreach ( $results as $result ){
    foreach( $result['coloane'] as $header ){
        echo '[th]'.$header.'[/th]';
    }
} 
echo '[/thead][tbody]';
foreach ( $records as $record ){
    echo '[tr][td]' . 
     $result['producator'] . '[/td][td]' .
     $result['model'] . '[/td][td]' .
     $result['imei'] . '[/td][td]' .
     $result['locatie'] . '[/td][/tr]';
}
echo '[/tbody]';

Is that what you're looking for?

e: I haven't tried this but I'd imagine it's something like that. Just:

  • get the headers by category ID

  • get the records by category ID.

  • Echo the first result set as headers with a loop,

  • echo the second result set as table rows with columns in a loop.

这篇关于PHP 在另一个 foreach() 循环中只显示 for() 循环中的第一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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