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 6531413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:52:31+00:00 2026-05-25T09:52:31+00:00

<?php $say = array(ann,brenda,charles,david, edward,florence,geoff,harry, ingrid,james,kelly,liam); $columns = 5; for ($p=0; $p<count($say); $p++) {

  • 0
<?php

$say = array("ann","brenda","charles","david",
        "edward","florence","geoff","harry",
        "ingrid","james","kelly","liam");

$columns = 5;

for ($p=0; $p<count($say); $p++) {

        // Start of table or line?
        if ($p==0) { // Start of table
                print "<table border=0><tr>";
        } elseif ($p%$columns == 0) { // Start of row
                print "<tr>";
        }

        print "<td>".htmlspecialchars($say[$p])."</td>";

        // End of table or line?
        if (($p+1)%$columns == 0) { // End of row
                print "</tr>";
        }
        if ($p==count($say)-1) { // End of table
                $empty = $columns - (count($say)%$columns) ;
                if ($empty != $columns) {
                        print "<td colspan=$empty>&nbsp;</td>";
                        }
                print "</tr></table>";
        }
}
?>

The result:

ann brenda  charles david   edward
florence    geoff   harry   ingrid  james
kelly   liam

I’m trying to do the same with mysql
so far i got

    <?php

$con = mysql_connect("localhost","root","lol");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("test", $con);

$result = mysql_query("SELECT * FROM test");



while($row = mysql_fetch_array($result))
  {

$id=$row['id']; 
$nam=$row['nam'];   
$columns = 3;

for ($p=0; $p<count($id); $p++) {

        // Start of table or line?
        if ($p==0) { // Start of table
                print "<table border=0><tr>";
        } elseif ($p%$columns == 0) { // Start of row
                print "<tr>";
        }

        print "<td>".$nam."</td>";

        // End of table or line?
        if (($p+1)%$columns == 0) { // End of row
                print "</tr>";
        }
        if ($p==count($nam)-1) { // End of table
                $empty = $columns - (count($nam)%$columns) ;
                if ($empty != $columns) {
                       print "<td colspan=$empty>&nbsp;</td>";
                        }
                print "</tr></table>";
        }
}
 }

mysql_close($con);
?>

Result:

ann  
brenda   
charles  
david    
edward   
florence     
geoff    
harry    
ingrid   
james    
kelly    
liam

Question: what’s wrong?

Dabase table

id  nam
1   ann  
2   brenda   
3   charles  
4   david    
5   edward   
6   florence     
7   geoff    
8   harry    
9   ingrid   
10  james    
11  kelly    
12  liam    
  • 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-05-25T09:52:32+00:00Added an answer on May 25, 2026 at 9:52 am

    I’d suggest that you split your code into two distinct functions.

    One function will read information from the database or the array, the other will format the output.

    Right now, it looks an awful lot like you took your first chunk of code and put it into the middle of the while loop in the second piece.

    MySQL is returning results to you, one result row at a time. So what you should do is collect all those results first and then print them out second (either that, or make a counter on the number of rows returned). In your second piece of code, you’re treating each result row as you were the entire array of results in the first piece.

    That is, the line while($row = mysql_fetch_array($result)) returns a single row from the table.

    Because of this, the line $id=$row['id']; does not assign an array to $id.

    Because of this, the line for ($p=0; $p<count($id); $p++) { iterates over a single item, resulting in what you’re seeing.

    My code still looks a little hackish, but it may give you an idea. I’m afraid I haven’t tested it.

    print "<table><tr>";
    $p=0;
    $columns=3;
    
    while( $row = mysql_fetch_array($result) ) {
        if ( $p>0 && ($p % $columns)==0 )
            print "</tr><tr>";
    
        print "<td>{$row['nam']}</td>";
        $p++;
    }
    
    for(true;($p % $columns)!=0;$p++) //Finish off $p from above
        print "<td>&nbsp;</td>";
    print "</tr></table>";
    

    To do this in a more modular way:

    function display($stuff,$cols){
        //Make sure the table is some multiple of $cols to eliminate special cases
        //Hackish 
        while( (count($stuff) % $cols)!=0 )
            $stuff.push_back("&nbsp;");
    
        //Start table and first row, eliminating another special case
        print "<table><tr>";
        for($i=0;$i<count($stuff);$i++){
            if($i>0 && ($i % $cols)==0)
                print "</tr><tr>";
            print "<td>{$stuff[$i]}</td>";
        }
        print "</tr></table>";
    }
    
    $names=array()
    while( $row=mysql_fetch_array($result) )
        $names.push_back($row['nam']);
    display($names,5);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have an array of strings in a php array called $foo with
Say I have an array of key/value pairs in PHP: array( 'foo' => 'bar',
is there something like PHP: max() in javascript? lets say i have an array
I have a PHP array. Let's say $array = array(20,1,0, 0, 0,0,8); I need
Say I have this as a PHP array $my = array('Google','Api','Key'); How can I
Say I have an array like this: <?php $colors=array(); $colors[0]['id']=1; $colors[0]['color']='blue'; $colors[1]['id']=2; $colors[1]['color']='green'; ......
Say I upload a file with PHP, CURL: $postData = array(); $postData['file_name'] = test.txt;
Say I have a simple PHP loop like this one // Bad example $array
Say I have an array in PHP that looks like so: $values = Array(
I have a PHP array, say, $array = Array(Name1,Name2,Name3,Name4,Name5); I want to find the

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.