I've searched this forum for 2 days now but I can't seem to get a grip on the "foreach" thing.
I found this:
Which is exactly what I need, but!
How do I get the output "echo "$key = \"$value\"
";" in reusable variables???
<?php
/* Create Array named $profile with contents of jos_users */
$user =& JFactory::getUser();
foreach ($user as $key => $value)
{
$profile[$key] = $value;
}
/* Extract jos_user_profiles data and add to $profile */
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('profile_key, profile_value');
$query->from('#__user_profiles');
$query->where('user_id = '. (int) $user->id);
$db->setQuery($query);
$rows = $db->loadObjectList();
foreach ($rows as $row)
{
$profile[$row->profile_key] = $row->profile_value;
}
/* Included for debugging purposes only 8*/
foreach ($profile as $key => $value)
{
echo "$key = \"$value\"<br/>";
}
?>
the output generated is like:
id = "636"
name = "test2"
username = "test2"
email = "test2@test.nl"
password = "$P$DcH9yo/xVoU7vTXgvmwCrCn5dDwfnE0"
password_clear = ""
block = "0"
sendEmail = "0"
registerDate = "2014-04-11 18:49:24"
lastvisitDate = "2014-04-12 07:51:45"
activation = ""
params = "{}"
groups = "Array"
guest = "0"
lastResetTime = "0000-00-00 00:00:00"
resetCount = "0"
_errors = "Array"
aid = "0"
otpKey = ""
otep = ""
testprofile.phonenumber = "523657543763"
What I want is the output into variables, like "id = "636"" becomes $id = "636", or $variablename = "636"
is there a way I can generate/declare automated variables from the above code? (and list them, so I can see which variables were declared).
What I want to do is:
get the data from $user and reuse that to insert/update changed mysql data into an MsSQL database through this code:
include ('includes/connectwk.php');
$sql = "select DEEL_ID, DEEL_TEAM, DEEL_NAAM, DEEL_TELEFOON, DEEL_MAIL from WK_DEELNEMERS where DEEL_ID = '$usr_id'";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false)
{
die( print_r( sqlsrv_errors(), true) );
echo 'connection failed!';
}
//script part
if ($usr_id === 0 or $usr_login === null)
{}
else
{
if (sqlsrv_has_rows($stmt))
{
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
{
$c_id = $row['DEEL_ID'];
$c_team = $row['DEEL_TEAM'];
$c_naam = $row['DEEL_NAAM'];
$c_tel = $row['DEEL_TELEFOON'];
$c_mail = $row['DEEL_MAIL'];
if ($usr_id === $c_id and $usr_login === $c_team and $usr_mail === $c_mail and $row['DEEL_NAAM'] === $usr_name)
{
$update = false;
}
else
{
$update = true;
}
}
}
else
{
$sql = "insert into WK_DEELNEMERS (DEEL_ID, DEEL_TEAM, DEEL_NAAM, DEEL_TELEFOON, DEEL_MAIL) values ('$usr_id','$usr_login','$usr_name','','$usr_mail')";
$stmt = sqlsrv_query( $conn, $sql );
echo 'insert uitgevoerd';
}
if ($update === true)
{
$sql ="update WK_DEELNEMERS set DEEL_TEAM = '$usr_login',DEEL_NAAM = '$usr_name',DEEL_TELEFOON ='bla3',DEEL_MAIL= '$usr_mail' where DEEL_ID = '$usr_id '";
$stmt = sqlsrv_query( $conn, $sql );
}
}
?>
Probably this is not the way to do it, but as I am learning, please feel free to "push" me in the right direction. :)
In short, what I am trying to accomplish is, get the data from the mssql prefix_user table and the prefix_user_profiles table.
compare those results with the results I got from the MsSQL table and insert/update the MsSQL table if the user updated/changed his profile through the website.
I hope someone can help me out.
Thanks in advance :)
Ok, when I do this:
<?php
$profile["id"] = "636";
$profile["name"] = "test2";
$profile["username"] = "test2";
$profile["email"] = "test2@test.nl";
$profile["password"] = "$P$DcH9yo/xVoU7vTXgvmwCrCn5dDwfnE0";
$profile["password_clear"] = "";
$profile["block"] = "0";
foreach ($profile as $key => $value)
{
$$key = $value;
}
echo $id;
?>
I get: 636 (which I want)
but when I do this:
<?php
/* Create Array named $profile with contents of jos_users */
$user =& JFactory::getUser();
foreach ($user as $key => $value)
{
$$key = $value;
}
?>
I get a blank page. and by blank page I mean, I run this php from a Joomla Article and the whole template shows up blank in the browser. not because I'm not using an echo in the 2nd script.
I don't get it :/
@ Hüseyin BABAL here is the var_dump($user);
object(JUser)#35 (26) { ["isRoot":protected]=> bool(false) ["id"]=> string(3) "636" ["name"]=> string(5) "test2" ["username"]=> string(5) "test2" ["email"]=> string(13) "test2@test.nl" ["password"]=> string(34) "$P$DcH9yo/xVoU7vTXgvmwCrCn5dDwfnE0" ["password_clear"]=> string(0) "" ["block"]=> string(1) "0" ["sendEmail"]=> string(1) "0" ["registerDate"]=> string(19) "2014-04-11 18:49:24" ["lastvisitDate"]=> string(19) "2014-04-12 13:38:55" ["activation"]=> string(0) "" ["params"]=> string(2) "{}" ["groups"]=> array(1) { [2]=> string(1) "2" } ["guest"]=> int(0) ["lastResetTime"]=> string(19) "0000-00-00 00:00:00" ["resetCount"]=> string(1) "0" ["_params":protected]=> object(JRegistry)#36 (1) { ["data":protected]=> object(stdClass)#37 (0) { } } ["_authGroups":protected]=> array(2) { [0]=> int(1) [1]=> int(2) } ["_authLevels":protected]=> array(3) { [0]=> int(1) [1]=> int(1) [2]=> int(2) } ["_authActions":protected]=> NULL ["_errorMsg":protected]=> NULL ["_errors":protected]=> array(0) { } ["aid"]=> int(0) ["otpKey"]=> string(0) "" ["otep"]=> string(0) "" }
@ Hüseyin BABAL here is the var_export($user); Thanks :)
JUser::__set_state(array( 'isRoot' => false, 'id' => '636', 'name' => 'test2', 'username' => 'test2', 'email' => 'test2@test.nl', 'password' => '$P$DcH9yo/xVoU7vTXgvmwCrCn5dDwfnE0', 'password_clear' => '', 'block' => '0', 'sendEmail' => '0', 'registerDate' => '2014-04-11 18:49:24', 'lastvisitDate' => '2014-04-12 17:27:36', 'activation' => '', 'params' => '{}', 'groups' => array ( 2 => '2', ), 'guest' => 0, 'lastResetTime' => '0000-00-00 00:00:00', 'resetCount' => '0', '_params' => JRegistry::__set_state(array( 'data' => stdClass::__set_state(array( )), )), '_authGroups' => array ( 0 => 1, 1 => 2, ), '_authLevels' => array ( 0 => 1, 1 => 1, 2 => 2, ), '_authActions' => NULL, '_errorMsg' => NULL, '_errors' => array ( ), 'aid' => 0, 'otpKey' => '', 'otep' => '', ))
@ Hüseyin BABAL and here is the output for var_dump(get_object_vars($user));
array(20) { ["id"]=> string(3) "636" ["name"]=> string(5) "test2" ["username"]=> string(5) "test2" ["email"]=> string(13) "test2@test.nlDit e-mailadres wordt beveiligd tegen spambots. JavaScript dient ingeschakeld te zijn om het te bekijken. " ["password"]=> string(34) "$P$DcH9yo/xVoU7vTXgvmwCrCn5dDwfnE0" ["password_clear"]=> string(0) "" ["block"]=> string(1) "0" ["sendEmail"]=> string(1) "0" ["registerDate"]=> string(19) "2014-04-11 18:49:24" ["lastvisitDate"]=> string(19) "2014-04-12 17:50:44" ["activation"]=> string(0) "" ["params"]=> string(2) "{}" ["groups"]=> array(1) { [2]=> string(1) "2" } ["guest"]=> int(0) ["lastResetTime"]=> string(19) "0000-00-00 00:00:00" ["resetCount"]=> string(1) "0" ["_errors"]=> array(0) { } ["aid"]=> int(0) ["otpKey"]=> string(0) "" ["otep"]=> string(0) "" }
thing I have to mention is that with this script:
<?php
$user =& JFactory::getUser();
$userArr = (array) $user;
foreach($userArr as $key => $value)
{
$$key = "$value";
}
echo $id;
?>
it throws a: 500 internal server error?
I got this popup message after I just installed the full_ajax plugin for Joomla.
ok, now I tried this (hope I did it correctly)
<?php
$user =& JFactory::getUser();
$userArr = get_object_vars($user);
foreach($userArr as $key => $value)
{
$$key = $value;
}
echo $id;
?>
still 500 internal server error. doesn't matter if I take the echo out or not.
I changed the complete approach and created this: (works for me, probably not the best solution, but like I said, it works and i'm still learning PHP)
<?php
session_start();
$user =& JFactory::getUser();
$session = JFactory::getSession(); //get cuurent session
$session->set('user', new JUser($user->id)); //update changed profile settings
//new data for your user after the update
$user = JFactory::getUser();
$usr_id = $user->get('id');
$usr_login = $user->get('username');
$usr_name = $user->get('name');
$usr_mail = $user->get('email');
$guest = $user->guest;
/*
echo $guest;
echo $usr_id;
echo $usr_login;
echo $usr_name;
echo $usr_mail;
*/
// init Joomla Framework
if ($guest === 0)
{
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$mainframe = JFactory::getApplication('site');
// DBQuery
$database =& JFactory::getDBO();
$query = "SELECT profile_value FROM #__user_profiles WHERE user_id = '$user->id' AND profile_key LIKE 'testprofile.%'";
$database->setQuery($query);
$telnr = $database->loadResult();
print_r($telnr);
echo '<br/>';
echo 'my id is '.$usr_id.' and username is '.$usr_login.' and phonenumber '.$telnr;
?>
This gives me the results as I need them.
Thanks for all the help :)