Jump to content
MakeWebGames

PAID REQUEST - TC cloned member dropdown function


Recommended Posts

You'll need to import the jquery-ui.css and a jquery.js

 

function getList($var = false, $value = false) {
   global $db;
   $selectUsers = $db->query('SELECT `username` FROM `users` ORDER BY `username` ASC');
   $users = [];
   while($row = $db->fetch_row())
       $users[] = $row['username'];
   ?><script>
   $(function() {
       var availableUsers = <?php echo json_encode($users);?>;
       $( "#users" ).autocomplete({
           source: availableUsers
       });
   });
   </script><?php
   if($var) {
       $ret = '<div class="ui-widget">';
       $ret .= '<input id="users"';
       $ret .= ' name="'.$var.'"';
       if($value)
           $ret .= ' value="'.$value.'"';
       $ret .= ' autofocus />';
       return $ret;
   }
}

 

Usage (as part of a form):

Returns an input box with a list of all users, with a $_REQUEST (_POST/_GET) named data_name_for_form.

echo getList('data_name_for_form');

Returns an input box with a list of all users, request data named user, automatically filled with $value

//Assuming getdata has been sanitized
$selectUser = $db->query('SELECT `username` FROM `users` WHERE `userid` = '.$_GET['id']);
$value = $db->num_rows($selectUser) ? $db->fetch_single($selectUser) : null;
echo getList('user', $value);

 

I'll take that $50, if that's still going, thanks.

Link to comment
Share on other sites

Usage (as part of a form):

Returns an input box with a list of all users, with a $_REQUEST (_POST/_GET) named data_name_for_form.

echo getList('data_name_for_form');

Returns an input box with a list of all users, request data named user, automatically filled with $value

//Assuming getdata has been sanitized
$selectUser = $db->query('SELECT `username` FROM `users` WHERE `userid` = '.$_GET['id']);
$value = $db->num_rows($selectUser) ? $db->fetch_single($selectUser) : null;
echo getList('user', $value);

 

Ok, can you explain what the code is I'm looking at here, and how/what should be modified if any?

Link to comment
Share on other sites

Here's an example of it in a form designed to send an event to a user.

<?php
require_once __DIR__ . '/globals.php';
// If the form has been submitted
if(array_key_exists('submit', $_POST)) {
   // Basic sanitation
   $_POST['user'] = array_key_exists('user', $_POST) && is_string($_POST['user']) ? $db->escape(trim($_POST['user'])) : null;
   $_POST['event'] = array_key_exists('event', $_POST) && is_string($_POST['event']) ? trim($_POST['event']) : null;
   // Basic validation
   if(empty($_POST['user'])) {
       echo 'You didn\'t select a valid user';
       exit($h->endpage());
   }
   if(empty($_POST['event'])) {
       echo 'You didn\'t enter a valid event text';
       exit($h->endpage());
   }
   // Get the user
   $select = $db->query('SELECT `userid` FROM `users` WHERE `username` = "'.$_POST['user'].'"');
   // Does the user exist?
   if(!$db->num_rows($select)) {
       echo 'The user you selected doesn\'t exist';
       exit($h->endpage());
   }
   // Return the userid
   $user = $db->fetch_single($select);
   // Send the event
   event_add($user, $_POST['event']);
   echo 'The event has been sent<br />';
}
// Display the form
?><form action="send_event.php" method="post">
   <table class="table" width="100%">
       <tr>
           <th width="25%">User</th>
           <td width="75%"><?php echo getList('user');?></td>
       </tr>
       <tr>
           <th>Event</th>
           <td><input type="text" name="event" /></td>
       </tr>
       <tr>
           <td colspan="2" class="center"><input type="submit" name="submit" value="Send Event" /></td>
       </tr>
   </table>
</form><?php
// Page footer
$h->endpage();

 

If you were to stick that in a file called send_event.php, you'd be able to simply start typing a name to select a user.

With the way the getList() function works, it'll return a username as the value so, we select the user based on the username entered. If the user doesn't exist, end the page. If the user does exist, return their respective userid and continue.

Long story short; getList() returns a username as a value. Do with it as you wish ;)

Link to comment
Share on other sites

if(!$_POST['user'])
{
echo "<br /><form action='trade.php' method='post'>
Please type the ID# of the user you wish to trade with below.<br /><br />
".getList('user')."
<input type='submit' value='Send Request' class='button'/>
</form><br /><a href='index.php' class='button'>Exit</a>";
}

 

TC allows a drop-down that shows your friends list, faction members, company, etc. to help for quick selection.

A plain text input would be a simple db query to change the username to and id before processing.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...