Hello everyone,

Im new at this site, and I hope I can get some help for my assignment.

I have been asked to do the following:
Build a pyramid using PHP and MySQL and depends on how many rows. For now I have made the following in my mysql:

blocks
- id (unique ID)
- position (a number that grows higher for every new data)

Lets say I put 6 data in the MySQL then I would like the pyramid to look like this:

6
54
321

And if I add a new row:

7
65
4321

And lets make another one:

8
76
543
21

And so on... The data with highest "position" will always be on top. I have looked for answers making a for loop, but I simply can't figure this out :/

Anyone that can help me? Would really appreciate it! Thanks in advance

Dani AI

Generated

— the simplest, most predictable rule is: pull the records ordered by position descending, then group them into rows whose lengths grow (1, 2, 3, ...) until the list is exhausted. That keeps the highest-position item at the top and is easy to implement. ’s point about sketching pseudo-code first is sound — below are two compact PHP examples (using PDO, not the old mysql_* functions) you can adapt: one that auto-grows rows (1,2,3,...) and a second that builds a fixed number of rows and places any leftover items in the bottom row.

<?php
$pdo = new PDO('mysql:host=localhost;dbname=yourdb;charset=utf8mb4', 'user', 'pass',
    [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);

$stmt = $pdo->query('SELECT position FROM blocks ORDER BY position DESC');
$items = $stmt->fetchAll(PDO::FETCH_COLUMN);

$rows = [];
$idx = 0;
$size = 1;
$total = count($items);

while ($idx < $total) {
    $rows[] = array_slice($items, $idx, $size);
    $idx += $size;
    $size++;
}

foreach ($rows as $r) {
    echo implode('', $r) . "<br>\n";
}
?>

If a fixed number of pyramid rows is required, start with the minimal 1..N sizes and add any extra items to the last row; if there aren’t enough items for 1..N, distribute roughly evenly and give extra slots to bottom rows so the top stays smallest:

<?php
$rowsCount = 3;
$total = count($items);
$minNeeded = ($rowsCount * ($rowsCount + 1)) / 2;

if ($total >= $minNeeded) {
    $sizes = range(1, $rowsCount);
    $sizes[$rowsCount - 1] += $total - array_sum($sizes);
} else {
    $base = intdiv($total, $rowsCount);
    $sizes = array_fill(0, $rowsCount, $base);
    $rem = $total - $base * $rowsCount;
    for ($i = 0; $i < $rem; $i++) {
        $sizes[$rowsCount - 1 - $i]++;
    }
}

// slice $items into rows using $sizes, then output as needed
?>

Notes: use prepared connections and error handling in real code, test with var_dump($rows) while debugging, and format output (spacing/HTML/CSS) for presentation.

We generally expect some work on your part to develop some code. If it's a programming course, then it makes sense that you would do the programming (and design) before you come here to ask a specific question. In this case you have basically defined a user requirement. Now you need to define how you are going to store it in the database (e.g. are you just storing the numbers or will you store a row or position for each number) and how you are going to generate the pyramid based on the number of rows that you need. If someone else were to hand you some code to create the pyramid, would you really deserve to get a passing mark for the assignment? I think that would normally be called cheating. You need to put some time in to do the work. If you aren't that comfortable with PHP syntax yet, then start with some pseudo-code to figure out the logic and then go from there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.