简体   繁体   English

将数据从视图传递到Codeigniter中的控制器

[英]passing data from view to a controller in codeigniter

well I have a problem. 好吧,我有一个问题。 I'm building a list of name.this name are obtained from a mysql query.The controller receives those names from model and after passes to a view and view prints out them in a list.When you click in a name another page opens showing every details about name (phone,ecc...). 我正在建立一个名称列表。这个名称是从mysql查询中获得的。控制器从模型中接收这些名称,然后传递给视图,然后视图将其打印在列表中。当您单击名称时,将打开另一个页面显示有关姓名的所有详细信息(电话,电子抄送...)。 My problem is how I can do this.I mean I know how create a list this is how I do: 我的问题是如何做到这一点,我的意思是我知道如何创建列表,这就是我的做法:

<ul>

        <?php foreach ($users as $item): ?>

            <li><a href=""><?php echo $item->name . " " . $item->surname; ?></a></li>

        <?php endforeach; ?>

</ul>

When I click on the link of name and surname I have to go to a page that contains details of that name and surname.How can I do this? 当我单击名称和姓氏链接时,我必须转到包含该名称和姓氏详细信息的页面,该怎么办? I have to pass name and surname to a controller from a view and do a query ? 我必须从视图中将名称和姓氏传递给控制器​​,然后执行查询?

I'm not sure how you have structured your controller and views, but you could use a structure such as this for users, where uid is the unique user id: 我不确定您如何构造控制器和视图,但是可以为用户使用这样的结构,其中uid是唯一的用户ID:

href="users.php?uid=123"

users.php would then use the GET variable uid to load the request the user information from your model and then communicate that information to your view. users.php然后将使用GET变量uid从模型中加载请求用户信息,然后将该信息传递给视图。 Reading in the GET variable would be like so: 读取GET变量将像这样:

if ( isset($_GET['uid']) ) {
    $uid = $_GET['uid'];
    // Load info from model and present in view
} else {
    // Handle invalid or unspecified users, perhaps show all available users
}

Once you have built the basic functionality you can employ Apache mod_rewrite rules such as Sureround has used to make the url pretty. 一旦构建了基本功能,就可以使用Apache mod_rewrite规则,例如Sureround曾使URL变得漂亮。

make your links like this: 使您的链接像这样:

 <li>
   <a href="/user/get_user_details/<?php echo $item->name; ?>">
     <?php echo $item->name . " " . $item->surname; ?>
   </a>
 </li>

this will go to the controller user, method get_user_details and will pass the $item->name as a parameter to the method. 这将转到控制器用户get_user_details方法,并将$ item-> name作为参数传递给该方法。 use this (or whatever property of item you pass) to show the details of that item. 使用它(或您传递的项目的任何属性)显示该项目的详细信息。

You should pass the unique ID for each name via the URL for example, "/users/getuser/1". 您应该通过URL为每个名称传递唯一的ID,例如“ / users / getuser / 1”。 If your controller is called "users" then you would create function called getuser and use $this->uri->segment(3) to retrieve the ID from the URL. 如果您的控制器称为“用户”,则可以创建名为getuser的函数,并使用$ this-> uri-> segment(3)从URL中检索ID。 YOu can then store that into a variable which you pass to your model to get the details for the record and then load your User detail view. 然后,您可以将其存储到变量中,然后将其传递给模型以获取记录的详细信息,然后加载“用户详细信息”视图。

function getuser()
{
  $userID = $this->uri->segment(3);

  // Then just load your model pass your $userID and load your view
}

In your href you would just output <?= "/users/getuser/" . $item->id; ?>

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

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