Jump to content

Creating a dynamic page ?id=


dustinto

Recommended Posts

I have info stored in an XML file and I want to parse it to a dynamic page (page.php?id={id})

XML file is setup like

<category id="something" name="name" address="bahh"...etc>
<category id="something" name="name" address="bahh"...etc>
<category id="something" name="name" address="bahh"...etc>

I don't want the data to be put into a table (like most XML stuff seems to do). I think I know how to get all this to work I just need help getting the dynamic part of the page to work.

I am guessing I am going to need some type of if statement, but I don't know where to start.

(if anyone knows of a good web site/tutorial on this that would be great.)

Thanks in advance
Link to comment
Share on other sites

Well, assuming that your file is setup

<category id="something" name="name" address="bahh"...etc>
<category id="something" name="name" address="bahh"...etc>
<category id="something" name="name" address="bahh"...etc>

with each <category /> on a separate line then you could read the contents of it like this:

[code]
<?php

$file = file("file.xml");
if (is_array($file)) {
  while(list($key,$val)=each($file)) {
    if ( strtolower(substr(trim($val), 0, 9)) == "<category" ) {
      $start = strpos($val, "id=\"")+4;
      $end = strpos($val, "\"", $start);
      $id[$key] = substr($val,  $start, $end);
      $start = strpos($val, "name=\"")+6;
      $end = strpos($val, "\"", $start);
      $name[$key] = substr($val, $start, $end);
      $start = strpos($val, "address=\"")+9;
      $end = strpos($val, "\"", $start);
      $addres[$key] = substr($val, $start, $end);
    }
  }
}

?>
[/code]

This still leaves a lot of room for error and may need to be adjusted slightly on where it begins and ends extracting your values, but this will give you the basic idea. I have not tested this code, it is just what I wrote on the fly, but it should do what you are looking for if I am not mistaken. It is supposed to give you three arrays which you can then sort in any manner you like and manipulate. If you need help with the manipulation or output of the arrays in some format, I would be happy to help.

Happy coding!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.