Eion
05-21-2008, 04:39 AM
===
These are the collected PHPQuestion threads I started and was helped with before the HostMatrix forums crashed. I hope they are helpful to anyone looking for some answers to the questions I posed, and I'd like to take the opportunity to re-thank all those who helped me when I first asked these questions.
===
Question 1:
Is there any way to SELECT the LAST 10 entries of a table WHERE id=$id?
I am writing the code for the "events" page for my game, and I wanted to create two while loops, one for the player's last 10 events, and one for the last 10 public events of the game. I only really want to show the last 10 events from the table where it is from their userid (in the first case) and where it is a public event (in the second case). So I only actually need the last 10 events shown, but dont know the code or line for that.
Posted by Tim:
SELECT * FROM `eventstable` WHERE id=$id DESC LIMIT 10
Posted by Eion:
Would DESC LIMIT 10 turn out the last 10 entries, or the first 10? My events table is ever growing, each time an event occurs it adds a new row to suit the event, and so I only want the last 10 entries that pertain to the player to show.
P.S. Just looked it up, appears to me that the answer is yes.
Posted by Tim:
By default, mySQL will return the results in beginning-to-end-order (ASC). DESC returns them in "descending order:" it orders them by ID, and starts with the highest number (which happens to be the most recent) and descends to the lowest. After 10 results are found, it stops.
This should work.
Question 2:
I was wondering what the best way for me to keep an item selected from one code to the next. Let me explain the situation in my game I need solved for:
1. Player goes to her/his military page, which displays all her/his available units.
2. Player selects the unit which they want to order.
(this sends the player to the Unit Orders Page)
3. Player selects an order for the unit.
(this begins the action by altering the unit data)
I know how to get all the units to show up on the first page (1) but I just dont now how to get the code to give a unique link to each unique unit. This is an issue which also demands a dynamic URL, which I do not know how to work out yet... I assume that each unit would need a dynamic URL created for it and then used for the orders page.
So anyone who could help me with either selecting an item over the pages or with setting up a dynamic URL system that would be extremely helpful to me.
Posted by Quantumstate:
I think the easiest solution for something like this is to use the way of passing variables with GET. So if the url you can have something like page.html?unit=4 then with the php you can access it using $_GET['unit'] and easily write it onto other urls to pass it forward onto another page. If your database gives id's to the units, which it should then it is probably best to use these rather than something like the unit name because you avoid any issues with whitespace etc. being changed to stuff that doesn't match your database.
Posted by Kendall14:
JUs tlike he said. A good way to use get variables is like this.
<?php
if(isset($_GET['unit']))
{
$unitType=$_GET['unit'];
//oder some units
}
?>
Posted by Eion:
I am not sure I know how to make a dynamic LINK to a dynamic URL.. how would I go about writing a code that would create such a dynamic link that would bring the page to the proper unit page?
Thanks again!
Posted by Kendall14:
ok what u need to do is set up a order page or something so they can tell you what kind of unit they what. lik this:
<FORM name=form action="units.php" method=get>
<select name="unit">
<option value ="soilder">soilder</option>
<option value ="tank">tank</option>
<option value ="bfg-">BFG</option>
</select>
<input type="submit" value="Submit">
</form>
now when they submit that form it will send the data using the GET method which is when it puts the form values in the URL (as apposed to POST which is sent different;y out of sight). Now in your php you will need to look for the variables from the form using the GET method which will show up in the $_GET variable. So your php code would be similar to this:
<?php
if(isset($_GET['unit']))
{
$unitType=$_GET['unit']
//code to process there order and such
}
?>
so if they chose soilder then $_GET['unit'] would equal soilder and if they picked tank it would be tank. But if the picked BFG it would be bfg- see how it works?
Posted by Eion:
There is still something which I dont know how to do which I need for this type of a code, I dont know how to properly explain it so I will do the best that I can.
Using the above method, I can create several drop down boxes for an Orders page. Say I am ordering a planet to build a frigate. I have three drop down boxes in this, Planet for the order to be carried out on, build as an order and the unit frigate to be built on the selected planet. Now this works if the planet a player would control would be static, but I dont know how to integrate a dynamic aspect to this system, where the code would need to accommodate for an increasing or decreasing number of planets available to the player.
This is why I was going to try to take a more dynamic approach to building the code, where each planet and each unit would have its own unique "Homepage" as sorts, where it would be Overview=planet=1 or Overview=planet=4 for the first and fourth planets in the database. If that is the case, where each planet and unit would have a unique page to command it from, then I do not know how to make the code create the A HREF link that would correspond to it using something like a WHILE command.
I hope this makes sense, and I hope that its not too much trouble to ask for. Any help is much appreciated.
The way I was thinking about this would be like on forums, where you have a page for members that say u=#### the HostMatrix forum example is:
http://forums.hostmatrix.org/member.php?u=9196
I'd like to create a code that creates a new page, or at least an order form for each unit/planet etc, so wouldn't it need to be something like the above? I just have no idea how to create a page like the above that would be created when a new unit comes into existence but then will be deleted (or at least useless) if the unit is destroyed.
Maybe I am totally thinking along the wrong lines with this, and if you think of any better way to do it I'd be grateful, but for at least the planets pages I wanted something like the above that would be a unique page for each planet.. Hope that helps conceptualize what I am trying to do..
Posted by Kendall14:
That's not a problem at all. what your going to need is a database which will have a record for each player saying what they can order and can't. From there you would make calls to your database and if they are allowed to make something you would add it to the list if not then you don't worry bout it. If you would like you can catch me on yahoo IM and ask me stuff your confused about.
These are the collected PHPQuestion threads I started and was helped with before the HostMatrix forums crashed. I hope they are helpful to anyone looking for some answers to the questions I posed, and I'd like to take the opportunity to re-thank all those who helped me when I first asked these questions.
===
Question 1:
Is there any way to SELECT the LAST 10 entries of a table WHERE id=$id?
I am writing the code for the "events" page for my game, and I wanted to create two while loops, one for the player's last 10 events, and one for the last 10 public events of the game. I only really want to show the last 10 events from the table where it is from their userid (in the first case) and where it is a public event (in the second case). So I only actually need the last 10 events shown, but dont know the code or line for that.
Posted by Tim:
SELECT * FROM `eventstable` WHERE id=$id DESC LIMIT 10
Posted by Eion:
Would DESC LIMIT 10 turn out the last 10 entries, or the first 10? My events table is ever growing, each time an event occurs it adds a new row to suit the event, and so I only want the last 10 entries that pertain to the player to show.
P.S. Just looked it up, appears to me that the answer is yes.
Posted by Tim:
By default, mySQL will return the results in beginning-to-end-order (ASC). DESC returns them in "descending order:" it orders them by ID, and starts with the highest number (which happens to be the most recent) and descends to the lowest. After 10 results are found, it stops.
This should work.
Question 2:
I was wondering what the best way for me to keep an item selected from one code to the next. Let me explain the situation in my game I need solved for:
1. Player goes to her/his military page, which displays all her/his available units.
2. Player selects the unit which they want to order.
(this sends the player to the Unit Orders Page)
3. Player selects an order for the unit.
(this begins the action by altering the unit data)
I know how to get all the units to show up on the first page (1) but I just dont now how to get the code to give a unique link to each unique unit. This is an issue which also demands a dynamic URL, which I do not know how to work out yet... I assume that each unit would need a dynamic URL created for it and then used for the orders page.
So anyone who could help me with either selecting an item over the pages or with setting up a dynamic URL system that would be extremely helpful to me.
Posted by Quantumstate:
I think the easiest solution for something like this is to use the way of passing variables with GET. So if the url you can have something like page.html?unit=4 then with the php you can access it using $_GET['unit'] and easily write it onto other urls to pass it forward onto another page. If your database gives id's to the units, which it should then it is probably best to use these rather than something like the unit name because you avoid any issues with whitespace etc. being changed to stuff that doesn't match your database.
Posted by Kendall14:
JUs tlike he said. A good way to use get variables is like this.
<?php
if(isset($_GET['unit']))
{
$unitType=$_GET['unit'];
//oder some units
}
?>
Posted by Eion:
I am not sure I know how to make a dynamic LINK to a dynamic URL.. how would I go about writing a code that would create such a dynamic link that would bring the page to the proper unit page?
Thanks again!
Posted by Kendall14:
ok what u need to do is set up a order page or something so they can tell you what kind of unit they what. lik this:
<FORM name=form action="units.php" method=get>
<select name="unit">
<option value ="soilder">soilder</option>
<option value ="tank">tank</option>
<option value ="bfg-">BFG</option>
</select>
<input type="submit" value="Submit">
</form>
now when they submit that form it will send the data using the GET method which is when it puts the form values in the URL (as apposed to POST which is sent different;y out of sight). Now in your php you will need to look for the variables from the form using the GET method which will show up in the $_GET variable. So your php code would be similar to this:
<?php
if(isset($_GET['unit']))
{
$unitType=$_GET['unit']
//code to process there order and such
}
?>
so if they chose soilder then $_GET['unit'] would equal soilder and if they picked tank it would be tank. But if the picked BFG it would be bfg- see how it works?
Posted by Eion:
There is still something which I dont know how to do which I need for this type of a code, I dont know how to properly explain it so I will do the best that I can.
Using the above method, I can create several drop down boxes for an Orders page. Say I am ordering a planet to build a frigate. I have three drop down boxes in this, Planet for the order to be carried out on, build as an order and the unit frigate to be built on the selected planet. Now this works if the planet a player would control would be static, but I dont know how to integrate a dynamic aspect to this system, where the code would need to accommodate for an increasing or decreasing number of planets available to the player.
This is why I was going to try to take a more dynamic approach to building the code, where each planet and each unit would have its own unique "Homepage" as sorts, where it would be Overview=planet=1 or Overview=planet=4 for the first and fourth planets in the database. If that is the case, where each planet and unit would have a unique page to command it from, then I do not know how to make the code create the A HREF link that would correspond to it using something like a WHILE command.
I hope this makes sense, and I hope that its not too much trouble to ask for. Any help is much appreciated.
The way I was thinking about this would be like on forums, where you have a page for members that say u=#### the HostMatrix forum example is:
http://forums.hostmatrix.org/member.php?u=9196
I'd like to create a code that creates a new page, or at least an order form for each unit/planet etc, so wouldn't it need to be something like the above? I just have no idea how to create a page like the above that would be created when a new unit comes into existence but then will be deleted (or at least useless) if the unit is destroyed.
Maybe I am totally thinking along the wrong lines with this, and if you think of any better way to do it I'd be grateful, but for at least the planets pages I wanted something like the above that would be a unique page for each planet.. Hope that helps conceptualize what I am trying to do..
Posted by Kendall14:
That's not a problem at all. what your going to need is a database which will have a record for each player saying what they can order and can't. From there you would make calls to your database and if they are allowed to make something you would add it to the list if not then you don't worry bout it. If you would like you can catch me on yahoo IM and ask me stuff your confused about.