Forums

Unfortunately no one can be told what FluxBB is - you have to see it for yourself.

You are not logged in.

Announcement

Security releases! FluxBB 1.5.3 and FluxBB 1.4.10 released - please update your forums!

#1 2011-04-23 18:52:07

adaur
Developer
From: France
Registered: 2010-01-07
Posts: 615
Website

Assembling 3 arrays

Hi there!

For a personnal project, I need to "merge" 3 arrays; one contains the note users give to a topic, the second contains users ID and the last usernames.

So I got something like this

$array_notes = array(5,10,7);
$array_ids = array(1,5,60);
$array_usernames = array(TestUser1, TestUser2, TestUser3);

How can I do to display

TestUser1 (link to his profile, ID 1) ranked this topic: 5/10
TestUser2 (link to his profile, ID 5) ranked this topic: 10/10
TestUser3 (link to his profile, ID 60) ranked this topic: 7/10

I think I'll need to use for or foreach, but I don't know what to do... Thanks!


Please excuse my bad english, I'm french tongue.

Offline

#2 2011-04-23 21:46:40

FSX
Former Developer
From: NL
Registered: 2008-05-09
Posts: 816
Website

Re: Assembling 3 arrays

foreach ($array_usernames as $k => $username) {
    echo $username.' (link to his profile, ID '.$array_ids[$k].') ranked this topic: '.$array_notes[$k].'/10'."\n";
}

But I think it's better if you store everything in one array.

// Username, ID, Note
$users = array(
    array('TestUser1', 1, 5),
    array('TestUser2', 5, 10),
    array('TestUser3', 60, 7)
);

Last edited by FSX (2011-04-23 21:47:06)

Offline

#3 2011-04-24 10:14:46

adaur
Developer
From: France
Registered: 2010-01-07
Posts: 615
Website

Re: Assembling 3 arrays

First of all, thank you. It almost works (2/3 tongue)

<?php
$array_notes = unserialize($cur_topic['votes']);
$array_ids = unserialize($cur_topic['voters']);
$array_usernames = unserialize($cur_topic['usernames']);
foreach ($array_usernames as $k => $username) {
    echo $username.' (ID '.$array_ids[$k].') note '.$array_notes[$k].'/10'."\n";
}
?>

returns

TestUser1 (link to his profile, ID 2) ranked this topic: /10

The note is missing, maybe you have an idea?

Last edited by adaur (2011-04-24 14:15:15)


Please excuse my bad english, I'm french tongue.

Offline

#4 2011-04-24 11:17:22

FSX
Former Developer
From: NL
Registered: 2008-05-09
Posts: 816
Website

Re: Assembling 3 arrays

Well, look what values your variables have. With var_dump() or print_r().

Offline

#5 2011-04-24 14:21:42

adaur
Developer
From: France
Registered: 2010-01-07
Posts: 615
Website

Re: Assembling 3 arrays

<?php
$array_notes = unserialize($cur_topic['votes']);
print_r($array_notes);
$array_ids = unserialize($cur_topic['voters']);
print_r($array_ids);
$array_usernames = unserialize($cur_topic['usernames']);
print_r($array_usernames);
foreach ($array_usernames as $k => $username) {
echo $username.' (ID '.$array_ids[$k].') note '.$array_notes[$k].'/10'."\n";
}
?>

Array ( [9] => 1 )
Array ( [0] => 2 )
Array ( [0] => TestUser1 )

TestUser1 (ID 2) ranked this topic /10


This user ranked the topic 9/10. It seems there is a problem with the script that inserts data into database, at least for what I want it to do.

The whole code is here: http://pastebin.com/EDUE0tzA, but I think the wrong part is

    // Don't ask blame phps silly error checking ;)
    if (isset($votes[$myvote]))
        $votes[$myvote]++;
    else
        $votes[$myvote] = 1;

Please excuse my bad english, I'm french tongue.

Offline

#6 2011-04-24 15:09:33

PascL
Member
From: France
Registered: 2010-02-21
Posts: 6
Website

Re: Assembling 3 arrays

The array $votes is supposed to have keys 1 to 9 to count how many people give these notes.
For your purpose, you need to modify the piece of code you wrote:
$votes[] = $myvote;


Bouh !
French !

Offline

#7 2011-04-24 16:10:19

adaur
Developer
From: France
Registered: 2010-01-07
Posts: 615
Website

Re: Assembling 3 arrays

I'm talking about your great mod PascL, thanks for replying ! If I'm doing this, will it break the normal running of the mod? Otherwise, I could create a new vote field... I think I'll do this, I'll keep you updated!


Please excuse my bad english, I'm french tongue.

Offline

#8 2011-04-24 20:15:41

adaur
Developer
From: France
Registered: 2010-01-07
Posts: 615
Website

Re: Assembling 3 arrays

It's working; thank you so much FSX and PascL!


Please excuse my bad english, I'm french tongue.

Offline

Board footer

Powered by FluxBB 1.5.3