Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8541913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:56:17+00:00 2026-06-11T11:56:17+00:00

I have a MySQL query that I am saving to a variable to output

  • 0

I have a MySQL query that I am saving to a variable to output on the page. I have the data in an unordered list, but I am trying to split the data into three columns using 3 unordered lists floated left. Here is my current code:

        $sqlCommand = 
           "SELECT  list_specialty.id, list_specialty.specialty  FROM list_specialty
            ORDER BY list_specialty.specialty ASC";
        $query = mysqli_query($myConnection,$sqlCommand) or die (mysqli_error($myConnection));

        $specialtyDisplay = '';

        $num = mysqli_num_rows($query);
        $split = intval($num/3); //number of items in every column

        $i = 0;

        while ($row = mysqli_fetch_array($query)) {
            $specialtyid = $row["id"];
            $specialtyname = $row["specialty"];

            $specialtyDisplay .= 
                '<li><a href="doctor.php?specialty=' . $specialtyid . '">' . $specialtyname . '</a></li>';
            $i++;
            if ($i == $split) {
                $specialtyDisplay .= '</ul> <ul>';
                $i = 0;
            }

        }

This code works GREAT as long as my data is exactly divisible by 3 (equal columns).
However, when my total number of rows is not divisible by 3, the above code won’t work.

I need a solution that will balance the columns, so I can have either 3 equal columns, one extra element in the first column, or one extra element in the first and second column.

What do I need to change in my code to account for that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:56:18+00:00Added an answer on June 11, 2026 at 11:56 am

    This will work:

    1. set $split to the length of the longest column
    2. rows have 0, 1 or 2 columns that are longer by one element. -> $longcols = $num % 3
    3. substract 1 from $split when column number $longcols is printed (unless all columns have equal length)

    Implemented in the code:

    $num = mysqli_num_rows($query);
    $split = ceil($num/3); // 1.
    
    $i = 0;
    $col = 1; //for counting columns
    $longcols = $num % 3; // 2.
    
    while ($row = mysqli_fetch_array($query)) {
        $specialtyid = $row["id"];
        $specialtyname = $row["specialty"];
    
        $specialtyDisplay .= 
            '<li><a href="doctor.php?specialty=' . $specialtyid . '">' . $specialtyname . '</a></li>';
        $i++;
        if ($i == $split) {
            $specialtyDisplay .= '</ul> <ul>';
    
            if( $longcols != 0 && $longcols == $col ) { $split--; } // 3.
            $i = 0;
            $col++; //increase columns count
        }
    
    }
    

    The lines with comments are those that I have changed/inserted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL query that: gets data from three tables linked by unique
I have a mysql query that uses union to join multiple queries into one
For example I have a mysql query that gets some data. Then runs another
I have a mysql db query that I'd like to take and condense into
I have a list of values I am trying to enter into a mysql
I have a MySQL query that I would like to optimize a little bit.
I have a mysql query that targets a single column in a single row
I have a PHP/MySQL query that returns to an HTML table, and I'm stuck
So I have this awesome MySQL query that's returning me an awesome array >
I'm running an MySQL query that returns results based on location. However I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.