glens1234 Posted March 16, 2008 Share Posted March 16, 2008 Hi there. I have posted this on many forums but cant seem to get a response! I have a php/msql task to do for a website im building. unfortunately im not a php programmer. Anyway here it goes... Let say i have two mysql queries... $query1 = "SELECT username, languages_spoken FROM jos_chronoforms_1 WHERE languages_spoken LIKE '$lang_imploded%'"; $query2 = 'SELECT username FROM #__session WHERE userid != 0; The first statement gets everyone who speaks a certain language. The second statement gets everyone who is logged in. I want my script to display their name, the languages they speak and if that person is logged in. So bascially the algorithm will look something like this... loop through the results of query1 if { username exists in query2 then print "online" } Does that make sense! My main question is..... can i take a user name and test if it exists in the array query1? Perhaps using the IN operator or something like that! If your interested in having a look at what i have done so far then read on. The code below only displays their name, the languages they speak and thier IP address, which i want to replace with "online now". Also note that the code was written for use in the joomla framework and therefore uses joomla database classes. here it is anyway... <?php // no direct access defined( '_JEXEC' ) or die( 'Restricted access' ); // require the html view class jimport( 'joomla.application.helper' ); $database = & JFactory::getDBO(); $lang_spoken = JRequest::getVar( 'languages_spoken' ); //this replaces the $_POST variable $lang_imploded = implode(",", $lang_spoken); $query = "SELECT name, languages_spoken, ipaddress FROM jos_chronoforms_1 WHERE languages_spoken LIKE '$lang_imploded%'"; $database->setQuery($query); $rows = $database->loadObjectList(); echo "<h2>Translator Search</h2><br />"; print "<table width=\"400\" border=\"0\">\n"; print "<tr>\n"; print "<td><h3>Name</h3></td><td><h3>Languages Spoken</h3></td><td><h3>Who's Online</h3></td>\n"; print "</tr>\n"; foreach ( $rows as $row ) { print "<tr>\n"; print "<td>$row->name:</td><td>$row->languages_spoken</td><td>$row->ipaddress:</td>\n"; print "</tr>\n"; } print "</table><br /><br /><br /><br />"; ?> i would very much appreciate your help on this one! thanks. Quote Link to comment Share on other sites More sharing options...
glens1234 Posted March 16, 2008 Author Share Posted March 16, 2008 i have just noticed a function called in_array() So could i do something like... <?php $query1 = "SELECT username, languages_spoken FROM jos_chronoforms_1 WHERE languages_spoken LIKE '$lang_imploded%'"; $query2 = 'SELECT username FROM #__session WHERE userid != 0; $database->setQuery($query1); $rows = $database->loadObjectList(); echo "<h2>Translator Search</h2><br />"; print "<table width=\"400\" border=\"0\">\n"; print "<tr>\n"; print "<td><h3>Name</h3></td><td><h3>Languages Spoken</h3></td><td><h3>Who's Online</h3></td>\n"; print "</tr>\n"; foreach ( $rows as $row ) { print "<tr>\n"; print "<td>$row->username:</td><td>$row->languages_spoken</td><td>if (in_array(#row->username, $query2)) { echo "online now"}</td>\n"; print "</tr>\n"; } print "</table><br /><br /><br /><br />"; what do you think??? Can i insert the if statement into the print statement in the way that i have??? cheers! Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted March 16, 2008 Share Posted March 16, 2008 try and see what happens. Quote Link to comment Share on other sites More sharing options...
glens1234 Posted March 16, 2008 Author Share Posted March 16, 2008 Hi. i have tested the following code... <?php // no direct access defined( '_JEXEC' ) or die( 'Restricted access' ); // require the html view class jimport( 'joomla.application.helper' ); $database = & JFactory::getDBO(); $lang_spoken = JRequest::getVar( 'languages_spoken' ); $lang_imploded = implode(",", $lang_spoken); $query1 = "SELECT name, languages_spoken FROM jos_chronoforms_1 WHERE languages_spoken LIKE '$lang_imploded%'"; $query2 = 'SELECT username FROM #__session WHERE userid != 0'; $database->setQuery($query1); $rows = $database->loadObjectList(); echo "<h2>Translator Search</h2><br />"; print "<table width=\"400\" border=\"0\">\n"; print "<tr>\n"; print "<td><h3>Name</h3></td><td><h3>Languages Spoken</h3></td><td><h3>Who's Online</h3></td>\n"; print "</tr>\n"; foreach ( $rows as $row ) { print "<tr>\n"; print "<td>$row->name:</td><td>$row->languages_spoken</td>"; print "<td>"; if (in_array($row->name, $query2)) { echo "<h4>online now</h4>";} print "</td>\n"; print "</tr>\n"; } print "</table><br /><br /><br /><br />"; ?> It seems to work ok, only i cant fully test it until the site is online and i have translators loggin in. I have only just started learning php. i was looking for a second opinion before i dive in and start trying to test and debug code unless i know im barking up the right tree. Anyway ill hang back on this one until my client has sorted their hosting out. Any comments would be good though! cheers! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.