patchido Posted September 20, 2011 Share Posted September 20, 2011 hey i dont know why my array is not being echoed, it it populated, but not echoed, here is my code im really new to php, thanks $query= "INSERT INTO tokens (producto, precio, photo)VALUES "; for ($i=1; $i<sizeof($precio_array); $i++){ global $nombre_array; global $pic_array; global $precio_array; $query .="($nombre_array[i] ,$precio_array[i] , $pic_array[i])"; } echo $query; Link to comment https://forums.phpfreaks.com/topic/247532-new-to-php/ Share on other sites More sharing options...
xyph Posted September 20, 2011 Share Posted September 20, 2011 We need to see more code. If this is in a function, you are attempting to use $precio_array before you make it global. Programming with all errors being reported would help determine the issue. Link to comment https://forums.phpfreaks.com/topic/247532-new-to-php/#findComment-1271129 Share on other sites More sharing options...
patchido Posted September 20, 2011 Author Share Posted September 20, 2011 ok, this is my whole code <?php $xml = simplexml_load_file("cursos.xml"); $precio_array=array(); $nombre_array=array(); $pic_array=array(); if (count($xml->Articulo) > 0) { $i=0; foreach ($xml->Articulo as $node) { // This prints out each of the models $precio= substr($node->Precio.chr(10), 0, -3); array_push($precio_array, $precio); array_push($pic_array,$node->Pic.chr(10)); array_push($nombre_array,$node->Nombre.chr(10)); $i++; } } //print_r($precio_array);echo"<br />"; //print_r($pic_array);echo"<br />"; //print_r($nombre_array); $query= "INSERT INTO tokens (producto, precio, photo)VALUES "; for ($i=1; $i<sizeof($precio_array); $i++){ global $nombre_array; global $pic_array; global $precio_array; $query .="($nombre_array[i] ,$precio_array[i] , $pic_array[i])"; } echo $query; ?> Link to comment https://forums.phpfreaks.com/topic/247532-new-to-php/#findComment-1271130 Share on other sites More sharing options...
xyph Posted September 20, 2011 Share Posted September 20, 2011 There's no need to use the global keyword. Anyways, the problem is this $nombre_array[i] ,$precio_array[i] , $pic_array[i] should be $nombre_array[$i] ,$precio_array[$i] , $pic_array[$i] Link to comment https://forums.phpfreaks.com/topic/247532-new-to-php/#findComment-1271131 Share on other sites More sharing options...
patchido Posted September 20, 2011 Author Share Posted September 20, 2011 thanks another issue, will that create a correct sql query??? this is my result INSERT INTO tokens (producto, precio, photo)VALUES ('Cuñas hexagonales para concreto ' ,'10 ' , 'images/tokens/2.jpg ')('Cutter reforzado 5'' ' ,'7 ' , 'images/tokens/3.jpg ')('Kg de Piola ' ,'16 ' , 'images/tokens/4.jpg ')('Brújula ' ,'' , 'images/tokens/5.jpg ')('Cutter super reforzado 6'' ' ,'20 ' , 'images/tokens/6.jpg ')('Cutter de Plastico ' ,'3 ' , 'images/tokens/7.jpg ')('Guantes de Carnaza ' ,'18 ' , 'images/tokens/8.jpg ')('Tanque de Propano ' ,'7 ' , 'images/tokens/9.jpg ')('Caja de plastico 16'' ' ,'25 ' , 'images/tokens/10.jpg ')('Metro de soga ' ,'3 ' , 'images/tokens/11.jpg ')('Caja de plastico 20'' ' ,'47 ' , 'images/tokens/12.jpg ')('Casero de madera ' ,'53 ' , 'images/tokens/13.jpg ')('Michigan de fibra 36'' ' ,'53 ' , 'images/tokens/14.jpg ')('Michigan de madera 36'' ' ,'55 ' , 'images/tokens/15.jpg ')('Doble michigan 36'' ' ,'65 ' , 'images/tokens/16.jpg ')('Pulaski 36'' ' ,'75 ' , 'images/tokens/17.jpg ')('Media labor 36'' ' ,'40 ' , 'images/tokens/18.jpg ')('Cazadoras 14'' ' ,'28 HT C' , 'images/tokens/19.jpg ')('Martillo golpe seco ' ,'31 ' , 'images/tokens/20.jpg ')('Mazo de hule ' ,'10 ' , 'images/tokens/21.jpg ')('Irrigación mango largo ' ,'35 ' , 'images/tokens/22.jpg ')('Pala cuadrada ' ,'35 ' , 'images/tokens/23.jpg ')('Pala Escarramán ' ,'35 ' , 'images/tokens/24.jpg ')('Navaja 15 funciones ' ,'38 ' , 'images/tokens/25.jpg ')('Zapapico 7lb ' ,'54 ' , 'images/tokens/26.jpg ')('Estufilla de propano ' ,'17' , 'images/tokens/27.jpg ')('Estufilla de gasolina coleman ' ,'130 ' , 'images/tokens/28.jpg ')('Hielera Igloo ' ,'49 ' , 'images/tokens/29.jpg ')('Lampara de propano coleman ' ,'34 ' , 'images/tokens/30.jpg ')('Lampara de gasolina coleman ' ,'164 ' , 'images/tokens/31.jpg ')('Tienda estilo Congaree ' ,'280 ' , 'images/tokens/32.jpg ')('Tienda estilo Bryce Canyon ' ,'309 ' , 'images/tokens/33.jpg ')('Generador de gasolina ' ,'535 ' , 'images/tokens/34.jpg ')('Motosierra ' ,'780 ' , 'images/tokens/35.jpg ')('Compresor de banda ' ,'1,168 ' , 'images/tokens/36.jpg ') Link to comment https://forums.phpfreaks.com/topic/247532-new-to-php/#findComment-1271133 Share on other sites More sharing options...
xyph Posted September 20, 2011 Share Posted September 20, 2011 No you need a comma between your values. Try this <?php $xml = simplexml_load_file("cursos.xml"); $values = array(); if (count($xml->Articulo) > 0) { $i=0; foreach ($xml->Articulo as $node) { // This prints out each of the models $precio = substr($node->Precio.chr(10), 0, -3); // You have to escape the data, so ' will be converted to \' etc. $precio = mysql_real_escape_string($precio); $nombre = mysql_real_escape_string($node->Nombre.chr(10)); $pic = mysql_real_escape_string($node->Pic.chr(10)); $values[] = "('$nombre','$precio','$pic'"; } } $query = "INSERT INTO tokens (producto, precio, photo) VALUES "; $query .= implode(',', $values); echo $query; ?> Link to comment https://forums.phpfreaks.com/topic/247532-new-to-php/#findComment-1271139 Share on other sites More sharing options...
patchido Posted September 21, 2011 Author Share Posted September 21, 2011 Thanks Link to comment https://forums.phpfreaks.com/topic/247532-new-to-php/#findComment-1271257 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.