2

I am trying to create loop that gives me this type of result just like excel rows and column.

A1 B1 C1 D1 E1 F1 G1 H1 I1 J1 K1 L1 M1 N1 O1 P1 Q1 R1 S1 T1 U1 V1 W1 X1 Y1 
A2 B2 C2 D2 E2 F2 G2 H2 I2 J2 K2 L2 M2 N2 O2 P2 Q2 R2 S2 T2 U2 V2 W2 X2 Y2
A3 B3 C3 D3 E3 F3 G3 H3 I3 J3 K3 L3 M3 N3 O3 P3 Q3 R3 S3 T3 U3 V3 W3 X3 Y3
A4 B4 C4 D4 E4 F4 G4 H4 I4 J4 K4 L4 M4 N4 O4 P4 Q4 R4 S4 T4 U4 V4 W4 X4 Y4

I have create basic loop but I am confused to create loop.

<?php
    for ($char = 'A'; $char <= 'Y'; $char++) {

        echo $char.'2 ';

    }   
?> 

I am not good with complex loop so I thankful If you guide me.

admdrew
  • 3,790
  • 4
  • 27
  • 39
Mr.Happy
  • 2,639
  • 9
  • 40
  • 73
  • possible duplicate of [Algorithm to get the excel-like column name of a number](http://stackoverflow.com/questions/3302857/algorithm-to-get-the-excel-like-column-name-of-a-number) – benomatis Oct 16 '14 at 08:35

3 Answers3

4

You could use another loop for that row:

for($i = 1; $i <= 4; $i++) { // loop for rows
    for($letter = 'A'; $letter != 'Z'; $letter++) { // loop for columns
        echo $letter.$i . ' ';
    }
    echo "<br/>\n";
}

Or tabular:

<table border="1">
    <?php for($i = 1; $i <= 4; $i++): ?>
    <tr>
        <?php for($letter = 'A'; $letter != 'Z'; $letter++): ?>
            <td><?php echo $letter.$i; ?></td>
        <?php endfor; ?>
    </tr>
    <?php endfor; ?>
</table>
Kevin
  • 41,694
  • 12
  • 53
  • 70
0

You should create a string with characters and loop through that string.

<?php
    $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for ($i = 0; $i < strlen($chars); $i++)
    {
        echo $chars[$i] . '2 ';
    }
?>
Jerodev
  • 32,252
  • 11
  • 87
  • 108
0
$letters = range('A', 'Z'); // Generates an array of letters from A-Z
foreach ($n=1; $n<= 10; $n++) { // Loop through numbers from 1-10
    foreach ($letters as $l) { // Loop through the letters
        echo $l.$i; // Print out Letter and number e.g. A1, B1, C1...
    }
    echo "<br>"; // New line after a row of letters
}
Jonathon
  • 15,873
  • 11
  • 73
  • 92