News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Get all members and save into Array?

Started by bobdole2281, August 19, 2013, 01:44:38 AM

Previous topic - Next topic

bobdole2281

Is there a way to get the member list and save it into the array? Any help would be appreciated :)

Sir Osis of Liver




function QueryIntoArray($query){
        settype($retval,"array");
       
$result= mysql_query($query);
        if(!$result){
print "Query Failed";
        }       
        for($i=0;$i<mysql_numrows($result);$i++){
                for($j=0;$j<mysql_num_fields($result);$j++){
                        $retval[$i][mysql_field_name($result,$j)] = mysql_result
($result,$i,mysql_field_name($result,$j));
                }
        }
return $retval;
}

OpenDb($db_server,$db_user,$db_passwd,$db_name) or die("Failed Opening Database");

settype($members,"array");

$query = "SELECT member_name FROM smf_members";
$members = QueryIntoArray($query);

var_dump($members);



Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

emanuele

Get all the members at once may be expensive...
Are you sure you need all of them?

ETA: krash the code you posted is valid in general, but in SMF would be better to use the functions provided by the system instead of the native php functions. ;)


Take a peek at what I'm doing! ;D




Hai bisogno di supporto in Italiano?

Aiutateci ad aiutarvi: spiegate bene il vostro problema: no, "non funziona" non è una spiegazione!!
1) Cosa fai,
2) cosa ti aspetti,
3) cosa ottieni.

bobdole2281

QuoteFatal error: Call to undefined function OpenDb() in /home/content/28/10289928/html/marveldcforum/allmemes.php on line 49

I got this error. Also, is their not an smf function for it?

Arantor

That's not an SMF function. SMF has its own and you're never supposed to call them directly.

If only I understood what you were trying to do and how you were trying to do it, I might be able to give you a proper answer.

Sir Osis of Liver

Quote from: Arantor on August 19, 2013, 01:19:09 PM
That's not an SMF function. SMF has its own and you're never supposed to call them directly.

If only I understood what you were trying to do and how you were trying to do it, I might be able to give you a proper answer.

Yes, it's a standalone script. Without knowing what you're doing, that's to give you an idea how it works.

Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

Arantor

#6
I wasn't referring to what you'd done (I can see it's a standalone script), I was referring to the OP and what he was trying to achieve.

Mind you, if you'd like me to demolish that script you have... you could cut QueryIntoArray() quite easily:

function QueryIntoArray($query)
{
$retval = array();

$result = mysql_query($query);
if (!$result)
return false;

while ($row = mysql_fetch_assoc($result))
$retval[] = $row;
mysql_free_result($result);

return $retval;
}


Does ultimately the same job as the code above, without triggering unnecessary errors. Just check empty($retval) on the other end before using it and if it's not empty, you have rows.

Sir Osis of Liver


Thanks, I'll try that.  Simpler = Better. 

Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

Arantor

It might help if there wasn't a typo in it, mysql_fetch_fetch_assoc isn't a real function... but yeah, you don't need to iterate over every column to obtain it when mysql_fetch_assoc will just give you an array with all the columns in it magically.

bobdole2281

Great. I've never seen that function "QueryintoArray." Seems like what I am looking for.

Although, I don't know the forum database login info. I can also try and go find it in my hosting account. But I'm curious how the command on the members list page works? Is their a way to copy some of the code there? Does anyone know where that is located?

Arantor

That's because it's not an SMF function and the simplification above is what any other query should be doing.

-sigh- If you actually answered the questions asked you'd get a better answer... like what are you trying to do with the list of member names?

Looking at the raw memberlist code is guaranteed to confuse you more than help you at this point in time.

Sir Osis of Liver


The database login info is in Settings.php in your forum root.  The code I posted is used in a script that accesses the SMF database directly, because I'm too ******ing lazy busy to figure out how the SMF db functions work.  It's just to give you an idea how to fetch info from the db and dump it into an array.

Quote from: Arantor on August 19, 2013, 08:25:05 PM
... what are you trying to do with the list of member names?

Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

Arantor

See... there is a reason I asked without giving that out.

Depending on where you're trying to use it, you might find SSI.php is a better bet, or you might find doing it elsewhere in SMF is the answer. But without knowing the point of the exercise, it's hard to give advice.

As far as the SMF DB functions go, it's worth learning them especially as it gives you a pretty solid anti-SQL injection mechanism.

bobdole2281

Quote from: Arantor on August 19, 2013, 08:25:05 PM
That's because it's not an SMF function and the simplification above is what any other query should be doing.

-sigh- If you actually answered the questions asked you'd get a better answer... like what are you trying to do with the list of member names?

Looking at the raw memberlist code is guaranteed to confuse you more than help you at this point in time.

You are right. My bad. I want to get the memberlist and put each member into a drop down box. Essentially so the user can select a member. It doesn't necessarily need to be in an array, I just thought that would be easier. I'm not as new as I might seem, but I trust the guys on smf because they haven't steered me wrong yet.

Quote from: Krash. on August 19, 2013, 11:07:35 PM

Thanks. I remember now, I knew I had scene it somewhere. I should be able to make my own Select query and save it accordingly now. I will try to post it up here if I get it working.

The database login info is in Settings.php in your forum root.  The code I posted is used in a script that accesses the SMF database directly, because I'm too ******ing lazy busy to figure out how the SMF db functions work.  It's just to give you an idea how to fetch info from the db and dump it into an array.

Quote from: Arantor on August 19, 2013, 08:25:05 PM
... what are you trying to do with the list of member names?



Arantor

So what are you going to do with this dropdown box? Sounds to me like you need more than just the member name...

Sir Osis of Liver


Sounds like he wants the member list to display in a dropmenu so users can select a member from the list.

Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

Arantor

Which implies needing at least the member id as well as the member name. And possibly other things depending on what is then going to happen...

Sir Osis of Liver


In the simplest case, you'd have to put together a link to each members profile, and stick the names and links into a select menu, in alphabetical order.  Sounds like fun.

Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

Matthew K.

In which case...a better solution may be what SMF does for sending new PMs and other similar tasks that involve defining a specific member (or members), which is to put an input field with the JS suggestion object behind it.

Sir Osis of Liver


That only works if you have an idea which member you're looking for.  If OP wants a complete, scrollable list of all members that users can select from, that's a whole different magilla.

Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

Sir Osis of Liver


Hmm, looks like this isn't necessary either -



     settype($members,"array");



Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

emanuele

Quote from: Krash. on August 20, 2013, 12:45:48 AM
That only works if you have an idea which member you're looking for.  If OP wants a complete, scrollable list of all members that users can select from, that's a whole different magilla.
And depending on the size of the forum, it may be as useless as the autosuggest (imagine scrolling a bow with all the users (in one single page) of this forum).


Take a peek at what I'm doing! ;D




Hai bisogno di supporto in Italiano?

Aiutateci ad aiutarvi: spiegate bene il vostro problema: no, "non funziona" non è una spiegazione!!
1) Cosa fai,
2) cosa ti aspetti,
3) cosa ottieni.

