Dynamic php collium of html table

Hi there i am trying to develop a dynamic collium of a php script using for loops and its not working the way i want it to do u can see a demo of what i am getting atm,

Here is my php code


$query="select * from members";
$rs=mysqli_query($con,$query);
if(!$rs)
{
	echo "Error".mysqli_error($con);
}
else
{
	//
	$count=$rs->num_rows;
	if($count>0)
	{
		while($data=$rs->fetch_assoc())
		{
			$total=count($data);
			$cols=3;
			$rows=$total/$cols;
			echo "<table>";
			for($i=1; $i<=$cols; $i++)
			{
				$e=0;
				for($e=1; $e<=$cols; $e++)
				{	
					//
					if($cols==1)
					{
						echo "<tr><td>".$data['username']."</td>";
					}
					else
					{
						echo "<td>".$data['username']."</td>";
					}
					echo "</tr>";
					//
				}	
			}
		}
	}
	//
}

What am i doing wrong with the code adove?

this is my output

http://willbc.com/bccms/mutipledata.php

can any1 help me plz? thanks

What should the output look like?

like this

name1 name 2 name 3
name 4 name 5 name 6
name 7name 8 name 9

like that using table thats how i want it what am i doing wrong?

Have a look at this modification, and see if you can spot the difference.
Does it deliver the result you want?


while($data=$rs->fetch_assoc())
{
	$total=count($data);
	$cols=3;
	$rows=ceil($total/$cols);
	echo "<table>";
	for($i=1; $i<=$cols; $i++)
	{
		echo "<tr>";
		for($e=1; $e<=$cols; $e++)
		{    
			echo "<td>".$data['username']."</td>";
		}
		echo "</tr>";				
	}
	echo "</table>";
}

yea but with that code u gave me is displaying this on the results

demo demo demo
demo demo demo
demo demo demo
dick dick dick
dick dick dick
dick dick dick
user13 user13 user13
user13 user13 user13
user13 user13 user13

This is the updated code


include("dbconnect.php");
$query="select * from members";
$rs=mysqli_query($con,$query);
if(!$rs)
{
	echo "Error".mysqli_error($con);
}
else
{
	//
	$count=$rs->num_rows;
	if($count>0)
	{
		while($data=$rs->fetch_assoc())
		{
			$total=count($data['username']);
			$cols=3;
			$rows=ceil($total/$cols);
			echo "<table>";
			for($i=1; $i<=$cols; $i++)
			{
				echo "<tr>";
				for($e=1; $e<=$cols; $e++)
				{    
					echo "<td>".$data['username']."</td>";
				}
				echo "</tr>";            
			}
			echo "</table>";
		}
	}
	//
}

What else am i doing wrong?

Can anyone else help me thanks

the table needs to be wrapped around the while loop, instead of inside the loop.

With the supplied code, each row ($data) opens a table, adds three rows with three columns inside.

Instead you probably want something like this: (not tested)


$query="select * from members";
$rs=mysqli_query($con,$query);
if(!$rs)
{
    echo "Error".mysqli_error($con);
}
else
{
    $count = $rs->num_rows;
    if($count > 0)
    {
		// data is present, we need a table
		echo "<table>";
		$i = 1;
		$cols = 3;
		$open_row = false; 
        while($data = $rs->fetch_assoc())
        {
			// using modulus (remainder) to determine when a new row opens
			if($i % $cols == 0)
			{
				echo "<tr>";
				$open_row = true;
			}
			echo "<td>".$data['username']."</td>";
			$i++;
			if($i % $cols == 0)
			{
				echo "</tr>";
				$open_row = false;
			}
		}
		if($open_row)
		{
			echo "</tr>";
		}
		echo "</table>";
    }
} 

Look at the resulting source code, and try to figure out where the missing “<tr>” and possible missing “<td></td>” might be placed, to make the table complete… :slight_smile: