简体   繁体   English

从函数调用变量并在文本字段oop php中显示

[英]Call a variable from function and display in text field oop php

i want to show user specific data in html form(in text fields or in select list) 我想以html形式显示用户特定的数据(在文本字段或选择列表中)

I have a function ShowUserInformation() in class MyClass: 我在类MyClass中有一个函数ShowUserInformation():

function ShowUserInformation()
{
    $query = "SELECT id, name, email FROM saloni WHERE id = '$_SESSION[ID_korisnika]'";
    $result = mysql_query($query);
    while($row=mysql_fetch_array($result)):
        $id= $row['id'];
        $name= $row['name'];
        $email = $row['email'];
        $address= $row['address'];
        $address2= $row['address2'];
        $address3= $row['address3'];
    endwhile;   

    return $result;
}   

My question is: How can i display value of $name, or $email, $id... on another page in text box or in select list? 我的问题是:如何在文本框或选择列表的另一页上显示$ name或$ email,$ id ...的值?

If i do it in procedural way it works when i do this: 如果我以程序方式进行操作,则在执行此操作时会起作用:

<input type="text" value="<?php echo $name ?>" name="name" class="" />

But, how can i display the $name,$email,$ID... in oop way? 但是,如何以oop方式显示$ name,$ email,$ ID ...? Is there a way to call it directly and not declare it as class variable and then call it. 有没有一种方法可以直接调用它,而不将其声明为类变量,然后再调用它。 i've included file, created object... 我已经包含了文件,创建了对象...

$my_class = new MyClass; //create an object of class

HTML - i've tried something like this... HTML-我已经尝试过类似的事情...

<input type="text" value="<?php echo $my_class->ShowUserInformation($name)?>" name="name" class="" />

I'm new in PHP and oop so be easy with me :) 我是PHP和oop的新手,所以对我来说很容易:)

Thank you 谢谢

If you only plan on one row being returned, then why not use mysql_fetch_assoc() 如果只计划返回一行,那为什么不使用mysql_fetch_assoc()

class MyClass{
    public function GetUserInformation(){
        $query = "SELECT id, name, email FROM saloni WHERE id = '$_SESSION[ID_korisnika]'";
        $result = mysql_query($query);
        $info = mysql_fetch_assoc($result);
        return $info;
    } 
}

$class = new MyClass;
$info = $class->GetUserInformation();?>

<input type="text" value="<?php echo $info['id']?>" name="id" class="" />
<input type="text" value="<?php echo $info['name']?>" name="name" class="" />

Note: mysql_* functions are deprecated, and you should move to use MySQLi or PDO 注意:不建议使用mysql_ *函数,您应该使用MySQLi或PDO

First, change the function to get the data: 首先,更改函数以获取数据:

function ShowUserInformation()
{
    // Assuming you need only one user, I have set "LIMIT" to "1"
    $query = "SELECT id, name, email, address, address2, address3 FROM saloni WHERE id = '$_SESSION[ID_korisnika]' LIMIT 1";
    $result = mysql_query($query);

    return mysql_fetch_array($result);
}

Now, get the information: 现在,获取信息:

$my_class = new MyClass;
$userData = $my_class->ShowUserInformation();

// HTML
<input type="text" value="<?php echo $userData['name']; ?>" name="name" class="" />

kindly try this: 请尝试以下方法:

<?php  
$show_info=ShowUserInformation();  

$data = $show_info->fetchAll(PDO::FETCH_ASSOC);  
foreach($data as $row){ ?>
<input type="text" value="<?php  $row['name']; ?>" name="name" class="" />
<?php } ?>

OR 要么

<?php $show_info=ShowUserInformation();  
while ($row= mysql_fetch_assoc($show_info){ ?>
<input type="text" value="<?php  $row['name']; ?>" name="name" class="" />
<?php } ?>

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

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