gamefreak13 Posted May 25, 2008 Share Posted May 25, 2008 As if this function wasn't confusing enough.. I need to do more with it. Under the second and third case, I need it to do a mysql query for the authorid or userid. If there is a result, then output the result instead of the authorid / userid. For example, "Bulletin 000000 read by 12121212" would become "Bulletin 000000 read by Mike" if there is a result (the result being "Mike"). The table stores the authorid/userid (same thing) with a name.. so it basically says "12121212 is Mike". Hopefully that made sense. If there is not a result, then just output the authorid/userid (e.g. 12121212). I know how I would do this normally, but inserting it in to a function/case (something I'm new at) is something I thought I'd ask for help on. I just wrote this here so I'm not sure if my PHP part is right or not.. but I believe this is what I need to plug in to the function. $result = mysql_query("SELECT name FROM identities WHERE friendid=$b['authorid'];"); if($result['name']) { return 'Bulletin '.$b['messageid'].' posted by '.$result['name']; } else { return 'Bulletin '.$b['messageid'].' posted by '.$b['authorid']; } For demo purposes.. $text = "http://www.site.com/index.cfm?fuseaction=bulletin.read&authorID=78378629&messageID=52943581"; or $text = "http://messaging.myspace.com/index.cfm?fuseaction=mail.readmessage&userID=78378629&type=Inbox&messageID=86143581&fed=True"; // PARSE THE REFERER SOURCES function my_parse($text) { $a = parse_url($text); parse_str($a['query'],$b); $b = array_change_key_case($b); switch($b['fuseaction']) { case 'user.viewprofile': return 'Profile '.$b['friendid']; break; case 'bulletin.read': return 'Bulletin '.$b['messageid'].' posted by '.$b['authorid']; break; case 'mail.readmessage': return 'Message '.$b['messageid'].' read by '.$b['userid']; break; default: return 'Unknown / Missing '.$text; } print_r($b); } Link to comment https://forums.phpfreaks.com/topic/107186-solved-ifelse-mysql-query-inside-a-function/ Share on other sites More sharing options...
BlueSkyIS Posted May 25, 2008 Share Posted May 25, 2008 doesn't make a difference. if/then in a function is mostly exactly the same as outside. Link to comment https://forums.phpfreaks.com/topic/107186-solved-ifelse-mysql-query-inside-a-function/#findComment-549530 Share on other sites More sharing options...
gamefreak13 Posted May 25, 2008 Author Share Posted May 25, 2008 Where would I insert my query and if/else for each one? I'd assume under "case" but above "return" right? Link to comment https://forums.phpfreaks.com/topic/107186-solved-ifelse-mysql-query-inside-a-function/#findComment-549532 Share on other sites More sharing options...
BlueSkyIS Posted May 25, 2008 Share Posted May 25, 2008 Under the second and third case, I need it to do a mysql query for the authorid or userid. yes, return means what it says: it returns without doing anything else. Link to comment https://forums.phpfreaks.com/topic/107186-solved-ifelse-mysql-query-inside-a-function/#findComment-549534 Share on other sites More sharing options...
gamefreak13 Posted May 25, 2008 Author Share Posted May 25, 2008 Does this look right? And/or is there a cleaner/better way of doing it? // PARSE THE REFERER SOURCES function my_parse($text) { $a = parse_url($text); parse_str($a['query'],$b); $b = array_change_key_case($b); switch($b['fuseaction']) { case 'user.viewprofile': return 'Profile '.$b['friendid']; break; case 'bulletin.read': $result = mysql_query("SELECT name FROM identities WHERE friendid=$b['authorid'];"); if($result['name']) { return 'Bulletin '.$b['messageid'].' posted by '.$result['name']; } else { return 'Bulletin '.$b['messageid'].' posted by '.$b['authorid']; } break; case 'mail.readmessage': $result = mysql_query("SELECT name FROM identities WHERE friendid=$b['userid'];"); if($result['name']) { return 'Message '.$b['messageid'].' posted by '.$result['name']; } else { return 'Message '.$b['messageid'].' read by '.$b['userid']; } break; default: return 'Unknown / Missing '.$text; } print_r($b); } Link to comment https://forums.phpfreaks.com/topic/107186-solved-ifelse-mysql-query-inside-a-function/#findComment-549538 Share on other sites More sharing options...
gamefreak13 Posted May 25, 2008 Author Share Posted May 25, 2008 Hmm.. it didn't work.. its just blank. Link to comment https://forums.phpfreaks.com/topic/107186-solved-ifelse-mysql-query-inside-a-function/#findComment-549539 Share on other sites More sharing options...
BlueSkyIS Posted May 25, 2008 Share Posted May 25, 2008 should be more like: case 'bulletin.read': $sql = "SELECT name FROM identities WHERE friendid='{$b['authorid']}'"; $result = mysql_query($sql) or die(mysql_error()); if (mysql_num_rows($result) > 0) { $line = mysql_fetch_assoc($result); return 'Bulletin '.$b['messageid'].' posted by '.$line['name']; } else { return 'Bulletin '.$b['messageid'].' posted by '.$b['authorid']; } if you're seeing 'just blank', your error reporting is probably turned off. put this at top: // Report all PHP errors (bitwise 63 may be used in PHP 3) error_reporting(E_ALL); // Same as error_reporting(E_ALL); ini_set('error_reporting', E_ALL); Link to comment https://forums.phpfreaks.com/topic/107186-solved-ifelse-mysql-query-inside-a-function/#findComment-549540 Share on other sites More sharing options...
gamefreak13 Posted May 25, 2008 Author Share Posted May 25, 2008 It works!! Awesome! Many thanks! I do wonder though.. do I have to use mysql_num_rows or can I do something like .. if($result > 0) { ? The reason I ask is because I'm running some insane queries on the page already. Does that count for two queries or just one? Like.. does $result run ONCE, and then whatever the value is.. it gets questioned by mysql_num_rows and by $line['name'] ? Oh.. and by "blank" I meant just where my PHP code was. The entire page wasn't blank white. I have E_ALL set at the top of my script already, so whatever was wrong with my code must have been considered "normal"? Link to comment https://forums.phpfreaks.com/topic/107186-solved-ifelse-mysql-query-inside-a-function/#findComment-549545 Share on other sites More sharing options...
BlueSkyIS Posted May 25, 2008 Share Posted May 25, 2008 this will be true whether any rows are returned or not: if ($result > 0) { so i suggest that you use mysql_num_rows(). it is not an extra query, it's just asking how many rows were returned by the previous query. Link to comment https://forums.phpfreaks.com/topic/107186-solved-ifelse-mysql-query-inside-a-function/#findComment-549565 Share on other sites More sharing options...
gamefreak13 Posted May 25, 2008 Author Share Posted May 25, 2008 Ok.. sounds good. Thanks again for your help! Link to comment https://forums.phpfreaks.com/topic/107186-solved-ifelse-mysql-query-inside-a-function/#findComment-549586 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.