Ruddy Posted February 26, 2012 Share Posted February 26, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/257831-printing-tablessql-getting-values-from-each-row/ Share on other sites More sharing options...
guinbRo Posted February 26, 2012 Share Posted February 26, 2012 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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/257831-printing-tablessql-getting-values-from-each-row/#findComment-1321478 Share on other sites More sharing options...
Ruddy Posted February 26, 2012 Author Share Posted February 26, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/257831-printing-tablessql-getting-values-from-each-row/#findComment-1321494 Share on other sites More sharing options...
Ruddy Posted February 26, 2012 Author Share Posted February 26, 2012 Bump, please guys I really need to get this done ASAP! Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/257831-printing-tablessql-getting-values-from-each-row/#findComment-1321515 Share on other sites More sharing options...
litebearer Posted February 26, 2012 Share Posted February 26, 2012 show us the WHOLE script sans sensitive data (db username/password) Quote Link to comment https://forums.phpfreaks.com/topic/257831-printing-tablessql-getting-values-from-each-row/#findComment-1321526 Share on other sites More sharing options...
Ruddy Posted February 26, 2012 Author Share Posted February 26, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/257831-printing-tablessql-getting-values-from-each-row/#findComment-1321528 Share on other sites More sharing options...
Ruddy Posted February 26, 2012 Author Share Posted February 26, 2012 Fixed my own problem guys, thanks for the replys tho! Quote Link to comment https://forums.phpfreaks.com/topic/257831-printing-tablessql-getting-values-from-each-row/#findComment-1321530 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.