Jump to content

[SOLVED] Sort columns in a database


mattal999

Recommended Posts

Hi guys,

 

I have a table structured like so:

 

id | name      | userid | songcount | song1 | song2 | ... | song20

---------------------------------------------------------------

6  | First      | 1      | 6            | 134    | NULL  | ... | 255

 

Now I want to go through this row, and put all of the numbers from song1 - song20 in order. Like say I had data like:

 

134, 234, NULL, NULL, 234, 577, 231, NULL, etc.

 

I want it like so:

 

134, 234, 234, 577, 231, NULL, NULL, NULL, etc.

 

So I want the empty columns shifted to the end, and the numbers moved forward to the next empty column, so in the end the numbers are all before the empty columns, still in the same order they were before. Any ideas how I would go about this? I was thinking some kind of foreach statement, but couldn't come up with anything.

Link to comment
Share on other sites

I would, but im looking for something like a foreach loop or while clause that would just sort them with UPDATE statements or something. I need to do this so that I can organise it properly. Kind of like a CRON script if you will. Example:

 

<?php

// Connect to DB
include("includes/connect.php");

// Grab row
$query = mysql_query("SELECT * FROM playlists WHERE id='".$id."'");
$row = mysql_fetch_array($query);

// Loop through row
while(xxxx) {
    // Sort the columns (No idea)
    // Run some UPDATE statements to make it sort properly
}

?>

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.