简体   繁体   English

在for循环中访问DB Column数据(名称末尾有$ i时不起作用)

[英]Accessing DB Column data within a for loop (It doesn't work when I have $i in the end of the name)

I think my problem might be fairly simple, but I can't get it to work. 我认为我的问题可能很简单,但是我无法解决它。

I have a custom DB Table with similar column names. 我有一个具有类似列名的自定义数据库表。 The difference between columns is the number in the end, so we have name11, name12, name13. 列之间的区别是最后的数字,所以我们有name11,name12,name13。 At the same time though, we have name21, name22, name23 and so on. 但同时,我们有name21,name22,name23等。

I am using a for loop inside of a for loop in order to retrieve the data of that specific column. 我在for循环内使用for循环,以检索该特定列的数据。 The following code, however, does not work. 但是,以下代码不起作用。

for($i=0; $i<10; $i++)
{
  echo('<table>');
  for($j=0; $j<10; $j++)
  {
    echo('<tr>');
      $field_name = $user_routines[$i]->exercise_name.($i+1).($j+1);
      echo('<td>' . $field_name . '</td>');
    echo('</tr>');
  } 
}

Besides that, I have also tried the following: 除此之外,我还尝试了以下方法:

for($i=0; $i<10; $i++)
{
  echo('<table>');
  for($j=0; $j<10; $j++)
  {
    echo('<tr>');
      echo('<td>' . $user_routines[$i]->(exercise_name . ($i+1) . ($j+1)) . '</td>');
    echo('</tr>');
  } 
}

But it didn't work either. 但这也不起作用。

Ps1: $user_routines is an array that contains my table rows for a specific user. $user_routines $user_routines是一个数组,其中包含特定用户的表行。

Thanks! 谢谢! =] =]

Try 尝试

  $tmp1 = ($i+1);
  $tmp2 = ($j+1);

  $id = "$tmp1"."$tmp2";
  echo '<td>' . $user_routines[$i]->{'exercise_name'.$id} . '</td>';

Assuming inside your array $user_routines[$i] is an object inside. 假设在$ user_routines [$ i]数组内部是一个对象。 Please paste a small piece of your array structure . 请粘贴您的数组结构的一小部分

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

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