arcanine Posted May 24, 2008 Share Posted May 24, 2008 hi there, I'm running a forum (smf) and decided I wanted to have a coverflow application http://dinogod.adrianhosting.com/smf/index.php?action=showcase you can see in action above, although there is a problem with it the links to the pictures are hardcoded so if I wanted a new image I have to recompile it in flex builder 3 I've asked around in flex forums as to a function to scan the image directory for new images but it has to be done in php then flex can consume it in xml format So on to my php problem I'm using a code snippet from phparadise which lists all images in my directory and echos each filename per line you can see the script live here http://dinogod.adrianhosting.com/smf/coverflow/list.php the problem is flex won't consume a standard php script so I need help in generating a xml feed from script for reference here is the source of the script <?php $imgdir = 'img/'; // the directory, where your images are stored $allowed_types = array('png','jpg','jpeg','gif'); // list of filetypes you want to show $dimg = opendir($imgdir); while($imgfile = readdir($dimg)) { if(in_array(strtolower(substr($imgfile,-3)),$allowed_types)) { $a_img[] = $imgfile; sort($a_img); reset ($a_img); } } $totimg = count($a_img); // total image number for($x=0; $x < $totimg; $x++) { $size = getimagesize($imgdir.'/'.$a_img[$x]); // do whatever $halfwidth = ceil($size[0]/2); $halfheight = ceil($size[1]/2); echo "$a_img[$x] <br />"; } ?> I've tried learning how to do this myself (on wc3.org) but either nothing shows at all, or xml complains at me furiously can someone take me through it, I'm not a php programmer by trade and need clear guidance, or an example thanks for your time Quote Link to comment https://forums.phpfreaks.com/topic/107059-converting-a-php-file-to-a-xml-feed/ Share on other sites More sharing options...
RichardRotterdam Posted May 24, 2008 Share Posted May 24, 2008 I dont think feed is the correct term. All you need is to use php to create an xml. The file extention would still be php only it would have xml data in it so flex can read the file. i dunno what that last forloop does but replace this <? for($x=0; $x < $totimg; $x++) { $size = getimagesize($imgdir.'/'.$a_img[$x]); // do whatever $halfwidth = ceil($size[0]/2); $halfheight = ceil($size[1]/2); echo "$a_img[$x] <br />"; } ?> with <gallery> <?php foreach($a_img as $imageUrl){ ?> <image> <url><?php echo($imageUrl) ?></url> </image> <?php } ?> </gallery> well something like that it really depends on how you want your xml to look its best to use a static xml first. Quote Link to comment https://forums.phpfreaks.com/topic/107059-converting-a-php-file-to-a-xml-feed/#findComment-548828 Share on other sites More sharing options...
arcanine Posted May 24, 2008 Author Share Posted May 24, 2008 Thanks for your reply I tried your suggestion and the php file becomes blank so I did a little testing it seems $x needs to be echoed for the filename to come out I tried echoing "$a_img" on its own and as the output I got Array Array but no file names just the word array, so $a_img and $x need to be merged or something to make your script work how would I go about doing this because I see you've changed $a_img to $imageUrl but it doesn't have all the information in that string also some of that code can be ignored the original script would output image size and other details I didn't need so I changed the echo however some of the original scripting will be in there thanks again for your help I appreciate it Quote Link to comment https://forums.phpfreaks.com/topic/107059-converting-a-php-file-to-a-xml-feed/#findComment-548845 Share on other sites More sharing options...
arcanine Posted May 24, 2008 Author Share Posted May 24, 2008 okay I've been working non stop on this but not really getting anywhere here is where I'm at thus far <?php //define the xml standard header("Content-type: text/xml"); echo "<?xml version='1.0' encoding='ISO-8859-1'?>"; ?> <?php //this section calculates the image names in the directory $imgdir = 'img/'; // the directory, where your images are stored $allowed_types = array('png','jpg','jpeg','gif'); // list of filetypes you want to show $dimg = opendir($imgdir); while($imgfile = readdir($dimg)) { if(in_array(strtolower(substr($imgfile,-3)),$allowed_types)) { $a_img[] = $imgfile; sort($a_img); reset ($a_img); } } $totimg = count($a_img); for($x=0; $x < $totimg; $x++) { $size = getimagesize($imgdir.'/'.$a_img[$x]); ?> <?php //echo xml tree echo "<gallery>"; echo "<image>"; echo "<url>$a_img[$x]"; echo "</url>"; echo "</image>"; echo "</gallery>"; } ?> whichoutputs <?xml version='1.0' encoding='ISO-8859-1'?><gallery><image><url>11.jpg</url></image></gallery><gallery><image><url>12.jpg</url></image></gallery> which is of course wrong I need the url tags to repeat not gallery and images you can see the errors for yourself at http://dinogod.adrianhosting.com/smf/coverflow/list.php how can I say only print gallery and image tags once but the url tags as many times as the string needs Quote Link to comment https://forums.phpfreaks.com/topic/107059-converting-a-php-file-to-a-xml-feed/#findComment-548863 Share on other sites More sharing options...
wildteen88 Posted May 24, 2008 Share Posted May 24, 2008 Try: <?php //define the xml standard header("Content-type: text/xml"); echo "<?xml version='1.0' encoding='ISO-8859-1'?>"; //this section calculates the image names in the directory $imgdir = 'img/'; // the directory, where your images are stored $allowed_types = array('png','jpg','jpeg','gif'); // list of filetypes you want to show $dimg = opendir($imgdir); $url = 'http://' . $_SERVER['HTTP_HOST'] . '/' . $imgdir; echo "<gallery> <image>\n"; while($imgfile = readdir($dimg)) { if(in_array(strtolower(substr($imgfile,-3)),$allowed_types)) { echo ' <url>' $url . $imgfile . "</url>\n"; } } echo ' </image> <gallery>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/107059-converting-a-php-file-to-a-xml-feed/#findComment-548880 Share on other sites More sharing options...
arcanine Posted May 24, 2008 Author Share Posted May 24, 2008 Thanks for your help but the script generates nothing not even in the source code :S I've uploaded your script to http://dinogod.adrianhosting.com/smf/coverflow/test.php so you can see for yourself I'm really confused as to why it won't echo anything :S Quote Link to comment https://forums.phpfreaks.com/topic/107059-converting-a-php-file-to-a-xml-feed/#findComment-548891 Share on other sites More sharing options...
BlueSkyIS Posted May 24, 2008 Share Posted May 24, 2008 you're probably not seeing the parse error: parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in /Users/lesbrown/Sites/site1/test.php on line 21 do you have error_reporting turned on? Quote Link to comment https://forums.phpfreaks.com/topic/107059-converting-a-php-file-to-a-xml-feed/#findComment-548895 Share on other sites More sharing options...
arcanine Posted May 24, 2008 Author Share Posted May 24, 2008 I imagine I don't if I'm not seeing anything I don't know how to find out whether error_reporting is on or off as I've mentioned I don't know php :-\ your error does mention line 21 though which is echo ' <url>' $url . $imgfile . "</url>\n"; does anything look wrong there? Quote Link to comment https://forums.phpfreaks.com/topic/107059-converting-a-php-file-to-a-xml-feed/#findComment-548904 Share on other sites More sharing options...
wildteen88 Posted May 24, 2008 Share Posted May 24, 2008 I missed a period from my code, I forgot to correct it before I posted the code. Fixed code <?php //define the xml standard header("Content-type: text/xml"); echo "<?xml version='1.0' encoding='ISO-8859-1'?>"; //this section calculates the image names in the directory $imgdir = 'img/'; // the directory, where your images are stored $allowed_types = array('png','jpg','jpeg','gif'); // list of filetypes you want to show $dimg = opendir($imgdir); $url = 'http://' . $_SERVER['HTTP_HOST'] . '/' . $imgdir; echo "<gallery> <image>\n"; while($imgfile = readdir($dimg)) { if(in_array(strtolower(substr($imgfile,-3)),$allowed_types)) { echo ' <url>' . $url . $imgfile . "</url>\n"; } } echo ' </image> <gallery>'; ?> If you still get a blank screen then you should turn on display_errors Quote Link to comment https://forums.phpfreaks.com/topic/107059-converting-a-php-file-to-a-xml-feed/#findComment-548907 Share on other sites More sharing options...
arcanine Posted May 24, 2008 Author Share Posted May 24, 2008 oh right, thank you for all this by the way the corrected script is at http://dinogod.adrianhosting.com/smf/coverflow/test.php I get XML Parsing Error: no element found Location: http://dinogod.adrianhosting.com/smf/coverflow/test.php Line Number 6, Column 10: looking at the source the output is <?xml version='1.0' encoding='ISO-8859-1'?><gallery> <image> <url>http://dinogod.adrianhosting.com/img/11.jpg</url> <url>http://dinogod.adrianhosting.com/img/12.jpg</url> </image> <gallery> which to me looks right, can I ignore that xml error and start trying to call the xml file to my flex project? or is it not valid yet? Quote Link to comment https://forums.phpfreaks.com/topic/107059-converting-a-php-file-to-a-xml-feed/#findComment-548911 Share on other sites More sharing options...
wildteen88 Posted May 24, 2008 Share Posted May 24, 2008 This: echo "<gallery> <image>\n"; should be: echo "\n<gallery> <image>\n"; Also this: echo ' </image> <gallery>'; should be: echo ' </image> </gallery>'; Quote Link to comment https://forums.phpfreaks.com/topic/107059-converting-a-php-file-to-a-xml-feed/#findComment-548919 Share on other sites More sharing options...
arcanine Posted May 24, 2008 Author Share Posted May 24, 2008 Thanks a lot just what I needed I'll be sure to bookmark this forum in google bookmarks Final script just in case anyone wanted something similar <?php //define the xml standard header("Content-type: text/xml"); echo "<?xml version='1.0' encoding='ISO-8859-1'?>"; //this section calculates the image names in the directory $imgdir = 'img/'; // the directory, where your images are stored $allowed_types = array('png','jpg','jpeg','gif'); // list of filetypes you want to show $dimg = opendir($imgdir); $url = 'http://' . $_SERVER['HTTP_HOST'] . '/' . $imgdir; echo "\n<gallery> <image>\n"; while($imgfile = readdir($dimg)) { if(in_array(strtolower(substr($imgfile,-3)),$allowed_types)) { echo ' <url>' . $imgfile . "</url>\n"; } } echo ' </image> </gallery>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/107059-converting-a-php-file-to-a-xml-feed/#findComment-548922 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.