Arantor

Which is why I wanted to know what the OP is actually trying to achieve with a list of all the members because of all these things...

You know, it's almost like I'm an Analyst/Programmer. Oh wait...

emanuele

Quote from: Arantor on August 20, 2013, 08:46:05 AM
You know, it's almost like I'm an Analyst/Programmer. Oh wait...
Stop doing your job! :P


Take a peek at what I'm doing! ;D




Hai bisogno di supporto in Italiano?

Aiutateci ad aiutarvi: spiegate bene il vostro problema: no, "non funziona" non è una spiegazione!!
1) Cosa fai,
2) cosa ti aspetti,
3) cosa ottieni.

Arantor

You should see the company whose books I'm on. I'm *officially* listed as Code Monkey.

bobdole2281

Quote from: Arantor on August 20, 2013, 08:46:05 AM
Which is why I wanted to know what the OP is actually trying to achieve with a list of all the members because of all these things...

You know, it's almost like I'm an Analyst/Programmer. Oh wait...


Good point. There are going to be way to many member to choose from. Is their a better way to have a user select a member? Like maybe a Search box?  Seems like way too much programming for a legit search bar though, am I wrong?

margarett

This
Quote from: Labradoodle-360 on August 20, 2013, 12:40:09 AM
In which case...a better solution may be what SMF does for sending new PMs and other similar tasks that involve defining a specific member (or members), which is to put an input field with the JS suggestion object behind it.
Maybe?
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

Arantor

-sigh- This is why I'm trying to find out what you're actually trying to achieve, and you're making it incredibly difficult because you're fixated on how you think you have to do it.

So, just humour me... you're making a system to pick a user out of all the users on the site. What exactly are you planning on doing having picked a member through some methodology?

Sir Osis of Liver


Um, I know this is a stoopid suggestion, but why not use the existing Members feature?  Sounds like it pretty much does what you want.

Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

bobdole2281

Quote from: Arantor on August 20, 2013, 11:42:43 AM
-sigh- This is why I'm trying to find out what you're actually trying to achieve, and you're making it incredibly difficult because you're fixated on how you think you have to do it.

So, just humour me... you're making a system to pick a user out of all the users on the site. What exactly are you planning on doing having picked a member through some methodology?

I'm making a custom profile page. It will involve an awards system for each member through a program I developed. I need a way for the users to select a member.

Matthew K.

Knowing that, I would definitely suggest what I posted before...using SMF's JavaScript suggest object, similarly to PMs and such.
Quote from: Labradoodle-360 on August 20, 2013, 12:40:09 AM
In which case...a better solution may be what SMF does for sending new PMs and other similar tasks that involve defining a specific member (or members), which is to put an input field with the JS suggestion object behind it.

Arantor

I wouldn't do it that way.

I'd personally have it set up to use their profile - if people want to select someone, the odds are they're going to do it from seeing their posts and clicking on their name.

Matthew K.

That's true, that would be a much more wise approach.

bobdole2281

Quote from: Labradoodle-360 on August 21, 2013, 12:17:00 AM
Knowing that, I would definitely suggest what I posted before...using SMF's JavaScript suggest object, similarly to PMs and such.
Quote from: Labradoodle-360 on August 20, 2013, 12:40:09 AM
In which case...a better solution may be what SMF does for sending new PMs and other similar tasks that involve defining a specific member (or members), which is to put an input field with the JS suggestion object behind it.

I am going to do it both ways. Basically changing the profile page, but that is a whole other question in which I will post a new thread about. How would I approach this javascript function you mentioned. It sounds perfect! Is their anymore information you have on it? Thanks a ton!

Arantor

No, the autosuggest is basically undocumented like most of the intricate stuff in SMF and for a single user account it's actually not as effective because it behaves in a very strange and slightly unreliable way (though I doubt many people actually knows where in SMF uses the auto suggester for a single username, rather than the multi-user name for PMs, there are 3 places buried in the guts)

Though I'd note that there's already multiple award systems as mods, I'd wonder why you need to reimplement things when they already handle it... including awarding them. Start with the closest thing to what you want (though I still have no real idea what you're trying to achieve anyway or why you seem somewhat determined to make it the most complex way possible...)

I don't think I'm going to be any more "help" at this point, best of luck with whatever it is you're trying to implement.

Advertisement: