I posted this at wordpress.org as well, but I tend to find the feedback I get here very useful. I'm building a plugin to send SMS text messages, but that part isn't really important in this context.

Presented with the need to add a user meta data field for the phone number, after experimenting with a few different plugins which accomplish the task of creating custom db table fields, and managing the data therein, "I decided to go with Advanced Custom Fields. I can get the data I want for this simple SMS text plugin using the following code, but I feel like there must be a much cleaner, better recommended way to do it. Looking for feedback. Note, the desired data is the 10-digit number to be used for sending SMS text messages. The custom field was added as wp_usermeta.meta_value where wp_usermeta.meta_key is “sms_number”:

global $wpdb;
 $res = $wpdb->get_results(  $wpdb->prepare('SELECT wp_users.ID, wp_usermeta.meta_key, wp_usermeta.meta_value, wp_users.user_login, wp_users.user_email, wp_users.display_name, wp_users.user_nicename 
    FROM wp_users	, wp_usermeta
	WHERE wp_users.ID = wp_usermeta.user_id
    AND wp_usermeta.meta_key = "sms_number"
	ORDER BY wp_users.user_login'), ARRAY_N
);

$htmlOut = '';
if($res){
$htmlOut .= '<ol>';
foreach($res as $postKey => $postVal){
  $valLength = count($res);
  for($pi=0;$pi<$valLength;$pi++){
	$htmlOut .= '<li class="'.$postKey.' meta_sms_item">$res['.$postKey.']['.$pi.'] : '.
    $postVal[$pi].'</li>';
  }
}
$htmlOut .= '</ol>';

}
echo "<h3>htmlOut is:</h3>\n".$htmlOut;

I get the following results, just testing for what the query results look like:

htmlOut is:
...
$res[0][5] : admin
$res[0][6] : admin
$res[1][0] : 3
$res[1][1] : sms_number
$res[1][2] : 1234567890
$res[1][3] : alycia21
$res[1][4] : domingo.sawayn@example.com
$res[1][5] : Rosalinda
$res[1][6] : ana38
$res[2][0] : 7
$res[2][1] : sms_number
$res[2][2] : 1234569870
$res[2][3] : gerhold.vern
$res[2][4] : ajaskolski@example.net
$res[2][5] : Claire
$res[2][6] : keshaun-farrell
$res[3][0] : 6
$res[3][1] : sms_number
$res[3][2] : 5683803132
$res[3][3] : lmitchell
...
$res[6][5] : Jade
$res[6][6] : amparo67

I could use $res[0][2] , $res[1][2] , $res[2][2]… and so on to, for example, put the desired numbers in a dropdown selector. If I tweak the code and use $wpdb->get_row( ) and ARRAY_A , I get the following:

htmlOut is:
$res[ID] : 1
$res[meta_key] : sms_number
$res[meta_value] : 8142340768
$res[user_login] : admin
$res[user_email] : ajaxStardust@example.com
$res[display_name] : admin
$res[user_nicename] : admin

which I like a lot better, but I can’t figure out how to execute a query(1) to return multiple rows. In either instance, I used ARRAY_N, or ARRAY_A.

(1) that is, I can’t figure out how to use $wpdb->get_results() to use the field names as keys, which seems more convenient. Perhaps I don’t really need that?

    Weedpacket

    Hi. Thank you.
    If I use ARRAY_A with get_results(), I can still use the res[0][2], res[1][2], res[2][2] etc for the keys, but the value is then shown as array, and my existing foreach(){ for(){} } loop doesn't work because the numerical keys aren't there. E.g. var_dump() shows:

    ... [5] => Array ( [ID] => 2 [meta_key] => sms_number [meta_value] => 1234567890 [user_login] => tyler.damore [user_email] => general35@example.net [display_name] => Kevon [user_nicename] => karianne12 ) [6] => Array ( [ID] => 4 [meta_key] => sms_number [meta_value] => 1234567890 [user_login] => weimann.clementine [user_email] => towne.mauricio@example.net [display_name] => Jade [user_nicename] => amparo67 ) )

    How do I make that useful, so I can iterate over the individual arrays returned? I was trying to avoid having to write out e.g. $postVal['user_login'] , $postVal['sms_number'] , etc. I suppose that's fine, it's not going to be that much code to write (see? i just did it. two vars!), but... what if (for future use, or whatever) I wanted to iterate over many more fields?

      maxxd

      Ahh... I see! YES! This is exactly the sort of feedback I was looking for actually. Haven't tried the code yet, but just wanted to say-- yes, this is good because I want to be better versed in the various ways to use $wpdb. I see I have a lot of studying to do to be any good at this. :sigh: Alas, so it goes if you truly want to be good at, or at least understand anything.
      😉

        For anyone who reads this thread, you might be interested in checking out this article. I plan to investigate it, and see what it's all about. I also read another interesting article about Corcel w/ WP as a Headless CMS. Interesting. I dunno...

        Feat:

        • Use Laravel's Eloquent ORM
        • Corcel (essentially Eloquent to WP)

          Wow, I'd not heard of Corcel before. I had this whole response to your post about how you can't really compare Eloquent with the WPDB object; clearly I'm gonna do some reading.

          However, I have to say that the way WordPress handles data is often - in my opinion - irresponsible.

          maxxd
          I'm pleased you found an interest there! 🙂
          cool!

          I realize i need to focus on one thing at a time (i tend to overwhelm myself with trying to learn too much at once), while there's so much exciting and interesting stuff going on in web dev. Much as I'd like to believe it weren't true, I suppose there's more to ... i dunno, riding motocross than just reading the manual for the bike, and having the right gear? hee hee.

          When I finish this WP project, I want to dig into Laravel. Hence the research.
          Thanks a bunch for the advice!

          ajaxStardust When I finish this WP project, I want to dig into Laravel

          When ready for that, I highly recommend https://laracasts.com/ . While you would need to subscribe to get access to all his stuff, there's a lot of good introductory content available without a subscription. He also seems like a nice guy. 🙂

          Thanks, NogDog !
          I'm aware of that website for some reason. I definitely appreciate the reminder! I don't know why Brad Traversy came to mind (a popular youtube coding tutorial channel... he probably mentioned it in something i'd watched)

          Edit: d*mn . that's a pretty far out website though. i didn't even look at the source! haha. I don't think a greater amount of useful stuff could fit so orderly on one page!

            Write a Reply...