trying to leave spaces in a pattern...

Hello, I am 1 week into learning PHP and I like it, but am stumped.

XXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXX

I accomplished this with this:
[php]
for ($row = 1; $row <= 6; $row++)
{
for ($col = 1; $col <= 20; $col++)
{
echo ‘X’;
}

echo "<br>";

}[/php]
I have to make a pattern…so columns wide, and 6 rows high. I have that. Then next step we have to make is to make the following pattern…

XXXXXXXXXXXXXXXXXXX
X XXXXXXXXXXXXXXXXXX
XX XXXXXXXXXXXXXXXXX
XXX XXXXXXXXXXXXXXXX
XXXX XXXXXXXXXXXXXXX
XXXXX XXXXXXXXXXXXXX

I tried to accomplish this by the following:
[php]
for ($row = 1; $row <= 6; $row++)
{
for ($col = 1; $col <= 20; $col++)

	if ($row == $col)
	{
   		echo "  ";
	}
   	 else 
   		{
   		 	echo "X";
		}
		   
	echo "<br>";

}
[/php]

The problem is the space is not being recognized and all my code is doing is making the “X”'s not line up.

Any suggestions for a coding rookie?

anybody? there has to be a way to leave a blank space in place for a letter.

First of all, you’ve got two spaces in:

[php]echo " ";[/php]

Second, you should put brackets around your for statements:

[php] for ($col = 1; $col <= 20; $col++)

	if ($row == $col)
	{
   		echo "  ";
	}
   	 else 
   		{
   		 	echo "X";
		}
		   
	echo "<br>";[/php]

Becomes:

[php]for ($col = 1; $col <= 20; $col++)
{
if ($row == $col)
{
echo " ";
}
else
{
echo “X”;
}
}

echo “
”;[/php]

Can you try it with those changes and post back?

[php]
for ($row = 1; $row <= 6; $row++)
{
for ($col = 1; $col <= 20; $col++)
{
if ($row == $col)
{
echo " ";
}
else
{
echo “X”;
}
}
echo “
”;
} [/php]

There does not seem to be any difference. If I put another character in the first echo, say echo “O”, it prints just fine.

Thanks for your help by the way. Much appreciated.

I tried it on codepad.org, and it appears to work just fine:

http://codepad.org/0UQihSwj

The cause is the font-family css style. To get the output you’re after you need to set the container of the pattern to have the css style:

font-family: monospace;

Or a similar font (you are after fixed width characters if possible)

 

Can you copy and paste here the actual output that you’re getting?

XXXXXXXXXXXXXXXXXXX
X XXXXXXXXXXXXXXXXXX
XX XXXXXXXXXXXXXXXXX
XXX XXXXXXXXXXXXXXXX
XXXX XXXXXXXXXXXXXXX
XXXXX XXXXXXXXXXXXXX

as you can see, the space being left is not a whole space, and it is throwing the columns off. If I put an “o” in place of the space, I get:

OXXXXXXXXXXXXXXXXXXX
XOXXXXXXXXXXXXXXXXXX
XXOXXXXXXXXXXXXXXXXX
XXXOXXXXXXXXXXXXXXXX
XXXXOXXXXXXXXXXXXXXX
XXXXXOXXXXXXXXXXXXXX

as you can see, the x’s are all lined up right in the columns. I have tried a tab instead of a space. I have tried the &nbsp, and still get the columns out of line…I am just confused as to why the space is not working right…it has to be something simple…

Thanks to all who have been helping on this. I know it must be frustrating for the PHP coders to work with newbies all the time, but I am trying…

Have you looked into my post above? The output is fine in terms of content, it is purely a display issue. To be more exact, the font needs to be a fixed-width font such as “monospace”.

I am not meaning to sound ignorant, but I don’t know what you mean by that.
[php]
font-family: monospace;
[/php]

Would I just put that above the

for ($row = 1; $row <= 6; $row++) line?

There is no CSS sheet, this is just supposed to be some introduction exercises. As I said…I am 2 weeks in.

I’ll google font-family.

I assume you are saying the font type is the issue?

If you’re learning PHP for web development, you really should know (at least the basics of) HTML and CSS. If you’re purely worried about the output of your PHP, then you need not worry as it is perfect. If, however, you want it to look as though the spaces are the same width as the X characters as in the example in the ‘codepad’ link provided by jSherz, then you need to worry about CSS.

My suggested fix is below:
[php]echo ‘

’;
for ($row = 1; $row <= 6; $row++)
{
for ($col = 1; $col <= 20; $col++)
{
if ($row == $col)
{
echo " ";
}
else
{
echo “X”;
}
}
echo “
”;
}
echo ‘

’;[/php]

Now I think of it, most browsers will by default create the styles mentioned in my previous post if you use the HTML

 tag. In light of this, see amended code:

[php]echo ‘

’;
for ($row = 1; $row <= 6; $row++)
{
for ($col = 1; $col <= 20; $col++)
{
if ($row == $col)
{
echo " ";
}
else
{
echo “X”;
}
}
echo “
”;
}
echo ‘
’;[/php]

again why i suggested using   instead of a space, html eats up whitespaces so a two space segment will display as 1.

But this is what I gathered, from the OP.

this depends on how you want your output displayed.
Do you want each character to be the same width, so making block patterns look good. (like old ansi art)
or do you need a method to display multiple whitespaces in your block pattern?

Sponsor our Newsletter | Privacy Policy | Terms of Service