Showing Users, id, and email on website!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Showing Users, id, and email on website!!

Post 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) &#123;
   die('Could not connect: ' . mysql_error());
&#125;
mysql_select_db('db_name');

$result = mysql_query('SELECT id,username,email FROM db_tbl');
if (!$result) &#123;
   die('Could not query:' . mysql_error());
&#125;
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~
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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) &#123;
   die('Could not connect: ' . mysql_error());
&#125;
mysql_select_db('db_name');

$result = mysql_query('SELECT id,username,email FROM db_tbl');
if (!$result) &#123;
   die('Could not query:' . mysql_error());
&#125;
//Something like...
while( $row = mysql_fetch_array($result))
&#123;
   echo $row&#1111;'username'].' '.$row&#1111;'email'];

&#125;
mysql_close($link);
?>
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

Thank you on that but now how would i put them in a table?
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post 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 ..
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

You mean like this??

Code: Select all

<?php 
$link = mysql_connect('localhost', 'db_username', 'db_password'); 
if (!$link) &#123; 
   die('Could not connect: ' . mysql_error()); 
&#125; 
mysql_select_db('db_name'); 

$result = mysql_query('SELECT id,username,email FROM db_tbl'); 
if (!$result) &#123; 
   die('Could not query:' . mysql_error()); 
&#125; 
//Something like... 
while( $row = mysql_fetch_array($result)) 
&#123; 
   echo $row&#1111;'username'].' '.$row&#1111;'email']; 

&#125; 
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&#1111;'id']; ?></B>
<TR>
<TD WIDTH=100 CLASS='alternate_rows_even'>
<SPAN CLASS='small_text'>
<B>Username:</B>:
</TD>
<TD WIDTH=250>
<? echo $row&#1111;'username']; ?>
</TD>
<TD WIDTH=100 CLASS='alternate_rows_even'>
<SPAN CLASS='small_text'>
<B>Email:</B>:
</TD>
<TD WIDTH=250>
<? echo $row&#1111;'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~
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post 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)) 
&#123;
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&#1111;'page']))&#123; 
    $page = 1; 
&#125; else &#123; 
    $page = $_GET&#1111;'page']; 
&#125; 

// 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))&#123; 
$userid = $row&#1111;'$userid'];
$first_name = $row&#1111;'first_name'];
$last_name = $row&#1111;'last_name'];
$user_name = $row&#1111;'user_name'];
$email_address = $row&#1111;'email_address'];
$location = $row&#1111;'location'];
$msn = $row&#1111;'msn'];
$icq = $row&#1111;'icq'];
$show_email = $row&#1111;'show_email'];
$show_msn = $row&#1111;'show_msn'];
$show_icq = $row&#1111;'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&#1111;'first_name']; ?> <?php echo $row&#1111;'last_name']; ?> 
     </td>
      <td width="11%" class="txt"><strong>Email</strong></td>
      <td width="47%"> <span class="txt"> 
        <?php if($row&#1111;"show_email"] == 1)&#123;

echo $row&#1111;'email_address'];

&#125;else&#123;

echo "undisclosed";

&#125;
?>
        </span></td>
    </tr>
    <tr> 
      <td class="txt"><strong>User Name</strong></td>
      <td class="txt"><?php echo $row&#1111;'user_name']; ?> </td>
      <td class="txt"><strong>MSN</strong></td>
      <td> <span class="txt"> 
        <?php if($row&#1111;"show_msn"] == 1)&#123;

echo $row&#1111;'msn'];

&#125;else&#123;

echo "undisclosed";

&#125;
?>
        </span></td>
    </tr>
    <tr> 
      <td class="txt"><strong>Location</strong></td>
      <td class="txt"><?php echo $row&#1111;'location']; ?> </td>
      <td class="txt"><strong>ICQ</strong></td>
      <td> <span class="txt"> 
        <?php if($row&#1111;"show_icq"] == 1)&#123;

echo $row&#1111;'icq'];

&#125;else&#123;

echo "undisclosed";

&#125;
?>
        </span></td>
    </tr>
    <tr> 
	  <td colspan="4" class="txt"><div align="center">

<?php

// Show only if user is = to admin

if($_SESSION&#1111;'user_level'] == 3) &#123;
	  
echo "<a href="../login/userlevel.php?userid=$userid">UPDATE USER LEVEL</a>";
&#125;else&#123;

echo " ";

&#125;

?>
</div></td>
    </tr>
  </table>
<hr width="98%" size="1" noshade>

<?php

&#125;

?>

<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)&#123; 
    $prev = ($page - 1); 
    echo "<font class="txt"><<&nbsp<a href="".$_SERVER&#1111;'PHP_SELF']."?pages=members&page=$prev">Previous</a>&nbsp|&nbsp</font>";
&#125; 

for($i = 1; $i <= $total_pages; $i++)&#123; 
    if(($page) == $i)&#123; 
        echo "<font class="txt" color="#FF0000">$i&nbsp;</font>"; 
        &#125; else &#123; 
            echo "<font class="txt"><a href="".$_SERVER&#1111;'PHP_SELF']."?pages=members&page=$i">$i</a>&nbsp</font>";
    &#125; 
&#125; 

// Build Next Link 
if($page < $total_pages)&#123; 
    $next = ($page + 1); 
    echo "<font class="txt">&nbsp|&nbsp<a href="".$_SERVER&#1111;'PHP_SELF']."?pages=members&page=$next">Next</a>&nbsp>></font>";

&#125; 

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
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post 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~
Post Reply