Creating a table with DB items[SOLVED]

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
MikeD
Posts: 294
Joined: Thu Sep 08, 2011 4:28 am

Creating a table with DB items[SOLVED]

Post by MikeD »

Title sounds kind of weird, oh well. Sorry for posting again, I am a newbie programmer :P

Well, when a player clicks on 'Warriors' I'd like it to show a table with all their characters and some of the characters info.

However, it is only pulling the users 1st character and putting it in the table, instead of all his characters.

Here is my code

Code: Select all

<?php
error_reporting(E_ALL);
  include_once 'connect.php';
  session_start();

  if (isset($_SESSION['player']))
{
  $player=$_SESSION['player'];
}
else
{
  
  header( 'Location: http://localhost/warrior/login.php' );
  exit;
}
    $playerinfo="SELECT * from players where name='$player'";
    $playerinfo2=mysql_query($playerinfo) or die("could not get player stats!");
    $playerinfo3=mysql_fetch_array($playerinfo2);
    
    $warriorinfo="SELECT * from warriors where pname='$player'";
    $warriorinfo2=mysql_query($warriorinfo) or die("could not get player stats!");
    $warriorinfo3=mysql_fetch_array($warriorinfo2);
    
    $name=$warriorinfo3['name'];
    $level=$warriorinfo3['level'];
    $gold=$warriorinfo3['gold'];
    $speed=$warriorinfo3['speed'];
    $power=$warriorinfo3['power'];
    $melee=$warriorinfo3['melee'];
    $mage=$warriorinfo3['magic'];
    $arch=$warriorinfo3['archery'];
    $meldef=$warriorinfo3['meleedef'];
    $magedef=$warriorinfo3['magedef'];
    $archdef=$warriorinfo3['archerydef'];
    $def=$warriorinfo3['defense'];
    $vision=$warriorinfo3['vision'];
    $health=$warriorinfo3['health'];
    $agility=$warriorinfo3['agility'];
    $dungs=$warriorinfo3['dungeons'];
    $battles=$warriorinfo3['battles'];
    $type=$warriorinfo3['ptype'];
    $exp=$warriorinfo3['exper'];
    $cid=$warriorinfo3['cid'];
    
    $total= $speed + $power + $melee + $mage + $arch + $meldef + $magedef + $archdef + $def + $vision + $health + $agility;
   

    print "<p><table border='1'>";
    print "<th>Warrior</th><th>Level</th><th>Total Levels</th><th>Experience</th><th>Gold</th><th>Dungeons</th><th>Battles</th><th>Type</th><th>Play</th>";
    print "<tr>";
    print "<td>$name</td><td>$level</td><td>$total</td><td>$exp</td><td>$gold</td><td>$dungs</td><td>$battles</td><td>$type</td><td><a href='playwar.php?&warrior=$cid'>Play</a></td>";
    print "</table>"
    
    
?>
Here's the picture of what it looks like...

http://gyazo.com/fb08a554db42645277106c22b4047cc8

It should be pulling all that users characters.
Last edited by MikeD on Thu Oct 20, 2011 1:55 am, edited 1 time in total.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Creating a table with DB items.

Post by Jackolantern »

It takes a few tweaks to be able to display multiple results from the database in a table. This has been asked a few times now, so I should probably end up writing a tutorial about it. Anyway, I usually end up showing the same script, which is from a now-abandoned Pokemon-style monster collecting game I was working on a while back. When the player logged in, I wanted to pull up every monster the player owned in the database and display them all, allowing them to select one and begin the game. Here is the monster selection script, with some notes following:

Code: Select all

<?php
    include 'connect.php';
    session_start();
    $exitScript = 0;
    $username = $_SESSION['name'];
    $playerid = $_SESSION['playerid'];
    
    //Get ready to skip main script if player not logged in
    if (!$_SESSION['name']) {
        $exitScript = 1;
    }

?>
</head>
<body>
<div id="container">
    <div id="header">
        <h1>Working Title</h1>
        
    </div>
    <div id="nav">
        <ul>
            
        </ul>        
    </div>
    <div id="content">
        <div id="page">
            <?php
                //main decision block for if player not logged in
                if ($exitScript == 0){
                    echo "Choose a monster by clicking its name, or create a new monster: <br />";
                    
                    //check to see what monsters the logged in player has from the 'playermonster' table
                    $sql = "SELECT * FROM playermonster WHERE playerid = '$playerid'";
                    if ($result = mysqli_query($db, $sql)) {
                        //fetch array and set up table
                        ?>
                        <table border="3" cellpadding="3" cellspacing="0">
                        <tr>
                            <th align="center">Name</th>
                            <th align="center">Level</th>
                            <th align="center">Monster Type</th>
                            <th align="center">Monster Species</th>
                            <th align="center">Max Health</th>
                            <th align="center">Attack</th>
                            <th align="center">Defense</th>
                            <th align="center">Speed</th>
                            <th align="center">Mind</th>
                            <th align="center">Will</th>                            
                        </tr>
                        <?php 
                        while ($row = mysqli_fetch_row($result)) {                        
                            
                            //create the table to choose a monster
                            ?> 
                            
                            <tr>
                                
                                <td align="center"><?php echo("<a href='monsterselected.php?mid=$row[0]'>$row[2]</a>") ?> </td>
                                    <td align="center"><?php echo($row[13]) ?> </td>
                                    <td align="center"><?php echo($row[3]) ?> </td>
                                    <td align="center"><?php echo($row[4]) ?> </td>
                                    <td align="center"><?php echo($row[6]) ?> </td>
                                    <td align="center"><?php echo($row[8]) ?> </td>
                                    <td align="center"><?php echo($row[9]) ?> </td>
                                    <td align="center"><?php echo($row[10]) ?> </td>
                                    <td align="center"><?php echo($row[11]) ?> </td>
                                    <td align="center"><?php echo($row[12]) ?> </td>
                                                            
                            </tr>                            
                        
                        <?php //go back into PHP for rest of script
                        }
                        ?>
                        </table>
                        <br /><br />
                        <a href='createnew.php'>Create a New Monster</a><br /><br />
                        <a href='deletemonster.php'>Delete Monster</a>
                        <?php
                    } 
                    
                } else {
                    echo "You are not logged in. Please go back and log in.<br />";
                    echo "<a href='index.php'>Home Page</a>";
                }
            ?>
