/    Sign up×
Community /Pin to ProfileBookmark

printing array contents to screen

hi guys,

i have an associative array $mydb that looks like this with print_r() …

Array ( [user1] => 135 [user2] => 16 [user3] => 16 )

(it has more than 30 users)

is it possible to create a for loop that will echo the contents of
the first 10 users only?

i tried this …

for( $i = 0; $i < sizeof($mydb); $i++ ){

if( $i < 10 ){

$ifix = $i + 1;
echo “<tr><td>$ifix</td><td>$mydb[$i][0]</td><td>$mydb[$i][1]</td></tr>”

}

}

i want it t create a table of the top ten users with the
first cell being their position,
the second being their username and
the third cell being the number of point accumulated

so the above should be outputted as …

<tr><td>1</td><td>user1</td><td>135</td></tr>
<tr><td>2</td><td>user2</td><td>16</td></tr>
<tr><td>3</td><td>user3</td><td>6</td></tr>

this works …

foreach ($mydb as $key => $value){
echo “<tr><td><b>” . $key . “</b></td><td>” . $value . “</td></tr>”;
}

but i can’t show their position as 1st, 2nd, 3rd, etc or
make it stop after echoing the first ten users

please help

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@JDM71488Jul 24.2007 — showing the index and stopping after a certain amount can be done with a while or foreach, but it is perfect for a for loop.

you had it right the first time, but tweak it a bit like so:
[code=php]for($i = 1; $i <= 10; $i++)
{
echo "<tr><td>$i</td><td>$mydb[$i][0]</td><td>$mydb[$i][1]</td></tr>";
}[/code]


i would have the userid as a numerical auto-incrementing field in your database so i can use foreach loops instead.
Copy linkTweet thisAlerts:
@bsmbahamasauthorJul 24.2007 — my database file is just a .php file with the user info on seperate lines ...

user1|12

user2|7

user3|3

user4|9

user5|1


$database = "database.php";

$myarray = file($database);

$myarray = array_map('trim',$myarray);

$mydb = array();

for( $i = 0; $i < sizeof($myarray); $i++ ){

$temp = explode("|",$myarray[$i]);

$mydb[$temp[0]] = $temp[1];

}

arsort($mydb);//sort - highest first

echo "<p><table border='1' cellpadding='5' cellspacing='0' style='border-collapse:collapse;border:1px solid #cccccc'>";

for( $i = 1; $i <= 10; $i++ )

{

echo "<tr><td>$i</td><td>$mydb[$i][0]</td><td>$mydb[$i][1]</td></tr>";

}

echo "</table></p>";

outputs the following ...

<p>

<table border='1' cellpadding='5' cellspacing='0' >

<tr><td>1</td><td>[0]</td><td>[1]</td></tr>

<tr><td>2</td><td>[0]</td><td>[1]</td></tr>

<tr><td>3</td><td>[0]</td><td>[1]</td></tr>

<tr><td>4</td><td>[0]</td><td>[1]</td></tr>

<tr><td>5</td><td>[0]</td><td>[1]</td></tr>

</table>

</p>

i think the problem its because i'm using an associative array,

rather a numbered array why it isn't reading correctly
Copy linkTweet thisAlerts:
@JDM71488Jul 24.2007 — you could always do something like this:

1|usernameA|12

2|usernameB|7

and then:

[code=php]foreach($users as $user)
{
echo "<tr><td>$user['id']</td><td>$user['username']</td><td>$user['etc']</td></tr>";
}[/code]


but if you wanted to limit it then you could either use a for loop and limit with the condition or put in an if statement in that foreach loop and use a break statement.

it's much easier with a database using the limit clause of a query and associative arrays because you only pull out what you need.
Copy linkTweet thisAlerts:
@bsmbahamasauthorJul 24.2007 — you could always do something like this:

1|usernameA|12

2|usernameB|7

and then:

[code=php]foreach($users as $user)
{
echo "<tr><td>$user['id']</td><td>$user['username']</td><td>$user['etc']</td></tr>";
}[/code]


but if you wanted to limit it then you could either use a for loop and limit with the condition or put in an if statement in that foreach loop and use a break statement.

it's much easier with a database using the limit clause of a query and associative arrays because you only pull out what you need.[/QUOTE]


Would this work for me though?

the database file order stays the same but the points each user has accumulated increases.

what i need is for the the database to be read into an array and

then re-ordered so that the user with the most points is at the front

of the array, so i can just loop through the first 10 users and print them

out already in the correct order.

the problem is i can't seem to use the sort functions to order them

by point value without creating an associative array, but when i do

create an associative array, then i can't seem to get it to print just

the first 10 users
Copy linkTweet thisAlerts:
@bsmbahamasauthorJul 24.2007 — thanks for your help i resolved it by implementing a counter ..

//the file is mm-dd-yyyy.php ...

$date = date('m-d-Y');

$database = $date . ".php";

//read the file into an array and trim each element

$myarray = file($database);

$myarray = array_map('trim',$myarray);

//create a new array named $mydb

$mydb = array();

//loop through $myarray and add the elements into $mydb

for( $i = 0; $i < sizeof($myarray); $i++ ){

$temp = explode("|",$myarray[$i]);

$mydb[$temp[0]] = $temp[1];

}

//sort highest to lowest ...

arsort($mydb);

$counter = 0;

$n = 3;

echo "<table border='1' cellpadding='5' cellspacing='0'>";

foreach ( $mydb as $user => $points ) {

//only show the first(top) $n users ...

if( $counter < $n ){

echo "<tr><td><b>$user</b></td><td>$points</td></tr>";

$counter += 1;

}

}

echo "</table>";

?>
×

Success!

Help @bsmbahamas spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...