mysql - PHP only parses first element in array -
i'm working on periodic table, in retrieve elements database. want place elements in respective position in periodic table.
e.g.: hydrogen in row 1, cell 1 - helium in row 1, cell 18. cells between 2 should therefore blank.
the problem is, when run for-loop , compare position - stored in database - , id i've given each cell (which same position-outcome database), parses first element , stops.
it returns first 1 1-1 (helium - row 1, cell 1) , parses should. next one, 1-2 (row 1, cell 2), , here on stops.
this query
// sql query retrieving elements $query = " select id, name, symbol, row, cell elements order 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 for-loops
// element counter $cnt = 0; // creating tr's , 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 can me, cus i'm pretty stuck.
you should rethink array , make associative. why don't make key of array field. this:
$ele[ $row['row'].'-'.$row['cell'] ] = array( 'id' => $row['id'], 'name' => $row['name'], 'symbol' => $row['symbol'] );
and in loop can find 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>';
Comments
Post a Comment