Jump to content

PHP and XML


westchester

Recommended Posts

I'm making this site for a local high school hockey team. They want to be able to update the scores and schedule themselves using a form. Their hosting has PHP, but not MYSQL. So I thought to use XML instead. I've never used XML with PHP before I've only used MySQL. So I've been searching around to find guides and tutorials, and I searched around here a little so I've got this:

 

 

to display the schedule and scores I also want to order them by the date

 

<table>

 

  <tr>

      <th>Date</th>

      <th>Rink</th>

      <th>Time</th>

      <th>Opposing Team</th>

      <th>Home Score</th>

      <th>Away Score</th>

<?php if (isEdit) echo "<th> </th>"; ?>

  </tr>

<?php

  $dom = new DOMDocument();

  $dom->load('games.xml');

 

  foreach ($dom->getElementsByTagname('game') as $element) {

      $id = $element->getAttribute('id');

 

      foreach (($element->childNodes) as $e) {

          if (is_a($e, 'DOMElement')) {

              if ($e->tagName == 'date') {

                  $date = $e->textContent;

              } elseif ($e->tagName == 'rink') {

                  $rink = $e->textContent;

              } elseif ($e->tagName == 'time') {

                  $time = $e->textContent;

              } elseif ($e->tagName == 'team') {

                  $team = $e->textContent;

              } elseif ($e->tagName == 'home') {

                  $home = $e->textContent;

              } elseif ($e->tagName == 'away') {

                  $away = $e->textContent;

              }

          }

      }

 

      echo "    <tr>\r" .

              "        <td>$date</td>\r" .

              "        <td>$rink</td>\r" .

              "        <td>$time</td>\r" .

              "        <td>$team</td>\r" .

              "        <td>$home</td>\r" .

              "        <td>$away</td>\r";

      if (isEdit) echo "        <td><a href=\"edit.php?id=$id\">Edit</a></td>\r";

      echo "    </tr>\r\r";

  }

?>

 

if someone with a username is using it it lets them edit the schedule or add a new game. When they click on edit it opens the edit page and sends the game id. Again I'm from a MySQL background and would query for the game with that id, but have no idea how to do it with XML.

 

On the edit page there is a form to edit and create games. I've been able to create a script to create a new game, but I don't know how to edit it.

 

<?php

  $dom new DOMDocument();

  $dom->load('games.xml');

  $game = $dom->createElement('game');

  $game->setAttribute('id', ' '); // not sure how to generate the next id  - it would have to be one greater than the last one

  $game->setAttribute('tstamp', mktime(0,0,0,$_POST['month'],$_POST['day'],$_POST['year'])); // i was going to use this to order the games ny date later when they're displayed

 

  $date = $dom->createElement('date');

  $datetext = $dom->createTextNode(date("D M j",mktime(0,0,0,$_POST['month'],$_POST['day'],$_POST['year'])));

  $date->appendChild($datetext);

 

  $rink = $dom->createElement('rink');

  $rinktext = $dom->createTextNode($_POST['rink']);

  $rink->appendChild($rinktext);

 

  $time = $dom->createElement('time');

  $timetext = $dom->createTextNode($_POST['hour'] . ":" . $POST['min'] . " " . $_POST['am_pm']);

  $time->appendChild($timetext);

 

  $team = $dom->createElement('team');

  $teamtext = $dom->createTextNode($_POST['team']);

  $team->appendChild($teamtext);

 

  $home = $dom->createElement('home');

  $hometext = $dom->createTextNode($_POST['home']);

  $home->appendChild($hometext);

 

  $away = $dom->createElement('away');

  $awaytext = $dom->createTextNode($_POST['away']);

  $away->appendChild($awaytext);

 

  $game->appendChild($date);

  $game->appendChild($rink);

  $game->appendChild($time);

  $game->appendChild($team);

  $game->appendChild($home);

  $game->appendChild($away);

 

  $dom->documentElement->appendChild($game);

  $dom->save('games.xml');

?>

 

So basically I need to figure out how to edit the xml given a game id, also delete a game entirely, and order the games in the schedule( again if it where MySQL I would just ORDER BY tstamp) using tstamp incase the add a game that goes in between.

 

Thanks,

Any help would be great!

Maybe if I figure this stuff out I'll write a tutorial 

Link to comment
https://forums.phpfreaks.com/topic/38927-php-and-xml/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.