Jump to content

Help at the displaying data from database

- - - - -

  • Please log in to reply
9 replies to this topic

#1
Epatron

Epatron

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
Hey, I'm practicing for maybe browser based game and I got problem with displaying data..

I have made register and login codes, which are working fine.

Database there are currently Users and Userstats tables

Users tab:
Posted Image

Userstats:
Posted Image

Now I want to make code what displays ONLY logged in users stats, this guy ( Me ) have ID 1.

I seriously never get this displaying part.. please help ^^

Young student from Finland!
PS: 15-year-old! :)


#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
At first, you need to find out which users are logged in. This is done, for example, by adding a timestamp in the user table and on each page loading, you need to update the time in it.

second, I don't understand why you can't have the userdata part in the user table, you only divide into several tables if one of them will have more than one reference to the id in the another table, which seems not to be the point here. just merge them.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
Epatron

Epatron

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
I put them together now all are in users table, so how I can code it? Please don't use just words ' you need to do this and that '
give me a codes how :3

---------- Post added at 06:50 AM ---------- Previous post was at 06:38 AM ----------

I figured it out when I google some help, thanks anyways ^^!

---------- Post added at 06:50 AM ---------- Previous post was at 06:50 AM ----------


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


echo "<table border='1'>

<tr>

<th>Money:</th><th>Level:</th><th>Team:</th>

</tr>";


while($row = mysql_fetch_array($result))

	{

	echo "<tr>";

	echo "<td>" . $row['money'] . "</td>";

	echo "<td>" . $row['level'] . "</td>";

	echo "<td>" . $row['team'] . "</td>";

	echo "</tr>";

	}

echo "</table>";


Young student from Finland!
PS: 15-year-old! :)


#4
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
Be carefull, even if you get the username from the session, when the user create the username he could insert some sql injection code inside his username.
You should validate against this.

Plus, try to don't use the select *, write the name of the columns you want, it's faster

#5
Epatron

Epatron

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
This is only testing and practicing , but thanks for the advice, and I have a problem with javascript.
I haven't study a single bit of the javascript and I wanted to make countdown timer and I found from google good script ( AND ITS JS )
<form name="counter"><input type="text" size="8" 

name="d2"></form> 


<script> 

<!-- 

// 

 var milisec=0 

 var seconds=30 

 document.counter.d2.value='30' 


function display(){ 

 if (milisec<=0){ 

    milisec=9 

    seconds-=1 

 } 

 if (seconds<=-1){ 

    milisec=0 

    seconds+=1 

 } 

 else 

    milisec-=1 

    document.counter.d2.value=seconds+"."+milisec 

    setTimeout("display()",100) 

} 

display() 

--> 

</script> 
It's working, but I want to do thing that when countdown gets to 0.00 something will happen, could someone help? :)

BTW, if you know how to do this at PHP , its fine too !

Young student from Finland!
PS: 15-year-old! :)


#6
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
This is a timer, so it has to be on the client side, so we can't do it in php.

To do something when it get to 0.00 you have to change this part

 if (seconds<=-1){ 

    milisec=0 

    seconds+=1 

 } 

to something like this
 if (seconds<=-1){ 

    alert('0.00 seconds!!!')

 } 


#7
Epatron

Epatron

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
Thanks, but now when it gets to 0.00 , that alert will spam all the time, again when press ok again and again , lol :D!
So how to do it just one more time?
And is it possible, because timer is for the mission and when you press ' Do the mission ' timer will appear and when its finished (0.00s) it should
update my user stats like money +20 in database and level +1 , you know.
So can you suddenly switch in php or is it possible to js, **** it I cant code js :(

Young student from Finland!
PS: 15-year-old! :)


#8
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
 if (seconds<=-1){ 

   alert('Time up!');

 } 

 else {

    milisec-=1 

    setTimeout("display()",100) 

}

 document.counter.d2.value=seconds+"."+milisec 

You have to put the setTimeout inside the else

I'm not sure what you are trying to do in the second part.
But if you want to start it only when the user click on a button remove the line
display() 
and on your button (or link) add a onClick="display(); return false;" like so
<input type="button" onClick="display(); return false;" value="Click Me!" />

And to update data, you will need to return to the server side.
So at the end of the count down, you could either do a redirect with the data you want as parameter
window.location = "http://www.test.com?user=1&stats=4&..."
Or you could use ajax to send data to the server without reloading the page. But this could be a little more complex, if you want ajax, I suggest to use some javascript library such as jquery or prototypejs

#9
Epatron

Epatron

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
Thanks very much!!!!!!!!!!!!!!!!!!
Love you Vaielab <3 : )

Young student from Finland!
PS: 15-year-old! :)


#10
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 554 posts
  • Location:/etc/passwd
Also make sure when this timer page is loaded that time is recorded in a MySQL table or something so when the user sends the signal that the timer has completed you can check that the user isn't ust hijacking the script and has actually been on that page for x seconds.
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users