wepnop Posted June 7, 2011 Share Posted June 7, 2011 Im iterating a array but it dont work. Its says that only exist the Memoria Ram A, ehen there are 12 elements. But the strangest thing is that: $arr = $categorias[$clave]['lista_referencias']; $max = count($arr) -1; It says that max is an array. Wtf? count is converting it to array? This is the code that iterate: $categorias = &$_SESSION['reg']->GetInvCategorias(); $arr = $categorias[$clave]['lista_referencias']; $max = count($arr) -1; pe($max); # Itera en la lista de referencias para mostrar una descripcion con todas ellas for ($i=0; $i<= $max ; $i++) { #foreach ($categorias[$clave]['lista_referencias'] as $referencia) { $referencia = $categorias[$clave]['lista_referencias'][$i]; p($categorias[$clave]['lista_referencias'][$i]); $id_ref = $referencia[1]; echo "<b><u>Selecciona $id_ref. </b></u>"; #echo "<br><select name='$id_ref'><br><br>"; $nombre_sql = $categorias[$referencia[0]]['nombre_sql']; $cons = "SELECT * FROM `$nombre_sql`"; #echo "<option value=Ninguno>Ninguno</option>"; #p($cons); $res_ref = consultar_bd($cons, true); #insp_res($res_ref); while($row = mysql_fetch_array($res_ref)) { $array_keys = array_keys($row); $id = $row['ID']; $descripcion = ''; $i=1; #insp_sql_row($row); foreach($array_keys as $key) { $campo = $row[$key]; #iv ($campo); if ($i%2 == 0) { $descripcion .= "( $key : $campo) "; } $i++; } echo $descripcion . '<br>'; #echo "<option value='$id'>$descripcion</option>"; } And this the array that contais the array thats iterated(lista referencias) $categorias['PC'] = array( # Codigo identificativo interno 'nombre' => 'PC', # Nombre que sera visto por los usuarios externos 'nombre_externo' => 'PC', # El nombre que se va a usar en la tabla sql 'nombre_sql' => 'PC', # Titulo de la pagina 'titulo' => 'Insertar ordenador', # Encabezado de la pagina 'encabezado' => 'Insertar ordenador', # Descripcion del modulo en general 'descripcion' => 'Los ordenadores contienen otros elementos.', # Imagen que se mostrar junto a la descripcion 'imagen' => 'ordenador.jpg', # Contenedora: las contenedoras tienen dentro de si mismas otras categorias, en una relacion por ejemplo, PC-Componenete. 'contenedora' => true, # Lista de referencias: aqui se ponen los nombres-codigo de cada componente que posee # cada contenedora # array(Nombre de la cateogira, Identificador unico que se vera por el usuario) 'lista_referencias' => array( array('Memoria Ram', 'Ranura Ram A'), array('Memoria Ram', 'Ranura Ram B'), array('CPU', 'CPU'), array('DVD', 'DVD'), array('Placa base', 'Placa base'), array('Disquetera', 'Disquetera'), array('Dico Duro', 'Dico Duro'), array('Raton', 'Raton'), array('Teclado', 'Teclado'), array('Caja', 'Caja'), array('Caja Disco Externo', 'Caja Disco Externo'), array('SO', 'SO') ), # Activada: si no lo esta no se vera en la aplicacion a nivel externo 'activada' => true, # Campos del formulario, tipo, nombre y descripcion para cada uno. Ordenados como saldran. 'campos' => array( 0 => array( # Tipo: 1: numero, 2: texto, 3: lista de opciones 'tipo' => 2, # Ha de coincidir con el campo sql 'nombre sql' => 'Modelo', # Ha de coincidir con el campo sql 'tipo sql' => 'Texto', # Tamaño del campo sql 'tamanyo sql' => 25, # Nombre usado por la web 'nombre' => 'Modelo', # Descripcion del elemento 'descripcion' => 'Modelo del ordenador' ) ) ); Quote Link to comment https://forums.phpfreaks.com/topic/238630-problem-iterating-array/ Share on other sites More sharing options...
PFMaBiSmAd Posted June 7, 2011 Share Posted June 7, 2011 What is your code for the pe() function? Quote Link to comment https://forums.phpfreaks.com/topic/238630-problem-iterating-array/#findComment-1226336 Share on other sites More sharing options...
wepnop Posted June 7, 2011 Author Share Posted June 7, 2011 Is a test function that prints a mark or variable: $cant_pe = 1; function pe ($val='') { global $cant_pe; # Si no se pasa ningun texto, muestra 'error x'. if (!$val) { $txt = "$cant_pe"; # Augmenta contador de pe's $cant_pe++; } else { $txt = (string)$val; } echo '<font color="red" >' . "<u><br><b>ERROR:</b>$txt<br> </u></font>"; } Quote Link to comment https://forums.phpfreaks.com/topic/238630-problem-iterating-array/#findComment-1226338 Share on other sites More sharing options...
PFMaBiSmAd Posted June 7, 2011 Share Posted June 7, 2011 I don't see anything obvious in the code you have posted concerning a problem with the $max variable. Perhaps if you posted the actual error message you are getting. Quote Link to comment https://forums.phpfreaks.com/topic/238630-problem-iterating-array/#findComment-1226477 Share on other sites More sharing options...
PFMaBiSmAd Posted June 7, 2011 Share Posted June 7, 2011 Its says that only exist the Memoria Ram A ^^^ That's interesting, because when I tried your code, I got that same result, due to a fatal runtime error that stopped the code in the first pass through the loop. Are you developing and debugging your code on a system with error_reporting set to E_ALL and display_errors set to ON so that php will report and display all the errors it detects? I also got a - Notice: Undefined index: Memoria Ram error in this statement - $nombre_sql = $categorias[$referencia[0]]['nombre_sql']; that indicates that your posted $categorias['PC'] array doesn't match what your code is trying to reference. This will mean that the query in $cons will fail with an error because there is no table name in the query. Quote Link to comment https://forums.phpfreaks.com/topic/238630-problem-iterating-array/#findComment-1226481 Share on other sites More sharing options...
mikosiko Posted June 7, 2011 Share Posted June 7, 2011 is a typo here ? #foreach ($categorias[$clave]['lista_referencias'] as $referencia) { $referencia = $categorias[$clave]['lista_referencias'][$i]; p($categorias[$clave]['lista_referencias'][$i]); /// is this other function or should be pe ?? $id_ref = $referencia[1]; and even with that fix (if an error) the Undefined Index error pointed by PFMAbisMad still there Quote Link to comment https://forums.phpfreaks.com/topic/238630-problem-iterating-array/#findComment-1226506 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.