简体   繁体   English

PHP SQL Joomla将具有两列的表存储到数组中,并在以后回显它们

[英]PHP SQL Joomla store table with two columns into array and echo them later

So I have a simple table with two columns. 所以我有一个包含两列的简单表。 The table is called "mytable" and the columns are "product" and "id". 该表称为“ mytable”,列为“ product”和“ id”。 The table has about 12 entries. 该表有大约12个条目。

            $db = JFactory::getDbo();
            $query = "SELECT * FROM mytable";
            $db->setQuery($query);
            $results = $db->loadObjectList();

So I've gotten this far querying successfully but I want to call back the results of each row. 因此,到目前为止,我已经成功查询了,但是我想回调每一行的结果。 That's the part I am stuck on. 那就是我坚持的部分。 I need to store this into an array so I can later make a while loop spitting out each row. 我需要将其存储到数组中,以便以后可以进行一会儿循环将每一行吐出来。 Also I cannot echo it right underneath that code. 我也无法在该代码的下面回显它。 This needs to be stored in an array that is then pulled from another page and then I spit out each row. 这需要存储在一个数组中,然后从另一个页面中拉出,然后吐出每一行。 Thanks! 谢谢!

Try this, 尝试这个,

        $db = JFactory::getDbo();
        $query = "SELECT * FROM mytable";
        $db->setQuery($query);
        $results = $db->loadObjectList();
        echo '<pre/>'
        print_r($results);//the resulted array is already in this variable you can iterate it later with foreach loop.

for looping and printing 用于循环和打印

foreach($results as $key=>$value){
 echo 'Product-->'.$value->product;
 echo 'ID-->'.$value->id;
}

check the Joomla DB query for more details. 检查Joomla数据库查询以获取更多详细信息。

Hope it helps.. 希望能帮助到你..

I would personally use more up to date coding standards for you query, like so: 我会亲自为您查询使用更多最新的编码标准,例如:

$db = JFactory::getDbo();
$query->select($db->quoteName('*'))
      ->from($db->quoteName('mytable'));
$db->setQuery($query);
$results = $db->loadObjectList();

Then to get the results, create a loop: 然后,要获得结果,请创建一个循环:

foreach($results as $result) {
    echo 'ID = ' . $result->id;
    echo 'Product = ' . $result->product;
}

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM