Jump to content

Printing tables(SQL) & getting values from each row


Ruddy

Recommended Posts

Hey guys.

 

I have ran into a problem once again. I want to print a list of data I have in my database being monsters (ID, Name, HP, attack) I want to be able to print them fromt he database then get the "ID" of the monster being selected by a submit button to give the id of that row in some form (a POST?) so i can get the information for that monster in a SELECT when the button is hit.

 

I will try show you what im trying to do.

 

    
        //Grt the id from the table print at the bottom.
$monster_id = $_POST["monsterid"];


                //this is at the top to get stats on the monster the user has selected to fight
	$query="SELECT * FROM monsters WHERE id='$monster_id'";
  		$result=mysql_query($query);
   		$result=mysql_fetch_array($result);
	$monster_exp=$result["exp"];
	$exp_dead = round(($monster_exp/100) * 10);
	$monster_hp =$result["hp"];
	$goldwon =$result["gold"];	
	$mindam =$result["mindam"];	
	$maxdam =$result["maxdam"];


// my battle code goes here using the stats I want above.
------------------------------------------------------------------------------ 

//This is where the user selects the monster they want to fight.

$query="SELECT * FROM monsters";
$result=mysql_query($query);

$num=mysql_numrows($result);
mysql_close();

$i=0;

<form action="training.php" method="post">
<table border='0' width="400" class="tablee">
<tr>
<th>Monster</th>
<th>Level</th>
<th>HP</th>
    <th>Protection</th>
    <th>Fight?</th>
</tr>
    <?
$query="SELECT * FROM monsters";
$result=mysql_query($query);

$num=mysql_numrows($result);
mysql_close();

$i=0;

while ($i < $num) {

$id=mysql_result($result,$i,"id");
$name=mysql_result($result,$i,"name");
$level=mysql_result($result,$i,"lvl");
$hp=mysql_result($result,$i,"health");
$pro=mysql_result($result,$i,"pro");
?>
<tr>
<th><? echo $mon_name; ?> </th>
    <th><? echo $mon_level; ?> </th>
<th><? echo $mon_hp; ?> </th>
    <th><? echo $mon_pro; ?> </th>
    <th><input type="hidden" value="<? echo $mon_id; ?>" name="monsterid" />
    <input type="submit" value="Fight!" name="submit" /></th>
</tr>
<?
$i++;
}
?> 
</table>
    </form>

 

 

I hope this makes sense because I am really having problems. My gues is use an array but I could get it to work.

 

PS: I did a get and it sends ALL the IDs instead of the selected row.

 

Thanks for any help guys,

Ruddy

Link to comment
Share on other sites

Your guess that the answer is an array is correct.  So something like this will work:

 

//Let's use mysql_fetch_assoc

while($row = mysql_fetch_assoc($mysql_query)) {
  //now you can call each individual ID
  echo'<html_form_here value = ' . $row[id] . '></end_html_form_here>';
}

Link to comment
Share on other sites

Your guess that the answer is an array is correct.  So something like this will work:

 

//Let's use mysql_fetch_assoc

while($row = mysql_fetch_assoc($mysql_query)) {
  //now you can call each individual ID
  echo'<html_form_here value = ' . $row[id] . '></end_html_form_here>';
}

 

But how would the array go? Could you show a bit more? I havebeen trying for ages!

 

Cheers for the reply.

 

 

EDIT: Bump, anyone else got any ideaS?

Link to comment
Share on other sites

Im still working on it and thats all that part has. All I want is:

 

How to grab rows from the database and print them in a table

 

Grab the ID that a user selects using a button, from one of the many rows in that created table.

 

That ID will be used in a SELECT statment in the WHERE clause to select the monster they will fight.

 

I have started from scratch again so any help would be great.

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.