Jump to content

Recommended Posts

Is it possible to store results that are parsed from an xml file and then use them on a different web page? Basically I want to store the data within the nodes and then use that data on another web page. Kinda like adding pagination but instead of gettign the info from a db I am getting it from a xml file.

 

Is it possible with the script I have written?

<?php
  $g_books = array();
  $g_elem = null;
  
  function startElement( $parser, $name, $attrs ) 
  {
  global $g_books, $g_elem;
  if ( $name == 'CARD' ) $g_books []= array();
  $g_elem = $name;
  }
  
  function endElement( $parser, $name ) 
  {
  global $g_elem;
  $g_elem = null;
  }
  
  function textData( $parser, $text )
  {
  global $g_books, $g_elem;
  if ( $g_elem == 'CREDITCARD' || $g_elem == 'ISSUER' || $g_elem == 'LINK' || $g_elem == 'PICTUREHTMLCARD' || $g_elem == 'BA' || $g_elem == 'BB' || $g_elem == 'BC' || $g_elem == 'BD' || $g_elem == 'BE' || $g_elem == 'BF' || $g_elem == 'BG' || $g_elem == 'BH' || $g_elem == 'CARDTYPE' || $g_elem == 'INTRODUCTORYRATE' || $g_elem == 'TIMEPERIOD' || $g_elem == 'APPLIEDTO' || $g_elem == 'INTRODETAILS' || $g_elem == 'APRPURCHNUM' || $g_elem == 'ANNUAL')
  {
  $g_books[ count( $g_books ) - 1 ][ $g_elem ] = $text;
  }
  }
  
  $parser = xml_parser_create();
  
  xml_set_element_handler( $parser, "startElement", "endElement" );
  xml_set_character_data_handler( $parser, "textData" );
  
  $f = fopen( 'xml.xml', 'r' );
  
  while( $data = fread( $f, 4096 ) )
  {
  xml_parse( $parser, $data );
  }
  
  xml_parser_free( $parser );
  foreach( $g_books as $book )
  {
  if (trim($book['ISSUER'],'"') == 'Capital One'){
  echo "<table width=\"500\" border=\"3\" style=\"border-collapse:collapse;\" bordercolor=\"#999999\">
  <TR>
  <TD style=\"border-collapse:collapse; background:#999999; font-size:14px;\" valign=\"top\">
  <div style=\"padding-left:5px;\"><a href=".$book['LINK'].">".trim($book['CREDITCARD'],'"')."</a></div>
  <table width=\"500\" border=\"1\" style=\"border-collapse:collapse; font-size:12px;\" bordercolor=\"#FFFFFF\">
  <tr>
  <td bgcolor=\"#FFFFFF\" style=\"padding-top:5px; padding-left:10px;\" valign=\"top\">
  <a href=".$book['LINK']."><IMG border=\"0\" SRC=".trim($book['PICTUREHTMLCARD'],'"')."><br><br><img src=\"https://www.cardoffers.com/images/buttons/apply_black.gif\" border=\"0\"></a>
  </TD>
  <TD bgcolor=\"#FFFFFF\" style=\"padding-top:5px;\" valign=\"middle\"><ul>";
if (!empty($book['BA'])) {
  		echo "<li>".$book['BA']."</li>";
}
if (!empty($book['BB'])) {
	echo "<li>".$book['BB']."</li>";
}
if (!empty($book['BC'])) {
	echo "<li>".$book['BC']."</li>";
}
if (!empty($book['BD'])) {
	echo "<li>".$book['BD']."</li>";
}
if (!empty($book['BE'])) {
	echo "<li>".$book['BE']."</li>";
}
if (!empty($book['BF'])) {
	echo "<li>".$book['BF']."</li>";
}
if (!empty($book['BG'])) {
	echo "<li>".$book['BG']."</li>";
}
if (!empty($book['BH'])) {
	echo "<li>".$book['BH']."</li>";
}
echo "</ul>
</TD>
</TR>
</TABLE>
<table width=\"500\" border=\"1\" style=\"border-collapse:collapse; font-size:11px;\" bordercolor=\"#FFFFFF\">
			<tr>
				<td bgcolor=\"#999999\" align=\"center\">Intro APR</td>
				<td bgcolor=\"#999999\" align=\"center\">Intro APR Period</td>
				<td bgcolor=\"#999999\" align=\"center\">Regular APR</td>
				<td bgcolor=\"#999999\" align=\"center\">Annual Fee</td>
				<td bgcolor=\"#999999\" align=\"center\">Balance Transfers</td>
				<td bgcolor=\"#999999\" align=\"center\">Credit Needed</td>
			</tr>
			<tr>";
if (!empty($book['INTRODUCTORYRATE'])) { echo "<td bgcolor=\"#CCCCCC\" align=\"center\">".$book['INTRODUCTORYRATE']."%</td>"; } else { echo "<td bgcolor=\"#CCCCCC\" align=\"center\">Unknown</td>"; }
if (!empty($book['TIMEPERIOD'])) { echo "<td bgcolor=\"#CCCCCC\" align=\"center\">".$book['TIMEPERIOD']."</td>"; } else { echo "<td bgcolor=\"#CCCCCC\" align=\"center\">Unknown</td>"; }
if (!empty($book['APRPURCHNUM'])) { echo "<td bgcolor=\"#CCCCCC\" align=\"center\">".$book['APRPURCHNUM']."%</td>"; } else { echo "<td bgcolor=\"#CCCCCC\" align=\"center\">Unknown</td>"; }
if (!empty($book['ANNUAL'])) { echo "<td bgcolor=\"#CCCCCC\" align=\"center\">".$book['ANNUAL']."</td>"; } else { echo "<td bgcolor=\"#CCCCCC\" align=\"center\">Unknown</td>"; }
if (!empty($book['BALANCETRANSFER'])) { echo "<td bgcolor=\"#CCCCCC\" align=\"center\">".$book['BALANCETRANSFER']."</td>"; } else { echo "<td bgcolor=\"#CCCCCC\" align=\"center\">Unknown</td>"; }
if (!empty($book['CREDITNEEDED'])) { echo "<td bgcolor=\"#CCCCCC\" align=\"center\">".$book['CREDITNEEDED']."</td>"; } else { echo "<td bgcolor=\"#CCCCCC\" align=\"center\">Unknown</td>"; }
			echo "</tr>
		</table>
	</td>
</tr>
</table><br>";
  }
  }
  ?>

Link to comment
https://forums.phpfreaks.com/topic/72676-storing-results-for-later-use/
Share on other sites

Its a large XML file, so I guess I would be storing data in a db. To do that would I just use a foreach loop? Also this xml file is updated on a daily basis by a third party, how would I go about updating the data from the xml file to my db?

 

This is what I believe should work, correct me where I am wrong:

<?php
//xml parsed
foreach( $g_books as $book ) {
$query = mysql_query("SELECT issuer FROM tablename WHERE issuer = trim($book['ISSUER'],'\"')"); //checking if it exists
$doesitexist = mysql_num_rows($query);
if ($doesitexist > 0) {
//do some updating
} else {
//do some inserting
}
}
?>

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.