创建一个带有MySQL表日期的html表,按价格排序

时间:2015-10-30 03:53:01

标签: php mysql sorting

我正在为我的家人制作一份愿望清单网络应用程序(因为缺少一个更好的术语。),使用php和mysql。

我有一张名为“TEST'”的表格,我已经用一堆测试值填充它,并且我试图按升序价格显示它,就像这样......

Terminal output

...但是在像这样的html表中。

HTML table of SQL table

我用来生成第二张图片的代码是......

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdn.jsdelivr.net/jquery.visible/1.1.0/jquery.visible.min.js"></script>

<div class="wrapper">
  <a class="previous-arrow" href="">&gt;</a>-->
  <!--
		-->
  <div id="1" class="item item1 wheat">a.</div>
  <!--
		-->
  <div id="2" class="item item2 pink">a.</div>
  <!--
		-->
  <div id="3" class="item item3 beige">a.</div>
  <!--
		-->
  <div id="4" class="item item4 gainsboro">a.</div>
  <!--
		-->
  <div id="5" class="item item5 coral">a.</div>
  <!--
		-->
  <div id="6" class="item item6 crimson">a.</div>
  <!--
		-->
  <div id="7" class="item item7 darkgoldenrod">a.</div>
  <!--
		-->
  <div id="8" class="item item8 aquamarine">a.</div>
  <!--
		-->
  <div id="9" class="item item9 aquamarine">a.</div>
  <!--
		-->
  <div id="10" class="item item10 aquamarine">a.</div>
  <!--
		-->
  <div id="11" class="item item11 aquamarine">a.</div>
  <!--
		--><a class="next-arrow" href="">&lt;</a>	
</div>

我已经尝试找到一种方法可以调整现有代码以按价格订购数据,但我还没有能够这样做。如果有人有想法可以提供帮助,那将会有很大的帮助。

UPDATE /修改

我尝试过以下代码:

    if (isset($_POST['button1'])) 
    { 
      $sql = "SELECT * FROM ".$m->get('NAME');
      //Feeds the statement to the mysql connection
      $resultPrice = $conn->query($sql); 

      echo "button 1 has been pressed.<br>";
      echo "$ m :".$m->get('NAME').".<br>";

      echo "<table id='results'>";
      echo "<tr ><td class='itemPrice'>PRICE</td><td class='itemID'> ID </td><td> ITEM </td><td> URL/LOCATOIN </td><td> NOTES </td></tr> "; 
      if ($resultPrice->num_rows != 0) 
      {
         // output data of each row
         while($row = $resultPrice->fetch_assoc()) 
          {
             if($row["id"] % 2 != 0) {echo"<tr class='rowDark'><td class='itemPrice' style='background-color:#69f;color:#fff;'> $" . $row["PRICE"]. " </td><td class='itemID'> " . $row["id"]. " </td><td> " . $row["ITEM"]. " </td><td> " . $row["URL"]. " </td><td> " . $row["NOTES"]. " </td></tr> ";}
           else {echo                     "<tr>                <td class='itemPrice' style='background-color:#69f;color:#fff;'> $" . $row["PRICE"]. " </td><td class='itemID'> " . $row["id"]. " </td><td> " . $row["ITEM"]. " </td><td> " . $row["URL"]. " </td><td> " . $row["NOTES"]. " </td></tr> ";}
        }
        //clear $Person after the data is displayed for the next update
    }

我收到以下错误:

    1    $query = "SELECT * FROM ".$m->get('NAME')."ORDER BY PRICE";
    2    $resultPrice0 = $conn->query($query); 

    3    echo "<table id='results1'>";
    4    echo "<tr ><td class='itemPrice'>PRICE </td><td class='itemID'> ID </td><td> ITEM </td><td> URL/LOCATOIN </td><td> NOTES </td></tr> ";

    5    if ($resultPrice->num_rows != 0) 
    6    {
    7       // output data of each row
    8       while($row0 = $resultPrice0->fetch_assoc()) 
    9        {
    10         echo"<tr><td> $" . $row["PRICE"]. " </td><td class='itemID'> " . $row["id"]. " </td><td> " . $row["ITEM"]. " </td><td> " . $row["URL"]. " </td><td> " . $row["NOTES"]. " </td></tr> ";              
    11       }
    12    }

我是php和mysql的新手,所以我不知道这意味着什么。

再次。感谢您提供的帮助。

2 个答案:

答案 0 :(得分:2)

您需要使用ORDER BY代码。

此语法按提供的字段按升序或降序排序行。

// Show highest price to lowest price

$sql = "SELECT * FROM ".$m->get('NAME') . " ORDER BY PRICE DESC";


// Show lowest price to highest price

$sql = "SELECT * FROM ".$m->get('NAME') . " ORDER BY PRICE ASC";

答案 1 :(得分:1)

这是你想要的吗?

$sql = "SELECT * FROM ".$m->get('NAME') . " ORDER BY price";