Foros del Web » Programando para Internet » PHP »

Ayuda con Tabla dinamica, arrays, checkboxes...

Estas en el tema de Ayuda con Tabla dinamica, arrays, checkboxes... en el foro de PHP en Foros del Web. Hola Amigos de Foros del web!! Ante todo muchas gracias por su inestimable labor, muchos aprendemos con vosotros!! Llevo leyéndo el foro mucho tiempo, pero ...
  #1 (permalink)  
Antiguo 11/02/2010, 16:12
 
Fecha de Ingreso: febrero-2010
Mensajes: 36
Antigüedad: 14 años, 2 meses
Puntos: 0
Ayuda con Tabla dinamica, arrays, checkboxes...

Hola Amigos de Foros del web!!

Ante todo muchas gracias por su inestimable labor, muchos aprendemos con vosotros!! Llevo leyéndo el foro mucho tiempo, pero normalmente siempre encontraba soluciones a mis dudas, esta vez no es así, y quiero probar suerte por si alguien pudiera ayudarme.

La cuestión es que estoy construyendo un horario/calendario de eventos. Para ello recurro a un par de loops for para construir dinámicamente una tabla con las diferentes franjas horarias y locales (es para unos locales de ensayo) y mediante unos querys a una base de datos obtengo los datos de quien usará dichos locales en los distintos horarios.

En los loops le doy a cada td de la tabla una id diferente, y meto un checkbox para apuntar a nuevos usuarios en el horario que ellos pidan.

El problema creo yo está en los arrays que he creado dinamicamente, ya que cuando quiero meter los datos en la base de datos me da error.

Lo curioso es que el primero de los checboxes si funciona, aunque me inserta el codigo dos veces, los demás no y fa un error en la funcion ajax que lo inserta en la bbdd.

Aquí les paso el código:

