for循环返回第5个计数的字符串1-30和来自使用php匹配数字的mysql数据库的13count

for循环返回第5个计数的字符串1-30和来自使用php匹配数字的mysql数据库的13count

问题描述:

I've created a for loop to count 1-30 and want to match mysql number columns 1 - 30 with its attach string data column name and display string inside for loop.

Mysql structure:

|-------------|-------------|--------------|
| id          | Number      | Name         |
|-------------|-------------|--------------|
| 90          |  3          | Test Data    |
|-------------|-------------|--------------|

tried simular attempts such as:

foreach($db->results() as $result) {
  $count = $result->Number;
  $results = $result->Name;
}
for($i=1;$i<=30;$i++) {
  if($count == $i) {
     echo $results;
  }else{
     echo '';
  }
}

Needing help! Thank you!!!

================================================================================================================================================================

Ok here is what I am trying to accomplish... It works with assigning to the correct number when matching (Thanks for the suggestion). Here is what I have:

$name = array();
$number = array();
foreach($forDB->results() as $for){
   $name = $for->firstname;
   $number = $for->number;
    for($i=1;$i<=30;$i++) {
      if($i<=5){
         if($number == $i) {
            echo $i.' = '.$name;
         }
        } //then the rest of remanding numbers to 30
    }
 }

this echos:

2 = name1

4 = name4

But what I would like to do is also echo the numbers of $i between the numbers and values as:

1

2 = name1

3

4 = name4

5

So I try this but it appends to each value:

$name = array();
$number = array();
foreach($forDB->results() as $for){
  $name = $for->firstname;
  $number = $for->number;
  for($i=1;$i<=30;$i++) {
     if($i<=5){
        if($number == $i) {
          echo $i.' = '.$name.'<br><br>';
        } else {
          echo $i.'<br>';
        }
     } else {
        echo $i;
     }
   }
 }

Results:

2

3

4

5

6

7

1

2 = name2

3

4

5

6

7

1

2

3

4 = Jeff

1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7

I think you should initialize your count and name variables before the foreach loop as an array then in the foreach loop fill it up with the results, then iterate through it in the for loop.