How does PHP read three XML data per page?

Source: Internet
Author: User
Tags cdata
The following code reads all the SHORTDESC fields of "9.xml":
-------------------------------------------------------------------------------
$xml _array=simplexml_load_file (' 9.xml '); Reads the data from the XML into the array object

foreach ($xml _array as $tmp) {
echo $tmp->shortdesc. "
";
}

?>
---------------------------------------------------------------------------------
Because the amount of data is very large, I want to achieve is only three data per page, when the "next" button, then the next three data display, until the full display of data, and then click the "Next" button, the page does not change. How to do it? Better have the code, thank you.


Reply to discussion (solution)

It should not be stored in XML and stored in the database.

It should not be stored in XML and stored in the database.
+1
Save the database, use limit.

If the database can exist, then the operation will be very convenient. If not, it is best to cache the parsed data first.

 
  

That's what you used to output.
foreach ($xml _array as $tmp) {
echo $tmp->shortdesc. "
";
}

It's going to be output like this.
$page = ($_get[' page ')-1) * 3;
for ($i = $page; $i < $page +3; $i + +) {
echo $xml [$i]->shortdesc.]
";
}

Can be detailed point, the level is limited, not very understanding AH!
That's what you used to output.
foreach ($xml _array as $tmp) {
echo $tmp->shortdesc. "
";
}

It's going to be output like this.
$page = ($_get[' page ')-1) * 3;
for ($i = $page; $i < $page +3; $i + +) {
echo $xml [$i]->shortdesc.]
";
}

Boss's code is clear enough.
Get array
$xml _array=simplexml_load_file (' 9.xml ');
Start with the first few. You're not asking for paging, you have to have a data start position and offset for paging.

So the starting position of this data is $page. Suppose you want the 2nd page, then pass in the parameter $_get[' page ' = = = 2
(2-1) is 3. This is the data obtained by the array key value, the array's key value starts from 0. So 0,1,2 is the first page, 3,4,5 is the second page
$page = ($_get[' page ')-1) * 3;
Loop 3 times, calculate the value of $i each time, respectively is 3, 4, 5. So that's the data that says the key value is 3,4,5.
for ($i = $page; $i < $page + 3; $i + +) {
echo $xml [$i]->shortdesc.]
";
}
The next page of the link passed in the parameter is $_get[' page ' + 1

This loop statement I have already understood, and now have a question: "$xml [$i]" How did it come? Is there a missing sentence? Please enlighten me, thank you!

Boss's code is clear enough.
Get array
$xml _array=simplexml_load_file (' 9.xml ');
Start with the first few. You're not asking for paging, you have to have a data start position and offset for paging.

So the starting position of this data is $page. Suppose you want the 2nd page, then pass in the parameter $_get[' page ' = = = 2
(2-1) is 3. This is the data obtained by the array key value, the key value of the array from ...

$xml [$i] is $xml _array[$i]. Pseudo code, which means the same thing.

$xml [$i] is $xml _array[$i]. Pseudo code, which means the same thing.

To simplify, I only display one page, I use the following code, neither result nor error hint:

---------------------------------------------------------------------------------------------
$xml _array=simplexml_load_file (' 9.xml '); Reads the data from the XML into the array object

$page = 3;
for ($i = $page; $i < $page +3; $i + +) {
echo $xml [$i]->shortdesc.]
";
}
?>
----------------------------------------------------------------------------------------
I use the following code, the error message is: "Warning:main () [function.main]: Cannot add element dataroot number 3 when only 0 such elements exist in D:\ Wwwroot\jupiter418app\wwwroot\temp\dx1.php on line 66 "
----------------------------------------------------------


$xml _array=simplexml_load_file (' 9.xml '); Reads the data from the XML into the array object

$page = 3;
for ($i = $page; $i < $page +3; $i + +) {
echo $xml _array[$i]->shortdesc. "
";
}
?>
-------------------------------------------------------------

Also please enlighten!

Because you have:
$xml _array=simplexml_load_file (' 9.xml '); Reads the data from the XML into the array object
That's why I have $xml _array[$i]->shortdesc an array.
In fact, in the structure returned by Simplexml_load_file, the first layer is not an array
Need to know your 9.xml specific content to know how to operate

Reference to the 8 floor of the reply:

$xml [$i] is $xml _array[$i]. Pseudo code, which means the same thing.


To simplify, I only display one page, I use the following code, neither result nor error hint:

---------------------------------------------------------------------------------------------
...... Stick out your XML code and look!

The 9.xml content is as follows:
--------------------------





sms



2012-3-23 14:32:30
http://www.282930. CN


sms



2012-3-23 14:32:30
http://www.csdn.ne T


sms



2012-3-23 14:32:30
http://www.csdn.ne T

...




That's your fault, you didn't provide the right information from the start.
Yours
foreach ($xml _array as $tmp) {
echo $tmp->shortdesc. "
";
}
does not output document content
That's what you can do.
foreach ($xml _array->news->new as $tmp) {
echo $tmp->shortdesc. "
";
}

And $xml _array->news->new is an array, so
$xml = $xml _array->news->new;
$page = min (3, COUNT ($xml)-3);
for ($i = $page; $i < $page +3; $i + +) {
echo $xml [$i]->shortdesc.]
";
}
It was established.

Thank you very much for your patient answers.

Don't wait, you stole my data, I changed the name directly!

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.