Hello I am having an issue assigning a variable to a select box selection so that i can feed the variable back into future queries. Please see code below:

<select name="room" id="msg-room">
                        <?php
                        $query = mysql_query("SELECT * FROM rooms ORDER BY ROOM asc");
                        while($data = mysql_fetch_assoc($query)) {
                        ?>
                        <option value="<?php echo $data["roomid"]; ?>"><?php echo $data["ROOM"]; ?></option>
                        <?php } ?>
                    </select>

The selection list loads and populates fine. I just need a way to assign $data["Room"] a variable so that I can insert it into the Heading of the page as well as back into a sql query that will return the number of users in a selected room.

<?php
        $query = mysql_query('SELECT * from user where room = $data[room] and account_status=1')
            or
die(mysql_error());
$num_rows = mysql_num_rows($query);

echo "$num_rows Users Online" 

Please help. Thank you

Recommended Answers

All 3 Replies

Member Avatar for LastMitch

@Peek@u

The selection list loads and populates fine. I just need a way to assign $data["Room"] a variable so that I can insert it into the Heading of the page as well as back into a sql query that will return the number of users in a selected room.

Since you are using JQuery why not just used AJAX the data rather than using a Database?

Is there an error? When you ran this code was there an error in the MYSQL side?

If there was an error, what message did it say?

Ima a newbie so bare with me.

The site im working on is an already functiong website (designed by a developer i hired but didn't finish the job and has now vanished) . What Im doing is recreating the site with jquerymobile to port out to phone gap, etc. On the original site the function of displaying the user count is working. I didn't know how to explain why the variable wasn't working for the user count now so instead I was trying to recreate it using php since I couldn't figure out whats going wrong with the ajax honestly.

Here is the code from the working site that will not load on my version.

<script>
function getChatText() {
                $('#onlineusers').load('ajax.php?type=userlist&room=' + escape($("#msg-room option:selected").text()));
                </script>






<div id="onlineboxtitle">Online Users (<span id="onlineusercount">0</span>)</div>
                    <div id="onlineusers"></div>

And here is the portion in the ajax.php file

if($_GET["type"] == "userlist") {
    //$query = mysql_query("SELECT * FROM user WHERE lastupdate >= '" . date("Y-m-d G:i:s", time()-(60*$minutes)) . "' ORDER BY nickname ASC"); 
    $query = mysql_query("SELECT * FROM user WHERE account_status='1'");
    while($data = mysql_fetch_assoc($query)) {
        $users[$data["room"]][] = $data;
    }
    if(count($users[$_GET["room"]]) > 0) {
        foreach($users[$_GET["room"]] as $user) {
            ?>
<div class="online_user <?php if(strtolower($data["gender"]) == "f") echo "post_female"; else echo "post_male"; ?>"><a href="javascript:;" onclick="show_profile(<?php echo $user["id"]; ?>);"><?php echo $user["nickname"]; ?></a></div>           
            <?php
        }
        ?>

        <?php
    }
    ?>
            <script type="text/javascript">
        $('#onlineusercount').html('<?php echo count($users[$_GET["room"]]); ?>');
        </script>
    <?php
    exit();
}
Member Avatar for LastMitch

I didn't know how to explain why the variable wasn't working for the user count now so instead I was trying to recreate it using php since I couldn't figure out whats going wrong with the ajax honestly.

I'm not familiar with AJAX. I also don't have a db to test this out. Was there an error on the query? I mean there's has to be an error at least.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.