Insert data in database

Discussion in 'PHP' started by katerina, Jun 1, 2008.

  1. #1
    Hi,
    I wrote the below code and I would like if anyone knows how can I insert all in a database.

    
    <?php
      $url="http://www.google.com";
      $file = file_get_contents($url);
     
    preg_match_all ( '#(?:<img )([^>]*?)(?:/?>)#is', $file, $imgtags, PREG_PATTERN_ORDER );
    
    $imgcontents = array();
    
    foreach ( (array) $imgtags[1] as $img )
    {
        preg_match_all ( '#([a-zA-Z]*?)=[\'"]([^"\']*)["\']#i', $img, $attributes, PREG_PATTERN_ORDER );
        $attrs = array();
        foreach ( (array) $attributes[1] as $key => $attr )
        {
            $attrs[$attributes[1][$key]] = $attributes[2][$key];
        }
        $imgcontents[] = $attrs;
    }
    
    var_dump ( $imgcontents );  
     ?>
    
    Code (markup):
    For example the out of this code is like that :

    array(57) {
    [0]=> array(5) {
    ["src"]=> string(66) "http://furious.adman.gr/banner?webspace=1946&dfl=yes&js=no"
    ["border"]=> string(1) "0"
    ["width"]=> string(1) "1"
    ["height"]=> string(1) "1"
    ["alt"]=> string(0) ""
    }
    [1]=> array(3) {
    ["src"]=> string(25) "/grfx/cNav/sports_sel.jpg"
    ["alt"]=> string(6) "sports"
    }

    And I would like to insert into database. For example
    ID | Src | border | width | length | alt

    Could someone help me?
    Thanks a lot
     
    katerina, Jun 1, 2008 IP
  2. mehmetm

    mehmetm Well-Known Member

    Messages:
    134
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #2
    you can add this at the end of the code.

    
    <?php
    
    foreach($imgcontents as $img => $key1) {
    
    	$id = $img;
    	
    	$query = "INSERT INTO tableImg (id) VALUES ('" .$id. "')";
    	mysql_query($query);
    
    	foreach($key1 as $imgs => $key2) {
    				
    		$query = "UPDATE tableImg SET " .$imgs. " = '" .$key2. "' WHERE id = '" .$id. "'";
    		mysql_query($query);
    	
    	}
    }
    
    ?>
    
    PHP:
     
    mehmetm, Jun 1, 2008 IP
  3. katerina

    katerina Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I would like to ask you and something else.

    First of all, I lose my first image with this code.

    Secondly, want to take only the name of image not all the path.
    Could you help me ?


    Thanks a lot for all!!!
     
    katerina, Jun 1, 2008 IP
  4. mehmetm

    mehmetm Well-Known Member

    Messages:
    134
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #4
    I wanna start from the second one.

    with this code you can do it:
    
    <?php
    
    foreach($imgcontents as $img => $key1) {
    
    	$id = $img;
    	
    	$query = "INSERT INTO tableImg (id) VALUES ('" .$id. "')";
    	mysql_query($query);
    
    	foreach($key1 as $imgs => $key2) {
    		
    		if ($imgs == 'src') {
    		
    			$img = array_reverse(split('/', $key2));
    			
    			$key2 = $img[0];
    		}
    		
    		$query = "UPDATE tableImg SET " .$imgs. " = '" .$key2. "' WHERE id = '" .$id. "'";
    		mysql_query($query);
    	
    	}
    }
    
    ?>
    
    PHP:
    For the first one, what do you mean by losing the first image?

    I tried the code using my site's url, and all of the images' name (with the first one) was inserted to db.

    Please, tell me if the problem occurs again ;)
     
    mehmetm, Jun 1, 2008 IP
  5. katerina

    katerina Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Nothing , I made fault for the first one.

    Thanks a lot!
    You are an EXPERT.

    Thank you again!
     
    katerina, Jun 1, 2008 IP
  6. mehmetm

    mehmetm Well-Known Member

    Messages:
    134
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #6
    your welcome ;)
     
    mehmetm, Jun 1, 2008 IP
  7. katerina

    katerina Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Could I find the line of every image in html code ?
    and save too in the table Imagetable

    Thanks!!
     
    katerina, Jun 4, 2008 IP