Basic Grid Output

This is my first time using PHP (and programming for that matter) and I’m having trouble getting my code to output a simple 6X4 grid of numbers. Also, I’m having an issue with getting a “zero” to come up in front of the single digit numbers in the grid (1-9). Also, I am not supposed to use HTML Table coding to get this to work. Heres what I’m trying to accomplish…

01 02 03 04 05 06
07 08 09 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24

Heres my current code and output:

<?php while($y<24) { $q=0; while ($q<06) { $y++; $q++; $number=q; echo $y."__"; } } ?>

The previous code generates this:
1_2_3_4_5_6_7_8_9_10_11_12_13_14_15_16_17_18_19_20_21_22_23_24_

Can anyone help me with this? This has bothered me for a while now and I’m desperate for help. Thank you.

[php]<?php

$num = 1;
$x = 1;
$y = 0;
$max_width = 6;
$max_height = 4;
/* Outer Loop /
while ( $x <= $max_width * $max_height) {
/
If length is < 1 put a 0 in front /
if ( strlen($num) < 2 ) {
echo ‘0’ . $num . ’ ';
} else {
echo $num . ’ ';
}
/
Put a new line if remainder of width is 0 /
if ( ($x % $max_width ) == 0 ) {
echo “
”;
}
/
Increment the Height /
while ( $y < $max_height ) {
$y += 1;
}
/
Increment the Width /
$x += 1;
/
Increment the Number */
$num += 1;
}[/php]

THANK YOU SOO MUCH!!! :slight_smile:

Oh my goodness Mr. Strider! So much code for such a simple problem. Although OP said no html “tables”, there is no need to use ANY html.

[php]$number = range(1,24);

$count = 0;
foreach ($number as $num) {
if($count && $count%6 == 0) {
echo “\n”;
}
echo $num = str_pad($num, 2, ‘0’, STR_PAD_LEFT).’ ';
$count++;
}[/php]

I never said my code was optimize and your code doesn’t produce a 6 X 4 grid, though technically it would still be right considering you do a CTRL-U :wink: Though I think my script would be a little more plausible for a homework assignment. LOL :slight_smile:

See it here: http://codepad.org/blXcdDg4

It does EXACTLY what the op asked for and does produce the exact grid specification. He didnt say it needed to be ouput in a web browser. If he needs a browser output, then yes, add the
tag.

You mean I just did someones homework AGAIN??? http://i300.photobucket.com/albums/nn15/Huskrrrr/charlie-brown-argh.jpg

Sponsor our Newsletter | Privacy Policy | Terms of Service