简体   繁体   中英

Show data from Mysql Database using ORDER BY from One Column

In my Mysql Database I've one Column called notesUpdate where I inserting many data with date ,username and notes. So that notesUpdate Column hold following data.

2014-07-23 04:01 AM alex : First Note{ASUSIBBIR}
2014-07-23 04:01 AM alex : Second Note{ASUSIBBIR}
2014-07-23 04:02 AM alex : Third Note{ASUSIBBIR}
2014-07-23 04:02 AM alex : Fourth Note{ASUSIBBIR}

Note: {ASUSIBBIR} is a separator I'm using.

Now I'm trying to fetch these data in Descending Order. I mean Last inserted data should be at First. So I'm using following query. I know it's not right but can't get any idea how can show these data by Descending Order ?

$query =  mysql_query("SELECT notesUpdate FROM contact_details WHERE cdid = '$id' ORDER BY 
notesUpdate DESC");

Data Show Should Be Look Like This:

2014-07-23 04:02 AM alex : Fourth Note
2014-07-23 04:02 AM alex : Third Note
2014-07-23 04:01 AM alex : Second Note
2014-07-23 04:01 AM alex : First Note

How I inserting Data to DB

$username = $_SESSION['front_username'];
$contetn =  $date;
$contetn .=  " ";
$contetn .=  "$username";
$contetn .=  " : ";
$contetn .=  mysql_real_escape_string(htmlspecialchars(trim($_POST['contentText'])));
$cdid = $_POST['cdid'];
$contetn .= "{ASUSIBBIR}";  // this is a separator
$query = mysql_query("UPDATE contact_details SET notesUpdate = CONCAT(notesUpdate, 
'$contetn') WHERE cdid = '$cdid' LIMIT 1")

Any help ?

Update:

This is the form where I insert data to db:

<table width="100" border="0" cellspacing="0" cellpadding="0">
 <tr>
    <td><h2>Write your Notes</h2></td>
  </tr>
  <input type="hidden" value="<?php echo $id; ?>" name="cdid" id="cdid"/>
  <tr>
    <td><textarea name="contentText" id="contentText"  cols="53" rows="5"></textarea></td>
  </tr>
  <tr><td>&nbsp;</td></tr>
  <tr>
    <td><input type="submit" value="Save" name="submit" class="submit" id="addNewNotes"/></td>
  </tr>
</table>

This is Jquery part:

$(document).ready(function() {
    $('body').on('click', '#addNewNotes', function(e){
        e.preventDefault();
        var formData = new FormData($(this).parents('form')[0]);

        var cid=$(this).parents('form:first').find('#cdid').val();
        $("#loading-image").show();

        $.ajax({
            url: 'response.php',
            type: 'POST',
            xhr: function() {
                var myXhr = $.ajaxSettings.xhr();
                return myXhr;
            },
            success: function(data){                                               
              getDetails(cid);
              $("#loading-image").hide(); //hide loading
              $("#Notesboxsuccess").html(data).show();        
            },

        //  complete: function(){
         // $("#loading-image").hide(); //hide loading here
        //},

            data: formData,
            cache: false,
            contentType: false,
            processData: false
        });
});

});

you can cast the first 19 characters as a datetime object like so

CAST(LEFT(notesUpdate, 19) as datetime)

so something like

SELECT 
    notesUpdate 
FROM contact_details 
ORDER BY 
    STR_TO_DATE(LEFT(notesUpdate, 19), '%Y-%m-%d %l:%i %p') DESC, 
    id DESC

NOTE: this is NOT a recommended way to store data.. you should consider changing it.. this doesn't follow the standards of normalization.

SEE DEMO

I suggest select notesUpdate and split it to an array, then reverse it and dispay :) But you should consider use some relative table in databese to store changes history.

$result =  mysql_query("SELECT notesUpdate FROM contact_details WHERE cdid = '$id'");
$row = mysql_fetch_assoc($result);
$notesUpdateArray = explode('{ASUSIBBIR}', $row['notesUpdate']);
$notesUpdateReversedArray = array_reverse($notesUpdateArray);


var_dump($notesUpdateReversedArray);

If you wont it as string just implod it back

$notesUpdateReversed = implode('{ASUSIBBIR}', $notesUpdateReversedArray);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM