bloodgoat Posted March 1, 2009 Share Posted March 1, 2009 I thought it was pretty straightforward... at least it was in my head, but once again, my novice is shining through, and it doesn't help I haven't done anything in PHP for the last 5 years save for the last month. Very rusty, to say the least. This is my first of probably many help threads I'll have. Basically, I wanted to assign the variables through my array and echo them respectively through my for() statement to produce automatic, alternating rows when adding or subtracting entries from my three arrays. $artist = array("Amen Ra", "Anaal Nathrakh", "Eminem", "Hellas Mounds"); $album = array("Mass I: Prayer I-VI", "The Codex Necro", "Marshall Mathers LP", "The Last Ferry to Cydonia"); $url = array("amen1", "anaal1", "eminem1", "hellas1"); for($i=0;$i<count($artist);$i++){ $tables = "<tr> <td align=\"right\" width=\"20%\"> $artist[$i] </td> <td align=\"right\" width=\"30%\"> <a href=\"$url[$i]\">$album[$i]</a> </td> <td align=\"right\" width=\"20%\"> $artist[$i] </td> <td align=\"right\" width=\"30%\"> <a href=\"?music=$url[$i]\">$album[$i]</a> </td> </tr>"; if(!$i){ die("Nothing here yet! "); } } But it just keeps dying. When I was fiddling around trying to see if the arrays even supposedly had any output, echoing anywhere other than immediately after the array just produced a null result which got me to thinking it might be a problem with order of operations, but again, I'm very rusty (and was never even that good to begin with lol). So, where am I screwing up? Quote Link to comment https://forums.phpfreaks.com/topic/147445-solved-echoing-arrays-through-a-for-statement/ Share on other sites More sharing options...
dad00 Posted March 1, 2009 Share Posted March 1, 2009 right from what i know <td align=\"right\" width=\"20%\"> $artist[$i] </td> <td align=\"right\" width=\"30%\"> <a href=\"$url[$i]\">$album[$i]</a> </td> <td align=\"right\" width=\"20%\"> $artist[$i] </td> <td align=\"right\" width=\"30%\"> <a href=\"?music=$url[$i]\">$album[$i]</a> </td> </tr> wont work unless its echoed because php thinks its php code Quote Link to comment https://forums.phpfreaks.com/topic/147445-solved-echoing-arrays-through-a-for-statement/#findComment-773886 Share on other sites More sharing options...
wildteen88 Posted March 1, 2009 Share Posted March 1, 2009 This if(!$i){ die("Nothing here yet! "); will stop your loop at the first iteration as $i will be set to 0 which means false. Also you'll need to echo your $table variable for any output to be displayed. Quote Link to comment https://forums.phpfreaks.com/topic/147445-solved-echoing-arrays-through-a-for-statement/#findComment-773890 Share on other sites More sharing options...
bloodgoat Posted March 1, 2009 Author Share Posted March 1, 2009 This if(!$i){ die("Nothing here yet! "); will stop your loop at the first iteration as $i will be set to 0 which means false. Also you'll need to echo your $table variable for any output to be displayed. Actually, I didn't even put that in until after the first half-hour or so of tinkering around (and it still not working), but thanks for letting me know, I'll remove it. Also, even when I assign the table columns/rows code to it's own variable, I'm still not able to retrieve the variables from any of my arrays. I'm being a real idiot here... I can almost feel what I'm doing wrong but I'm not quite there on my own. Like having a word on the tip of your tongue but you can't quite recall what it is... Quote Link to comment https://forums.phpfreaks.com/topic/147445-solved-echoing-arrays-through-a-for-statement/#findComment-773894 Share on other sites More sharing options...
wildteen88 Posted March 1, 2009 Share Posted March 1, 2009 Your code should work fine as it is. Are you sure you're echoing the $table variable after your loop. Quote Link to comment https://forums.phpfreaks.com/topic/147445-solved-echoing-arrays-through-a-for-statement/#findComment-773899 Share on other sites More sharing options...
bloodgoat Posted March 1, 2009 Author Share Posted March 1, 2009 Here's the entire code for my script as it is now, maybe it can shed some light: <? $artist = array("Amen Ra", "Anaal Nathrakh", "Eminem", "Hellas Mounds"); $album = array("Mass I: Prayer I-VI", "The Codex Necro", "Marshall Mathers LP", "The Last Ferry to Cydonia"); $url = array("amen1", "anaal1", "eminem1", "hellas1"); $header = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"; $footer = "\n</table>"; $tables = "\n <tr> <td align=\"right\" width=\"20%\"> $artist[$i] </td> <td align=\"right\" width=\"30%\"> <a href=\"$url[$i]\">$album[$i]</a> </td> <td align=\"right\" width=\"20%\"> $artist[$i] </td> <td align=\"right\" width=\"30%\"> <a href=\"?music=$url[$i]\">$album[$i]</a> </td> </tr>"; echo $header; for($i=0;$i<count($artist);$i++){ echo $tables; } echo $footer; ?> Of course, this could just be the server once again. I have two (ugh) free-host servers I work my scripts on and generally both have conflict with each other. My content-include script works fine on A but returns either all null or just include errors on B. And another of my scripts works fine on B but has a lot of conflict with A. Unfortunately, A is non-responsive for me right now so I can't work on there. (God, I wish I had an actual host.) Quote Link to comment https://forums.phpfreaks.com/topic/147445-solved-echoing-arrays-through-a-for-statement/#findComment-773904 Share on other sites More sharing options...
wildteen88 Posted March 1, 2009 Share Posted March 1, 2009 Use full PHP tags (<?php ?>). Short tags (<? ?> or <?= ?>) are normally disabled. Also you can develop/test your scripts locally if you installed what is known as the AMP stack (Apache, PHP and MySQL). That way you don't have to use a host to test your scripts. You can either install Apache, PHP and MySQL yourself or install preconfigured packages such as WAMP (for Windows) or XAMPP (for Windows, *nix or Mac). Quote Link to comment https://forums.phpfreaks.com/topic/147445-solved-echoing-arrays-through-a-for-statement/#findComment-773914 Share on other sites More sharing options...
bloodgoat Posted March 1, 2009 Author Share Posted March 1, 2009 I've been wondering about installing to test locally but I wasn't aware of what I needed, thanks for the tip. And aside from the full tags tidbit, is there anything noticeably wrong that would be causing the problems I'm receiving? Quote Link to comment https://forums.phpfreaks.com/topic/147445-solved-echoing-arrays-through-a-for-statement/#findComment-773920 Share on other sites More sharing options...
wildteen88 Posted March 1, 2009 Share Posted March 1, 2009 If short tags is disabled then your code wont run and thus you'd get a blank page. You can tell if short tags is disabled as your source code will be present when you view your pages source code. Also you saving your PHP code within .php files? Quote Link to comment https://forums.phpfreaks.com/topic/147445-solved-echoing-arrays-through-a-for-statement/#findComment-773930 Share on other sites More sharing options...
bloodgoat Posted March 1, 2009 Author Share Posted March 1, 2009 If short tags is disabled then your code wont run and thus you'd get a blank page. You can tell if short tags is disabled as your source code will be present when you view your pages source code. Also you saving your PHP code within .php files? All of it as one file called music.php, and I used full tags after your suggested it. Again, still a blank page. Ugh Quote Link to comment https://forums.phpfreaks.com/topic/147445-solved-echoing-arrays-through-a-for-statement/#findComment-773941 Share on other sites More sharing options...
wildteen88 Posted March 1, 2009 Share Posted March 1, 2009 What do you get when you view source code? I have tested your code and it is error free and works fine. You must be doing something wrong somewhere. Post a screenshot of how your running your script. Quote Link to comment https://forums.phpfreaks.com/topic/147445-solved-echoing-arrays-through-a-for-statement/#findComment-773944 Share on other sites More sharing options...
Mad Mick Posted March 1, 2009 Share Posted March 1, 2009 Unless what you are doing is urgent I would get on and set yourself up so you are confident in your hosting. I have my own linux web server test setup on an old PC chucked away from work but have used www.awardspace.com as a testbed as well without any problems. Writing PHP etc. is frustrating enough on its own without the additional hassle of not knowing whether your hosting service is working properly... Quote Link to comment https://forums.phpfreaks.com/topic/147445-solved-echoing-arrays-through-a-for-statement/#findComment-773953 Share on other sites More sharing options...
bloodgoat Posted March 1, 2009 Author Share Posted March 1, 2009 What do you get when you view source code? I have tested your code and it is error free and works fine. You must be doing something wrong somewhere. Post a screenshot of how your running your script. View my page online, sir http://exosphere.110mb.com/music.php, and view the source. The table, rows, and columns all show up fine, but it still won't fetch the variables from their respective arrays. One additional problem I have is that I moronically set my variables column-by-column, as I want to echo my four respective sets of array variables in two columns and two rows (four cells) but instead of getting the only four cells I desire to echo all my variables, I get eight (because of how I coded my column/row HTML into the for() statement). But I suppose I'll solve that later... Unless what you are doing is urgent I would get on and set yourself up so you are confident in your hosting. I have my own linux web server test setup on an old PC chucked away from work but have used www.awardspace.com as a testbed as well without any problems. Writing PHP etc. is frustrating enough on its own without the additional hassle of not knowing whether your hosting service is working properly... Yeah, awardspace is my main at the moment, 110mb my secondary. AwardSpace is REALLY unreliable, though. My site is down 50% of the time I try accessing it. I can access the FTP fine, but the actual site is a no go, which doesn't really make sense to me but whatever... Quote Link to comment https://forums.phpfreaks.com/topic/147445-solved-echoing-arrays-through-a-for-statement/#findComment-773958 Share on other sites More sharing options...
wildteen88 Posted March 1, 2009 Share Posted March 1, 2009 Oh! I didn't check over your code correctly here I corrected your code <?php $artist = array("Amen Ra", "Anaal Nathrakh", "Eminem", "Hellas Mounds"); $album = array("Mass I: Prayer I-VI", "The Codex Necro", "Marshall Mathers LP", "The Last Ferry to Cydonia"); $url = array("amen1", "anaal1", "eminem1", "hellas1"); $header = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"; $footer = "\n</table>"; $tables = null; for($i=0;$i<count($artist);$i++) { $tables .= "\n <tr> <td align=\"right\" width=\"20%\"> $artist[$i] </td> <td align=\"right\" width=\"30%\"> <a href=\"$url[$i]\">$album[$i]</a> </td>"; // increment $i to get the next record in the array $i++; $tables .= "\n <td align=\"right\" width=\"20%\"> $artist[$i] </td> <td align=\"right\" width=\"30%\"> <a href=\"?music=$url[$i]\">$album[$i]</a> </td> </tr>"; } echo $header; echo $tables; echo $footer; ?> Quote Link to comment https://forums.phpfreaks.com/topic/147445-solved-echoing-arrays-through-a-for-statement/#findComment-773971 Share on other sites More sharing options...
DarkSuperHero Posted March 1, 2009 Share Posted March 1, 2009 <?php header('Content-type: text/html'); $artist = array("Amen Ra", "Anaal Nathrakh", "Eminem", "Hellas Mounds"); $album = array("Mass I: Prayer I-VI", "The Codex Necro", "Marshall Mathers LP", "The Last Ferry to Cydonia"); $url = array("amen1", "anaal1", "eminem1", "hellas1"); $cnt = count($artist); echo "<table>"; for($i=0; $i < $cnt; $i++){ $tables = "<tr> <td align=\"right\" width=\"20%\"> $artist[$i] </td> <td align=\"right\" width=\"30%\"> <a href=\"$url[$i]\">$album[$i]</a> </td> <td align=\"right\" width=\"20%\"> $artist[$i] </td> <td align=\"right\" width=\"30%\"> <a href=\"?music=$url[$i]\">$album[$i]</a> </td> </tr>"; echo $tables; } echo"</table>"; interesting...try the code above.... PS- I uploaded the above code to an ftp... http://2pointhost.com/php.php Quote Link to comment https://forums.phpfreaks.com/topic/147445-solved-echoing-arrays-through-a-for-statement/#findComment-773972 Share on other sites More sharing options...
bloodgoat Posted March 1, 2009 Author Share Posted March 1, 2009 Oh! I didn't check over your code correctly here I corrected your code <?php $artist = array("Amen Ra", "Anaal Nathrakh", "Eminem", "Hellas Mounds"); $album = array("Mass I: Prayer I-VI", "The Codex Necro", "Marshall Mathers LP", "The Last Ferry to Cydonia"); $url = array("amen1", "anaal1", "eminem1", "hellas1"); $header = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"; $footer = "\n</table>"; $tables = null; for($i=0;$i<count($artist);$i++) { $tables .= "\n <tr> <td align=\"right\" width=\"20%\"> $artist[$i] </td> <td align=\"right\" width=\"30%\"> <a href=\"$url[$i]\">$album[$i]</a> </td>"; // increment $i to get the next record in the array $i++; $tables .= "\n <td align=\"right\" width=\"20%\"> $artist[$i] </td> <td align=\"right\" width=\"30%\"> <a href=\"?music=$url[$i]\">$album[$i]</a> </td> </tr>"; } echo $header; echo $tables; echo $footer; ?> Wow, you did exactly what I wanted it to do. Thank you. But for the sake of my own learning experience, can you explain what you did that I wasn't doing? Also, can you explain what the period in this is for, or what it accomplishes? $tables .= Quote Link to comment https://forums.phpfreaks.com/topic/147445-solved-echoing-arrays-through-a-for-statement/#findComment-773978 Share on other sites More sharing options...
DarkSuperHero Posted March 1, 2009 Share Posted March 1, 2009 the .= combination meands add this value to the end of the string... eg: $greetings = 'hello '; $greetings .= 'world!'; would be the same as $greetings = 'hello world!'; its called Concatenation, if im right...i might not be... :-( basically adding two things together... Quote Link to comment https://forums.phpfreaks.com/topic/147445-solved-echoing-arrays-through-a-for-statement/#findComment-773982 Share on other sites More sharing options...
bloodgoat Posted March 1, 2009 Author Share Posted March 1, 2009 Oh, that explains why I always saw what I thought were unusual echo or print lines that had them stringing together variables. I wondered why they wouldn't just quotation everything together... once again, my novice shines through! But thanks for the explanation anyways, maybe I'll put it to good use in the future. Quote Link to comment https://forums.phpfreaks.com/topic/147445-solved-echoing-arrays-through-a-for-statement/#findComment-773984 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.