Page 1 of 1
Showing Users, id, and email on website!!
Posted: Sat Jan 29, 2005 2:40 pm
by Smackie
Im somewhat of a n00bie at PHP but what i came here for was....
Im making my own login Script and well i cant seem to get my users put on a page where only admins can view all of them like a list or something i would like to have it as a table and well every time i do a script for it.. it gives me a blank page or i get errors or i only get 1 user and i want to show all users if you can help me please email me at
webmaster@hauntedgraveyard.southsidedesigns.net i dont have much time to get back here im usually scripting alot of codes for my game im building please help me
I want to post everything like id, username, and email in a table on a page on my website so only admins can see on admin page
Code: Select all
<?php
$link = mysql_connect('localhost', 'db_username', 'db_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('db_name');
$result = mysql_query('SELECT id,username,email FROM db_tbl');
if (!$result) {
die('Could not query:' . mysql_error());
}
echo mysql_result($result, 2); // outputs third employee's name
mysql_close($link);
?>
or you can post a better way either way it will help me out alot thank you
Smackie
~Fear the one they call Smackie for he will send his dark army's to kill those who try and bring him down~
Posted: Sat Jan 29, 2005 4:39 pm
by neophyte
If you want everyone to display from the database you need a while loop...
Code: Select all
<?php
$link = mysql_connect('localhost', 'db_username', 'db_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('db_name');
$result = mysql_query('SELECT id,username,email FROM db_tbl');
if (!$result) {
die('Could not query:' . mysql_error());
}
//Something like...
while( $row = mysql_fetch_array($result))
{
echo $rowї'username'].' '.$rowї'email'];
}
mysql_close($link);
?>
Posted: Sat Jan 29, 2005 4:43 pm
by Smackie
Thank you on that but now how would i put them in a table?
Posted: Sat Jan 29, 2005 5:28 pm
by Jim_Bo
Hey,
Just create a single table below your while loop with $row['username'] etc in the fields you want .. Just put a <br> or two after the table if you want to break each record into a seperate table .. It will then duplicate the table for each record it brings back ..
Posted: Sat Jan 29, 2005 5:42 pm
by Smackie
You mean like this??
Code: Select all
<?php
$link = mysql_connect('localhost', 'db_username', 'db_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('db_name');
$result = mysql_query('SELECT id,username,email FROM db_tbl');
if (!$result) {
die('Could not query:' . mysql_error());
}
//Something like...
while( $row = mysql_fetch_array($result))
{
echo $rowї'username'].' '.$rowї'email'];
}
mysql_close($link);
?>
<DIV ALIGN='CENTER'>
<TABLE BORDER=0 COLS=4 WIDTH=600>
<TR>
<TD CLASS='table_header' COLSPAN=4 ALIGN='CENTER'>
<B>ID</B>: <? echo $rowї'id']; ?></B>
<TR>
<TD WIDTH=100 CLASS='alternate_rows_even'>
<SPAN CLASS='small_text'>
<B>Username:</B>:
</TD>
<TD WIDTH=250>
<? echo $rowї'username']; ?>
</TD>
<TD WIDTH=100 CLASS='alternate_rows_even'>
<SPAN CLASS='small_text'>
<B>Email:</B>:
</TD>
<TD WIDTH=250>
<? echo $rowї'email']; ?>
</TD>
</TR>
</TABLE>
<BR><BR>
</DIV>
or what?
Smackie
~Fear the one they call Smackie for he will send his dark army's to kill those who try and bring him down~
Posted: Sat Jan 29, 2005 5:59 pm
by Jim_Bo
Hey,
Your above code wont work because your table layout is below the loop .. it needs to be after:
Code: Select all
while( $row = mysql_fetch_array($result))
{
Here is what I use to list my members .. It also has pagination and an option that shows edit user level link if admin is logged in .. It should be enough to get you started .. Pull it to bits to suit your own needs ..
Code: Select all
<div align="center">
<fieldset>
<legend><strong> <span class="txt2">MEMBER PROFILES</span></strong></legend><br>
<?php
// Database Connection
include 'db.php';
// If current page number, use it
// if not, set one!
if(!isset($_GETї'page'])){
$page = 1;
} else {
$page = $_GETї'page'];
}
// Define the number of results per page
$max_results = 15;
// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results);
// Perform MySQL query on only the current page number's results
$sql = mysql_query("SELECT * FROM users LIMIT $from, $max_results");
while($row = mysql_fetch_array($sql)){
$userid = $rowї'$userid'];
$first_name = $rowї'first_name'];
$last_name = $rowї'last_name'];
$user_name = $rowї'user_name'];
$email_address = $rowї'email_address'];
$location = $rowї'location'];
$msn = $rowї'msn'];
$icq = $rowї'icq'];
$show_email = $rowї'show_email'];
$show_msn = $rowї'show_msn'];
$show_icq = $rowї'show_icq'];
?>
<table width="100%" cellspacing="1" cellpadding="3">
<tr>
<td width="16%" class="txt"><strong>Name</strong></td>
<td width="26%" class="txt"><?php echo $rowї'first_name']; ?> <?php echo $rowї'last_name']; ?>
</td>
<td width="11%" class="txt"><strong>Email</strong></td>
<td width="47%"> <span class="txt">
<?php if($rowї"show_email"] == 1){
echo $rowї'email_address'];
}else{
echo "undisclosed";
}
?>
</span></td>
</tr>
<tr>
<td class="txt"><strong>User Name</strong></td>
<td class="txt"><?php echo $rowї'user_name']; ?> </td>
<td class="txt"><strong>MSN</strong></td>
<td> <span class="txt">
<?php if($rowї"show_msn"] == 1){
echo $rowї'msn'];
}else{
echo "undisclosed";
}
?>
</span></td>
</tr>
<tr>
<td class="txt"><strong>Location</strong></td>
<td class="txt"><?php echo $rowї'location']; ?> </td>
<td class="txt"><strong>ICQ</strong></td>
<td> <span class="txt">
<?php if($rowї"show_icq"] == 1){
echo $rowї'icq'];
}else{
echo "undisclosed";
}
?>
</span></td>
</tr>
<tr>
<td colspan="4" class="txt"><div align="center">
<?php
// Show only if user is = to admin
if($_SESSIONї'user_level'] == 3) {
echo "<a href="../login/userlevel.php?userid=$userid">UPDATE USER LEVEL</a>";
}else{
echo " ";
}
?>
</div></td>
</tr>
</table>
<hr width="98%" size="1" noshade>
<?php
}
?>
<br>
<?php
// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM users"),0);
// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);
// Build Page Number Hyperlinks
echo "<center>";
// Build Previous Link
if($page > 1){
$prev = ($page - 1);
echo "<font class="txt"><< <a href="".$_SERVERї'PHP_SELF']."?pages=members&page=$prev">Previous</a> | </font>";
}
for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
echo "<font class="txt" color="#FF0000">$i </font>";
} else {
echo "<font class="txt"><a href="".$_SERVERї'PHP_SELF']."?pages=members&page=$i">$i</a> </font>";
}
}
// Build Next Link
if($page < $total_pages){
$next = ($page + 1);
echo "<font class="txt"> | <a href="".$_SERVERї'PHP_SELF']."?pages=members&page=$next">Next</a> >></font>";
}
echo "</center>";
?>
</fieldset>
</div>
I need to get a search function in there somewhere as well, got to many members .. havnt got that far yet .. not sure how to get it all in one page .. bla bla
Hope that helps
Posted: Sat Jan 29, 2005 6:21 pm
by Smackie
Thank you so much for the help.......
Smackie
~Fear the one they call Smackie for he will send his dark army's to kill those who try and bring him down~