Forum Moderators: coopster

Message Too Old, No Replies

Displaying Usernames in a Table

Not showing up

         

Jeigh

6:25 am on Jun 13, 2007 (gmt 0)

10+ Year Member



I'm not the best with PHP, I really only know the basics but I'm using this script (which obviously I didn't make myself) to display usernames from my database (under the users table).

I'm using the following script:

<?
include('header.php');
include('navigation.php');
include('content.php');

function pagenav() {
global $limit,$offset,$numpage,$where;
if ($where) {
$safewhere=urlencode($where);
}
echo "
<table cellpadding=0 border=0 cellspacing=5 width=100>
<tr>
<td align=right>";

if ($offset>=$limit) {
$newoff=$offset-$limit;

echo "<a href=\"$PHP_SELF?offset=$newoff&where=$safewhere\">
&lt;-- PREV</a>
</td>";
} else {
echo "&lt;-- PREV";
}

echo "<td align=center> &nbsp; ";

for ($i=1;$i<=$numpage;$i++) {
if ((($i-1)*$limit)==$offset) {
print "$i ";
} else {
$newoff=($i-1)*$limit;

echo "<a href=\"$PHP_SELF?offset=$newoff&where=$safewhere\">
$i</a> ";
}
}
echo "&nbsp; </td>
<td align=left>";
if ($offset!=$limit*($numpage-1)) {
$newoff=$offset+$limit;
echo "<a href=\"$PHP_SELF?offset=$newoff&where=$safewhere\">
NEXT--&gt;</a>
</td>";
}else{
echo "NEXT--&gt;</td>";
}
echo "</tr>
</table>";

} // END FUNCTION

$limit=10;

if (!$offset) {
$offset=0;
}

$result=mysql_query("select count(*) from users");
list($numrec)=mysql_fetch_row($result);
#calc num pages
$numpage=intval($numrec/$limit);
if ($numrec%$limit) $numpage++; // add one page if remainder

$result=mysql_query("select * from users limit $offset,$limit");

if ($numpage>1) {
pagenav();
print "<p>";
}

if ($numpage>1) {
pagenav();
print "<p>";
}

include('endcontent.php');
include('advert.php');
include('footer.php');
?>

I had a few errors coming up which I got rid of and now it just displays my layout normally with nothing in the content area.

Thanks for any help.

phparion

6:44 am on Jun 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



there could be many problems and you need to check lots of things e.g your queries, print your queries to see their actual shape and then copy paste them to the phpmyadmn or command line and execute them directly against db to see if they are working.

then check your IF statements and you will get somewhere surely.

colandy

12:07 pm on Jun 13, 2007 (gmt 0)

10+ Year Member



This could be me as I am also reasonably new to PHP, but where exactly is this code displaying the users names, as I don't see them being displayed at all.

Jeigh

1:19 pm on Jun 13, 2007 (gmt 0)

10+ Year Member



The script should extract usernames from the 'users' table in my database and show 10 per page, but as I said it's showing up blank.

jatar_k

1:36 pm on Jun 13, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Well this is the query that pulls the user data

$result=mysql_query("select * from users limit $offset,$limit");

I don't see that code doing anything with the return from that query though

so you aren't actually outputting anything

colandy

2:49 pm on Jun 13, 2007 (gmt 0)

10+ Year Member



OK, so after the:

$result=mysql_query("select * from users limit $offset,$limit");

line you want something like:

$row=mysql_fetch_assoc($result);
while($row['field_name'])
{
echo $row['field_name']."<br>";
}

This will display the user names a line at a time, (field_name being the field name for the user name within the db).

Jeigh

6:32 am on Jun 14, 2007 (gmt 0)

10+ Year Member



Thanks for the advice, I tried that but it made one username repeat itself a lot of time (enough to freeze my browser :P), I tried messing around with a few things but I can't get it right.

Habtom

6:58 am on Jun 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>> enough to freeze my browser

It is because the loop was giving true every time the loop runs.

Something like this should be ok:

$result = mysql_query("SELECT * FROM users LIMIT $offset, $limit");
while($row = mysql_fetch_array($result))
{
echo $row['field_name']."<br>";
}

Good Luck.

Habtom

Jeigh

7:12 am on Jun 14, 2007 (gmt 0)

10+ Year Member



Thanks, it appears to be working now.

Thanks everyone for your help, just got to see if the pages work now.

Edit: All works! :D

[edited by: Jeigh at 7:14 am (utc) on June 14, 2007]