Jump to content

beginner php/mysql question! Pleease!!!


glens1234

Recommended Posts

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.

 

 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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!

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.