cyimking Posted March 16, 2012 Share Posted March 16, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/259033-display-all-data/ Share on other sites More sharing options...
guinbRo Posted March 16, 2012 Share Posted March 16, 2012 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/259033-display-all-data/#findComment-1327942 Share on other sites More sharing options...
requinix Posted March 16, 2012 Share Posted March 16, 2012 That while loop is great, but you have to wrap it around the stuff that outputs the table rows. while ($row = mysql_fetch_array($get_members)) { ?> Quote Link to comment https://forums.phpfreaks.com/topic/259033-display-all-data/#findComment-1327943 Share on other sites More sharing options...
scootstah Posted March 16, 2012 Share Posted March 16, 2012 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; Quote Link to comment https://forums.phpfreaks.com/topic/259033-display-all-data/#findComment-1327944 Share on other sites More sharing options...
cyimking Posted March 16, 2012 Author Share Posted March 16, 2012 Thank you. Its working. I just have to make it look durable. But thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/259033-display-all-data/#findComment-1327945 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.