I am currently working on a member login script that uses PHP, Mysql, and Jquery (What I need help with only pertains to the PHP and Mysql database though). I am using a tutorial/script for this: Tutorial
So my scripting at the moment looks similar to the coding in the tutorial, with changes for my specific Mysql database. What I am currently trying to achieve is that once a user is logged in, I want specific information to be shown to them, such as their name, rank, etc. I have rows in the mysql database for this that also houses the ID, username, etc. Specifically, there is a spot that I want it to show in the script, which is shown in the "demo.php" part:
<?php
// FROM TUTORIAL PAGE
if(!$_SESSION['id']):
?>
[...]
<?php
else:
?>
<div class="left">
<h1>Members panel</h1>
<p>You can put member-only data here.
</p>
View a special member page
<p>- or -</p>
Log off
</div>
<div class="left right">
</div>
<?php
endif;
?>
Where it says "you can put member-only data here", I want the code to pull from the database to show the information I want specific for the username logged in. For example, if user "dan124" is logged in, where member-only data is, I want it to show dan124's current rank, points accumulated, stuff like that. An example is one row that I want pulled from the database is "pts".
I have tried several different codes and tinkered with some stuff I have found, but I cannot get what I'm trying to achieve. I know my way around basic database pulls, but this is my first time working specifically with logged in user based info.
If anyone can help with this, I'd really appreciate it. I'm sure I'm not the first person who has used the tutorial I'm working with and wanted this kind of information pull, I just can't find anything! If you need more info, let me know.
Ok so much thanks to Tom Haws whose answer helped me figure out the coding I needed with some of the coding he provided. After a lot of trial and error, I managed to work out the php coding to pull specific information out of a persons database and show it when they're logged in. This is the coding I got it to work with:
<?php
if($_SESSION['id'])
$result = mysql_query("SELECT * FROM database WHERE `id` = $_SESSION[id]")
or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
echo '<b>Points Accumulated:</b>' .$row['pts'];
echo '<br>';
echo '<b>Rank:</b>' .$row['rank'];
}?>
This resulted in the logged in user being shown ONLY HIS/HER points and rank, which is what I was trying to achieve.
I may be misunderstanding what you are asking, but I am hearing that you need help with the basic database architecture and SQL.
A very common way to use MySQL would be to have a visitor table that looks like this:
visitor_id|visitor_login_name|visitor_current_rank|visitor_points_accumulated
----------|------------------|--------------------|---------------------------
1 |dan124 |veteran |1254
6892 |mary8 |honored guest |1
Now, for the life of me I don't know how you are getting the visitor logged in without doing more complicated things than this already, but assuming you have them logged in with their login_name available as a session variable, you would use the following query to get the info you want.
$query = "SELECT * FROM `visitor` WHERE `visitor_login_name` = $_SESSION[visitor_login_name]";
Then you will run that query, and the results will have the information you need as members of the result array, like this:
$rank = $result['visitor_current_rank'];
If you can give me some feedback as to how well I have guessed your intent, we can move a little further from here.
Related
Example I have a code select data in sever
$sql = "SELECT * FROM ..."
$result = mysqli_query($conn,$sql)
$row = array()
if($result){
while($r = mysqli_fetch_assoc($result)) {
$rows[] = $r;
}
I often use ajax to show data in html . But I see some people render in html by php like
index.html
<div> <?php echo $row ?> </div>
I think it not good but i dont know why . Please explain for me. Many thanks
It's not a 'bad practice' but rather a choice of user experience. It depends on the fact that if or not you have the records to be shows while the page is being rendered by php or not.
Let me give you two use use cases :
You already know that user is on this page to see the records for row-39, you get this details from some other action like a form submit in the previous page or from url which reads example.php?showMe=row-39
In this case, you'll fetch the row 39 while the page is being rendered and add the result then and there like
Your data:
Your database has 10K records and you are not sure which one the user is looking for. In that case,for security reasons (row-39 is not meant to be seen by current user) or for simply speed issues (fetching 10K records take time and so does transferring them to the user) you can't put in those records on the page and later hide what user doesn't have to see.
In such cases, you'd take the row number that user wants to see through some active like a input box, fetch that row with AJAX and show it to the user.
Now, note how 'case 2' takes more time to effectively show the data to user (first user interaction, ajax, network transfer etc) and this is why you'd want to use the 'case 1' whenever you already know what to show user
I'm a total noob when it comes to PHP, as I'm sure you'll discover once you read this question.
I'm messing around and trying to learn, and I've made a chat room using PHP. I'm trying to add functionality so that a user can change which room they are in by using a drop-down box with some pre-defined rooms. I have a login page which asks for a username, password, and which room (the dropdown box). The login page then directs to the index.php page which is where the rest of the chat code is at. The index page has a section that checks to see if the user has changed rooms. If they have changed, it displays a message in the OLD room to say that they've left, and a welcome message in the NEW room.
The issue that I've run into is that when I log in, the room variable gets broken somehow. It puts me in a "blank" room. If I use the drop down box after that to change rooms, everything works fine. I'm just having issues with the initial login.
I tried removing the change room "routine" completely, and I'm able to log in just fine (but I can't switch rooms, obviously) -- so that leads me to believe that there's a logic error somewhere here.
Sorry for the long-winded description - I'm new here and I don't know how much info to provide.
Here's the code that I have that checks/changes rooms -- anyone have any recommendations?
//check for room change
if(!($_SESSION['room'] == $_POST[r])){
$query = "INSERT INTO roomdata (message, room) VALUES (' * $_SESSION[username] ($flags$ip$flags) has joined the room. Welcome!!', '$_POST[r]'), ('* $_SESSION[username] has left the room.','$_SESSION[room]')";
$result = mysql_query($query);
$_SESSION['room'] = $_POST['r'];
}
Again, I'm new to PHP and I'm sure my code is pretty crappy. I'm also aware that mysql is depreciated, but I'm just doing this for a hobby and I'll learn mysqli once I've got this sorted out.
Thank you in advance to anyone who has time to help!
I have currently have a search form that specifies specific information of the users interest and generates a table with the filtered data.
I want to make this table interactive, specifically by allowing the user to click on the row (entry) of interest and be sent to a page with more detailed information about that entry.
I'm a novice at best and was wondering if anyone knew a way to approach this. I can provide more information if required but my MySQL and PHP search is pretty straightforward: The search locates specific columns in the MySQL table and the data that is filtered provides some - not all - the information in a table that is generated from the query.
Simply output a link that goes to a page that loads based on RowId, but don't forget security involved with passing an ID around.
<?php
foreach($resultset as $row)
echo "<a href='action.php?rowId={$row['id']}'>View Profile</a>";
?>
Then in action.php
<?php
$profile = user_load_profile_function( $_REQUEST['rowId'] );
// do whatever you want to here.
?>
In the web application I'm building a user has a profile where they can list their skills. I would like the kind of functionality StackOverflow has when making posts, where you can type tags into the tag input and select ones that already exist, and create them if they don't already exist.
At the moment, I've got a select box appearing on the page with with the id of the users current skills as values. I'm achieving this by doing:
// ProfilesController.php
$skills = $this->Profile->ProfilesSkill->find('list');
// edit.ctp
<?php echo $this->Form->input('Skill', array('value' => $skills)); ?>
I've got no idea how to progress further, though. First of all, the name field for the skills should be shown instead of their id, which I'm confused about because by Cake's convention it will use the name field by default, even though it's not. And secondly when I enter my skills into my profile Cake should automatically make all the required entries in the profiles_skills table. How can I make that work?
The solution is pretty straight forward, though it's pretty much work.
First you'll need a Tag-System. You could build one on your own (like any habtm-relation), or use a plugin like https://github.com/CakeDC/tags
For the second part, the function is called "Autocomplete". It's basically a ajax call every time you enter a letter in a form field. There are a bunch of tutorials out there, e.g: http://blogfreakz.com/cakephp/cakephp-jquery-autocomplete-tutorial/
Hope this points you in the right direction
I am working on a search script and on each search result I want a report link button. I am not sure how to make one. I have looked on google for answers but cannot come up with anything. Can anyone give me an idea of what to do? or does anyone know where there is an example of this? I plan on using the link id and making a new table on mysql to send reports to. I am just looking for a basic button to send reports to mysql I am just not sure what would be the best way to do it. I have the data for the link id's I just need to be able to report it to a new table I am assuming. Any suggestions or examples are very appreciated. Thanks.
Chris,
First you will want to create that new table in your database to capture this information. Lets assume to you will have the following fields (very basic, you may want to add more): ReportId, LinkId, DateReported. ReportId is our primary key, LinkId is the ID you reference in your question and the DateReported is the server time we logged the transaction.
With this table created you are going to want to create a new php page, lets call it report-link.php. You are going to want to make this page accept a querystring variable called linkid (accessible in the $_GET[] collection). In the code of this page you will want to write a SQL query that inserts the value of the linkid parameter into the new link report table in the database (along with the date()).
On your search page you will be able to have users report an entry by clicking a link with the href of /path/to/report-link.php?linkid=<?php echo $link_id; ?>
Please note this example is very simplistic in nature, and offers no security for spamming, pleasant end user experience after they click the link, but this is the process you will want to follow in setting this feature up. After you have it working you can spruce up the experience for your users.