Page 1 of 1

PHP - > XML Frontend Help

Posted: Mon Dec 14, 2009 11:16 pm
by xodesign
Hello PHP experts,

I am a graphic designer working at a publishing company. We have recently had our PHP tech developer leave and I have done the silly thing of modifying a once working PHP frontend.

I was just curious if I could get some help in regards to fixing the issue. I have tried to narrow it down and I suspect it is simply a formatting issue. If anyone could give me some words of advice that would be great.
Any help would be greatly appreciated.

Here is the code:
INDEX.PHP
if (isset($_POST['save'])) {
$count = (count($_POST) - 1) / 4;

file_put_contents('data.xml', '<?xml version="1.0" encoding="ISO-8859-1"?>' . "\n" . '<ads>' . "\n");

for ($i = 1; $i <= $count; $i++) {
$image = trim(preg_replace('/[^\d\w\.:\-\/]/', '', $_POST['image-' . $i]));
$caption = trim(preg_replace('/[^\d\w\.:\-\/]/', '', $_POST['caption-' . $i]));
$destination = trim(preg_replace('/[^\d\w:\/\.\-_]/', '', $_POST['destination-' . $i]));
$site = trim(preg_replace('/[^\d\w:\/\.\-_]/', '', $_POST['site-' . $i]));


$str = '<banner image="' . $image . ' " caption="' . $caption . '" destination="' . $destination . '" site="' . $site . ' " />' . "\n";


file_put_contents('data.xml', $str, FILE_APPEND);
}

file_put_contents('data.xml', "</ads>", FILE_APPEND);
}

$xml = file_get_contents('data.xml');

$items = array();

if (preg_match_all('/<banner image="(.*?)" caption="(.*?)" destination="(.*?)" site="(.*?)"\/>/', $xml, $matches) > 0) {
$count = count($matches[0]);

for ($j = 0; $j < $count; $j++) {
$i = array();

$i['image'] = $matches[1][$j];
$i['caption'] = $matches[2][$j];
$i['destination'] = $matches[3][$j];
$i['site'] = $matches[4][$j];

$items[] = $i;
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-GB">
<head>
<title>300x250 House Ad Configuration</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jq ... ery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#preview').click(function () {
window.open('preview.php', 'preview', 'menubar=no,resizable=no,width=400,height=400,toolbar=no,status=no,location=no,scrollbars=no');
return false;
});

$('#add').click(function () {
count++;

$('#items div:last').before('<p id="item-' + count + '">\n<input type="text" name="image-' + count + '" size="15" value="" />\n<input type="text" name="caption-' + count + '" size="35" value="" />\n<input type="text" name="destination-' + count + '" size="35" value="" />\n<input type="text" name="site-' + count + '" size="10" value="" />\n<input type="button" id="remove-' + count + '" name="remove-' + count + '" value="Remove" />\n</p>');
$('#item-' + count).after('<scr' + 'ipt>$("#remove-' + count + '").click(function (){$("#item-' + count + '").remove();});</scr' + 'ipt>');
});
});
</script>
</head>
<body>
<h1>300x250 House Ad Configuration</h1>
<p id="header">
<input type="text" size="15" name="image" value="IMAGE FILENAME" readonly="readonly" />
<input type="text" size="35" name="caption" value="CAPTION TEXT" readonly="readonly" />
<input type="text" size="35" name="destination" value="DESTINATION URL" readonly="readonly" />
<input type="text" size="15" name="site" value="SITES" readonly="readonly" />
<input type="button" id="add" name="add" value="Add" />
</p>
<form id="items" action="" method="post">
<?php $count = 0; ?>
<?php foreach ($items as $i) : ?>
<?php $count++; ?>
<p id="item-<?php echo $count; ?>">
<input type="text" name="image-<?php echo $count; ?>" size="15" value="<?php echo $i['image']; ?>" />
<input type="text" name="caption-<?php echo $count; ?>" size="35" value="<?php echo $i['caption']; ?>" />
<input type="text" name="destination-<?php echo $count; ?>" size="35" value="<?php echo $i['destination']; ?>" />
<input type="text" name="site-<?php echo $count; ?>" size="15" value="<?php echo $i['site']; ?>" />
<input type="button" id="remove-<?php echo $count; ?>" name="remove-<?php echo $count; ?>" value="Remove" />
</p>
<script type="text/javascript">
$('#remove-<?php echo $count; ?>').click(function () {
$('#item-<?php echo $count; ?>').remove();
});
</script>
<?php endforeach; ?>
<script type="text/javascript">
var count = <?php echo $count; ?>;
</script>
<div>
<input type="submit" name="save" value="Save Changes" />
<input type="button" id="preview" name="preview" value="Open Preview" />
</div>
</form>
</body>
</html>


PREVIEW.PHP
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-GB">
<head>
<title>300x250 House Ad Configuration - Preview</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">body { text-align: center; }</style>
</head>
<body>
<div>
<object codebase="http://download.macromedia.com/pub/shoc ... on=8,0,0,0" width="300" height="250">
<param name="allowScriptAccess" value="sameDomain" />
<param name="allowFullScreen" value="false" />
<param name="movie" value="http://network.alluremedia.com.au/uploa ... 00x250.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
<embed src="http://network.alluremedia.com.au/uploa ... 00x250.swf" quality="high" bgcolor="#ffffff" width="300" height="250" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" />
</object>
</div>
<p><a href="preview.php">Refresh</a></p>
</body>
</html>

DATA.XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<ads>
<banner image="Microsoft.jpg" caption="Microsoft: Updates Have Not Caused Black Screen" destination="http://www.lifehacker.com.au/2009/12/mi ... ck-screen/" site="lif" />
<banner image="Childparentcomputer.jpg" caption="Why Is It Difficult For Parents To Re-connect With Their Kids?" destination="http://www.babble.com.au/2009/12/02/com ... ion-error/" site="bab" />
<banner image="RBBeetles.jpg" caption="The Ultimate PS3 Buyer's Guide" destination="http://www.kotaku.com.au/2009/11/the-pl ... ers-guide/" site="kot" />
<banner image="Keyboard.jpg" caption="Top 10 Aussie Google Searches Of 2009" destination="http://www.gizmodo.com.au/2009/12/the-t ... s-of-2009/" site="giz" />
<banner image="RBBeetles.jpg" caption="The Ultimate XB360 Buyer's Guide" destination="http://www.kotaku.com.au/2009/11/the-xb ... ers-guide/" site="kot" />
</ads>

Re: PHP - > XML Frontend Help

Posted: Mon Dec 14, 2009 11:29 pm
by requinix
How about telling us what's wrong? In as much detail as you can.

Re: PHP - > XML Frontend Help

Posted: Tue Dec 15, 2009 12:07 am
by xodesign
Sorry about that. It seems that the Parsing of the XML doesn't populate the input fields. The Preview.php displays the object correctly so I assume the problem does not reside there.