dsxay48646 2014-07-22 18:11
浏览 24
已采纳

使用一列中的ORDER BY显示来自Mysql数据库的数据

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
        });
});

});
  • 写回答

2条回答 默认 最新

  • drhwb470572 2014-07-22 18:28
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法