Jump to content

Recommended Posts

Hello,

 

I just reach the ultimate high in frustration because  i can not display all the data from my database in a table using php.

 

Im trying to display all the members from my database (id, username, email and account type) in my admin cp, and what is happening is that i am displaying all my data in one td.

 

(Ex, in the <td></td>, it will display all the usernames at once, rather than separating them).

 

<?php

include_once "../config.php";
include_once "admin_check.php";

$get_members = mysql_query("SELECT * FROM members ORDER BY id");

while ($row = mysql_fetch_array($get_members))
{
$id .= $row['id'];
$username .= $row['username'];
$account_type .= $row['account_type'];
$email .= $row['email'];
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome Admin!</title>
</head>

<body>

<h1>Welcome admin! <a href='../index.php'> Click here to go back! </a></h1>

<table cellspacing="5" cellpadding="5" style="border: black thin solid" align="center">

<tr>
<td>ID</td>
<td>Username</td>
<td>Email</td>
<td>Account Type</td>
</tr>

<tr>
<td><?php echo $id;?></td>
<td><?php echo $username;?></td>
<td><?php echo $email;?></td>
<td><?php echo $account_type;?></td>
</tr>

</table>


</body>
</html>

 

Now i tried making the table in the while loop, but i am also having the same problem. Please help.

Link to comment
https://forums.phpfreaks.com/topic/259033-display-all-data/
Share on other sites

Something like this should work, although someone may have a cleaner solution.  Something you could also do is just simply print table as your looping through the results the query yields.  My approach using the style you have is to try something like this:

 

<?php
$id = array();
$username = array();
//etc...

while($row = mysql_fetch_array($get_members) {
    $id[] = $row['id']
    $username = $row['username'];
    //etc....
}

//...now down to the html, use a nice for loop.  A could be cleaner, with a two dimensional array
for($i = 0; $i<count($id); $i++) {
  //Insert table code here, and call like this....
  echo'<td>' . $id[i] . '</td>';
  echo'<td>' . $username[i] . '</td>';
  //etc
}
?>

Link to comment
https://forums.phpfreaks.com/topic/259033-display-all-data/#findComment-1327942
Share on other sites

It's because you are simply appending all of the values to the same variable. You'll have to use an array and loop over it later.

$members = array();

while ($row = mysql_fetch_array($get_members))
{
$members[] = $row;
}

foreach($members as $member): ?>
<tr>
<td><?php echo $member['id'];?></td>
<td><?php echo $member['username'];?></td>
<td><?php echo $member['email'];?></td>
<td><?php echo $member['account_type'];?></td>
</tr>
<?php endforeach;

Link to comment
https://forums.phpfreaks.com/topic/259033-display-all-data/#findComment-1327944
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.