Gossamer Forum
Home : Products : DBMan SQL : Development, Plugins and Globals :

simple global making my head hurt...

Quote Reply
simple global making my head hurt...
Hi there. I'm sure PHP must seem like a toy language to most perl developers. But when you're used to PHP, perl can sure seem unnecessarily complex...

I'm trying to write a simple global to do the following:

1) Check if the logged in user has any records in the "events" table.
2) If he does, return a list of the titles of those events.

Here's what I've got:

Code:
sub {
use GT::SQL;
my $tags = shift;
my $user = $tags->{Username};
my $db = new GT::SQL '/path/to/admin/defs';
my $table = $db->table ('events');
my $output = "";
my $results = $table->query ({Owner_userid => $user});
if ($results) {
foreach my $result (@$results) {
$output .= "<br>$result[2]";
}
}
return $output;
}


I'm sure that's not the most direct way to accomplish this, but it seems like it's close to working.

Nonetheless, here's the error I'm getting:

Global symbol "@result" requires explicit package name at (eval 15) line 12. Unable to compile 'events_list'. Reason: at /path/to/admin/Dbsql/Home.pm line 1505.

I would be very grateful if someone could tell me what I'm doing wrong. Thanks!

Fractured Atlas :: Liberate the Artist
Services: Healthcare, Fiscal Sponsorship, Marketing, Education, The Emerging Artists Fund
Quote Reply
Re: [hennagaijin] simple global making my head hurt... In reply to
Try
Code:
foreach my $result (@$results) {
$output .= "<br>".$result->[2];
}

http://www.bookings.org
Quote Reply
Re: [jaspercram] simple global making my head hurt... In reply to
Perfect! Thanks!!

Fractured Atlas :: Liberate the Artist
Services: Healthcare, Fiscal Sponsorship, Marketing, Education, The Emerging Artists Fund
Quote Reply
Re: [hennagaijin] simple global making my head hurt... In reply to
Hi,

I'm not sure about your coding but I think you could get the required result using -

sub {
my $tags = shift;
my $output;
my $loged_user = $tags->{Username};
my $table = $DB->table('events');
my $sth = $table->select (['col_name'],{Owner_userid => $loged_user });
while (my $row = $sth->fetchrow_hashref) {
($output .= "$row->{col_name}<br>");
}
return $output;
}

Replace col_name with your title column name.

Hope it works.

Simon.
Quote Reply
Re: [jai] simple global making my head hurt... In reply to
Thanks, Simon. That works, too, and I agree with you that it's a bit cleaner. Thanks for your help!

Fractured Atlas :: Liberate the Artist
Services: Healthcare, Fiscal Sponsorship, Marketing, Education, The Emerging Artists Fund
Quote Reply
Re: [hennagaijin] simple global making my head hurt... In reply to
Using Simon's code, I have this:



sub {
my $tags = shift;
my $output;
my $loged_user = $tags->{Username};
my $table = $DB->table('idhorse_main');
my $sth = $table->select (['IDHORSE_Horse_Name'],{IDHORSE_Ownership => $loged_user });
while (my $row = $sth->fetchrow_hashref) {
($output .= "$row->{IDHORSE_Horse_Name}<br>");
}
return $output;
}



The problem is this, however. The way the idhorse_main table is set up, the current owner (the IDHORSE_Ownership field) isn't alone in the field. All previous owners are listed first, then the current owner with a * at the end (ie: User1, User2, User3*). The way this sub goes, it will only give a list of records that match the Username EXACTLY. I've played around with it, but to no avail. Can someone please help? I need it so the sub will pull only the horses owned by the current user. The appropriate query is:

SELECT IDHORSE_ID,IDHORSE_Horse_Name FROM idhorse_main WHERE IDHORSE_Ownership like '%HFSHCpres*' ORDER BY IDHORSE_ID

So you can see exactly what I mean, where HFSHCpres, of course, would be the username. Thanks!
Quote Reply
Re: [ArtistikDD] simple global making my head hurt... In reply to
Hi,

I'm sure there is a way to do what you want but I would strongly advise against storing multiple values in the same field (especially if you want to search or retrieve only one value). What you are trying to do would also break the rules of database normalization.

I would suggest that you try to re-design your main table to have each owner stored in a different field.

Simon.
Quote Reply
Re: [jai] simple global making my head hurt... In reply to
Hi. :) Thanks for the tip. As much as I would love to have each owner in a different field, I'm just lost as to how I could actually manage pulling it off. The database was originally created years ago as a flat file, and now has in excess of 17,000 records, and I must admit, MySQL is all very very new to me. :) Thanks!