Jump to content

filter mysql reults in my browser using php


andybrooke

Recommended Posts

Hi.

Im using the following script that looks up a table on my phpMySQL database and displays the whole table!

I am Trying to create an online tracker, i want to only display in the browser the fields that are relevent to who ever is logged on.

(they logon and sessions are set : $_SESSION['User']  which i believe to be 'Username' in my table)

 

i have pasted the whole script below but i pressume the line i need help with is

$result = mysql_query( "SELECT Userid, ClientName, DateOfAction, Action, Status FROM OnlineTracker" )

 

does anyone have any idea what the code would be to filter the results to only show the data that is specific to the 'user' that is loged in?

 

the script below show the entire data in the browser and doesnt filter it down to the user.

 

Hope you can help!!!

 

<?php

$database="db231930750";

mysql_connect ("**************", "***********", "**********");

@mysql_select_db($database) or die( "Unable to select database");

$result = mysql_query( "SELECT Userid, ClientName, DateOfAction, Action, Status FROM OnlineTracker" )

or die("SELECT Error: ".mysql_error());

$num_rows = mysql_num_rows($result);

print "There are $num_rows records.<P>";

print "<table width=900 border=1>\n";

while ($get_info = mysql_fetch_row($result)){

print "<tr>\n";

foreach ($get_info as $field)

print "\t<td><font face=arial size=1/>$field</font></td>\n";

print "</tr>\n";

}

print "</table>\n";

?>

Link to comment
Share on other sites

Hi,

 

what you mean by 'relevent to who ever is logged on'. if it is somethng to do with empty data then you can simply check and show

 

i.e

while ($get_info = mysql_fetch_row($result)){

print "<tr>\n";

foreach ($get_info as $field)

{

if(trim($field) != '')

{

    print "\t<td><font face=arial size=1/>$field</font></td>\n";

}

}

print "</tr>\n";

 

Or if you are talking with fields which are applicable to the user then you have to fetch only those fields i.e in your select statement.

 

Regards

 

}

 

Link to comment
Share on other sites

Hi. What i mean by relevent is, i have a number of clients who pass me business. they all want to keep track of there own cases they have passed to me so to solve this i am attempting to create an online tracker. i have made the table on mysql and i can display everything in this table on my browser BUT

i only want to display the information that is relevent to who ever is logged in. they log in using a username and password. i pressume that the username is storred somewere so i want the code above to filter the rows to show only those rows that are connected to that user.

 

hope this makes it clearer ;D

Link to comment
Share on other sites

well, you can use sessions. When the user logins in set a session to = the username. Then when you pull all the information from the database you can use the WHERE clause.

 

In you database i persume you have linked the cases to each user. This will need to be the case for the above idea to work.

Link to comment
Share on other sites

If it is not possible or to comlex to get the code to only display the rows aplicable to the logged in 'user' then could i have a search page that asked you for "your username" then on submit this would display the rows that match your "username" if this is a simpler solution would anyone be able to help me out with what the code for this would be?

Link to comment
Share on other sites

hi. soory if this is a pain for you!!!!!

 

i am not sure but i have pasted the login php below if this helps.

 

<?php

 

require 'files/header.inc.php';

 

if ( isset($_SESSION['User']) )

{

echo "You are already logged in.. redirecting\n";

echo "<meta http-equiv=\"refresh\" content=\"1;url=members.php\">\n";

} else {

if ( !isset($_POST['username']) || !isset($_POST['password']) )

{

echo "Login:<br />\n";

echo "<form method=\"post\" action=\"index.php\">\n";

echo "Username: <input type=\"text\" name=\"username\"><br />\n";

echo "Password: <input type=\"password\" name=\"password\"><br />\n";

echo "<input type=\"submit\" value=\"Login\"><br />\n";

echo "</form>\n";

echo "<a href=\"register.php\">Create an account!</a>";

} else {

$username = addslashes($_POST['username']);

$password = sha1(addslashes($_POST['password']));

$ret = mysql_query("SELECT * FROM $dbtable WHERE UserName = '$username' AND UserPass = '$password' LIMIT 1");

if (@mysql_num_rows($ret) != 0)

{

$ret = mysql_fetch_array($ret);

$_SESSION['User'] = $ret;

$username = $_SESSION['User']['UserName'];

echo "Welcome back $username.. redirecting\n";

echo "<meta http-equiv=\"refresh\" content=\"1;url=members.php\">\n";

} else {

echo "Sorry, Incorrect Login Information\n";

}

}

}

 

 

 

echo "</body>\n</html>\n";

 

?>

Link to comment
Share on other sites

change

 

 if (@mysql_num_rows($ret) != 0)
     {
        $ret = mysql_fetch_array($ret);
        $_SESSION['User'] = $ret;
        $username = $_SESSION['User']['UserName'];
        echo "Welcome back $username.. redirecting\n";
        echo "<meta http-equiv=\"refresh\" content=\"1;url=members.php\">\n";
     }

 

to

 

 if (@mysql_num_rows($ret) != 0)
     {
        $ret = mysql_fetch_array($ret);
        $_SESSION['User'] = $username;
        $username = $_SESSION['User']['UserName'];
        echo "Welcome back $username.. redirecting\n";
        echo "<meta http-equiv=\"refresh\" content=\"1;url=members.php\">\n";
     }

 

 

then where you want the info for the specific user do something like

 

$username = $_SESSION['username'];
$result = mysql_query("SELECT * FROM person
WHERE username='$username'");

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.