稍后返回? PHP查看文件

发布于 2024-10-14 19:54:56 字数 738 浏览 2 评论 0原文

假设我有一个像这样构建的视图文件:

<html>
...
   <title><?= Functions::Text('title'); ?></title>
....
<body>

....
<?= Functions::Text('sometext'); ?>

</body>
</html>

Functions::Text - 将在表 texts 中为我提供一个数据库条目,其中包含标题和某些文本的 search_string

我想立即提取数据,而不是按请求提取数据(这意味着 - 收集给予文本的字符串数组(这并不难) - 但我希望数据在选择查询之后转到请求数据的确切位置

意味着 -

$query = select ... ;
... fetch ...
$results_of_fetch = array ('title'=>'Welcome!','sometext' => 'sometext!!');

以及视图文件 -

<html>
...
   <title>Welcome!</title>
....
<body>

....
sometext!!

</body>
</html>

Lets say I have a view file that is built like this:

<html>
...
   <title><?= Functions::Text('title'); ?></title>
....
<body>

....
<?= Functions::Text('sometext'); ?>

</body>
</html>

Functions::Text - would give me a db entry in table texts with search_string of title and sometext.

I want to pull out the data at once, and not per request (which mean - to collect an array of strings given to Texts (which is not that hard) - but I want the data, after the select query, to go to the exact places which requested the data.

which mean -

$query = select ... ;
... fetch ...
$results_of_fetch = array ('title'=>'Welcome!','sometext' => 'sometext!!');

And the view file -

<html>
...
   <title>Welcome!</title>
....
<body>

....
sometext!!

</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清醇 2024-10-21 19:54:56

我认为你的问题与对象而不是 MVC 更相关。

所以,我想提出建议。

如果必须多次重用对象,请不要使用静态方法
通过有效地使用非静态方法,您不必一遍又一遍地查询数据库。

//create an object that takes parameter
//from a constructor or some other public method

class Function{

    public $title;
    public $text;
    public $footer;

    function _construct($id)
    {
    $conn = mysql_connect("localhost", "mysql_user", "mysql_password");
    if (!$conn) {
        echo "Unable to connect to DB: " . mysql_error();
        exit;
    }

    if (!mysql_select_db("mydbname", $con)) {
        echo "Unable to select mydbname: " . mysql_error();
        exit;
    }

    $sql = "SELECT * FROM table1 WHERE id=".$id." LIMIT 1";
    //if you are using id, then don't forget to add limit 1


    $result = mysql_query($sql);

    if (!$result) {
        echo "Could not successfully run query ($sql) from DB: " . mysql_error();
        exit;
    }

    if (mysql_num_rows($result) == 0) {
        echo "No rows found, nothing to print so am exiting";
        exit;
    }

    $row = mysql_fetch_assoc($result);
    //alternatively you can add loop check it at php manual
    $this->title = $row['title'];
    $this->text = $row['text'];
    $this->footer = $row['footer'];
    }
}

在您的布局(或视图)文件中

//the first thing you need to do is instantiate an object
//don't use static method if you are reusing object again

<?php
    $function = new Function($id); 
            //pass some id or other parameter
     ?>

 <html>
...
   <title>
         <?= $function->title; ?>
        <!-- alternatively you can do with some method also -->
        </title> 
....
<body>

....
<?= $function->text; ?>

</body>
</html>

,我可能不理解您的必要性,您可以发表评论,并请检查您的问题。

I think your question is more related to Object rather than MVC.

So, i would like to make suggestion.

Don't use static method if you have to reuse object more that one time.
By using non static method efficiently, you don't have to query database over and over again.

//create an object that takes parameter
//from a constructor or some other public method

class Function{

    public $title;
    public $text;
    public $footer;

    function _construct($id)
    {
    $conn = mysql_connect("localhost", "mysql_user", "mysql_password");
    if (!$conn) {
        echo "Unable to connect to DB: " . mysql_error();
        exit;
    }

    if (!mysql_select_db("mydbname", $con)) {
        echo "Unable to select mydbname: " . mysql_error();
        exit;
    }

    $sql = "SELECT * FROM table1 WHERE id=".$id." LIMIT 1";
    //if you are using id, then don't forget to add limit 1


    $result = mysql_query($sql);

    if (!$result) {
        echo "Could not successfully run query ($sql) from DB: " . mysql_error();
        exit;
    }

    if (mysql_num_rows($result) == 0) {
        echo "No rows found, nothing to print so am exiting";
        exit;
    }

    $row = mysql_fetch_assoc($result);
    //alternatively you can add loop check it at php manual
    $this->title = $row['title'];
    $this->text = $row['text'];
    $this->footer = $row['footer'];
    }
}

And in you layout(or view) file

//the first thing you need to do is instantiate an object
//don't use static method if you are reusing object again

<?php
    $function = new Function($id); 
            //pass some id or other parameter
     ?>

 <html>
...
   <title>
         <?= $function->title; ?>
        <!-- alternatively you can do with some method also -->
        </title> 
....
<body>

....
<?= $function->text; ?>

</body>
</html>

And I might not be understanding your necessity, you can comment, and please review your question.

想你的星星会说话 2024-10-21 19:54:56

您可以使用 AJAX 和 jQuery 来完成此操作。 jQuery,这样就容易多了。

jQuery

    $.post("fetchstuff.php", function(data){
       document.title = data.title;
       $("#txt1").html(data.sometext);
    });

fetchstuff.php
    <?php
        $sometext = "sometext";
        $title = "sometitle"
    ?>

完全未经测试,但输出应该如下所示。

    <title>sometitle</title>
    ....
    <div id="txt1">sometext</div>

You can do this with AJAX and jQuery. jQuery, so that it'll be much easier.

jQuery

    $.post("fetchstuff.php", function(data){
       document.title = data.title;
       $("#txt1").html(data.sometext);
    });

fetchstuff.php
    <?php
        $sometext = "sometext";
        $title = "sometitle"
    ?>

Totally untested but output should look like this.

    <title>sometitle</title>
    ....
    <div id="txt1">sometext</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文