Mysidia Adoptables Support Forum

Mysidia Adoptables Support Forum (http://www.mysidiaadoptables.com/forum/index.php)
-   Questions and Supports (http://www.mysidiaadoptables.com/forum/forumdisplay.php?f=18)
-   -   MySQL + Arrays? (http://www.mysidiaadoptables.com/forum/showthread.php?t=2421)

PTGigi 09-29-2011 03:21 PM

MySQL + Arrays?
 
Right, so having an issue here >.> I have two arrays I'm trying to make (one of which is working fine :3) but the second one seems to be running into an issue with the fact I'm trying to call an array from a MySQL cell.

So basically this MySQL cell has the phrase "3,4,5,6" (no quotes). It is set to a variable $adoptid which I'm trying to put in an array. Problem is it treats the whole variable as an array. So instead of using '3', '4', '5', and '6' as variables in the array it treats "3,4,5,6" as a variable in the array. (if that just made sense...)

Does anyone know a way I can put values into a MySQL cell and then pick one randomly from an array?

Trying to do something like:
PHP Code:

$variable = array($adoptid); 

Fails because of the issue mentioned in the second paragraph D: And trying to put "array(3,4,5,6)" in a cell fails as well D: (I don't even know what it does but basically it picks from the characters o-o (ie a,r,r,a,y, etc))

Hall of Famer 09-29-2011 03:33 PM

Well you cannot send an array or object directly into mysql database, but there is a way to do the trick. Lets assume you have an array as below, the script will be able to perform magic for you:

PHP Code:

$idarray = array(1,2,3,4,5);
$idstring implode(","$idarray);
mysql_query("UPDATE table {$prefix}.adoptables SET idstring = '{$idstring}' WHERE id = '$id'"); 

This way your id array will be stored as a string in the table prefix.adoptables, column idstring, the implode function converts an array into a string.

To retrieve this data from your mysql table, use the explode function:
PHP Code:

$result mysql_query("SELECT * from table {$prefix}.adoptables WHERE id = '$id'");
$row mysql_fetch_array($result);
$idarray explode(","$row['idstring']); 

Its also possible to generate a random number from the id array, I will explain to you later after I return from dinner.

PTGigi 09-29-2011 05:00 PM

Ah thank you! :D That worked perfectly!

And I do have a random feature right now, but it seems to be picking the lower numbers quite more often than it should D:

PHP Code:

            //ID ARRAY
            
$input explode(","$adoptid);
            
$rand_keys array_rand($input2);
            
$species $input[$rand_keys[0]]; 

I ran a quick test and made that loop 20 times. 3 showed up eight times, 4 eight times as well, 5 four times, and 6 zero times D: The array I used was "3,4,5,6"

(then I ran the loop again and 5/6 didn't appear at all D: )

Hall of Famer 09-29-2011 06:06 PM

Well the problem is that you are choosing two random values from the array input[], while the function array_rand() automatically sort the two generated values. The possible combinations from the array $rand_keys[] is:

PHP Code:

$rand_keys[0] = 3$rand_keys[1] = 4
$rand_keys
[0] = 3$rand_keys[1] = 5
$rand_keys
[0] = 3$rand_keys[1] = 6
$rand_keys
[0] = 4$rand_keys[1] = 5
$rand_keys
[0] = 4$rand_keys[1] = 6
$rand_keys
[0] = 5$rand_keys[1] = 

As you can see from the above 6 combinations, the first element $rand_keys[0] never contains 6, while the second element $rand_keys[1] never contains 3. If you print the first element to the screen, it has 1/2 chance to be 3, 1/3 chance to be 4, and 1/6 chance to be 5. This is why 5 appears in a much smaller chance than 3 and 4, while 6 never shows up. Hope this explanation is clear enough, I personally would not use the function array_rand() to generate more than one random numbers.

There are many ways to resolve this glitch. If you insist on working with array_rand() function, Id recommend you use PHP shuffle which destroys the orders in an array and randomize it. The instruction on how to use shuffle can be found from php's official site:
http://php.net/manual/en/function.shuffle.php

PTGigi 09-29-2011 07:10 PM

Hm, well the thing is if I try to get only one output it just doesn't output anything D: So I have to leave the two there for it to work at all D: But I will check out that link you gave :3 Thank you!

Hall of Famer 09-29-2011 08:31 PM

umm what do you mean by that? It is supposed to work however many output you use. Mind showing me your entire script so I will see what is wrong with it?

PTGigi 09-30-2011 06:37 AM

What I mean is if I change
PHP Code:

            //ID ARRAY
            
$input explode(","$adoptid);
            
$rand_keys array_rand($input2);
            
$species $input[$rand_keys[0]]; 

to

PHP Code:

            //ID ARRAY
            
$input explode(","$adoptid);
            
$rand_keys array_rand($input1);
            
$species $input[$rand_keys[0]]; 

The output is nothing. D:

And this is my code: (it's for the Altara site so it's not Mysidia code at all >.<" I hope it's alright to ask for help with that here o-o)
PHP Code:

<?php
define
('IN_PHPBB'true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH './forum/';
$phpEx substr(strrchr(__FILE__'.'), 1);
include(
$phpbb_root_path 'common.' $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();
$place $_GET['place'];
?>
<?php 
include('head.html'); 

if (
$place == "center"){
echo 
'Altara - Adoption Center';
}
else if (
$place == "shop"){
echo 
'Altara - Shopping District';
}
else {
echo 
'Altara - Adopt';
}
include(
'head2.html'); 
if (
$place == "center"){
echo 
'Adoption Center';
}
else if (
$place == "shop"){
echo 
'Shopping District';
}
else {
echo 
'Adopt';
}
?>
</h2>
<div class="story">
<p>
<?php
$result 
mysql_query("SELECT * FROM forum_banlist WHERE ban_userid =  ".$user->data['user_id']) or die(mysql_error());
if (
$user->data['user_id'] == ANONYMOUS)
{
   echo 
'Please <a href="http://www.altara.99webs.info/forum/ucp.php?mode=login">login</a> or <a href="http://www.altara.99webs.info/forum/ucp.php?mode=register">register</a> to access more of the site.';
}
else if (
$result == "TRUE"){
   echo 
'You have been banned from access to this site.';
}
else
{
$sql_result mysql_query("SELECT * FROM adopt_user WHERE uid =  '".$user->data['user_id']."'") or die(mysql_error());    
    if(
$sql_result=TRUE){
    
        
$adoptid $_POST['adoptid'];
        
$arraynum $_POST['arraynum'] - 1;
        
$price1 $_POST['price1'];
        
$price2 $_POST['price2'];
    
        
$sql_result mysql_query("SELECT * FROM adopt_user WHERE uid =  '".$user->data['user_id']."'") or die(mysql_error());    
        while(
$row mysql_fetch_array($sql_result))
            {
            
$usercash1 $row['cash'];
            
$usercash2 $row['cash2'];
            }
            
//ID ARRAY
            
$input explode(","$adoptid);
            
$rand_keys array_rand($input2);
            
$species $input[$rand_keys[0]];

        
$result mysql_query("SELECT * FROM adopt_species WHERE id =  '".$adoptid."'") or die(mysql_error());    
        while(
$row mysql_fetch_array($result))
            {
            
$genratio $row['genratio'];
            
$eggimg $row['eggimg'];
            
$hp $row['defhp'];
            
$att $row['defatt'];
            
$def $row['defdef'];
            
$spd $row['defspd'];
            
$trade $row['trade'];
            
$breed $row['breed'];
            
$limit $row['ownlimit'];
            
$itemup $row['itemup'];
            
$tradeup $row['tradeup'];
            
$friendup $row['friendup'];
            
$growthrate $row['growthrate'];
            
$specname $row['name'];
            }
        
$newcash1 $usercash1 $price1;
        
$newcash2 $usercash2 $price2;
        
        if (
$newcash1 || $newcash2 0){
            
//GENDERS
            
if ($genratio == "male"){
                
$gender "Male";
            }
            else if (
$genratio == "female"){
                
$gender "Female";
            }
            else if (
$genratio == "moremale"){
                
$rand rand(1,4);
                if (
$rand == 1){
                    
$gender "Female";
                }
                else {
                    
$gender "Male";
                }
            }
            else if (
$genratio == "morefemale"){
                
$rand rand(1,4);
                if (
$rand == 1){
                    
$gender "Male";
                }
                else {
                    
$gender "Female";
                }
            }
            else if (
$genratio == "50"){
                
$rand rand(0,1);
                if (
$rand == 1){
                    
$gender "Female";
                }
                else {
                    
$gender "Male";
                }
            }
            else {
                
$gender "Ungendered";
            }
            
//NATURES
            
$input = array("Bossy""Brave""Calm""Cautious""Curious""Deceitful""Docile""Excitable""Loyal""Funny""Hardy""Hasty""Kind""Lonely""Modest""Moody""Naive""Nervous""Quiet""Quirky""Rash""Reserved""Serious""Shy""Sincere");
            
$rand_keys array_rand($input2);
            
$nature $input[$rand_keys[0]];

            
mysql_query("INSERT INTO adopt_adopts VALUES ('0','".$user->data['user_id']."', 'Unnamed', '".$gender."', '".$nature."', '".$hp."', '".$hp."', '".$att."', '".$def."', '".$spd."', '0', '0', '".$eggimg."', '".$species."', '".$trade."', '".$breed."', '0', '0', '".$user->data['user_id']."', '0', '0', '0', '".$itemup."', '".$tradeup."', '".$friendup."', '".$growthrate."', '".$specname."')") or die(mysql_error());
            
//id,owner,name,gender,nature,currenthp,maxhp,att,def,spd,stage,exp,image,species,trade,breed,traded,friendship,ot,mother,father,group,itemup,tradeup,friendup,growthrate, speciesname
            
$newid mysql_insert_id();        
            echo
'<p><strong>Congrats!</strong> You\'ve just recieved a new egg! </p>
            <img src="'
.$eggimg.'">
            <p>You can click <a href="/feed.php?id='
.$newid.'">here</a> to warm the egg and help it grow, or you can click <a href="/creature.php?id='.$newid.'">here</a> to view the newly recieved egg.</p>';
        }
        else {
            echo 
'Sorry there was an issue! Please try again. If the problem persists, contact the Admin or ask for help on the forums';
        }
    }
    else {
       echo 
$user->data['username'] . ' you have NOT completed registration! Please go <a href="/complete.php">here</a> to complete your registration to fully access the adoptable portion of this site.';
    }
}
?>
<?php 
include('foot.html'); ?>

I haven't gotten around to checking that link out, but I should be able to during school today :3

Hall of Famer 09-30-2011 07:32 AM

Well, change:

PHP Code:

$species $input[$rand_keys[0]]; 

To this then:

PHP Code:

$species $input[$rand_keys]; 

Remember array_rand() returns a string if the number is set to be 1, it returns an array if the number is greater than 1.

PTGigi 09-30-2011 08:06 AM

You sir, are amazing 8D

Thank you very much! :D

Hall of Famer 09-30-2011 11:18 AM

You are very welcome, glad I can be of any help. It seems that you are using object-oriented codes in your script, are you comfortable with classes/objects already?


All times are GMT -5. The time now is 09:17 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.