Jump to content

Recommended Posts

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?

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

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.

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...

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.)

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).

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?

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

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...

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...

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;

?>

<?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

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 .=

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...

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.

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.