Joomla 3.0 not echoing usertype - variables

I made the following code to get some user variables in a flash app:
<?php
$user =& JFactory::getUser();
echo $user->get('username') ;
echo $user->get('id') ;
echo $user->get('name') ;
echo $user->get('usertype') ;
?>
Everything but usertype works, for some reason. Usertype is vital to be able to monetize my app. I followed this as a reference, so it seems alright:
http://docs.joomla.org/Accessing_the_current_user_object
Whats wrong here?

Right, I've had a look around and I can't actually find a decent solution that simply provides you with the name of the group that the user belongs to. Everything else gives you an array or the ID, so I have written a simple function that will get you exactly what you want:
function getUserGroup($userId){
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('title')
->from('#__user_usergroup_map AS map')
->where('map.user_id = '.(int) $userId)
->leftJoin('#__usergroups AS a ON a.id = map.group_id');
$db->setQuery($query);
$result = $db->loadResult();
return $result;
}
echo getUserGroup($user->id);
Hope this helps

Related

Pagination with PDO prepare statement

I am using David Carr's pagination class successfully for a whole-table call (i.e. where I'm calling all the rows in the table without filtering). After including my config and the class, I accomplish it this way:
$pages = new Paginator('3','p');
$stmt = $dbh->query('SELECT count(id) FROM sermons');
$row = $stmt->fetch(PDO::FETCH_NUM);
$total = $row[0];
//pass number of records to
$pages->set_total($total);
$results = $dbh->query('SELECT * FROM sermons ORDER BY date_preached DESC '.$pages->get_limit()); ?>
<div class="paging"><p><?php echo $total; ?> sermons found</p> <?php echo $pages->page_links();?></div>
<?php
foreach($results as $row)
Etc.
That works great, but now I'm trying to implement it for the result of user filtering via forms, and because I can't use an unnamed placeholder in the query, I'm trying to do it with a prepare (which I'm guessing is more secure anyway):
$pages = new Paginator('2','p');
$series = isset($_POST['series']) ? $_POST['series'] : false;
try {
$stmt = $dbh->prepare("SELECT count(id) FROM sermons WHERE series = ?");
$stmt->execute(array($series));
$row = $stmt->fetch(PDO::FETCH_NUM);
$total = $row[0];
//pass number of records to
$pages->set_total($total);
$results = $dbh->prepare("SELECT * FROM sermons WHERE series = ? ".$pages->get_limit());
$results->bindParam(1, $series, PDO::PARAM_STR);
$results->execute(array($series));
// the call
echo $total; ?> sermons found</p> <?php echo $pages->page_links();
When I first get to the results page from the form, it looks like everything is working properly. The call correctly identifies the number of sermons found for the search result, and displays the first two (or however many I set in the $pages statement). But when I click to another page result, no sermons display, the count is gone, and my !isset(found_rows) echo returns a zero as well.
I was originally thinking the issue was that at first I was trying to do this without binding. That is questionable, since without the pagination I can display the page without binding. In any case, I've been trying that with both bindValue and bindParam, but with no luck.
Am I mishandling something in the prepare statement? Or what?
Note: I have the results page displaying fine without the pagination with this:
$series = isset($_POST['series']) ? $_POST['series'] : false;
try {
$results = $dbh->prepare("SELECT * FROM sermons WHERE series = ?");
$results->execute(array($series));
// Etc.

Insert SQL statement error

Hi Im trying to insert data into a database using an insert statement. So basically, the user inputs data into a form and then once the submit button is clicked its meant to get the property_id of the table Property.
My code is this:
<?php
$id = intval($_GET['id']);
$query = mysql_query('SELECT * FROM review WHERE property_id="'.$id.'"');
if(isset($_POST['submit']))
{
$review = mysqli_real_escape_string($mysqli, $_POST['review']);
if(mysqli_query($mysqli, "INSERT INTO review(review) VALUES ('$review')"))
{
?>
<script>alert('Successfully Updated ');</script>
<?php
}
else
{
?>
<script>alert('Error...');</script>
<?php
}
}
?>
At the top of the page is my other code which is as followed:
<?php
include_once '../db/dbconnect.php';
$id = intval($_GET['id']);
$sql = 'SELECT* FROM property WHERE property_id="'.$id.'"';
$result = mysqli_query($mysqli, $sql);
$row=mysqli_fetch_array($result);
?>
The code above basically displays all the data for that individual property. Any help would be great.
There are several errors in your code:
$id = intval($_GET['id']);
$query = mysql_query('SELECT * FROM review WHERE property_id="'.$id.'"');
The mysql_* functions are deprecated in PHP 5, and totally removed in PHP 7. Don't use them!
Moreover, it's not possible to use mysql_* and mysqli_* functions together.
Yet another error: you are executing a SELECT query, but you never fetch the results!
Note: you don't need to concatenate $id. It makes the code harder to read with plenty of useless single and double quotes, and increases the likeliness of a typo. Just enclose the variables in a double-quoted string.
You are casting $id to an int value. If the field property_id is an integer, there is no need to put single quotes around $id in the query.
Updated snippet:
$id = intval($_GET['id']);
$query = mysqli_query($mysqli, "SELECT * FROM review WHERE property_id=$id") or die(mysqli_error($mysqli));
while($r = $query->fetch_assoc()) {
// do something here with the current record $r
}
Your code:
if(mysqli_query($mysqli, "INSERT INTO review(review) VALUES ('$review')"))
[...]
<script>alert('Error...');</script>
When developing, you should display (or write to a log file) the MySQL error message from each failing query. It will make debugging much easier:
<script>alert('Error: <?= mysqli_error($mysqli) ?>');</script>

Insert Returning last ID with PDO

I am trying to get the last/current ID submitted in an Insert, I have tried lastInsertId however that didn't work. Alternatively I have used returning on the end of my insert. However that was using pg_sql. How would I use the returning line, with PDO? I am stuck with the logic of getting the value displayed using PDO in the second option.
php 5.1.6
See below
Doesn't Work
$stmt ->execute();
$newsheetID = $conn->lastInsertId('sheet_id');
echo $newsheetID . "last id";
Works But is pg_sql, I would like to get this working for PDO
$sql = "INSERT INTO sheet_tbl (site_id, username, additionalvolunteers) VALUES ('$_POST[site_id]', '$username','$_POST[additionalvolunteers]') returning sheet_id";
echo $sql;
$result = pg_query($sql);
while ($row = pg_fetch_row($result)) {
$sheet_id_post = $row[0];
echo $sheet_id_post . '<br/>';
Your looking for this, if you cannot get lastInsertId to work, this will do the job, a couple of extra lines tho:
foreach ($stmt as $row)
{
$sheet_id_post = $row[0];
echo $sheet_id_post;
}
You can always msg me. Or head to php website look for a similar loop and alter it, my answer is very similar to yours and when I first did this I just went to php.net :-)

fetch image and text from database using joomla 2.5

i have one one issue in fetch image and text from database by module what to do for this issue and i add my table name and field name #__home_service_item this is my table name in that table two field one is image and image_name than i have one error for that question i display my error
Warning: Invalid argument supplied for foreach() in C:\wamp\www\Joomla_2.5.8-Stable-Full_Package\modules\mod_home\tmpl\default.php on line 40
please give me any clue for that problem i also add my code
<?php
defined('_JEXEC') or die('Restricted access');
$items = $params->get('items', 1);
$db =& JFactory::getDBO();
$query = "SELECT id
FROM #__home_service_item
WHERE published = '1'
ORDER BY id DESC";
$db->setQuery( $query, 0 , $items );
$rows = $db->loadObjectList();
foreach($rows as $row)
{
echo 'ID: '.$row->id.' </br>';
}
?>
please give one clue
do print_r($rows) and see if any records are returning from the database. I think that you have a problem with your query. If there are no results returning try enclosing your foreach statement with in a try catch or ignore warnings.
Also try to set $db->setQuery($query); instead of $db->setQuery( $query, 0 , $items );
If you just need one row result use $db->loadResult();

symfony get data from array

I'm trying to use an SQL query to get data from my database into the template of a symfony project.
my query:
SQL:
SELECT l.loc_id AS l__loc_id, l.naam AS l__naam, l.straat AS l__straat,
l.huisnummer AS l__huisnummer, l.plaats AS l__plaats, l.postcode AS l__postcode,
l.telefoon AS l__telefoon, l.opmerking AS l__opmerking, o.org_id AS o__org_id, o.naam AS o__naam
FROM locatie l
LEFT JOIN organisatie o
ON l.org_id = o.org_id
This is generated by this DQL:
DQL:
$this->q = Doctrine_Query::create()
->select('l.naam, o.naam, l.straat, l.huisnummer, l.plaats, l.postcode, l.telefoon, l.opmerking')
->from('Locatie l')
->leftJoin('l.Organisatie o')
->execute();
But now when i try to acces this data in the template by either doing:
<?php foreach ($q as $locatie): ?>
<?php echo $locatie['o.naam'] ?>
or
<?php foreach ($q as $locatie): ?>
<?php echo $locatie['o__naam'] ?>
i get the error from symfony:
500 | Internal Server Error | Doctrine_Record_UnknownPropertyException
Unknown record property / related component "o__naam" on "Locatie"
Does anyone know what is going wrong here? i dont know how to call the value from the array if the names in both query's dont work.
Doctrine will have hydrated your results into objects corresponding to the models in your query. In your case these will be Locatie and Organisatie. You should therefore be able to access the data as follows:
<?php foreach ($q as $obj): ?>
<?php echo $obj->Locatie->naam; ?>
<?php echo $obj->Organisatie->naam; ?>
<?php endforeach; ?>
If you have the above method in eg the Locatie table class and use self::create("l") to create your method, the object you use in the view won't need the ->Locatie part.
Edit: table method example:
class LocatieTable extends Doctrine_Table
{
public function getLocaties()
{
$q = self::createQuery("l")
->select('l.naam, o.naam, l.straat, l.huisnummer, l.plaats, l.postcode, l.telefoon, l.opmerking')
->leftJoin('l.Organisatie o')
->execute();
return $q;
}
}
You should be able to find this class (probably empty) already auto-generated in lib/model/doctrine/LocatieTable.class.php. Now call it with:
$this->q = Doctrine::getTable("Locatie")->getLocaties();
If you want to know how to get some value from the result DoctrineRecord object I advise to use var_dump($obj->toArray()) method to get clear view of the object structure. After that you can use several types of getters to retrive what you want (e.g. $obj->A->b, $obj->getA()->getB() etc..)