You did not indicate in your two questions what a database table is, so I cannot answer it specifically, but we will present our proposal.
You can use the aggregation functions in mysql to get ordered and ordered news items by topic, including. their score. You can execute two requests to get an invoice first, it depends a little on how you want to process your data.
In any case, using the mysql_... functions, all the data you select from the database will be in memory (even twice due to internal elements). Thus, the presence of another array, as in your previous question, should not hurt much due to copying to write optimization in PHP. Only small overheads are effective.
Next to the fact that before you take care of the actual output, you must organize your data so that you do not need to mix data processing and output logic. Blending makes things more complicated, making them harder to solve. For example, if you put your output in simple functions, it becomes easier:
function render_list($title, array $entries) { echo '<ul><li>', $title, '<ul>'; foreach($entries as $entry) { echo '<li>', $entry['NewsID'], '</li>'; } echo '</ul></li></ul>; } function render_column(array $topics) { echo '<div class="column">'; foreach($topics as $topic) { render_list($topic['title'], $topic['entries']); } echo '</div>'; }
This already solves the output problem, so we no longer need to worry about that. We just need to take care of what to serve in these functions as parameters.
X options for each column:
In this option, the data should be an array with one type per value, as with the previous question. I would say that this has already been decided. I donβt know what specific problem you have with the number of columns, the calculation looks good, so I will skip this until you provide specific information about it. "Not working" does not fit.
X news column option:
This is more interesting. Itβs easy to jump to the previous topic in the next column by adding the topic title again. Sort of:
Topic A Topic A Topic B - A-1 - A-5 - B-4 - A-2 Topic B - B-5 - A-3 - B-1 - B-6 - A-4 - B-2 - B-3
To do this, you need to process your data in a different way, namely in terms of quantity (news).
Let's say you managed to get the data grouped (and therefore sorted) from your database:
SELECT TopicName, NewsID FROM news GROUP BY 1;
Then you can simply iterate over all returned rows and create your own columns, finally output them (already resolved):
$itemsPerColumn = 4; // get columns $topics = array(); $items = 0; $lastTopic = NULL; foreach ($rows as $row) { if ($lastTopic != $row['TopicName']) { $topic = array('title' => $row['TopicName']); $topics[] = &$topic; } $topic['entries'][] = $row; $items++; if ($items === $itemsPerColumn) { $columns[] = $topics; $topics = array(); $lastTopic = NULL; } } // output foreach($columns as $column) { render_column($column); }
Thus, this is actually comparable to the previous answer, but this time you do not need to reconfigure the array to receive news ordered by their topic, because the database query does this already (you could do this for the previous answer as well).
Then again the same thing: Iterating over the returned result set and transferring the data to a structure that you can output. Input, processing, output. It is always the same.
Hope this will be helpful.