Make $i++ loop work


#1

I have 4 groups of people: category 1 to 4 (each is in a different one: none can have 2 categories)
I can write a regular MySql-query to show all results: than order them by name or category.

But I want to separate each category with just a title/category and some white space in between.
So I think I need a for- or while-loop?!

This is basically what I tried, cuz I have something similar working - my header and footer loads but the actual content stays empty:

<?php
$i = 1;
while($i > 5){
    $results = mysqli_query($connect, "SELECT * FROM list WHERE category = $i AND online = 1 ORDER BY name ASC");
    while ($data = mysqli_fetch_array($results))
    {
        echo 'all kind of data-stuff that works';
    }
}
$i++;
?>

What I want:

Category 1

  • result 1
  • result 2

Category 2

  • result 1
  • result 2
  • result 3

Etc.


#2

Good day!

That is one way to do it, however not very optimal. Good practice would be to retrieve all records in a single request to the SQL database:

$results = mysqli_query($connect, 
                        "SELECT * 
                        FROM list 
                        WHERE list.online = '1' 
                        ORDER BY name ASC;");

print_r(mysqli_fetch_all($results));

#3

Thx for the reply!

But with this I get all the results at once, and I want to work with some kind of subtitle per category.
So with this I would need a way to echo a subtitle and some white space after i ordered all online-results by category. I don’t know if that’s possible.
That’s why i thought about working with a loop to perform 4 different requests (it would fetch about 50 results in total - in a table with about 250 rows) - with the name of the category echoing right in front of the results. But what i posted didn’t work: it leaves 0 results…


#4

It’s better in terms of performance to fetch everything at once and then work with the whole array/object.

If it’s lagging select only some results: SELECT id, username, category, etc FROM ...

It’s up to you.


#5

And if I fetch everything at once, than how do I echo a subtitle and line break between every category? Anyone?!


#6

I’m not sure if I get your question correctly. But, as far as I understood this is my answer. Also, we are sorry for the late reply.

$results = mysqli_query($connect, "SELECT * FROM list WHERE category IN (1,2,3,4) AND online = 1 ORDER BY name ASC");
while ($row = mysqli_fetch_assoc($results)) {
     echo $row['name'] . ' ' . $row['category'];
}

#7

I have 4 categories (column ‘category’, values 1 to 4). I want to fetch all the results with ‘1’ in column ‘online’, but i want to echo the results ordered by category, but also: in between those results, i want to echo a name for the category (1 time, not with every result separately) and some white space.
[Like 4 subtitles of 4 alineas, where the alineas are the fetched results in that specific subtitle/category.]
Any suggestions?!


#8

No one?! It’s probably something straight forward…?


#9

stackoverflow.com?

The 000webhost is generally for issues relating to the hosting or accounts.


#10

My account is on here… But whatever.


#11

Hello BegEnd,

We are sorry for late reply. But, I think stackoverflow is the best place for such questions. They have a great community.

Thanks