noobstar Posted April 22, 2009 Share Posted April 22, 2009 Good evening everyone I'm building a very small game to experiment with the explode() option in PHP since i never have tried it before i thought this would be a interesting way to getting to know it. In my table in the database i have a column that stores values like this: 16,12,14,6,12,1,17,1 Here is the code i have as it is now: // Takes in the values using the commas it separates $weap_pieces = explode(",", $player_inv['stats_weapons']); // Counts how many values there are in $weap_pieces & subtracts 1.. // so it starts from 0 (zero) $weap_piece_count = count($weap_pieces) - 1; $weapID=0; // I only need the IDs in other words i increment weapID by 2.. // so i take only 16,14,12 & 17 (these numbers are only examples) for($i=0;$weapID <= $weap_piece_count;$i++) { $piece[$i] = $weap_pieces[$weapID]; $weapID+=2; } My question.. finally lol.. is how do you go about sorting $piece from the lowest number to the highest so using the example from above so I get it sorted to 12,14,16,17? Thank you in advance for any help what so ever! Link to comment https://forums.phpfreaks.com/topic/155164-sort-numbers-after-explode/ Share on other sites More sharing options...
Yesideez Posted April 22, 2009 Share Posted April 22, 2009 // so it starts from 0 (zero) $weap_piece_count = count($weap_pieces) - 1 Whenever you create an array unless you're defining the indexes from the start the index will always start at 0 so I've no idea why you're counting the size of the array then subtracting 1. This goes for explode() as well. $pieces=count($$weap_pieces); for ($i=0;$i<$pieces;++$i) { echo 'Piece '.$i.' is '.$pieces[$i].'<br>'; } That will show you that it lists all items. Link to comment https://forums.phpfreaks.com/topic/155164-sort-numbers-after-explode/#findComment-816238 Share on other sites More sharing options...
noobstar Posted April 22, 2009 Author Share Posted April 22, 2009 Thank you very much for your reply Yea you got a point there though it works with what i got there at the moment, it lists them out properly as well. How do you go about sorting those values though? Link to comment https://forums.phpfreaks.com/topic/155164-sort-numbers-after-explode/#findComment-816242 Share on other sites More sharing options...
Yesideez Posted April 22, 2009 Share Posted April 22, 2009 Depends on how you want to sort them although this will give you a few pointers... http://uk2.php.net/sort Link to comment https://forums.phpfreaks.com/topic/155164-sort-numbers-after-explode/#findComment-816244 Share on other sites More sharing options...
noobstar Posted April 22, 2009 Author Share Posted April 22, 2009 Yea, I did a lot of Googling and I did check that link out since it was pretty much the first one that popped up though I couldn't make heads or tails of it. As I am seeing it now from the PHP Sort (manual) they have an Array with preset values that they sort, well i wish i could do this and believe me i've been trying for about a day and a half :S Since I don't know how many values there will be in the column within the database table I cannot preset an Array to perform the sort.. soo back to square one lol Btw, its supposed to sort from Lowest to Highest number. EDIT: I've done each thing i needed into its separate function so here is the whole code so you have an general idea of what i'm trying to do function weapon_fill() { $username = $_SESSION['player_ID']; $player_inv_q = mysql_query("SELECT * FROM `mobUplayer_stats` WHERE `player_ID` = '$username'"); $weapons_q = mysql_query("SELECT * FROM `mobUitems` WHERE `items_cat` = 'weapon'"); $player_inv = mysql_fetch_assoc($player_inv_q); // IDs need to go from low to high $weap_pieces = explode(",", $player_inv['stats_weapons']); $weap_piece_count = count($weap_pieces) - 1; $weapi=0; $weapID=0; $id=0; for($i=0;$weapID <= $weap_piece_count;$i++) { $piece[$i] = $weap_pieces[$weapID]; $weapID+=2; } echo $piece[0].",".$piece[1].",".$piece[2].",".$piece[3]; while($weapon_r = mysql_fetch_assoc($weapons_q)) { echo "<tr><td align='right'>".$weapon_r['items_product']."</td>"; echo "<td> <span style='color:lightgreen'>$</span><input type='text' name='txtweap_cost[".$id."]' value='".weapon_item_event($weap_type=$id)."' style='background-color:#733030;border:none;color:lightgreen;' size='7' disabled /></td>"; echo "<td> <button onclick='if(document.getElementById(\"weap".$id."\").style.display==\"none\"){document.getElementById(\"weap".$id."\").style.display=null}else{document.getElementById(\"weap".$id."\").style.display=\"none\"}'>Buy</button></td>"; //echo "ID: ".$weapon_r['items_ID']." Weap piece ID: ".$weap_pieces[$weapID]." Weap ID: ".$weapID."<br/>"; // IF the player has the certain weapon, display Sell button if($weapi == 0) { if($weap_pieces[$weapID] == $weapon_r['items_ID']) { echo "<td><button onclick='if(document.getElementById(\"weapsell".$id."\").style.display==\"none\"){document.getElementById(\"weapsell".$id."\").style.display=null}else{document.getElementById(\"weapsell".$id."\").style.display=\"none\"}'>Sell</button></td>"; $weapID+=2; } $weapi=1; } else { $weapi=0; } echo "<td valign='top'><div id='weap".$id."' style='display:none'> <form method='post' action='admin/game_core.php?cmd=buy_weapon' style='display:inline'> <input type='text' name='weapon_".$weapon_r['items_product']."' size='2' maxlength='6' /> units <input type='submit' name='weap".$weapon_r['items_ID']."_Done' value='Done' /> </form></div></td>"; $weapID--; echo "<td valign='top'><div id='weapsell".$id."' style='display:none'> <form method='post' action='admin/game_core.php?cmd=sell_drug' style='display:inline'> <input type='text' name='drug_".$weapon_r['items_product']."' value='".$drug_pieces[$drugID]."' size='2' maxlength='6' /> Kg <input type='submit' name='drug".$weapon_r['items_ID']."_Done' value='Done' /> </form></div></td></tr>"; $weapID++; $id++; } } There is a whole heap of it so i highly doubt you'd wanna waste your time reading through it :S Basically speaking why I am trying to sort the numbers of $piece from lowest to highest is so i can check it one by one against the loading items (weapons) and display a Sell button next to the Buy button Link to comment https://forums.phpfreaks.com/topic/155164-sort-numbers-after-explode/#findComment-816247 Share on other sites More sharing options...
Yesideez Posted April 22, 2009 Share Posted April 22, 2009 Once you explode() your data into an array using your code: $weap_pieces = explode(",", $player_inv['stats_weapons']); Try this: $arrSorted=sort($weap_pieces); That should sort everything for you. Please explain if I'm missing something. Link to comment https://forums.phpfreaks.com/topic/155164-sort-numbers-after-explode/#findComment-816249 Share on other sites More sharing options...
noobstar Posted April 22, 2009 Author Share Posted April 22, 2009 Sadly no dice it throws out an error Warning: sort() expects parameter 1 to be array, null given in /hermes/bosweb/web279/b2793/ipw.ab270387/public_html/gameomen/admin/game_core.php I tried that as well yesterday but this wouldn't work with what im trying to do, here i'll give you an example on why: Take the these as an example: 16(item_ID),12(quantity),14(item_ID),6(quantity) If I sorted this which would be inside the $weap_pieces it would completely bring the order into chaos hence me using $piece to grab the item_IDs only and then trying to sort them out. I hope your catching on what i'm saying.. im generally terrible at explaning stuff haha Link to comment https://forums.phpfreaks.com/topic/155164-sort-numbers-after-explode/#findComment-816262 Share on other sites More sharing options...
Yesideez Posted April 22, 2009 Share Posted April 22, 2009 So you don't want the items returned as 6,12,14,16? Link to comment https://forums.phpfreaks.com/topic/155164-sort-numbers-after-explode/#findComment-816278 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.