展示显示结果的数量

6
我们正在使用以下代码基于所选的“起始日期”和“结束日期”筛选行。
我们能够成功地进行过滤并显示结果。
PHP
/* to show selected date */

if (isset($_POST['post_at']) && $_POST['post_at'] != '')
    {
    $orderFromDate = $_POST['post_at'] . "  00:00:00 ";
    }
    else
    {
    $orderFromDate = '';
    }

if (isset($_POST['post_at_to_date']) && $_POST['post_at_to_date'] != '')
    {
    $orderToDate = $_POST['post_at_to_date'] . "    23:59:59  ";
    }
    else
    {
    $orderToDate = '';
    }

/* to show selected date end*/

function getDesignerCollection()
{
    /* date search */
    if (isset($_POST['post_at']) && $_POST['post_at'] != '')
        {
        $orderFromDate = $_POST['post_at'] . " 00:00:00 ";

        }
        else
        {
        $orderFromDate = '';
        }

    if (isset($_POST['post_at_to_date']) && $_POST['post_at_to_date'] != '')
        {
        $orderToDate = $_POST['post_at_to_date'] . "    23:59:59  ";

        }
        else
        {
        $orderToDate = '';
        }
   /* date search end*/

    $accountType = $rows['type'];
    if ($accountType == "admin")
        {

        if ($orderFromDate != '') $order->addFieldToFilter('created_at', array(
            'gteq' => $orderFromDate
        ));
        if ($orderToDate != '') $order->addFieldToFilter('created_at', array(
            'lteq' => $orderToDate
        ));
        }

表单

<form name="frmSearch" method="post" action="">
    <input type="text" placeholder="From Date" id="post_at" 

    value="<?php

if ($orderFromDate != '')
    {
    $newPostStartDate = date('Y-m-d', strtotime($_POST['post_at']));
    echo $newPostStartDate;
    } ?>" name="post_at"  />  

<input type="text" placeholder="To Date" id="post_at_to_date" 


value="<?php

if ($orderToDate != '')
    {
    $newPostEndDate = date('Y-m-d', strtotime($_POST['post_at_to_date']));
    echo $newPostEndDate;
    } ?>"name="post_at_to_date"  />

<input type="submit" name="search" value="search" id="searchButton">

    <input type="button" value="Reset" id="clear-dates">
    </form>

jQuery

jQuery.datepicker.setDefaults({
    showOn: "button",
    buttonImage: "assets/img/datepicker.png",
    buttonText: "Date Picker",
    buttonImageOnly: true,
    dateFormat: 'yy-mm-dd'
});
$(function() {
    $("#post_at").datepicker();
    $("#post_at_to_date").datepicker();
});

enter image description here

现在我们想要显示有多少行被选中。如果结果如上图所示,我们希望显示“行数:1”。我对PHP非常陌生,并尝试了下面的代码,但它没有显示任何内容:
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

if ($result = mysqli_query($link, "SELECT entity_id , created_at  FROM sales_flat_order ORDER BY Name")) {

    /* determine number of rows result set */
    $row_cnt = mysqli_num_rows($result);

    printf("Result set has %d rows.\n", $row_cnt);

    /* close result set */
    mysqli_free_result($result);
}

编辑

现在我们想要显示显示的行数计数。因此我们尝试了下面的代码。它显示如下图所示。它显示“rows = 3”。因为它考虑了“sales_flat_order”表的“entity_id”列。但是我想要“rows:9”,因为您可以在图像中看到9行。

enter image description here

enter image description here

require_once '../../app/Mage.php';
Mage::app();

// specify the date picker date-format
$format = 'Y-m-d';


// if posted data are not empty
if(!empty($_POST['post_at']) && !empty($_POST['post_at_to_date']))
{

       if (!empty($_POST['post_at'])) {
           $dateFrom = DateTime::createFromFormat($format, $_POST['post_at']);
       }

       if (!empty($_POST['post_at_to_date'])) {
           $dateTo = DateTime::createFromFormat($format, $_POST['post_at_to_date']);
       }

        // Get the resource model
        $resource = Mage::getSingleton('core/resource');

       // Retrieve the read connection
          $read = $resource->getConnection('core_read');

      // MySQL query
        $query =  'SELECT entity_id, created_at, dproduct_id FROM sales_flat_order WHERE created_at BETWEEN :fromdate AND :todate ;';

// Bind MySQL parameters
$binds = array(
'fromdate' => $dateFrom->format('Y-m-d H:i:s'),
'todate' => $dateTo->format('Y-m-d H:i:s')
);
// Execute the query and store the results in $results
$results = $read->fetchAll($query,$binds);
echo "rows : ".count($results);
echo "<br>";
print_r($results);
}


else {
echo "error you have not specified any dates";
}

完整代码:http://pastebin.com/hMUEusvb


这段代码在哪里运行?它是否确实输出到您期望的页面部分?我注意到它没有保存数字并将其返回到UI层,而是立即打印它。在呈现后搜索页面源字符串。如果没有,请检查查询是否实际返回结果。另一种方法是使用SQL Count()。 - ADyson
@ADyson,我不知道如何做到这一点,你能否请你发布一篇带有代码的回答呢? - user7029249
@ADyson 这是那个文件的完整代码:http://pastebin.com/0x3375Mh,请在您有空的时候检查一下。 - user7029249
@ADyson,我真的不会忘记你的帮助,谢谢..... - user7029249
@moni_dragu,我真的不会忘记你的帮助,谢谢..... - user7029249
显示剩余14条评论
4个回答

2
您可以从mysqli查询中直接计算所选的id。请尝试在mysqli查询中使用以下代码:
   if ($result = mysqli_query($link, "SELECT count(entity_id) as Total_count,entity_id , created_at  FROM sales_flat_order ORDER BY Name")) {
    // Your process
}

我尝试了你的代码,当我点击“搜索”按钮时,它会显示行,但不会显示“有多少行”,你能否帮助我将“搜索”按钮连接到“我的代码结尾”?这是必需的吗?还是我们可以只使用JavaScript来获得结果? - user7029249
请加入此处:http://chat.stackoverflow.com/rooms/131215/date-search - user7029249

1
你可以使用JS来计算行数。将以下代码添加到你的代码末尾。将selector1替换为仅用于行的类名。在浏览器中检查你的页面,找到仅在行中出现一次并仅用于行的类。 然后将selector2替换为你想要显示行数的类或ID。
<script>    
$(document).ready(function () {
    var count = 0;
    //use class unique in each row.
    $("selector1").each(function () {
         count++;
    });
    //use class or id where you want to display count
    $("selector2").prepend("Number of rows "+count);
});

如果您想通过PHP处理它,请粘贴代码以呈现搜索结果。您当前的代码无法正常工作,因为您的代码在文件http://pastebin.com/QKMj0k2p的第293行发送响应。您可以在浏览器的网络选项卡中找到它,并在搜索提交时查看它发送请求和从那里返回的内容。如果您能找到它发送响应的位置,就很容易知道哪些代码正在呈现行。这将更有助于解决您的问题。

在Magento中,sales_flat_order中的计数将不会与图像中提供的视图中的行匹配。结果将是3个订单(请检查order_id列)。取消的订单不会被查询结果获取。 - user2560539

1

我认为您的问题中存在一个错误。如果您只想解决这个问题,您可以使用JavaScript。您只需要知道确切的选择器。

$(document).ready( function (){
   //your selector would direct li or td if there is no other li td    then it works other wise you have select parent then li or td like below comment
   //$(".class li").length

   var count = $("li").length;
   $("yourCounter").html(count);
} );

1
你好兄弟。希望你为我祈祷。祝你好运。 - Aabir Hussain

0

你的PHP代码应该像这样

// if posted data are not empty
if(!empty($_POST['post_at']) && !empty($_POST['post_at_to_date'])) { 
// specify the date picker date-format
  $format = 'Y-m-d';
// create dates
  $dateFrom = DateTime::createFromFormat($format, $_POST['post_at']);
  $dateTo = DateTime::createFromFormat($format, $_POST['post_at_to_date']);   
// if equal dates are applied to form 
  if($_POST['post_at']==$_POST['post_at_to_date']) {
    $dateTo->add(new DateInterval('T23H59M59S')); 
  }
// Get the resource model
  $resource = Mage::getSingleton('core/resource');    
// Retrieve the read connection
  $read = $resource->getConnection('core_read');
// MySQL query
  $query = 'SELECT `entity_id`, `created_at` FROM `sales_flat_order`  WHERE `created_at` BETWEEN :fromdate AND :todate ;';
// Bind MySQL parameters  
  $binds = array(
    'fromdate' => $dateFrom->format('Y-m-d H:i:s'),
    'todate' => $dateTo->format('Y-m-d H:i:s')
);
// Execute the query and store the results in $results
  $results = $read->fetchAll($query,$binds);
  echo "Orders count: ".count($results);
} else {
  echo "error you have not specified any dates";
}

我收到了一个错误信息,内容为 Fatal error: Class 'Mage' not found in,出现在 $resource = Mage::getSingleton('core/resource'); 这一行代码中。 - user7029249
请问您安装的Magento版本是哪个?@abcd - user2560539
尝试在 require_once 'app/Mage.php'; 之前添加这个。 - user2560539
现在出现错误:`Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Name' in 'order clause'' in /var/www/html/sbdev2/lib/Zend/Db/Statement/Pdo.php:228 Stack trace: #0 /var/www/html/sbdev2/lib/Zend/Db/Statement/Pdo.php(228): PDOStatement->execute(Array) #1 /var/www/html/sbdev2/lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array) #2 /var/www/html/sbdev2/app/code/core/Ze - user7029249

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接