Stipe Posted October 23, 2012 Share Posted October 23, 2012 (edited) Basically I'm working on a game and have decided to change the way banks work on it, i had it so selecting which bank you used was randomized, but i achieved this by using locations, now I've scraped them and don't know how to sort the code out to randomize with row numbers instead of location names, if that makes sense.... Here is the relevent code: $username=$_SESSION['username']; $query=mysql_query("SELECT * FROM users WHERE username='$username'"); $fetch=mysql_fetch_object($query); $bank = mysql_fetch_object(mysql_query("SELECT * FROM `bank` WHERE `location` = '".$fetch->location."'")); $fetch_owner = mysql_fetch_object(mysql_query("SELECT * FROM `users` WHERE `username` = '".$bank->owner."'")); $bankle = mysql_fetch_object(mysql_query("SELECT * FROM `bank` WHERE `location` = 'Arnhem - Netherlands'")); $bankpf = mysql_fetch_object(mysql_query("SELECT * FROM `bank` WHERE `location` = 'Barcelona - Spain'")); $bankri = mysql_fetch_object(mysql_query("SELECT * FROM `bank` WHERE `location` = 'Birmingham - England'")); $bankbg = mysql_fetch_object(mysql_query("SELECT * FROM `bank` WHERE `location` = 'New York - America'")); $bankms = mysql_fetch_object(mysql_query("SELECT * FROM `bank` WHERE `location` = 'Munich - Germany'")); $banksp = mysql_fetch_object(mysql_query("SELECT * FROM `bank` WHERE `location` = 'Sicily - Italy'")); $bankdu = mysql_fetch_object(mysql_query("SELECT * FROM `bank` WHERE `location` = 'Oslo - Norway'")); $banktc = mysql_fetch_object(mysql_query("SELECT * FROM `bank` WHERE `location` = 'Mexico City - Mexico'")); $rand = rand(1, ; if ($rand == "1"){ $selected1 = "selected"; }elseif ($rand == "2"){ $selected2 = "selected"; }elseif ($rand == "3"){ $selected3 = "selected"; }elseif ($rand == "4"){ $selected4 = "selected"; }elseif ($rand == "5"){ $selected5 = "selected"; }elseif ($rand == "6"){ $selected6 = "selected"; }elseif ($rand == "7"){ $selected7 = "selected"; }elseif ($rand == "8"){ $selected8 = "selected"; } And the HTML side: <td bgcolor="#555555" align="left"><select name="whatbank" class="select_box" id="whatbank" style="width:250px" title="The bank to transfer the funds with."> <option value="<?php echo "$bankle->id"; ?>" <?php if ($rand == "1"){ echo "selected"; } ?>><?php echo "$bankle->bank"; ?> (<?php echo "$bankle->commission"; ?>%)</option> <option value="<?php echo "$bankpf->id"; ?>" <?php if ($rand == "2"){ echo "selected"; } ?>><?php echo "$bankpf->bank"; ?> (<?php echo "$bankpf->commission"; ?>%)</option> <option value="<?php echo "$bankri->id"; ?>" <?php if ($rand == "3"){ echo "selected"; } ?>><?php echo "$bankri->bank"; ?> (<?php echo "$bankri->commission"; ?>%)</option> <option value="<?php echo "$bankbg->id"; ?>" <?php if ($rand == "4"){ echo "selected"; } ?>><?php echo "$bankbg->bank"; ?> (<?php echo "$bankbg->commission"; ?>%)</option> <option value="<?php echo "$bankms->id"; ?>" <?php if ($rand == "5"){ echo "selected"; } ?>><?php echo "$bankms->bank"; ?> (<?php echo "$bankms->commission"; ?>%)</option> <option value="<?php echo "$banksp->id"; ?>" <?php if ($rand == "6"){ echo "selected"; } ?>><?php echo "$banksp->bank"; ?> (<?php echo "$banksp->commission"; ?>%)</option> <option value="<?php echo "$bankdu->id"; ?>" <?php if ($rand == "7"){ echo "selected"; } ?>><?php echo "$bankdu->bank"; ?> (<?php echo "$bankdu->commission"; ?>%)</option> <option value="<?php echo "$banktc->id"; ?>" <?php if ($rand == "8"){ echo "selected"; } ?>><?php echo "$banktc->bank"; ?> (<?php echo "$banktc->commission"; ?>%)</option></td> Hope you guys can help me! Much appriciated! Edited October 23, 2012 by Zane code tags added Quote Link to comment https://forums.phpfreaks.com/topic/269797-dont-know-how-to-word-this/ Share on other sites More sharing options...
gizmola Posted October 23, 2012 Share Posted October 23, 2012 Sorry, but no, it's not clear what your question is, because we don't really know what the structure of your database looks like. I'm going to just take a guess that the bank table has 1 row per bank. If that is the case (and what you're doing, is completely wrong, if it is not), then rather than doing a set of hardwired queries for each bank, you should simply be doing a single query that gets all banks other than the user's originating bank. Then you can use that result to loop through it and output your select box: $transferBanksResult = mysql_query("SELECT * FROM `bank` WHERE `location` != '$fetch->location'"); if ($transferBanksResult) { $rowcount = mysql_num_rows($transferBanksResult); if ($rowcount > 0) { $rand = rand(1, $rowcount); $rowid = 1; echo '<td bgcolor="#555555" align="left"><select name="whatbank" class="select_box" id="whatbank" style="width:250px" title="The bank to transfer the funds with.">'; while ($row = mysql_fetch_assoc($transferbanksResult)) { $option = '<option value="' . $row->id . '"'; if ($rand == $rowid) { $option .= " selected"; } $option .= ">$bankle->bank($bankle->commission %)</option>"; echo $option; $rowid++; } echo '</select></td>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/269797-dont-know-how-to-word-this/#findComment-1387130 Share on other sites More sharing options...
Stipe Posted October 23, 2012 Author Share Posted October 23, 2012 Not quite what i was after but thank you suppose my explaining was a bit off lol Basically you were right about each bank having its own row, but what the script does is randomize which bank is primarily selected and you can choose another one from there, but the banks before were picked up depending where you were, instead i want it so you can create them and view anywhere, so locations in bank has been deleted and instead of picking one up and it just changing the owner in the DB its done now by creating a line up to a max and deleting if you lose it... So instead of the $bankle = mysql_fetch_object(mysql_query("SELECT * FROM `bank` WHERE `location` = 'Arnhem - Netherlands'")); part, i want it to work so the vaibles are by what row number it is rather than what location it is Quote Link to comment https://forums.phpfreaks.com/topic/269797-dont-know-how-to-word-this/#findComment-1387145 Share on other sites More sharing options...
White_Lily Posted October 23, 2012 Share Posted October 23, 2012 What I'm not quite understanding is if the system worked using locations, why scrap it? Also, you should probably plan the page before coding it so that you wouldn't have the problem of posting on a forum to help sort your code out. Quote Link to comment https://forums.phpfreaks.com/topic/269797-dont-know-how-to-word-this/#findComment-1387146 Share on other sites More sharing options...
Stipe Posted October 23, 2012 Author Share Posted October 23, 2012 What I'm not quite understanding is if the system worked using locations, why scrap it? Also, you should probably plan the page before coding it so that you wouldn't have the problem of posting on a forum to help sort your code out. Because there are 8 locations and i want 4 banks, and if you are a bank owner i want you to be able to access your bank CP from everywhere so it doesnt matter on where your bank is or where you are Quote Link to comment https://forums.phpfreaks.com/topic/269797-dont-know-how-to-word-this/#findComment-1387153 Share on other sites More sharing options...
PFMaBiSmAd Posted October 23, 2012 Share Posted October 23, 2012 You would do the following - 1) Use one query for all the rows you are interested in. 2) Get a count of the number of rows that query returned. 3) Generate a random number from 1 to the number of rows the query returned. 4) Use a for(){} loop, instead of the traditional while(){} loop to iterate over the rows in your result set. This will give you a loop counter variable that you can compare with the random number you generated in item #3. 5) As you are looping through the result of the query, if the for(){} loop counter variable is equal to the random number you generated, select the current option. Quote Link to comment https://forums.phpfreaks.com/topic/269797-dont-know-how-to-word-this/#findComment-1387160 Share on other sites More sharing options...
gizmola Posted October 24, 2012 Share Posted October 24, 2012 Not quite what i was after but thank you suppose my explaining was a bit off lol Basically you were right about each bank having its own row, but what the script does is randomize which bank is primarily selected and you can choose another one from there, but the banks before were picked up depending where you were, instead i want it so you can create them and view anywhere, so locations in bank has been deleted and instead of picking one up and it just changing the owner in the DB its done now by creating a line up to a max and deleting if you lose it... So instead of the $bankle = mysql_fetch_object(mysql_query("SELECT * FROM `bank` WHERE `location` = 'Arnhem - Netherlands'")); part, i want it to work so the vaibles are by what row number it is rather than what location it is We're really good guessers, but in all honesty, i still really don't know what you're asking for. You really need to provide the structure of your tables, and a better description of what you are trying to do, why, and how you are trying to do it. In particular this quote is inscrutable: its done now by creating a line up to a max and deleting if you lose it... Part of the problem is that you don't seem to understand the right terminology to use. I have no idea what you mean when you write "its done by creating a line up to". What is "it" and what does "done" mean? "deleting" in this context means deleting what exactly? It's all as clear as mud. Relational databases work on set theory, so there is no inherent concept of a row number. Result sets are inherently unordered until the point that you apply an order using ORDER BY. You can simulate row numbers procedurally, or using database engine specific mechanisms like LIMIT, but those concepts are only good for the lifetime of a single query. If you need to persist data beyond that, those concepts aren't usable. If I have 3 rows of fruits (apple, orange, pear) and you think of those as (1,2,3) what happens to your row numbering scheme when I insert "banana"? If you have code that tried to reference 2=orange, that code is going to be badly broken. As far as we can see you have a user table and a bank table. That is about all we know right now. It appears you want to establish some sort of relationship between a user and a bank. At one point it appears that your user table had a location, and you used that to associate a user with a bank? You have provided only snippets of code that are completely out of context and missing key pieces of information. I'm not doing you any favors by not stating outright, that your question asking skills as demonstrated in this thread are severely lacking in the area of clarity and specificity. I've put way more effort into this thread, in pointing out how vague and incomprehensible your questions and elaborations were, then you did in asking the question in the first place, and that needs to change if you want to get anywhere in this process. Quote Link to comment https://forums.phpfreaks.com/topic/269797-dont-know-how-to-word-this/#findComment-1387546 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.