The important part of the script is the way I set up the table: I made the SELECT query, but without doing the final step (so I now just have a SQL query result object in the $result variable), and then I opened the table tag as well as all of the table header tags:

Code: Select all

$sql = "SELECT * FROM playermonster WHERE playerid = '$playerid'";
                    if ($result = mysqli_query($db, $sql)) {
                        //fetch array and set up table
                        ?>
                        <table border="3" cellpadding="3" cellspacing="0">
                        <tr>
                            <th align="center">Name</th>
                            <th align="center">Level</th>
                            <th align="center">Monster Type</th>
                            <th align="center">Monster Species</th>
                            <th align="center">Max Health</th>
                            <th align="center">Attack</th>
                            <th align="center">Defense</th>
                            <th align="center">Speed</th>
                            <th align="center">Mind</th>
                            <th align="center">Will</th>                            
                        </tr>
Then I started a WHILE loop to iterate through each of the results, one-by-one. Inside that loop, I make a table row, <tr>, with several table data cells in it, <td>, that each contain a piece of the data retrieved from the query. Each pass through of the WHILE loop represents one record returned from the SELECT query, so you can see how this WHILE loop would build multiple rows in the table, all filled with each monster's stats. One thing to note is that instead of being able to use associative arrays (like $row['name']), you must use a numerically indexed array with this command. So $row[0] means the first column of the retrieved record (likely the ID of whatever you are selecting), $row[1] would be the second column, $row[2] the third, etc. Also note that all through my code I use the "mysqli" extension to PHP, not "mysql". It is newer and has better performance and more security, but this code will work fine if you just remove the "i" from the method calls and also removed the reference to $db from the method calls as well (mysqli requires the link to the database connection in each call, whereas traditional mysql method calls do not):

Code: Select all

<?php 
                        while ($row = mysqli_fetch_row($result)) {                        
                            
                            //create the table to choose a monster
                            ?> 
                            
                            <tr>
                                
                                <td align="center"><?php echo("<a href='monsterselected.php?mid=$row[0]'>$row[2]</a>") ?> </td>
                                    <td align="center"><?php echo($row[13]) ?> </td>
                                    <td align="center"><?php echo($row[3]) ?> </td>
                                    <td align="center"><?php echo($row[4]) ?> </td>
                                    <td align="center"><?php echo($row[6]) ?> </td>
                                    <td align="center"><?php echo($row[8]) ?> </td>
                                    <td align="center"><?php echo($row[9]) ?> </td>
                                    <td align="center"><?php echo($row[10]) ?> </td>
                                    <td align="center"><?php echo($row[11]) ?> </td>
                                    <td align="center"><?php echo($row[12]) ?> </td>
                                                            
                            </tr>
The only thing left to do is go back into PHP (using <?php) and close the brace I opened to create the WHILE loop--you will get an error if you don't--and then close the table:

Code: Select all

<?php //go back into PHP for rest of script
                        }
                        ?>
                        </table>
If you have any questions or need any help let me know! As I usually suggest, do not copy and paste this code and then tweak it to work for you. Learn what it is doing and then write it from scratch for your game. That way you will have a much better handle on any errors that will pop up, and it would be more of a pain to copy'n'paste it anyway because of all the minute details you would have to change.
The indelible lord of tl;dr
User avatar
MikeD
Posts: 294
Joined: Thu Sep 08, 2011 4:28 am

Re: Creating a table with DB items.

Post by MikeD »

Alright, I tried that.

Got this error

Warning: mysql_query() expects parameter 1 to be string, resource given in C:\wamp\www\Warrior\warriors2.php on line 54

which is this line

if ($result = mysql_query($db, $sql)) {
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Creating a table with DB items.

Post by Jackolantern »

See my post above. You must also remove the $db database resource variable if you are changing from mysqli to mysql db extensions.
The indelible lord of tl;dr
User avatar
MikeD
Posts: 294
Joined: Thu Sep 08, 2011 4:28 am

Re: Creating a table with DB items.

Post by MikeD »

Hmm, it is pulling all the players now, nothing is in the right column though. I'll have to look at it again tomorrow.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Creating a table with DB items.

Post by Jackolantern »

If you have problems tomorrow, post your code so we can take a look :)
The indelible lord of tl;dr
User avatar
MikeD
Posts: 294
Joined: Thu Sep 08, 2011 4:28 am

Re: Creating a table with DB items.[Solved]

Post by MikeD »

Got this done now! Thanks for the help!
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Creating a table with DB items[SOLVED]

Post by Jackolantern »

No problem. Glad you got it working :)
The indelible lord of tl;dr
Post Reply

Return to “Beginner Help and Support”