使用变量从PHP中获取XML数据

时间:2022-11-27 21:29:43

I started mixing XML with PHP today and I'm pretty bad at it, even though it looks super simple.

我今天开始将XML与PHP混合使用,虽然它看起来非常简单,但我还是很糟糕。

Right now, I'm trying to make something that sounds very easy but I can't understand how it works. I'm basically trying to create a fake mailbox for a game.

现在,我正在尝试制作一些听起来很简单的东西,但我无法理解它是如何工作的。我基本上是想为游戏创建一个假邮箱。

So I stored my emails in an XML file, classed by categories (received, sent, etc.). I managed to get the list of emails depending on the category, but I can't get to the part where I click on an email and it shows the content of this particular email.

所以我将我的电子邮件存储在一个XML文件中,按类别(收到,发送等)分类。我设法根据类别获取电子邮件列表,但我无法访问我点击电子邮件的部分,它显示了此特定电子邮件的内容。

Here is my simplified code:

这是我的简化代码:

XML :

<mailbox>

    <received>
        <expediter>James</expediter>
        <content>Blah blah blah</content>
    </received>

    <received>
        <expediter>Paul</expediter>
        <content>Bluh bluh bluh</content>
    </received>

    <sent>
        <expediter>Jack</expediter>
        <content>Blah blah blah</content>
    </sent>

    <sent>
        <expediter>John</expediter>
        <content>Bluh bluh bluh</content>
    </sent>

</mailbox>
XML;
?>

PHP :

<?php
    include 'emails.php';
    $emails = new SimpleXMLElement($xmlstr);

    $cat = $_GET['cat'];

    if(!isset($_GET['id'])){    
        $i = 0;
        foreach($emails->$cat as $mailbox){
            echo '<a href="?page=mailbox&cat='.$cat.'&id='.$i.'">'.$mailbox->expediter.'</a><br />';    
            $i++;
        }
    }
    else{
        $id = $_GET['id'];
        echo $emails->$cat[$id]->content;
    }
?>

So if there is no ID in the url, it shows the list of expediters with links to the email and if there is an ID in the url, it should show the content of the email designed by this number.

因此,如果网址中没有ID,则会显示包含指向电子邮件的链接的加速器列表,如果网址中有ID,则应显示由此编号设计的电子邮件的内容。

It works if I write manually :

如果我手动编写它可以工作:

echo $emails->received[1]->content;

But of course, I want that part to be dynamic and it doesn't work with :

但是,当然,我希望这部分是动态的,它不适用于:

echo $emails->$cat[$id]->content;

Is there any way to do that?

有没有办法做到这一点?

Thank you!

Camille

1 个解决方案

#1


0  

Try this:

$a = new stdClass();
$b = new stdClass();

$b->field = 5;
$a->list = array(
    1 => $b
);

print_r($a);
$param = 'list';
$id = 1;
print_r($a->list[1]->field);       // outputs 5;
print_r($a->{$param}[$id]->field); // outputs 5;

The key is:

关键是:

$a->{$param}[$id]->field  // notice the curly brackets.

Adapting to your question, you should use:

根据您的问题,您应该使用:

echo $emails->{$cat}[$id]->contenu;

As a good practice, you might want to check if it exists first:

作为一种好的做法,您可能想要先检查它是否存在:

if(isset($emails->{$cat}[$id])){
   // echo it here, after you know it exists
}

You can see it online at 3v4l example

您可以在3v4l示例中在线查看

#1


0  

Try this:

$a = new stdClass();
$b = new stdClass();

$b->field = 5;
$a->list = array(
    1 => $b
);

print_r($a);
$param = 'list';
$id = 1;
print_r($a->list[1]->field);       // outputs 5;
print_r($a->{$param}[$id]->field); // outputs 5;

The key is:

关键是:

$a->{$param}[$id]->field  // notice the curly brackets.

Adapting to your question, you should use:

根据您的问题,您应该使用:

echo $emails->{$cat}[$id]->contenu;

As a good practice, you might want to check if it exists first:

作为一种好的做法,您可能想要先检查它是否存在:

if(isset($emails->{$cat}[$id])){
   // echo it here, after you know it exists
}

You can see it online at 3v4l example

您可以在3v4l示例中在线查看