MySQL ORDER BY和while循环

时间:2015-07-22 12:05:10

标签: php

我已经问过一个属于这个问题的问题here

RiggsFolly接受的答案中的代码非常合适,但是存在一个小问题。我需要一些时间来测试它,并搜索为什么这不是解决主要目标的最佳方法的原因。我很高兴提出一个新问题。

RiggsFolly的代码基于$current_provider,因此如果$current_provider发生了变化,那么while循环会检查每一轮。到现在为止还挺好。但现在我需要添加一个全面的逻辑。我注意到我添加了一个true/false变量,它只是检查来自获取对象的值是否等于某个字符串。此比较侧重于特定列表项,而不是基本$current_provider

因此,目标是$current_provider检查true/false的每个已获取对象,并且将独立于$current_provider。目前我尝试使用第二个循环进行扩展,但只是想举一个例子,希望它能清楚地实现:

$service = $db->query("SELECT * FROM `system` ORDER BY provider, artist");
$provider = NULL;
$close = false;

while ($data = $service->fetch_object()) {

    $amount_1 = $data->digit_1; //db-structure: float
    $amount_2 = $data->digit_2; //db-structure: float

    if ($amount_1 == $amount_2) {
        $close = true;
    }
    if ( $current_provider != $data->provider ) {  
        if ( $current_provider !== NULL ) {
            echo '</div>close container in case of current_provider is already set';
        }
        echo '<div class="provider">open a new container in case of current_provider is not like data->provider or empty';
        $current_provider = $data->provider;
    }
    echo 'some styling for each object';   
    if ($close === true ) { 
        echo '<div class="specific">if the amount_1 is same like amount_2 for a single object add only for this object a certain div';  
    } else {
        echo '<div>show standard container even on specific object';
    }   
echo '</div><!--close container provider-->';
}

亲切的问候。

2 个答案:

答案 0 :(得分:2)

为了避免玩开关元件更好地将它们存储在数组中并最终输出它们。

看看我的例子,很简单:

private GestureDetectorCompat simpleGestureHandler = new GestureDetectorCompat(getContext(), new MySimpleGestureListener());

swipe_area.setOnTouchListener(new View.OnTouchListener()
{
    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        if(simpleGestureHandler.onTouchEvent(event))
        {
           //if true then a single tap
        }
        else
        {
           //calculate swipe action
        }
    }
}

class MySimpleGestureListener extends GestureDetector.SimpleOnGestureListener
{
    public boolean onSingleTapUp(MotionEvent e)
    {
        //single tap returns true so we know to register it over a swipe
        return true;
    }

    @Override
    public void onLongPress(MotionEvent event)
    {

    }

    @Override
    public boolean onDown(MotionEvent event)
    {

        return false;
    }

    @Override
    public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY)
    {

        return false;
    }

    @Override
    public boolean onScroll(MotionEvent event1, MotionEvent event2, float distanceX, float distanceY)
    {


        return false;
    }
}

答案 1 :(得分:2)

我仍然不确定我是否理解你在这里想要实现的目标,但也许如果你做出这样的决定,除了我的建议之外,你可能需要做的更明显。< / p>

移动

$amount_1 = $data->digit_1; 
$amount_2 = $data->digit_2; 

从一个非常好的对象属性到2个标量变量是完全不必要的,为什么两次存储所有内容,最终会耗尽内存,但更重要的是如果你将它们留在对象中并使用if ($data->digit_1 == $$data->digit_2) {测试它们永远不会混淆这些数据的来源!

同样测试循环顶部的数字只是为了设置另一个标量变量以便稍后在循环的底部使用是浪费时间。这些属性不会在循环的顶部和底部之间发生变化,因此请测试您想要做出决定的实际对象,然后在那里输出所需的HTML。避免了另一个潜在的混乱,并且浪费了8到16个字节的内存!

$service = $db->query("SELECT * FROM `system` ORDER BY provider, artist");
$current_provider = NULL;

while ($data = $service->fetch_object()) {

    if ( $current_provider != $data->provider ) {  

        if ( $current_provider !== NULL ) {
            echo '</div>close container in case of current_provider is already set';
        }

        echo '<div class="provider">open a new container in case of current_provider is not like data->provider or empty';
        $current_provider = $data->provider;
    }

    echo 'some styling for each object';   


    // at this point we test the 2 digits and if equal
    // add an extra bit of HTML to the output
    if ($data->digit_1 == $data->digit_2) {
        echo '<div class="specific">if the amount_1 same like amount_2 for a single object add only for this object an certain div';  
    } else {
        echo '<div>show standard container even on specific object';
    }   
echo '</div>;
}