PHP只解析数组中的第一个元素。

时间:2022-05-28 03:36:21

I'm working on a periodic table, in which I retrieve all the elements from my database. I want to place the elements in their respective position in the periodic table.

我正在编写一个元素周期表,从数据库中检索所有元素。我想把元素放到它们在元素周期表中的位置。

E.g.: Hydrogen is in row 1, cell 1 - Helium is in row 1, cell 18. The cells between those two should therefore be blank.

例:氢在第1行,1 -氦在第1行,第18单元。因此,两者之间的细胞应该是空白的。

The problem is, that when I run my for-loop and compare the position - which is stored in my database - and the ID I've given each cell (which is the same as the position-outcome from the database), it only parses the first element and then stops.

问题是,当我运行for循环并比较在我的数据库中存储的位置和我给每个单元格的ID(与数据库中的位置结果相同)时,它只解析第一个元素,然后停止。

It returns the first one 1-1 (Helium - Row 1, cell 1) and parses it like it should. Then the next one, which would be 1-2 (Row 1, cell 2), and from here on it just stops.

它返回第一个1-1(氦-第1,单元1),并像它应该的那样对它进行解析。然后下一个,也就是1-2(第1行,第2单元),从这里开始。

This is my query

这是我的查询

// SQL Query for retrieving elements
$query = "
    SELECT 
        id, name, symbol, row, cell
    FROM 
        elements
    ORDER BY
        id
";
$objResult = $objConnection->query($query);

while ($row = $objResult->fetch_assoc()) {
    // Creating two-dimensional, associative array containing each element
    $ele[] = array(
        'id' => $row['id'],
        'name' => $row['name'],
        'symbol' => $row['symbol'],
        'position' => $row['row'].'-'.$row['cell']
    );
}

And my for-loops

和我的for循环

// Element counter
$cnt = 0;

// Creating tr's and td's
for($i = 1; $i <= 9; $i++) {
    echo '<tr>';
        for($h = 1; $h <= 18; $h++) {
            $cell = $i.'-'.$h;
            $pos = $ele[$cnt]['position'];

            echo '<td id="'.$cell.'">';
            if($pos == $cell) {
                echo $ele[$cnt]['symbol'];
            }
            echo '</td>';
            $cnt++;
        }
echo '</tr>';

I hope you can help me, cus I'm pretty stuck.

我希望你能帮我,我很困。

1 个解决方案

#1


2  

You should rethink your array and make it associative. Why don't you make the key of your array the field. like this:

你应该重新考虑你的数组并使它结合起来。你为什么不把你的数组的键设置成这个字段呢?是这样的:

$ele[ $row['row'].'-'.$row['cell'] ] = array(
    'id' => $row['id'],
    'name' => $row['name'],
    'symbol' => $row['symbol']
);

and then in your loop you can easily find the required element:

在循环中,你可以很容易地找到所需的元素:

for($i = 1; $i <= 9; $i++) {
echo '<tr>';
    for($h = 1; $h <= 18; $h++) {
        $cell = $i.'-'.$h;

        echo '<td id="'.$cell.'">';
        if(isset($ele[$cell]) {
            echo $ele[$cell]['symbol'];
        }
        echo '</td>';
        $cnt++;
    }
echo '</tr>';

#1


2  

You should rethink your array and make it associative. Why don't you make the key of your array the field. like this:

你应该重新考虑你的数组并使它结合起来。你为什么不把你的数组的键设置成这个字段呢?是这样的:

$ele[ $row['row'].'-'.$row['cell'] ] = array(
    'id' => $row['id'],
    'name' => $row['name'],
    'symbol' => $row['symbol']
);

and then in your loop you can easily find the required element:

在循环中,你可以很容易地找到所需的元素:

for($i = 1; $i <= 9; $i++) {
echo '<tr>';
    for($h = 1; $h <= 18; $h++) {
        $cell = $i.'-'.$h;

        echo '<td id="'.$cell.'">';
        if(isset($ele[$cell]) {
            echo $ele[$cell]['symbol'];
        }
        echo '</td>';
        $cnt++;
    }
echo '</tr>';