Search code examples
phppaginationlimitoffset

PHP, mySQL : Set an Array limit \ offset


Hello Guys how can i set an limit and offset vor an sql Array im executing an .sql file and write it in $sql but i need an offset for paging cause i get the whole data set.

public function getSqlDetailsAction()
{
    $fileName = $this->Request()->getParam('fileName');
    $pluginFolder = dirname(dirname(__DIR__));
    $sqlFolder = $pluginFolder . '/SqlFiles/';
    $filePath = $sqlFolder . $fileName;
    $sql = file_get_contents($filePath);
    $sql.=" limit ".$start.", ".$limit;
    $sqlResultList = Shopware()->Db()->query($sql);
    $this->View()->success = true;
    $this->View()->data = $sqlResultList->fetchAll();
}

Solution

  • I am guessing you are new to development or amending someone elses code in an environment you are not familiar with or just using SO as free development. So I will code it for you.

    public function getSqlDetailsAction()
    {
        $fileName = $this->Request()->getParam('fileName');
        $start    = $this->Request()->getParam('start');
        $limit    = $this->Request()->getParam('limit');
    
        $pluginFolder = dirname(dirname(__DIR__));
        $sqlFolder = $pluginFolder . '/SqlFiles/';
        $filePath = $sqlFolder . $fileName;
        $sql = file_get_contents($filePath);
        $sql.=" limit ".$start.", ".$limit;
        $sqlResultList = Shopware()->Db()->query($sql);
        $this->View()->success = true;
        $this->View()->data = $sqlResultList->fetchAll();
    }
    

    This appears obvious from the info you have given us, but as I see you have a page=1 in the querystring maybe you intended to do something slightly different and calc the LIMIT values from a page number and a lines-per-page mechanism, but thats just a extra F.L.O.C and you should be able to work that out.