Código PHP:
    case 'listEvents':
        $day = $_POST['d'];
        $month = $_POST['m'];
        $year = $_POST['y'];
        
        $timeStamp = mktime(0,0,0, $month, $day, $year);
        
        $eventQuery = mysql_query("SELECT *
         FROM lista_horario
        INNER JOIN grupo_fianza
        USING (id_fianza)
        WHERE lista_horario.timestamp='$timeStamp' ORDER BY id DESC", $conn);
        if($eventQuery) {
            if(mysql_num_rows($eventQuery) >0) {
                echo '<br><b>D&iacute;a: '. date("d", $timeStamp) .'/'. date("m", $timeStamp) .'/'. date("Y", $timeStamp) .'</b>';
        
    
?>
<form name="schedule" method="post">
<table align="left%" width="100%" >

 <thead>
 <tr>
 <th class="twuelve pink"></th>
 <th>1</th>
 <th>2</th>
 <th>3</th>
 <th>4</th>
 </tr>
</thead>

<tbody>

<?php

$row
mysql_fetch_array($eventQuery);

for (
$i=0$i<15$i++) {
    
?>
<tr>
<td class="twuelve"><?php  echo $i .":00" ;  ?></td>
<?php
for ($j=0$j<4$j++) {
    
?>

<td <?php $id[$k]='Lo'.($j 1).'Hr'.($i 9); echo 'id="'.$id[$k].'"'?> > <?php


 
if ($row[horario]== $id[$k]) {echo $row[nombre_grupo] ;} else {echo '<input type="checkbox" id="horario" name="horario" value="'.$id[$k].'" />';}  ?> </td>
<?php
        
    
}
echo 
"</tr>";
        
}    
        
$d =date('j');
        
$m =date('n');
        
$a =date('Y');

?>
</tbody>
</table>
</form>
             
        <?php
    
                  
            
} else {
                echo 
'No hay reservas';
                echo 
'<br><b>D&iacute;a: 'date("d"$timeStamp) .'/'date("m"$timeStamp) .'/'date("Y"$timeStamp) .'</b>';            
            }
        } else {
            echo 
'Error getting the results.';
        }
        
        break;
Si pueden ayudarme les estaría eternamente agradecido, cualquier duda digánmela. Gracias.

Última edición por Fennesz; 11/02/2010 a las 16:19
  #2 (permalink)  
Antiguo 12/02/2010, 07:44
 
Fecha de Ingreso: febrero-2010
Mensajes: 36
Antigüedad: 14 años, 2 meses
Puntos: 0
Respuesta: Ayuda con Tabla dinamica, arrays, checkboxes...

Hola, alomejor soy un pesado... es que creo que no me explique bien. El problema consiste en que el "value" del checkbox se representa en el html, pero en el insert del query da error. Solo toma el value el primer checkbox, aunque toma el valor dos veces, justo así:

"Lo1Hr9Lo1Hr9" en lugar de "Lo1Hr9"

Código:
<?php

$row= mysql_fetch_array($eventQuery);

for ($i=0; $i<15; $i++) {
	
?>
<tr>
<td class="twuelve"><?php  echo $i + 9 .":00" ;  ?></td>
<?php
for ($j=0; $j<4; $j++) {
	
?>

<td <?php $id[$k]='Lo'.($j + 1).'Hr'.($i + 9); echo 'id="'.$id[$k].'"'; ?> > <?php



 if ($row[horario]== $id[$k]) {echo $row[nombre_grupo] ;} else {echo '<input type="checkbox" id="horario" name="horario[]" value="'.$id[$k].'" />';}  ?> </td>

//junto el checkbox de encima. Su valor se representa en el html de la tabla, pero da error cuando se hace el insert en la bbdd.

<?php	
	}
echo "</tr>";
		
}	

?>
</tbody>
</table>
</form>
Aquí les dejo también el cofigo html que resulta del loop:


Código HTML:
<table width="100%" align="left%">

 <thead>
 <tr>
 <th class="twuelve pink"></th>
 <th>1</th>
 <th>2</th>
 <th>3</th>
 <th>4</th>
 </tr>
</thead>

<tbody>

<tr>
<td class="twuelve">9:00</td>

<td id="Lo1Hr9"> <input type="checkbox" value="Lo1Hr9" name="horario[]" id="horario"> </td>

<td id="Lo2Hr9"> <input type="checkbox" value="Lo2Hr9" name="horario[]" id="horario"> </td>

<td id="Lo3Hr9"> <input type="checkbox" value="Lo3Hr9" name="horario[]" id="horario"> </td>

<td id="Lo4Hr9"> <input type="checkbox" value="Lo4Hr9" name="horario[]" id="horario"> </td>
</tr><tr>
<td class="twuelve">10:00</td>

<td id="Lo1Hr10"> <input type="checkbox" value="Lo1Hr10" name="horario[]" id="horario"> </td>

<td id="Lo2Hr10"> <input type="checkbox" value="Lo2Hr10" name="horario[]" id="horario"> </td>

<td id="Lo3Hr10"> <input type="checkbox" value="Lo3Hr10" name="horario[]" id="horario"> </td>

<td id="Lo4Hr10"> <input type="checkbox" value="Lo4Hr10" name="horario[]" id="horario"> </td>
</tr><tr>
<td class="twuelve">11:00</td>

<td id="Lo1Hr11"> <input type="checkbox" value="Lo1Hr11" name="horario[]" id="horario"> </td>

<td id="Lo2Hr11"> <input type="checkbox" value="Lo2Hr11" name="horario[]" id="horario"> </td>

<td id="Lo3Hr11"> <input type="checkbox" value="Lo3Hr11" name="horario[]" id="horario"> </td>

<td id="Lo4Hr11"> <input type="checkbox" value="Lo4Hr11" name="horario[]" id="horario"> </td>
</tr><tr>
<td class="twuelve">12:00</td>

<td id="Lo1Hr12"> <input type="checkbox" value="Lo1Hr12" name="horario[]" id="horario"> </td>

<td id="Lo2Hr12"> <input type="checkbox" value="Lo2Hr12" name="horario[]" id="horario"> </td>

<td id="Lo3Hr12"> <input type="checkbox" value="Lo3Hr12" name="horario[]" id="horario"> </td>

<td id="Lo4Hr12"> <input type="checkbox" value="Lo4Hr12" name="horario[]" id="horario"> </td>
</tr><tr>
<td class="twuelve">13:00</td>

<td id="Lo1Hr13"> <input type="checkbox" value="Lo1Hr13" name="horario[]" id="horario"> </td>

<td id="Lo2Hr13"> <input type="checkbox" value="Lo2Hr13" name="horario[]" id="horario"> </td>

<td id="Lo3Hr13"> <input type="checkbox" value="Lo3Hr13" name="horario[]" id="horario"> </td>

<td id="Lo4Hr13"> <input type="checkbox" value="Lo4Hr13" name="horario[]" id="horario"> </td>
</tr><tr>
<td class="twuelve">14:00</td>

<td id="Lo1Hr14"> <input type="checkbox" value="Lo1Hr14" name="horario[]" id="horario"> </td>

<td id="Lo2Hr14"> <input type="checkbox" value="Lo2Hr14" name="horario[]" id="horario"> </td>

<td id="Lo3Hr14"> <input type="checkbox" value="Lo3Hr14" name="horario[]" id="horario"> </td>

<td id="Lo4Hr14"> <input type="checkbox" value="Lo4Hr14" name="horario[]" id="horario"> </td>
</tr><tr>
<td class="twuelve">15:00</td>

<td id="Lo1Hr15"> <input type="checkbox" value="Lo1Hr15" name="horario[]" id="horario"> </td>

<td id="Lo2Hr15"> <input type="checkbox" value="Lo2Hr15" name="horario[]" id="horario"> </td>

<td id="Lo3Hr15"> <input type="checkbox" value="Lo3Hr15" name="horario[]" id="horario"> </td>

<td id="Lo4Hr15"> <input type="checkbox" value="Lo4Hr15" name="horario[]" id="horario"> </td>
</tr><tr>
<td class="twuelve">16:00</td>

<td id="Lo1Hr16"> <input type="checkbox" value="Lo1Hr16" name="horario[]" id="horario"> </td>

<td id="Lo2Hr16"> <input type="checkbox" value="Lo2Hr16" name="horario[]" id="horario"> </td>

<td id="Lo3Hr16"> <input type="checkbox" value="Lo3Hr16" name="horario[]" id="horario"> </td>

<td id="Lo4Hr16"> <input type="checkbox" value="Lo4Hr16" name="horario[]" id="horario"> </td>
</tr><tr>
<td class="twuelve">17:00</td>

<td id="Lo1Hr17"> <input type="checkbox" value="Lo1Hr17" name="horario[]" id="horario"> </td>

<td id="Lo2Hr17"> <input type="checkbox" value="Lo2Hr17" name="horario[]" id="horario"> </td>

<td id="Lo3Hr17"> <input type="checkbox" value="Lo3Hr17" name="horario[]" id="horario"> </td>

<td id="Lo4Hr17"> <input type="checkbox" value="Lo4Hr17" name="horario[]" id="horario"> </td>
</tr><tr>
<td class="twuelve">18:00</td>

<td id="Lo1Hr18"> <input type="checkbox" value="Lo1Hr18" name="horario[]" id="horario"> </td>

<td id="Lo2Hr18"> <input type="checkbox" value="Lo2Hr18" name="horario[]" id="horario"> </td>

<td id="Lo3Hr18"> <input type="checkbox" value="Lo3Hr18" name="horario[]" id="horario"> </td>

<td id="Lo4Hr18"> <input type="checkbox" value="Lo4Hr18" name="horario[]" id="horario"> </td>
</tr><tr>
<td class="twuelve">19:00</td>

<td id="Lo1Hr19"> <input type="checkbox" value="Lo1Hr19" name="horario[]" id="horario"> </td>

<td id="Lo2Hr19"> <input type="checkbox" value="Lo2Hr19" name="horario[]" id="horario"> </td>

<td id="Lo3Hr19"> <input type="checkbox" value="Lo3Hr19" name="horario[]" id="horario"> </td>

<td id="Lo4Hr19"> <input type="checkbox" value="Lo4Hr19" name="horario[]" id="horario"> </td>
</tr><tr>
<td class="twuelve">20:00</td>

<td id="Lo1Hr20"> <input type="checkbox" value="Lo1Hr20" name="horario[]" id="horario"> </td>

<td id="Lo2Hr20"> <input type="checkbox" value="Lo2Hr20" name="horario[]" id="horario"> </td>

<td id="Lo3Hr20"> <input type="checkbox" value="Lo3Hr20" name="horario[]" id="horario"> </td>

<td id="Lo4Hr20"> <input type="checkbox" value="Lo4Hr20" name="horario[]" id="horario"> </td>
</tr><tr>
<td class="twuelve">21:00</td>

<td id="Lo1Hr21"> <input type="checkbox" value="Lo1Hr21" name="horario[]" id="horario"> </td>

<td id="Lo2Hr21"> <input type="checkbox" value="Lo2Hr21" name="horario[]" id="horario"> </td>

<td id="Lo3Hr21"> <input type="checkbox" value="Lo3Hr21" name="horario[]" id="horario"> </td>

<td id="Lo4Hr21"> <input type="checkbox" value="Lo4Hr21" name="horario[]" id="horario"> </td>
</tr><tr>
<td class="twuelve">22:00</td>

<td id="Lo1Hr22"> <input type="checkbox" value="Lo1Hr22" name="horario[]" id="horario"> </td>

<td id="Lo2Hr22"> <input type="checkbox" value="Lo2Hr22" name="horario[]" id="horario"> </td>

<td id="Lo3Hr22"> <input type="checkbox" value="Lo3Hr22" name="horario[]" id="horario"> </td>

<td id="Lo4Hr22"> <input type="checkbox" value="Lo4Hr22" name="horario[]" id="horario"> </td>
</tr><tr>
<td class="twuelve">23:00</td>

<td id="Lo1Hr23"> <input type="checkbox" value="Lo1Hr23" name="horario[]" id="horario"> </td>

<td id="Lo2Hr23"> <input type="checkbox" value="Lo2Hr23" name="horario[]" id="horario"> </td>

<td id="Lo3Hr23"> <input type="checkbox" value="Lo3Hr23" name="horario[]" id="horario"> </td>

<td id="Lo4Hr23"> <input type="checkbox" value="Lo4Hr23" name="horario[]" id="horario"> </td>
</tr></tbody>
</table>


Como ven todos los checkbox tienen su value, pero al intentar insertar me da error... Si pudieran ayudarme....

Gracias!!!!
  #3 (permalink)  
Antiguo 12/02/2010, 07:48
Colaborador
 
Fecha de Ingreso: octubre-2009
Ubicación: Tokyo - Japan !
Mensajes: 3.867
Antigüedad: 14 años, 6 meses
Puntos: 334
Respuesta: Ayuda con Tabla dinamica, arrays, checkboxes...

en html la etiqueta ID se usa solo para 1 elemento
es un identificador UNICO ..
o le quitas le etiqueta o le pones id diferentes a cada uno

saludos
__________________
More about me...
~ @rhyudek1
~ Github
  #4 (permalink)  
Antiguo 12/02/2010, 08:18
 
Fecha de Ingreso: febrero-2010
Mensajes: 36
Antigüedad: 14 años, 2 meses
Puntos: 0
Respuesta: Ayuda con Tabla dinamica, arrays, checkboxes...

Toda la razón llevas!!
Aunque no creo que influya en este caso, pero si, se me había pasado.
Gracias!!
  #5 (permalink)  
Antiguo 12/02/2010, 08:21
Colaborador
 
Fecha de Ingreso: octubre-2009
Ubicación: Tokyo - Japan !
Mensajes: 3.867
Antigüedad: 14 años, 6 meses
Puntos: 334
Respuesta: Ayuda con Tabla dinamica, arrays, checkboxes...

bueno podrias poner el error que te sale aqui para ver a donde apunta el tema
otra cosa.. ? esos checkbox .. estan dentro de un formulario (?)
__________________
More about me...
~ @rhyudek1
~ Github
  #6 (permalink)  
Antiguo 12/02/2010, 08:39
 
Fecha de Ingreso: febrero-2010
Mensajes: 36
Antigüedad: 14 años, 2 meses
Puntos: 0
Respuesta: Ayuda con Tabla dinamica, arrays, checkboxes...

Cita:
Iniciado por Hidek1 Ver Mensaje
bueno podrias poner el error que te sale aqui para ver a donde apunta el tema
otra cosa.. ? esos checkbox .. estan dentro de un formulario (?)
Gracias Hidek1 por tu interés!!

Es algo complejo ya que también depende de funciones ajax etc.

El tema es que he creado esa tabla, (si, la tabla y los checkbox están dentro de un form) y en ella se reflejan los horarios y los números de los locales. En el loop se van formando los ids de la forma:

"Lo" + "numero de local" + "Hr" + "horario en numero"

Y despues los checkboxes toman el mismo valor y ahora (el mismo id), es decir, a cada vuelta del loop van cambiando sus valores y sus ids. También en name: puse horario[], con la intención de que se convirtiera en un array, pero creo que no funciona.

Mi intención es que el usuario elija el horario que quiera pulsando uno o varios checboxes, y a eso se añada automaticamente el número de fianza (para saber que grupo es) y el horario que se convierte en timestamp mediante mktime.

Cuando alguien pulsa el botón para ingresar en la bbdd salta la siguiente función:


Código:
	function addEvent(day, month, year, horario, id_fianza) {
		if(day && month && year && horario && id_fianza) {
			// alert('Add Event\nDay: '+day+'\nMonth: '+month+'\nYear: '+year+'\nBody: '+body);
			new Ajax.Request('rpc.php', {method: 'post', postBody: 'action=addEvent&d='+day+'&m='+month+'&y='+year+'&horario='+horario+'&id_fianza='+id_fianza+'', onSuccess: highlightEvent(day)});
	    	$('horario').value = '';
			$('id_fianza').value = '';
		} else {
			alert('There was an unexpected script error.\nPlease ensure that you have not altered parts of it.');
		}
		
		// highlightEvent(day);
	} // addEvent.
Y el php:

Código:
	case 'addEvent':
	
		$horario .= $_POST[$id];
		$day = $_POST['d'];
		$month = $_POST['m'];
		$year = $_POST['y'];
		$id_fianza = $_POST['id_fianza'];
		
		$timeStamp = mktime(0,0,0, $month, $day, $year);
	//	$bodyF = addslashes(trim($body));
		$addEvent = mysql_query("INSERT INTO lista_horario (timestamp, horario, id_fianza) VALUES ('$timeStamp', '$horario', '$id_fianza')", $conn);
		break;
Y el html:

Código HTML:
	<div style="display: none; margin-top: 10px;" id="addEventForm">	
						Fecha: <input type="text" size="2" id="evtDay" name="d"  <?php  if (empty($_REQUEST["d"])) {echo 'value="'.$d.'"'; }    ?>  disabled /> 

<input type="text" size="2" id="evtMonth" name="m" <?php  if (empty($_REQUEST["m"])) {echo 'value="'.$m.'"'; }    ?>  disabled  /> <input type="text" size="4" id="evtYear" name="y" <?php  if (empty($_REQUEST["y"])) {echo 'value="'.$a.'"'; }    ?>  disabled  />
			<input type="text" name="id_fianza" id="id_fianza" size="6" maxlength="11" value="<?php echo $row2[id_fianza];  ?>" />
			<br />
			<?php

?>
			
			<input type="button" value="Reservar" onClick="addEvent($F('evtDay'), $F('evtMonth'), $F('evtYear'), $F('horario'), $F('id_fianza'));" />
		</div> 
Y acabo de caer en que esto: " $F('horario') " puede ser la clave, pq esto hace referencia a un id, no un name por Post, no??

Vaya...

Etiquetas: arrays, checkboxes, dinamicas, tablas
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 05:21.