我從數據庫中拉出一些數據,這是一個私人網站,所以我不太擔心現在使用MySQL,雖然我明白我應該使用PDO,只是沒有' t做了開關呢:在MySQL中設置默認排序從MySQL查詢
<table id="table" border="1" bordercolor="#000000" cellpadding="2" cellspacing="0">
<thead>
<tr>
<th>
<span class="th">
<span class="arrow"></span>
<span class="icon"></span>
<span class="title">Exception ID</span>
</span>
</th>
<th>
<span class="th">
<span class="arrow"></span>
<span class="icon"></span>
<span class="title">Exception</span>
</span>
</th>
<th>
<span class="th">
<span class="arrow"></span>
<span class="icon"></span>
<span class="title">First 250 chars of code</span>
</span>
</th>
<th>
<span class="th">
<span class="arrow"></span>
<span class="icon"></span>
<span class="title"># of exceptions</span>
</span>
</th>
<th>
<span class="th">
<span class="arrow"></span>
<span class="icon"></span>
<span class="title">Bug #</span>
</span>
</th>
</tr></thead><tbody>
<?php
//Need to find the total rows in the snippets_link_email_id because well need to show the last x records, so we get total number or rows and then minus the total RECORDS to only select the last x
$total_snippet_check = mysql_query("SELECT COUNT(email_id) as num_rows FROM snippets_link_email_id");
$row = mysql_fetch_object($total_snippet_check);
$total_rows = $row->num_rows;
//finding out how many exceptions there was in the last 24 hours from a table that records total exceptions every hour
$how_many_recent_crashes = mysql_query("SELECT * FROM crash_log_entries ORDER BY crash_id DESC LIMIT 24");
while ($row_recent_crashes = mysql_fetch_array($how_many_recent_crashes))
{
$crash_processed = $row_recent_crashes['crash_processed'];
$crash_processed_total += $crash_processed;
}
$which_records = $total_rows - $crash_processed_total;
//need info on that snippet
$feedback_query_first =
"SELECT *, COUNT(*) AS tot_snippets
FROM snippets
LEFT JOIN snippets_link_email_id
ON (snippets.snippet_id = snippets_link_email_id.snippet_id)
WHERE snippets_link_email_id.email_id > $which_records
GROUP BY snippets.snippet_id
ORDER BY tot_snippets asc";
$result1 = mysql_query($feedback_query_first);
while ($row1 = mysql_fetch_array($result1))
{
$i = $row1['snippet_id'];
//Need to find the total snippets for the current snippet
$feedback_query = mysql_query(
"SELECT * FROM snippets
LEFT JOIN snippets_link_email_id
ON snippets.snippet_id = snippets_link_email_id.snippet_id
WHERE snippets_link_email_id.snippet_id = $i
AND snippets_link_email_id.email_id > $which_records");
$tot_snippets = mysql_num_rows($feedback_query);
$snippet_text_pre = $row1['snippet_text'];
$snippet_text_pre1 = htmlspecialchars($snippet_text_pre);
$snippet_text = str_replace("<br />", "<br />",$snippet_text_pre1);
$snippet_text = substr($snippet_text,0,250);
$comment = $row1['comment'];
$comment_short = substr($comment, 0, 35);
$note_length = strlen($comment);
$snippet_id = $row1['snippet_id'];
$email_id = $row1['email_id'];
$query_exceptions = "SELECT * FROM emails WHERE email_id = $email_id ORDER BY email_id DESC";
$result2 = mysql_query($query_exceptions);
while ($row2 = mysql_fetch_array($result2))
{
$actual_exception = $row2['actual_exception'];
}
echo '<tr><td>'.$snippet_id.'</td>';
echo '<td>'. $actual_exception.'</td>';
echo '<td>'. $snippet_text.'....</td>';
echo '<td>'. $tot_snippets.'</td>';
$tot_tot += $tot_snippets;
echo '<td>'. $comment . '</td></tr>';
}
echo "</tbody></table>";
echo "the total exceptions for this time period is: " . $tot_tot . "<br />";
?>
好吧,希望這是好的,我濾除了一切無關緊要,所以希望代碼有一定意義。現在有沒有一種方法可以在頁面加載時默認排序爲$tot_snippets
,而無需使用完整的jQuery排序解決方案,因爲我只想按此列對此進行排序,因爲我不需要更新它。我不認爲我可以使用orderby
來排序,因爲tot_snippets
的值不是列值,但我似乎找到的所有解決方案都有一個完整的排序解決方案,並且大多數涉及Jquery,如果Jquery是最好的選擇,我認爲可能有更簡單的方法?
//編輯
我更新了代碼,並添加了基本上整個代碼,我意識到我的代碼很混亂,我自學成才,只是學會了做我需要做的事情。我相信它可以被改變得更加緊湊,但是我的主要問題是即使使用以前的解決方案之一,它仍然不能排序在異常列中,我猜測解決方案的提供對我來說很好遺漏其他一些代碼意味着它不起作用,所以我決定這次包括整個代碼。我最初放棄它以使其更加複雜,但現在我意識到這可能是反效果的。
謝謝對於解決方案,我更新了我的代碼,因爲您的解決方案對我而言並不奏效,我懷疑這是因爲我遺漏了代碼的重要部分。 – user1547410 2013-02-19 11:02:59