1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Inserting PHP multidimensional arrays into a mySQL database

Discussion in 'PHP' started by linkinpark2014, Jul 17, 2008.

  1. #1
    Well as the title describes I need to know how to insert a multidimensional arrays into a mySQL database..
    I have 5 columns in the first table (time,date,competition,first_team,second_team)
    and another table has 2 columns (links, home_away)



    here what the array outputs:

    Array
    (
    [0] => Array
            (
                [0] => time
                [1] => date
                [2] => competition
                [3] => first_team
                [4] => second_team
                [5] => Array
                    (
                        [0] => http://blabla.com
                        [1] => http://blabla2.com
                    )
    
                [6] => Array
                    (
                        [0] => Home
                        [1] => Away
                    )
    
            )
    
    [1] => Array
            (
                [0] => time
                [1] => date
                [2] => competition
                [3] => bla_team
                [4] => bla2_team
                [5] => Array
                    (
                        [0] => http://blabla.com
                        [1] => http://blabla2.com
                    )
    
                [6] => Array
                    (
                        [0] => AWAY
                        [1] => HOME
             
                    )
    
            )
    
    )
    
    Code (markup):

    I want to insert
      [0] => time
                [1] => date
                [2] => competition
                [3] => first_team
                [4] => second_team
    Code (markup):
    into first table

    then insert
     [5] => Array
                    (
                        [0] => http://blabla.com
                        [1] => http://blabla2.com
                    )
    
     [6] => Array
                    (
                        [0] => Home
                        [1] => Away
                    )
    Code (markup):
    into table 2







    any ideas? thanx in advance :)
     
    linkinpark2014, Jul 17, 2008 IP
  2. Mozzart

    Mozzart Peon

    Messages:
    189
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Mozzart, Jul 17, 2008 IP
  3. linkinpark2014

    linkinpark2014 Peon

    Messages:
    153
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    can't I just use 2 foreach loops??
     
    linkinpark2014, Jul 17, 2008 IP
  4. Mozzart

    Mozzart Peon

    Messages:
    189
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    That depends, because you would have to do a table in the database with time,date,competition,first_team,second_team columns

    It really depends on how much you know at the moment, if you just want to log a plain array you can just serialize it into the database and unserialize it in the code

    e.g.
    
    $array = array('one','two','three'); 
    
    $array = serialize($array);
    
    var_dump($array);
    
    $unseralize = unserialize($array);
    
    var_dump($unserialize);
    
    PHP:
    Something tells me you might want to just put it over the database and query the server then do the loop.
     
    Mozzart, Jul 17, 2008 IP
  5. linkinpark2014

    linkinpark2014 Peon

    Messages:
    153
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    i want to insert each element inside specific column...

    for instance

    time
    date
    competition
    first_team
    second_team

    I want to insert each element from the above into my table columns..
    like competition inside competition coloumn and so on...

    serializing is not what I'm lookin for...I'm sure there is somthing with for loop..I found somthing but with normal arrays not multidimensional arrays...
    I might implement my own codes...there must b away to do this..


    anyway thanx for ur help...
     
    linkinpark2014, Jul 17, 2008 IP
  6. Mozzart

    Mozzart Peon

    Messages:
    189
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yes, you will have to learn about mysql, get phpmyadmin to set up the tables and columns and a few other things. Everything you have said is do-able, you just need to research :)
     
    Mozzart, Jul 17, 2008 IP
  7. linkinpark2014

    linkinpark2014 Peon

    Messages:
    153
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I think that i need to use for loops..
    yesterday i made this script that posts all elements inside the array..

    Code:
    for($i = 0; $i < 10; $i++) //loop for first array elements
    {
    for($j = 0; $j < 7; $j++)//loop for second array elements
    {
    for($k = 0; $k <200; $k++)//loop for third array elements
    {
    echo $array[$i][$j][$k];
    }
    }
    }


    but still confused how to get each element variable..do I need to define somthing??
    any Ideas??
     
    linkinpark2014, Jul 18, 2008 IP
  8. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #8
    Web's pretty thin on this stuff....far as databases. Maybe this will help as how to structure the loops:

    http://www.webcheatsheet.com/PHP/multidimensional_arrays.php

    Just to save you a trip there, here's the code snippet for handling a 2-dim array. Um after I look at it, aint sure it helps or not. Your example has arrays and values right?

    
    for ($row = 0; $row < 3; $row++)
    {
        echo "<li><b>The row number $row</b>";
        echo "<ul>";
    
        foreach($shop[$row] as $key => $value)
        {
            echo "<li>".$value."</li>";
        }
    
        echo "</ul>";
        echo "</li>";
    }
    
    PHP:
     
    shallowink, Jul 18, 2008 IP
  9. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #9
    Maybe this will help more:

    
    $david = array
    (
    richie=>array
    (
    Sue,
    Natasha
    ),
    Mason=>array
    (
    Nichole,
    Salma,
    Amber
    ),
    Rose,
    Floyd
    );
    echo "<pre>";
    print_r($david);
    
    echo "</pre>";
    
        while (list($var, $val) = each($david)) 	{
    if(is_array($val))
    {
            print "$var \n";
    		    while (list($var2, $val2) = each($val)) {
            				print "$val2 ";
    								}
    }
    else {
            print "$val \n";
    } 
    						}
    PHP:
    Outputs :

    richie Sue Natasha Mason Nichole Salma Amber Rose Floyd

    Which isn't exactly what you want but closer. Has to be a better way of doing it though.
     
    shallowink, Jul 18, 2008 IP
  10. linkinpark2014

    linkinpark2014 Peon

    Messages:
    153
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    okay thanx..I will try it out and give u final result later :)
     
    linkinpark2014, Jul 18, 2008 IP
  11. linkinpark2014

    linkinpark2014 Peon

    Messages:
    153
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    ok I tried ur way but it was same like foreach loop..anyway I found good solution for now..check snippet:

    $first_element=count($match_array);
    
    for($i=0; $i<$first_element; $i++)
    {	
    ///////////////////////////////date //indexing 1st element in 1st array
    $date=$match_array[$i][0];
    
    ///////////////////////////////time
    $tmp_time=$match_array[$i][1]; //indexing 2nd element in 1st array
    /////////////////////////////inserting data database
    $query = "bla bla bla".
    //////////////////////////////showing data
    	echo 	"<br>date: ".$date."<br>";		
    	echo 	"time: ".$time."<br>";		
    	echo 	"competition: ".$match_array[$i][2]."<br>";	
    	echo 	"first team: ".$match_array[$i][3]."<br>";	
    	echo 	"second team: ".$match_array[$i][4]."<br>"; 
    
    /////////////////////////////////////////////////////////links and other data 
    $second_element=count($match_array[$i][5]); //counting number of elements insde this array
    for($k=0; $k<$second_element; $k++)
    {	
    $links=$match_array[$i][5][$k];	//getting all elements from this array(6th element in 1st array)
    $links_type=$match_array[$i][6][$k];//getting all elements from this array (7th element in 1st array)
    //////////////////////////////////////////////////////sql query for my links and other data in arrays 6-7
    $query = "bla bla bla".
    ///////////////////////////echo the data and so on
    echo $match_array[$i][5][$k];	
    echo $match_array[$i][6][$k];
    }
    
    
    	
    }
    
    echo ‘done’;
    Code (markup):
    now everything worx like charm...as I told u its 2 or 3 for loops no need to add while loop or foreach loop...anyway thanx for ur help really appreciated :)
     
    linkinpark2014, Jul 19, 2008 IP
  12. echofiend

    echofiend Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Hi I am having this same issue right now. However I do not need to echo it to the screen. In the first post you mentioned you need it to go into the database, but in you solution you just type $query = "bla bla bla"...welll I need to know what the bla bla bla is lol. my query is inserting info...but it is inserting array[name], array[relation] and so on.

    anyone know how to possibly fix this?
     
    echofiend, Apr 2, 2009 IP