CSnovice Posted March 16, 2015 Share Posted March 16, 2015 I have the following general mySQL query: $query = sprintf( "SELECT a.A, a.B, ..., a.C, FROM a WHERE a.A = %s ORDER by a.A;", implode(" OR a.A = ", $_SESSION['values'])); for some records, B has a value, and for others, B is NULL. That is what I want. I extract the query with the following PHP: for ($i = 0; $i < $nrows; $i++){ $row = mysqli_fetch_assoc($result);//fetches data stored within each row extract($row); echo "<tr>"; foreach($row as $column => $field){ if($field == $...){ ... } elseif($field == $C){ echo"<td> <input type='text' name='C+[]' value='$C'> </td>"; } echo "</tr>" } In the resulting html table, records containing not null B fields are presented accurately, while records with null B fields incur a duplication of field C. This offset occurs at the beginning of the record, pushing the final field in the record outside the table boundaries. I think I've narrowed down the problem to the extract() function, as r_print readouts of every other variable in the script returns the accurate field names and values. But, running print_r on $row after extract() provides an identical printout to other variables in the script. What are some possible ways I can stop the duplication of field C from occurring? Happy to provide more information upon request. Quote Link to comment Share on other sites More sharing options...
CSnovice Posted March 16, 2015 Author Share Posted March 16, 2015 (edited) I've also tried using GROUP BY in place of ORDER BY in the SQL query, but the output remains the same. Edited March 16, 2015 by CSnovice Quote Link to comment Share on other sites More sharing options...
CSnovice Posted March 16, 2015 Author Share Posted March 16, 2015 It was also suggested to me that I should refrain from using extract($row), as it creates unnecessary duplicate variables, but I am uncertain of another way to acess $result in order to format the html table. Also, the use of extract() only seems contentious when using it with $_POST or $_GET methods, a process that I am not following. I am happy to try an alternative, but would like some suggestions as to what could work first. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted March 16, 2015 Share Posted March 16, 2015 You have the query results that come to you in array format. Why do you need to use extract() on that? Simply loop thru as you are doing and generate your html table rows. I fail to see a problem. while($row = mysqli_fetch_assoc($result)) { echo "<tr>"; foreach ($row as $k=>$v) echo "<td>$v</td>"; echo ("</tr>"; | will output all the elements from your query results in table rows. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.