summerpewp Posted December 14, 2007 Share Posted December 14, 2007 Ok, well i'm new to these forums but i can't find a solution to this to save my life... i figured someone here might know the answer. I'm fairly new to php so bare with my code... as I'm sure it has flaws... I'm trying to create a database for a friend who is into online games and he needs a weapons database to list all the weapons in specific categories. For instance, all bows go under bow, all spears under spear. etc.. So I need a table to repeat 1 time per category with the "Title" of the class. and inside the table i want another table to be repeated in each cell with the info of the weapon. (icon, description, damage, etc) and at the end of this list, it closes and br's a few times and pops up the next class & weapons.. this is what i would like it to look like: Bow WEAPON INFO:WEAPON NAME:weapon imageweapon durabilityetc... WEAPON INFO2:WEAPON NAME2:weapon image2weapon durability2etc... WEAPON INFO3:WEAPON NAME3:weapon image3weapon durability3etc... Spear WEAPON INFO:WEAPON NAME:weapon imageweapon durabilityetc... WEAPON INFO2:WEAPON NAME2:weapon image2weapon durability2etc... WEAPON INFO3:WEAPON NAME3:weapon image3weapon durability3etc... REPEAT for all 9 weapons This is my code: <?php include 'con.php'; $query = "SELECT * FROM weapons"; $sql = mysql_query($query); while($row = mysql_fetch_array($sql)){ echo ' <table><tr><th>'.$row['class'].'</th></tr><tr><td> <table class="instable"> <tr> <th width="35px">Image</th> <th>Name</th> <th width="20px">Level</th> <th width="60px">Durability</th> <th width="60px">Attack</th> </tr> '; while($row = mysql_fetch_array($sql)){ echo ' <tr> <td width="35px"><img src="'.$row['image'].'" /> ' . '</td> <td align="left">'.$row['wname'].'</td> <td width="20px">'.$row['wlevel'].'</td> <td width="60px">'.$row['wdurability'].'</td> <td width="60px">'.$row['wattack'].'</td> </tr> '; } echo ' </table></td></tr></table> '; } ?> Thanks in advance! Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 14, 2007 Share Posted December 14, 2007 Well you can't use the same result set you have to create a new one with another select statement... Quote Link to comment Share on other sites More sharing options...
summerpewp Posted December 14, 2007 Author Share Posted December 14, 2007 can i see an example? I tried that as well, but not sure if i did it right... i had 2 query's (1 and 2) - not sure what i'm supposed to query, the way i organized my table was : ID: - int auto incre. CLASS: a varchar of one of the following (bow, spear, axe, .. etc) IMAGE: etc.. NAME: LEVEL: DURABILITY: ATTACK: .. 2 $sql's (1 and 2) and 2 while statements... so the class field is the one i want to repeat 1 time per big table.. then repeat the 7 or 8 weaopns per class then repeat again... sorry for the newbness Quote Link to comment Share on other sites More sharing options...
jcd Posted December 15, 2007 Share Posted December 15, 2007 Try this. It works. But it queries the database each time it makes a new table so is not very effecient. i'm sure it can be done with one query using recursion. <?php $q1 = 'SELECT DISTINCT class FROM weapons ORDER BY class'; $rh1 = mysql_query($q1); while ($row = mysql_fetch_row($rh1)) { echo '<table><tr><th>',$row[0],'</th></tr><tr><td> <table class="instable"><tr><th width="35px">Image</th> <th>Name</th><th width="20px">Level</th> <th width="60px">Durability</th> <th width="60px">Attack</th></tr>'; $q2 = "SELECT * FROM weapons WHERE class='$row[0]'"; $rh2 = mysql_query($q2); while ($row = mysql_fetch_assoc($rh2)) { echo '<tr> <td width="35px"><img src="'.$row['image'].'" /></td> <td align="left">'.$row['wname'].'</td> <td width="20px">'.$row['wlevel'].'</td> <td width="60px">'.$row['wdurability'].'</td> <td width="60px">'.$row['wattack'].'</td> </tr>'; } echo '</table></td></tr></table>'; } ?> Quote Link to comment Share on other sites More sharing options...
summerpewp Posted December 15, 2007 Author Share Posted December 15, 2007 awesome code works, but REALLY slow... what is this "recrussion"? Quote Link to comment Share on other sites More sharing options...
trq Posted December 15, 2007 Share Posted December 15, 2007 Its not recursion your looking for, but a mysql JOIN. Quote Link to comment Share on other sites More sharing options...
summerpewp Posted December 15, 2007 Author Share Posted December 15, 2007 awesome, i will have to learn that, thanks for the link! Quote Link to comment Share on other sites More sharing options...
jcd Posted December 15, 2007 Share Posted December 15, 2007 I think I was on the wrong track with recursion (just sent my brain round in circles), but I don't see how joins will help you since you only have one table you're extracting data from. But I'm new to PHP/SQL myself so I could be wrong there too. Meanwhile give this a try. It's not exactly pretty, but it should do what you want, fast. <?php $q = 'SELECT * FROM weapons ORDER BY class'; $rh = mysql_query($q); $num_rows = mysql_num_rows($rh); for ($i = 0; $i < $num_rows; $i++) { if (!mysql_data_seek($rh,$i)) { die("Could not seek to row $i."); } $row = mysql_fetch_assoc($rh); if ($i == 0) { $current_class = $row['class']; outer_table($row,$i,$num_rows); continue; } if ($current_class == $row['class']) { print_row($row,$i,$num_rows); continue; } else { $current_class = $row['class']; close_tables(); outer_table($row,$i,$num_rows); continue; } } function outer_table($row,$counter,$total_rows) { echo '<table><tr><th>',$row['class'],'</th></tr><tr><td> <table class="instable"><tr><th width="35px">Image</th> <th>Name</th><th width="20px">Level</th> <th width="60px">Durability</th> <th width="60px">Attack</th></tr>'; print_row($row,$counter,$total_rows); } function print_row($row,$counter,$total_rows) { echo '<tr> <td width="35px"><img src="'.$row['image'].'" /></td> <td align="left">'.$row['wname'].'</td> <td width="20px">'.$row['wlevel'].'</td> <td width="60px">'.$row['wdurability'].'</td> <td width="60px">'.$row['wattack'].'</td> </tr>'; if ( $counter + 1 == $total_rows) { close_tables(); } } function close_tables() { echo '</table></td></tr></table>'; } ?> Quote Link to comment 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.