/    Sign up×
Community /Pin to ProfileBookmark

cant create table from query correctly

Hi – I am really stuck on what I am sure is a simple question!

I have to create a table of results for school children. It has a number of sets of grades that are called assessblock_id – which correspond to different times of the year.


——————————

subject | Nov | Jan| Mar|
Speaking | P2ic| P9a | H1b
Listening | P3 | H1 | H3
etc

I query the database which retuns arrays like this:

[CODE]Array ( [assessment_id] => 47 [pupil_id] => 288 [assessblock_id] => 13 [Speaking] => P2ic [Listening] => P2ib…
[/CODE]

If there was only one column of results it would be dead easy.

[CODE]
$query1 = “SELECT * FROM assessments WHERE pupil_id=288 AND assessblock_id=’15′”;
$result = $conn->query($query1);
if ($result->num_rows > 0) {

?>
<table border=”1″ cellspacing=”0″ cellpadding=”0″>
<tr><th>Subject</th><th>grade</th></tr>
<?php
while($row = $result->fetch_assoc()) {
foreach ($subjectListArray as $subject){
$sub = str_replace(” “, “_”, $subject);
echo ‘<tr><td>’.$subject.'</td><td>’.$row[$sub].'</tr>’;
}
}// endwhile
echo ‘</table>’;
}//endif
[/CODE]

Which produces two columns.

If the names of the subjects wnet along the top rather than down the side I think icould do it.

Please, How can I add in extra columns given that i get one set of results ($row) at a time and they have to go down.

I’d really appreciate any help / pointers on who to do this.?

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 12.2016 — I'll admit to being a bit confused as to what you actually need to do, but my guess is that you may need to build an array with the data from the query to get the desired structure, and then loop through that array to output the table.
×

Success!

Help @maxelcat spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.22,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...