Jump to content

Mysql output to a table


JackNETUK

Recommended Posts

Right ive got a database that i need to output to a table in html like thistable.PNG. I need to get the data from a few databases as its a relational database model.

Heres my track database

trackdb.PNG as you can see the following rows are the same as the table datetime, subject_id, notes, youdo

Now to get the skills they are stored in a different table

track2skills.PNG

As you can see with the track2skills db there is a track_id and skill_id the track_id will be the same as the track_id in the database track so thats where the skills are stored.

 

What i would like to know is how would i get all this information into the table using php

I can use this query to get the results i want from the db but i cant really use it because it will give me lots of uneeded results

queryoutput.PNG

 

Thanks for your help

Link to comment
Share on other sites

Well, you left out some information. Where is the query that you used for that last table. Also, you apparently have another table with the subject name.

 

If I am understanding your problem it is that you want the multiple skills for a track record to be reported in a single field. If that is the case, the query you are using is the right one. You just need to create your PHP code to process it to your liking. Assuming you have run the query and save the results to the variable $results, the following will output the results according to your specs.

 

<?php

echo "<table>\n";
echo "  <tr>\n";
echo "    <th>Date & time</th>\n":
echo "    <th>Subject</th>\n":
echo "    <th>What you did</th>\n":
echo "    <th>Notes</th>\n":
echo "    <th>Skills</th>\n":
echo "  </tr>\n";

$current_track_id = '';

while ($record = mysql_fetch_assoc()) {

  //Determine if this is a new track record
  if ($current_track_id != $record['track_id']) {

    //Close preceeding track ID row
    if ($current_track_id != '') {
      echo "    </td>\n";
      echo "  </tr>\n";
    }

    $current_track_id = $record['track_id'];

    //Create new track record row
    echo "  <tr>\n";
    echo "    <td>".$record['datetime']."</td>\n":
    echo "    <td>".$record['subjectname']."</td>\n":
    echo "    <td>".$record['youdo']."</td>\n":
    echo "    <td>".$record['notes']."</td>\n":
    //Show first skill for the current record
    echo "    <td>".$record['skillname'];

  } else { //Additional record for current track id

    //Show additional skill for current track record
    echo ", " . $record['skillname'];
  }

}

//Close the last record & the table
echo "    </td>\n";
echo "  </tr>\n";
echo "</table>\n";

?>

 

EDIT: fixed a couple minor errors

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.