Mysidia Adoptables Support Forum  

Home Community Mys-Script Creative Off-Topic
Go Back   Mysidia Adoptables Support Forum > Mysidia Adoptables > Questions and Supports

Notices

Reply
 
Thread Tools Display Modes
  #1  
Old 09-29-2011, 03:21 PM
PTGigi's Avatar
PTGigi PTGigi is offline
Crazily Friendly~HoF
 
Join Date: Jul 2009
Location: Somewhere >.>
Posts: 370
Gender: Female
Credits: 26,283
PTGigi
Default 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))
__________________


"I see now that the circumstances of one's birth are irrelevant; it is what you do with the gift of life that determines who you are."~Mewtwo
My Adoptables|Nuzlocke Webcomic
Reply With Quote
  #2  
Old 09-29-2011, 03:33 PM
Hall of Famer's Avatar
Hall of Famer Hall of Famer is offline
Administrator, Lead Coder
 
Join Date: Dec 2008
Location: South Brunswick
Posts: 4,448
Gender: Male
Credits: 333,363
Hall of Famer is on a distinguished road
Default

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.
__________________


Mysidia Adoptables, a free and ever-improving script for aspiring adoptables/pets site.
Reply With Quote
  #3  
Old 09-29-2011, 05:00 PM
PTGigi's Avatar
PTGigi PTGigi is offline
Crazily Friendly~HoF
 
Join Date: Jul 2009
Location: Somewhere >.>
Posts: 370
Gender: Female
Credits: 26,283
PTGigi
Default

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: )
__________________


"I see now that the circumstances of one's birth are irrelevant; it is what you do with the gift of life that determines who you are."~Mewtwo
My Adoptables|Nuzlocke Webcomic
Reply With Quote
  #4  
Old 09-29-2011, 06:06 PM
Hall of Famer's Avatar
Hall of Famer Hall of Famer is offline
Administrator, Lead Coder
 
Join Date: Dec 2008
Location: South Brunswick
Posts: 4,448
Gender: Male
Credits: 333,363
Hall of Famer is on a distinguished road
Default

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
__________________


Mysidia Adoptables, a free and ever-improving script for aspiring adoptables/pets site.
Reply With Quote
  #5  
Old 09-29-2011, 07:10 PM
PTGigi's Avatar
PTGigi PTGigi is offline
Crazily Friendly~HoF
 
Join Date: Jul 2009
Location: Somewhere >.>
Posts: 370
Gender: Female
Credits: 26,283
PTGigi
Default

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!
__________________


"I see now that the circumstances of one's birth are irrelevant; it is what you do with the gift of life that determines who you are."~Mewtwo
My Adoptables|Nuzlocke Webcomic
Reply With Quote
  #6  
Old 09-29-2011, 08:31 PM
Hall of Famer's Avatar
Hall of Famer Hall of Famer is offline
Administrator, Lead Coder
 
Join Date: Dec 2008
Location: South Brunswick
Posts: 4,448
Gender: Male
Credits: 333,363
Hall of Famer is on a distinguished road
Default

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?
__________________


Mysidia Adoptables, a free and ever-improving script for aspiring adoptables/pets site.
Reply With Quote
  #7  
Old 09-30-2011, 06:37 AM
PTGigi's Avatar
PTGigi PTGigi is offline
Crazily Friendly~HoF
 
Join Date: Jul 2009
Location: Somewhere >.>
Posts: 370
Gender: Female
Credits: 26,283
PTGigi
Default

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
__________________


"I see now that the circumstances of one's birth are irrelevant; it is what you do with the gift of life that determines who you are."~Mewtwo
My Adoptables|Nuzlocke Webcomic
Reply With Quote
  #8  
Old 09-30-2011, 07:32 AM
Hall of Famer's Avatar
Hall of Famer Hall of Famer is offline
Administrator, Lead Coder
 
Join Date: Dec 2008
Location: South Brunswick
Posts: 4,448
Gender: Male
Credits: 333,363
Hall of Famer is on a distinguished road
Default

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.
__________________


Mysidia Adoptables, a free and ever-improving script for aspiring adoptables/pets site.
Reply With Quote
  #9  
Old 09-30-2011, 08:06 AM
PTGigi's Avatar
PTGigi PTGigi is offline
Crazily Friendly~HoF
 
Join Date: Jul 2009
Location: Somewhere >.>
Posts: 370
Gender: Female
Credits: 26,283
PTGigi
Default

You sir, are amazing 8D

Thank you very much! :D
__________________


"I see now that the circumstances of one's birth are irrelevant; it is what you do with the gift of life that determines who you are."~Mewtwo
My Adoptables|Nuzlocke Webcomic
Reply With Quote
  #10  
Old 09-30-2011, 11:18 AM
Hall of Famer's Avatar
Hall of Famer Hall of Famer is offline
Administrator, Lead Coder
 
Join Date: Dec 2008
Location: South Brunswick
Posts: 4,448
Gender: Male
Credits: 333,363
Hall of Famer is on a distinguished road
Default

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?
__________________


Mysidia Adoptables, a free and ever-improving script for aspiring adoptables/pets site.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hall of Famer's PHP Tutorials: Chapter 6 - PHP Arrays Hall of Famer Tutorials and Tips 0 03-16-2013 07:14 PM
My MySQL? Rozel Questions and Supports 6 05-01-2011 08:53 AM
Mysql help? rickasawr Questions and Supports 5 04-26-2011 09:31 AM
MySQL: What to do? Knyfe Questions and Supports 18 03-11-2011 12:52 PM
mySQL help aroymart Questions and Supports 4 04-20-2009 12:13 AM


All times are GMT -5. The time now is 05:32 AM.

Currently Active Users: 9731 (0 members and 9731 guests)
Threads: 4,080, Posts: 32,024, Members: 2,016
Welcome to our newest members, jolob.
BETA





What's New?

What's Hot?

What's Popular?


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
vBCommerce I v2.0.0 Gold ©2010, PixelFX Studios
vBCredits I v2.0.0 Gold ©2010, PixelFX Studios
Emoticons by darkmoon3636