简体   繁体   English

如何使用表的ID从数据库中检索某些信息以提取某些信息?

[英]How do I retrieve certain information from database using the ID of the table to extract certain information?

I am currently doing a small web project and am struggling to get individual names from the database to appear in different panels of my website. 我目前正在做一个小型Web项目,正在努力从数据库中获取个人名称,以使其出现在我网站的不同面板中。 There are 18 different names in the table I need and I need to display them in 18 individual panels as shown here Screenshot of Panels . 还有在我需要的表18个不同的名字,我需要在18个单独的面板中显示它们如下图所示面板的屏幕截图

I am using MvC and have this SQL statement in my model - 我正在使用MvC,并且在我的模型中有此SQL语句-

public function fetchMonarch($Monarch, $MonarchID)
 {
     $sqlQuery = "SELECT * FROM monarchy WHERE Monarch = '" . $Monarch ."'AND MonarchID = '".$MonarchID."'";

     $statement = $this->_dbHandle->prepare($sqlQuery); //Prepare PDO statement
     $statement->execute(); //Executes PDO statement

     $dataSet = [];
     while ($row = $statement->fetch()) { //Fetches the next row matching the query
         $dataSet[] = new LoginData($row);
     }

     return $dataSet;
 }

How would I set it up in my controller so that I can access the names I want to in each panel using Session ? 如何在控制器中进行设置,以便可以使用Session在每个面板中访问想要的名称?

Any ideas of how to do this even if you think I maybe going about this incorrectly would be a massive help as I have hit a snag. 即使您认为我可能不正确地处理此问题,任何有关如何执行此操作的想法都将为您带来巨大帮助,因为我遇到了麻烦。

This is my panel code within the HTML - 这是我在HTML中的面板代码-

 <div id="monarch1"> <div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title">Panel Title</h3> </div> <div class="panel-body"> Panel content </div> </div> </div> <div id="monarch2"> <div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title">Panel title</h3> </div> <div class="panel-body"> Panel content </div> </div> </div> 

I believe the solution you are after is something like this: 我相信您所追求的解决方案是这样的:

$dataSet = [];
while ($row = $statement->fetch()) { //Fetches the next row matching the query
    $dataSet[$row['MonarchID'] = new LoginData($row); // note the ID
}

Then within your HTML: 然后在您的HTML中:

foreach ($dataSet as $id=>$monarch){
    echo '<div id="monarch2">
    <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title">'.$monarch->objectData.'</h3>
        </div>
        <div class="panel-body">
            '.$monarch->objectData.'
        </div>
    </div>
    </div>';
}

$monarch->objectData would reference your keys in LoginData($row) or alternatively store a new key in $allData['MonarchID'] = $row; $ monarch-> objectData将在LoginData($ row)中引用您的密钥,或者将新密钥存储在$ allData ['MonarchID'] = $ row;中。 and then foreach on $alldata and instead of $monarch->objectData you would use $monarch['FieldInfo']. 然后在$ alldata而不是$ monarch-> objectData上进行foreach,将使用$ monarch ['FieldInfo']。

If you need to specifically place your elements - use ordering in the SQL or alternatively you can access $dataSet['MonarchID']->objectData to place your content in specific places. 如果需要专门放置元素-在SQL中使用排序,或者可以访问$ dataSet ['MonarchID']-> objectData将内容放置在特定位置。

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

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