I want to print (or return at some point), the user input that is located in a hash using ids.
puts "Introduzca su usuario"
user_name = gets.chomp
puts "Ahora su contraseƱa"
user_password = gets.chomp
puts "Un recordatorio de su contraseƱa"
user_reminder = gets.chomp
data = {
user: user_name,
password: user_password,
reminder: user_reminder
}
data.each {|form, value|
puts "Your information is #{value}"
}
Using this I can print every user's input, but what if I want to print just the first, the second or the third?
EDIT
I know I can print directly the user_name, user_password and user_reminder variables, but I want to retrieve a value from the hash itself
Related
I tried to build a User search function but i ran into a problem a while ago. My function, which looks like this:
"Select id, username FROM users WHERE username LIKE '%search%'"
Is showing me only the user Janamaris ( userlist is a bit lower )
but as you see jns.drws has also a j in his username ( J = $search ).
So my Problem now i want to show all users with a J in his/her username
So how can i get all users with WHERE?
My Signed Users are [id, username]
Tolkosino
47, Janamaris
48, TheCrazyMan
49, jns.drws
Beispiel einer Foreach Schleife bzw. While Schleife Posten?
English: Can someone post an example of a foreach e.g. while loop?
The sql is fine and will get all rows you need:
"Select id, username FROM users WHERE username LIKE '%search%'"
If you have problems with upper/lower case then read this:
https://stackoverflow.com/a/2876820/4916265
Example with while
if($result = $mysqli->query($sql)){
if(is_object($result)){
while($row = $result->fetch_assoc()){
echo "<div id=\"content-b\">";
echo "<p class=\"usrsrch\">User ID : " . $row['id'] . "";
echo "<br>";
echo "<p class=\"usrsrch\">Username : . $row['username'] . "";
echo "</div>";
}
} else {
echo 'nix gefunden';
}
}
Here's my code:
// Register User
$sql = "INSERT INTO users (username, password, email, register_date)
VALUES ('$username', '$md5pass', '$email', '$date')";
if (mysqli_query($conn, $sql)) {
$last_id = mysqli_insert_id($conn);
$sql = "INSERT INTO skills (user_id)
VALUES ('$last_id')" or die(mysqli_error($conn));
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
Well the first table is created and everything seems ok but the second table (skills) it's not being inserted.
Can anyone tell what I'm doing wrong?
You create the second insert query, but you are not executing it.
Also, you should be checking the result of mysqli_insert_id, in case this call fails.
what this script does is pull down a list of usernames and passowrds and hashes it and then adds it to
a list. When there are non asci characters, the quotemeta escapes them, which allows to htpasswd command to
consume the vaule. When I run it it prints the results of /usr/local/apache2/bin/htpasswd to the terminal.
I want to pass all the results to a file
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
use File::Copy;
unshift #INC,"/production/lib";
require "config.pl";
$configFile = "/production/cfg/syncUsers.cfg";
readConfig($configFile);
#doBackup($prefs{passwdFile});
generatePasswdFile("tmpusers");
getUsers($prefs{dbUser},$prefs{dbPass},$prefs{dbSid});
#copyPasswdFile($prefs{passwdFile});
# getUsers - grab user from the db and set them up
sub getUsers {
$dbUser = shift;
$dbPass = shift;
$dbSid = shift;
$dbh = DBI->connect("dbi:Oracle:$dbSid","$dbUser","$dbPass") or die( "Couldn't connect: $!" );
$sql = "SELECT user_name,passwd FROM login_tbl";
$sth = $dbh->prepare($sql);
$sth->execute();
while ( ( $user_name,$passwd ) = $sth->fetchrow_array ) {
$user_name = quotemeta($user_name);
$passwd = quotemeta($passwd);
#print "$user_name,$passwd\n";
updatePasswdFile($user_name,$passwd);
}
$dbh->disconnect();
}
# generatePasswdFile - generate a dummy file to start with
sub generatePasswdFile {
$file = shift;
$cmd = "$prefs{htpasswd} -bc /tmp/$file dummyuser dummyuser";
system($cmd);
}
# updatePasswdFile - update the passwdfile
sub updatePasswdFile {
$file = "/tmp/tmpusers";
$user = shift;
$pass = shift;
print "Adding user: $user\n";
$cmd = "$prefs{htpasswd} -b $file $user $pass";
system($cmd);
}
this is what it does its I print out to screen.
Adding user: dev_ops
Adding password for user dev_ops
Adding user: dev_shed
Adding password for user dev_shed
Adding user: robogrl
Adding password for user robogrl
Adding user: a_foo
Adding password for user a_foo
Adding user: atank
Adding password for user atank
Adding user: acar
Adding password for user acar
Adding user: acarlson
Adding password for user acarlson
Adding user: dnick
Adding password for user dnick
When I pass it to a file so I can check for errors only one half goes to a file
and the other half is output to the screen.
bash-3.00$ ./caspser.pl > /tmp/checkFORerrors2013Nov19
this output still goes to the screen -
Adding password for user dev_ops
Adding password for user dev_shed
Adding password for user robogrl
Adding password for user a_foo
Adding password for user atank
Adding password for user acar
Adding password for user acarlson
Adding password for user dnick
and this goes to the file
Adding user: dev_ops
Adding user: dev_shed
Adding user: robogrl
Adding user: a_foo
Adding user: atank
Adding user: acar
Adding user: acarlson
Adding user: dnick
how do i get everything to go to a file?
better yet - is there a way to just print errors?
The htpasswd command you use prints to STDERR (standard error). The redirect you have performed redirects STDOUT (standard out) into the file. To redirect both, you need to update your command:
bash-3.00$ ./caspser.pl > /tmp/checkFORerrors2013Nov19 2>&1
Basically, errors are printed to a different stream. Both streams are showing in the terminal without redirections, and the difference only becomes obvious when you start changing them.
I have a perl script (abc.pl) and 2 config files (one var.pl and one config.txt)
Now
in var.pl
$admin_userid = "admin";
$guest_userid = "guest";
in config.txt - this has the value of user - can be either admin or guest
user=admin/guest
in abc.pl
require var.pl
$get_user = admin or guest (get this value from config.txt)
**$myfinal_userid = ??**
I want the value of myfinal_user_id as admin if user in config.txt is admin and guest if it is guest.
i.e. based on $get_user's value I want the value of userid - ${$get_user}."_userid"
eg: if config.txt has user=admin, the $get_user = admin and I want $myfinal_userid = $admin_userid. similarly for guest. This has to be dynamic.
Finally what I want is, know the user from config.txt and based on it, get the userid from var.pl and store that in myfinal_userid.
Let me know how can I achieve this in perl?
Use a hash to store the id's:
my %id = ( admin => 'admin',
guest => 'guest',
);
my $get_user = 'admin'; # Read this from the config.
my $final_id = $id{$get_user};
my $other_user = 'guest';
my $another_final_id = $id{$other_user};
print $final_id, "\n", $another_final_id, "\n";
I have added a field in 'Manage User Fields' & when an email is sent to the administrator notifying them of the new user registration, I want to include this new field.
I have written some code to get this new field from #__vm_user_info in /administrator/components/com_virtuemart/classes/ps_shopper.php, in the _sendMail function, as well as added the variable to $message2.
ASEND_MSG has been modified to accept the parameter, but the field is not included in the email to the admin when a user is created. When I go look in the table, the data is there. So to trouble shoot, I hard coded a user name in the select statement, added another user & the correct value was sent for the hard coded user, not the one just added. I am now thinking that it is a commit issue with MySQL, so I put a sleep(4) in the code before I attempt to get the value...no luck.
Can anyone shine some light on this for me??
LarryR....
administrator/components/com_virtuemart/classses/ps_shopper.php
Need to add with the following code in function add() before "return true" line :
/**************************** ***********************/
$pwd = $_POST['password'];
$db = JFactory::getDBO();
$query = "SELECT id, name, email, username"
. "\n FROM #__users"
. "\n ORDER by id DESC LIMIT 1"
;
$db->setQuery( $query );
$rows = $db->loadObjectList();
$namee = $rows[0]->name;
$emaill = $rows[0]->email;
$usern = $rows[0]->username;
$pwd;
$lid = $rows[0]->id;
$dbv = new ps_DB;
echo $query = "SELECT *"
. "\n FROM #__{vm}_user_info"
. "\n WHERE user_id=$lid"
;
$dbv->setQuery( $query );
$fid = $db->loadObjectList();
$field = $fid[0]->extra_field_1;
$user = clone(JFactory::getUser());
$usersConfig = &JComponentHelper::getParams( 'com_users' );
if ($usersConfig->get('allowUserRegistration') == '0') {
JError::raiseError( 403, JText::_( 'Access Forbidden' ));
return false;
}
// If user activation is turned on, we need to set the activation information
$useractivation = $usersConfig->get( 'useractivation' );
if ($useractivation == '1')
{
jimport('joomla.user.helper');
$user->set('activation', md5( JUserHelper::genRandomPassword()) );
$user->set('block', '1');
}
$component = 'com_user';
$activation_link = $mosConfig_live_site."/index.php?option=$component&task=activate&activation=".$user->get('activation');
$this->_sendMail( $namee , $emaill, $usern, $pwd, $activation_link);
/************************************************** Spinz ********************************************/
Note : Here we created the mail function for username and password to users mail.
administrator/components/com_virtuemart/classses/ps_shopper.php
Need to comment the line in function register_save() before "return true" line:
// Send the registration email
//$this->_sendMail( $name, $email, $username, $password, $activation_link );
Note: Here the mail function generated we need to comment that mail functions and create another mail function in add() function of ps_shopper.php in first point.
administrator/components/com_virtuemart/classses/ps_shopper.php
Need to get the extra added field (extra_field_1) in jos_vm_user_info table in the function _sendmail() with following code and that field sent through the mail to user.
/****************************************************************/
$db = JFactory::getDBO();
$query = "SELECT id, name, email, username"
. "\n FROM #__users"
. "\n ORDER by id DESC LIMIT 1"
;
$db->setQuery( $query );
$rows = $db->loadObjectList();
$lid = $rows[0]->id;
$dbv = new ps_DB;
$query = "SELECT *"
. "\n FROM #__{vm}_user_info"
. "\n WHERE user_id=$lid"
;
$dbv->setQuery( $query );
$fid = $db->loadObjectList();
$field = $fid[0]->extra_field_1;
$subject = sprintf ($VM_LANG->_('SEND_SUB',false), $name, $mosConfig_sitename);
$subject = vmHtmlEntityDecode($subject, ENT_QUOTES);
if ($mosConfig_useractivation=="1"){
$message = sprintf ($VM_LANG->_('USEND_MSG_ACTIVATE',false), $name, $mosConfig_sitename, $activation_link, $mosConfig_live_site, $username, $pwd, $field );
} else {
$message = sprintf ($VM_LANG->_('PHPSHOP_USER_SEND_REGISTRATION_DETAILS',false), $name, $mosConfig_sitename, $mosConfig_live_site, $username, $pwd, $field);
}
/*************************************/
Note :
Initialize the variable "$field" get the extra added field value using query. Then that the extra field value is assigned by message section of the mail.(initialize variable $field having the a value added extra fields in virtuemart).
administrator/components/com_virtuemart/languages/common/english
replace the messages for the following code:
'USEND_MSG_ACTIVATE' => 'Hello %s,
Thank you for registering at %s. Your account is created and must be activated before you can use it.
To activate the account click on the following link or copy-paste it in your browser:
%s
After activation you may login to %s using the following username and password:
Username - %s
Password - %s
Degree - %s'
2.'PHPSHOP_USER_SEND_REGISTRATION_DETAILS' => 'Hello %s,
Thank you for registering at %s. Your customer account has been created.
You may login to %s using the following username and password:
Username - %s
Password - %s
Degree - %s
'
Note:
The extra added values assigned by the string %s in language file.
The message having the string values of extra added field value in virtuemart.
The degree shows the added extra field