Code Comments
Programming Forum and web based access to our favorite programming groups.Hi One and all, Having a bit of a problem with text formatting in the code below I hope some one can tell / show me where I'm going wrong. Code 1 works ok, but as the server is down some times it makes the page hang.So I'm trying to put a time out i.e. will try for X seconds and give up load the page with out.Which is where code2 comings in,it seems to work but the text formating is all over the place. I can't see where I have gone wrong,but then I wouldn't if I fell over it. Where have I gone wrong! Thanks Chris Code1 http://molehole.info/test/code1.php <?php // Get data from audioscrobbler $file = @file("http://ws.audioscrobbler.com/txt/recent/jok er7"); // Check if file was readable if ($file === false) { echo "<p>I could not read file</p>\n"; } else { // Remove empty entry at beginning array_shift($file); // Create an empty track array $tracks = array(); // Get title and date from file, put them in track array // also, remove the linebreak at the end of each entry for ($i = 0, $x = count($file); $i < $x; $i+=2) { $track['title'] = rtrim($file[$i]); $track['date'] = rtrim($file[$i+1]); array_push($tracks, $track); } // Now that we've got the tracks in the $tracks array, // print a simple list of tracks echo "\n"; foreach ($tracks as $track) { printf("\t<font face=Verdana size=1 color=#6699CC><b>\n\t\t%s</b></font><br><font face=Times New Roman size=1 color=#72609F><i>%s<br>\n\t</i></font>\n", $track['title'], $track['date'] ); } echo "\n"; } ?> Code2 http://molehole.info/test/code2.php <?php // Get data from audioscrobbler $file = null; $fp = @fopen("http://ws.audioscrobbler.com/txt/recent/jo ker7", "r"); if($fp != null) { stream_set_timeout($fp, 8); $data = fread($fp, 100000); fclose($fp); $file = explode("n", $data); } // Check if file was readable if ($file == null) { echo "<p>could not read file</p>n"; } else { // Remove empty entry at beginning array_shift($file); // Create an empty track array $tracks = array(); // Get title and date from file, put them in track array // also, remove the linebreak at the end of each entry for ($i = 0, $x = count($file); $i < $x; $i+=2) { $track['title'] = trim($file[$i]); $track['date'] = trim($file[$i+1]); array_push($tracks, $track); } // Now that we've got the tracks in the $tracks array, // print a simple list of tracks echo "n"; foreach ($tracks as $track) { printf("t<font face=Verdana size=1 color=#6699CC><b>ntt%s</b></font><br><font face=Times New Roman size=1 color=#72609F><i>%s<br>nt</i></font>n", $track['title'], $track['date'] ); } echo "n"; } ?> -- Never argue with an idiot. They will drag you down to their level,and beat you with experience
Post Follow-up to this message"Joker7" <sat_ring@hotmail.com> kirjoitti viestissä:1113310471. a1a089a8a7b89cf101c019dd80f250a7@teranew s... > > > Hi One and all, > Having a bit of a problem with text formatting in > the code below I hope some one can tell / show me > where I'm going wrong. > Code 1 works ok, but as the server is down some > times it makes the page hang.So I'm trying to > put a time out i.e. will try for X seconds and > give up load the page with out.Which is where > code2 comings in,it seems to work but the text > formating is all over the place. > I can't see where I have gone wrong,but then I > wouldn't if I fell over it. > Where have I gone wrong! > > Thanks > Chris > > Code1 http://molehole.info/test/code1.php > > <?php > // Get data from audioscrobbler > $file = > @file("http://ws.audioscrobbler.com/txt/recent/jok > er7"); > > // Check if file was readable > if ($file === false) > { > echo "<p>I could not read file</p>\n"; > } > else > { > // Remove empty entry at beginning > array_shift($file); > > // Create an empty track array > $tracks = array(); > > // Get title and date from file, put them in track > array > // also, remove the linebreak at the end of each > entry > for ($i = 0, $x = count($file); $i < $x; $i+=2) > { > $track['title'] = rtrim($file[$i]); > $track['date'] = rtrim($file[$i+1]); > array_push($tracks, $track); > } > > // Now that we've got the tracks in the $tracks > array, > // print a simple list of tracks > echo "\n"; > foreach ($tracks as $track) > { > printf("\t<font face=Verdana size=1 > color=#6699CC><b>\n\t\t%s</b></font><br><font > face=Times New Roman size=1 > color=#72609F><i>%s<br>\n\t</i></font>\n", > $track['title'], > $track['date'] > ); > } > echo "\n"; > } > ?> > > Code2 http://molehole.info/test/code2.php > > <?php > // Get data from audioscrobbler > $file = null; > $fp = > @fopen("http://ws.audioscrobbler.com/txt/recent/jo > ker7", "r"); > if($fp != null) { > stream_set_timeout($fp, 8); > $data = fread($fp, 100000); > fclose($fp); > $file = explode("n", $data); A wild guess, but is this supposed to be a linebrake \n, not the character 'n'. If it splits text for each letter n, it would explain somewhat why the data is messed up. > } > > // Check if file was readable > if ($file == null) { > echo "<p>could not read file</p>n"; > } > else { > // Remove empty entry at beginning > array_shift($file); > > // Create an empty track array > $tracks = array(); > > // Get title and date from file, put them in track > array > // also, remove the linebreak at the end of each > entry > for ($i = 0, $x = count($file); $i < $x; $i+=2) { > $track['title'] = trim($file[$i]); > $track['date'] = trim($file[$i+1]); > array_push($tracks, $track); > } > > // Now that we've got the tracks in the $tracks > array, > // print a simple list of tracks > echo "n"; > foreach ($tracks as $track) { > printf("t<font face=Verdana size=1 > color=#6699CC><b>ntt%s</b></font><br><font > face=Times New Roman > size=1 color=#72609F><i>%s<br>nt</i></font>n", > $track['title'], > $track['date'] > ); > } > echo "n"; > } > ?> > > > -- > Never argue with an idiot. They will drag you down > to their level,and beat you with experience > > >
Post Follow-up to this message"Joker7" <sat_ring@hotmail.com> kirjoitti viestissä:1113310471. a1a089a8a7b89cf101c019dd80f250a7@teranew s... > > > Hi One and all, > Having a bit of a problem with text formatting in > the code below I hope some one can tell / show me > where I'm going wrong. > Code 1 works ok, but as the server is down some > times it makes the page hang.So I'm trying to > put a time out i.e. will try for X seconds and > give up load the page with out.Which is where > code2 comings in,it seems to work but the text > formating is all over the place. > I can't see where I have gone wrong,but then I > wouldn't if I fell over it. > Where have I gone wrong! > > Thanks > Chris > > Code1 http://molehole.info/test/code1.php > > <?php > // Get data from audioscrobbler > $file = > @file("http://ws.audioscrobbler.com/txt/recent/jok > er7"); > > // Check if file was readable > if ($file === false) > { > echo "<p>I could not read file</p>\n"; > } > else > { > // Remove empty entry at beginning > array_shift($file); > > // Create an empty track array > $tracks = array(); > > // Get title and date from file, put them in track > array > // also, remove the linebreak at the end of each > entry > for ($i = 0, $x = count($file); $i < $x; $i+=2) > { > $track['title'] = rtrim($file[$i]); > $track['date'] = rtrim($file[$i+1]); > array_push($tracks, $track); > } > > // Now that we've got the tracks in the $tracks > array, > // print a simple list of tracks > echo "\n"; > foreach ($tracks as $track) > { > printf("\t<font face=Verdana size=1 > color=#6699CC><b>\n\t\t%s</b></font><br><font > face=Times New Roman size=1 > color=#72609F><i>%s<br>\n\t</i></font>\n", > $track['title'], > $track['date'] > ); > } > echo "\n"; > } > ?> > > Code2 http://molehole.info/test/code2.php > > <?php > // Get data from audioscrobbler > $file = null; > $fp = > @fopen("http://ws.audioscrobbler.com/txt/recent/jo > ker7", "r"); > if($fp != null) { > stream_set_timeout($fp, 8); > $data = fread($fp, 100000); > fclose($fp); > $file = explode("n", $data); A wild guess, but is this supposed to be a linebrake \n, not the character 'n'. If it splits text for each letter n, it would explain somewhat why the data is messed up. > } > > // Check if file was readable > if ($file == null) { > echo "<p>could not read file</p>n"; > } > else { > // Remove empty entry at beginning > array_shift($file); > > // Create an empty track array > $tracks = array(); > > // Get title and date from file, put them in track > array > // also, remove the linebreak at the end of each > entry > for ($i = 0, $x = count($file); $i < $x; $i+=2) { > $track['title'] = trim($file[$i]); > $track['date'] = trim($file[$i+1]); > array_push($tracks, $track); > } > > // Now that we've got the tracks in the $tracks > array, > // print a simple list of tracks > echo "n"; > foreach ($tracks as $track) { > printf("t<font face=Verdana size=1 > color=#6699CC><b>ntt%s</b></font><br><font > face=Times New Roman > size=1 color=#72609F><i>%s<br>nt</i></font>n", > $track['title'], > $track['date'] > ); > } > echo "n"; > } > ?> > > > -- > Never argue with an idiot. They will drag you down > to their level,and beat you with experience > > >
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.