Hi all,
I am trying to develop a dynamic table using php and mysql i cant seem to get this to show at all but when i set my col’s variable to 14 it shows it all into one line not sure what i am exactly doing wrong here is my code
include("dbconnect.php");
$r2Query="SELECT * FROM console WHERE rankgroup='Members'";
$rs=mysqli_query($con,$r2Query);
if(!$rs)
{
echo "Error:".mysqli_error($con);
}
else
{
$count=$rs->num_rows;
if($count>0)
{
for ($i=0;$i<$rows;$i++)
{
echo "<tr>";
$cols = "14";
for ($x=0;$x<$cols;$x++)
{
if($x==3)
{
echo "</tr><tr>";
}
else
{
while($data=$rs->fetch_assoc())
{
echo "<td><a href=members.php?cmd=perms&cid=".$data['cid'].">".$data['consolename']."</a></td>";
}
}
}
echo "</tr>";
//
}
echo "</table>";
}
}
even if i change $col to count it shows nothing
How can i fix this and what am i doing wrong?
Thanks,William
Hi there,
If it were me, I’d use a foreach loop. Something like:
<[COLOR="Red"][B]?php[/B][/COLOR]
include("dbconnect.php");
$r2Query="SELECT * FROM console WHERE rankgroup='Members'";
$rs=mysqli_query($con,$r2Query);
if(!$rs)
{
echo "Error:".mysqli_error($con);
}
while($row = mysqli_fetch_assoc($rs))
{
$members[] = $row;
}
if(isset($members)):
[COLOR="red"][B]?[/B][/COLOR]>
[COLOR="SeaGreen"]<table>[/COLOR]
<[COLOR="red"][B]?php[/B][/COLOR] foreach ($members as $member): [COLOR="red"][B]?[/B][/COLOR]>
[COLOR="SeaGreen"]<tr>[/COLOR]
<a href=members.php?cmd=perms&cid="<[COLOR="red"][B]?php[/B][/COLOR] echo $member['cid'] [COLOR="red"][B]?[/B][/COLOR]>"><[COLOR="red"][B]?php[/B][/COLOR] echo $data['consolename'] [COLOR="red"][B]?[/B][/COLOR]></a>
[COLOR="SeaGreen"]</tr>[/COLOR]
<[COLOR="red"][B]?php[/B][/COLOR] endforeach; [COLOR="red"][B]?[/B][/COLOR]>
[COLOR="SeaGreen"]</table>[/COLOR]
<[COLOR="Red"][B]?php[/B][/COLOR] endif; [COLOR="red"][B]?[/B][/COLOR]>
Don’t know if that’s any good for ya?
Cheers,
Mike
Hi there,
Ya it only displays it i want it to do somthing like this show 3 records per line and keep going till there are no records left to be displayed.
Okay, lets take a moment to break down what you want it to do.
- Retrieve all records in the table.
- Display 3 per line. (Think you mean ‘per row’ here instead of per line, but we’ll roll with it)
You’ve got the code for a [FPHP]foreach[/FPHP]. (I always like to give manual links for functions).
At this point all you need is the “3 per row” constraint. There are quite a few different ways to go about this.
How would I do it?
$out = ""
$count = 0;
while ($row = mysqli_fetch_assoc($rs)) {
$out .= '<td><a href=members.php?cmd=perms&cid="'.$row['cid'].'">'.$row['consolename'].'</a></td>';
$count++;
if($count % 3 == 0) {
$table[] = $out;
$out = "";
}
}
if($out != "") { $table[] = $out; }
echo "<table><tr>".implode('</tr><tr>',$table)."</tr></table>"
PS: Unless your table only contains cid and consolename, or you’re using the data elsehwere on the page, dont SELECT *, SELECT cid,consolename instead.
EDIT: Changed my mind.