Welcome Guest, Not a member yet? Register   Sign In
Getting data from multidimensional array
#1

[eluser]RaGe10940[/eluser]
Hi,

Currently I have a script that is used as a back end update kinda script for a database. My friend is working on a website to help women choose clothing and what not. She has around 300 or 400 some arrays and she also needs to store those values in a database.

This is my current code that some what works but needs debugging :

Code:
<?php

//Set our database variables
$host="xxxxxxxxxxxx"; // Host name
$username="xxxxxxxxxxxxx"; // Mysql username
$password="xxxxxxxxxxxxx"; // Mysql password
$db_name="xxxxxxxxxxx"; // Database name

// Connect to database via PHP Data Object
$conn = new PDO("mysql:host=$host;dbname=$db_name;charset=utf8", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE);

$colors['Colors_All'] = array("Black","Charcoal"); // Add unique indexes
$colors['Colors_Bright_All'] = array("Silver","White"); // Add unique indexes
  
$AllArrays = get_defined_vars(); // Get all defined vars
$Arrays = array(); // Set a default array
              
foreach ($AllArrays as $varName => $value) { // Run through all the variables set in AllArrays
        if(is_array($value) && $varName == 'colors') { // If array is colors then
                $Arrays = array_merge($Arrays, $value); // Merge those arrays into the new array
        }
}

var_dump($Arrays);

$sql = "INSERT INTO `product_features` ("; // Create the initial query

foreach ($Arrays as $column => $value) { // ForEach over the array
        $sql .= "$column,"; // Use the Key Example : 'Colors_All and Color_Bright_All' as a column name
}

$sql2 = rtrim($sql, ","); // trim the initial "," from the columns at the end
$sql2 .= ")"; // Close off the columns names
$sql2 .= " VALUES ";

foreach ($Arrays as $column => $value) { // This is where the problem starts -_-
        foreach ($value as $key => $insert) { // Get the value
                $sql2 .= "('$insert', '$insert'),"; // I need to have unique values here :(
        }
}

$finSQL = rtrim($sql2, ","); // Strip off the remaining ","

var_dump($finSQL); //

$stmt = $conn->prepare($finSQL); // Prepare our finished query
$stmt->execute(); // And then execute this query

?>

Doing a var_dump of $finSQL I get this :

Code:
string(152) "INSERT INTO `product_features` (Colors_All,Colors_Bright_All) VALUES ('Black', 'Black'),('Charcoal', 'Charcoal'),('Silver', 'Silver'),('White', 'White')"

As you notice the columns work just fine and what not, but my issue is in the VALUES() where the value is being duplicated. I have added comments to my code block. Hopefully some one can help me.

Now also the above code is only handling 2 arrays (or 2 columns if you will) How can I dynamically expand on that depending on the amount of arrays she has? (probably around ~350)
#2

[eluser]TheFuzzy0ne[/eluser]
What's the $colors array for? It doesn't seem to be used in your code.

I'm sort of getting lost in your application flow. Please could you post a print_r() of say, 10 rows of the array containing the data you want to add to the database?

I'm trying to get an idea of your data structure and I'm finding it difficult. Where do these arrays come from, exactly?

Can you be certain that the insert array won't contain duplicate data? Are you sure that the insert array doesn't contain data that's already in the database?
#3

[eluser]RaGe10940[/eluser]
She literally has around 300 arrays being stored client side. the arrays are coming from her pages. only reason i am using colors is becsuse we will have to store them all in a single table. and yes i am 100% positive she does not hsve these stored in the database already.
#4

[eluser]TheFuzzy0ne[/eluser]
I'm not sure what you mean when you say the arrays are being stored client-side, but I'd still really appreciate a print_r() of about 10 rows from that array. Smile

It would also be nice to see the underlying database structure, if possible.
#5

[eluser]RaGe10940[/eluser]
What I mean about those client side arrays is pretty much she has no database interaction to populate the arrays. She litterally has 300 arrays mangled throughout her pages that are not received from a database.

Code:
$colors['Colors_All'] = array("Black","Charcoal"); // Add unique indexes
$colors['Colors_Bright_All'] = array("Silver","White"); // Add unique indexes

and those are the two values within the Colors_All and Colors_Bright_All array
#6

[eluser]TheFuzzy0ne[/eluser]
Does she use a consistent naming convention for those pages/arrays? I think you'd be better of doing it by parsing the text from each page all in one go, using a CLI script, and getting the data that way. If I understand how you are trying to do it at the moment, each page would have to be called once, and only once, otherwise you might have duplicate entries.

At the very least, I would suggest you build your database in a more manual fashion. It will mean that you can moderate your data, making sure it's all in the format you expect it to be in, there are no duplicates or typos. I know it's more work, but at least then you can be sure your database is solid. Better yet, does she have some kind of product sheet that you can parse?

Sorry, I'm just struggling here trying to figure out how this is going to work. I can't help thinking you're going to end up with a table full of useless data. Does each product have a name, an ID, and a category? Does each product have different sizes?
#7

[eluser]RaGe10940[/eluser]
Every array is pretty much a category / color / size / style / (all that other stuff women care about lol) and yes I agree it should be done with only database. Its just the point of creating and entering all that data that is what is making her be stumped.

I wrote a script that did all this, but on a one by one basis... so we would have to of done it 300 times lol. Now I wanted to do it dynamically... This is going to be a pain.
#8

[eluser]TheFuzzy0ne[/eluser]
Can you post an example view, which contains some of these arrays? I still don't understand the format of the arrays, which makes it difficult to find a solution to extract the data.
#9

[eluser]jairoh_[/eluser]
you will find hard time on the future if you use array rather than getting it from the database. you cannot rely on static for long terms.




Theme © iAndrew 2016 - Forum software by © MyBB