Reply
Arrays and Classes
Old 04-28-2008, 01:55 PM Arrays and Classes
amw_drizz's Avatar
Ultra Talker

Posts: 301
Name: Jon
Location: New York
Okay I am working on a database interaction script. And it is a class so I am trying to get it so I only have to write it out once then just call it every time I need to connect to the db.

And I am looking at it would be easier to put the field information (like all the inputed data) in an array and send the field count as well to try and break the array apart and gather the information in the array and then use it to work with the db.

my test script so i can get it working
PHP Code:
<?php
if(!isset($_POST['Submit'])){
?>

<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <label>test
  <input type="text" name="textfield" />
  </label>
  <input type="text" name="textfield1" />
  <input type="text" name="textfield2" />
  <label>
  <input type="submit" name="Submit" value="Submit" />
  </label>
</form>
<?php
}
else{
define('allowed','allowed');
require_once(
"config/config.php");
$values = array(
            
"0" => $_POST['textfield'],
            
"1" => $_POST['textfield1'],
            
"2" => $_POST['textfield2'],

            );
$DB->addinfo('users',$values,'3');
}
?>
and the part of my class that will format the info, sterilize the data in a basic form, and then add it
PHP Code:
    function sterilize($values)
    {
        foreach(
$values as $key => $value)
        {
            if(
is_array($value))
            {
                
$values[$key] = $this->sterilize($value);
            }
            else
            {
                
$values[$key] = mysql_real_escape_string($value);
            }
        }
        
$data $values;
        
$data array_values($data);
        return 
$data;    
    }
    function 
formatinput($values,$fields)
    {
        
$i=0;
        do{
        echo 
"<br>";
        echo 
$i;
        echo 
"<br>";
            
$input $values[$i];
            echo 
$input;
            
$i++;
        }
        while(
$i $fields);
        return 
$input;
    }    
    function 
addinfo($table,$values,$fields)
    {    
        echo 
$table;
        echo 
"<br>";
    
//    print_r( $values );
        
$input $this->sterilize($values);
        
$layout $this->tablefields($table);    
        
print_rexplode(" ",$values) );    
        
//$input = $this->formatinput($input,$fields);
    //    echo $input;
        
$query sprintf("INSERT INTO %s (%s) VALUES(%s)",$table,$layout,$this->formatinput($input,$fields));
        
//$res = mysql_query($query,$this->DB);
        
echo $query;
    } 
and when i try the explode method it just states Array. All I want to do is take the values array and remove all the [0] => [1] => etc and just use the data from the form fields.
__________________
AMW_Drizz
Php 5.2 Mysql 4.1 IIS 6 Win2k3 Server && PHP 5.2 MySQL 4.1 Apache 2.2 (separate machine of course)
amw_drizz is offline
Reply With Quote
View Public Profile Visit amw_drizz's homepage!
 
When You Register, These Ads Go Away!
Old 04-28-2008, 04:37 PM Re: Arrays and Classes
VirtuosiMedia's Avatar
Webmaster Talker

Posts: 738
Are you sure you aren't over-complicating it?

PHP Code:
function addInfo ($table$column$values) {
     if (
is_array($values)) { 
          foreach (
$values as $value) {
               
$value mysql_real_escape_string($value);
               
$query sprintf("INSERT INTO %s (%s) VALUES (%s)"$table$column$value);
          }
     } else {
          
$value mysql_real_escape_string($values);
          
$query sprintf("INSERT INTO %s (%s) VALUES (%s)"$table$column$value);
     }
     echo 
$query;


Last edited by VirtuosiMedia : 04-28-2008 at 04:39 PM.
VirtuosiMedia is offline
Reply With Quote
View Public Profile Visit VirtuosiMedia's homepage!
 
Old 04-28-2008, 06:40 PM Re: Arrays and Classes
amw_drizz's Avatar
Ultra Talker

Posts: 301
Name: Jon
Location: New York
what i am trying to get an array to break apart. as even the code snippet above only gets one of the fields and the rest isnt there.

the other thing if i cant break the array apart i would like to figure out a way to format the array in to a string so it can go in to the db
__________________
AMW_Drizz
Php 5.2 Mysql 4.1 IIS 6 Win2k3 Server && PHP 5.2 MySQL 4.1 Apache 2.2 (separate machine of course)

Last edited by amw_drizz : 04-28-2008 at 07:15 PM.
amw_drizz is offline
Reply With Quote
View Public Profile Visit amw_drizz's homepage!
 
Old 04-28-2008, 07:23 PM Re: Arrays and Classes
VirtuosiMedia's Avatar
Webmaster Talker

Posts: 738
I guess I'm not sure what you are trying to do, then. I haven't tested it, but the code I provided should be able to take a variable, determine whether or not it is an array. If it is an array, it will take each instance and sanitize it and upload it to the database. If it is not an array, it will sanitize it and upload it to the database.

From what you wrote, that is what I thought you were trying to do. However, you can't explode an array. explode() is to turn a string into an array. implode() is to turn an array into a string.

If you are trying to remove the [0], [1], [2], etc., why are you including them to begin with? You can write an array as array('value', 'another value', 'still another value') and the numbered keys are implied. It's only when you need to name the keys as something other than ordered numbers that you need to write them out.
VirtuosiMedia is offline
Reply With Quote
View Public Profile Visit VirtuosiMedia's homepage!
 
Old 04-28-2008, 07:40 PM Re: Arrays and Classes
amw_drizz's Avatar
Ultra Talker

Posts: 301
Name: Jon
Location: New York
the implode is what i was looking for. been tired when i work on that earlier, thanks for pointing out the implode. cause what i am doing is sanitizing the whole array then splitting it up to be put in the db
__________________
AMW_Drizz
Php 5.2 Mysql 4.1 IIS 6 Win2k3 Server && PHP 5.2 MySQL 4.1 Apache 2.2 (separate machine of course)

Last edited by amw_drizz : 04-28-2008 at 07:41 PM.
amw_drizz is offline
Reply With Quote
View Public Profile Visit amw_drizz's homepage!
 
Old 04-28-2008, 08:52 PM Re: Arrays and Classes
VirtuosiMedia's Avatar
Webmaster Talker

Posts: 738
Glad I could help.
VirtuosiMedia is offline
Reply With Quote
View Public Profile Visit VirtuosiMedia's homepage!
 
Reply     « Reply to Arrays and Classes
 

Thread Tools

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.15749 seconds with 12 queries