Foros del Web » Programando para Internet » PHP »

Incluir una variable

Estas en el tema de Incluir una variable en el foro de PHP en Foros del Web. Hola, queria preguntar una cosa: Si tengo en una variable($footer) en un archivo yamado xxx.php que quiero que se muestre en el index.php seria asi: ...
  #1 (permalink)  
Antiguo 28/06/2007, 09:54
Borjimante
Invitado
 
Mensajes: n/a
Puntos:
Incluir una variable

Hola, queria preguntar una cosa:

Si tengo en una variable($footer) en un archivo yamado xxx.php que quiero que se muestre en el index.php seria asi:

Código PHP:
<?
echo ("$footer");
include(
"xxx.php");
?>
o como seria
  #2 (permalink)  
Antiguo 28/06/2007, 10:07
AlvaroG
Invitado
 
Mensajes: n/a
Puntos:
Re: Incluir una variable

el include primero.

Luego no es necesario que hagas ("$footer") basta con
echo $footer;


Saludos.
  #3 (permalink)  
Antiguo 28/06/2007, 10:24
Borjimante
Invitado
 
Mensajes: n/a
Puntos:
Re: Incluir una variable

y si hago eso no me muestra la variable footer en el index, te pongo el codigo:


poll.php:
Código PHP:
<?php 

// Author: PHPFront.com © 2005
// License: Free (GPL)
//
// Version: 1.0
//
// Created: 8.12.2005 
//
// More information and downloads 
// available at http://www.PHPFront.com
//
// #### poll.php ####



include("admincp/config.php");

$user_ip $_SERVER['REMOTE_ADDR'];

$ipquery mysql_query("SELECT * FROM ips WHERE ip='$user_ip'");
$select_banned mysql_num_rows($ipquery);

if(
$select_banned){

    
//display results
    
    
    
$poll mysql_fetch_array(mysql_query("select * from poll"));
    
$totalvotes $poll['totalvotes'];
    
$question $poll['question'];
            
    echo(
"<div class=poll>$question<br /><br />");
    
    
$get_questions mysql_query("select * from options");
    while(
$r=mysql_fetch_array($get_questions)){
    
    
        
extract($r);
        
$per $votes 100 $totalvotes;
        
$per floor($per);
        
        echo 
htmlspecialchars($field); 
        
?> <strong><? echo("$votes"); ?></strong><br />
        <div style="background-color: <? echo config(bg1); ?>;"><div style="color: <? echo config(text); ?>; font-size: <? echo config(size); ?>px; text-align: right;background-color: <? echo config(bg2); ?>; width: <? echo($per); ?>%;"><? echo("$per%"); ?></div></div>
        <?
            
    
}
    
    echo(
"<br />Total de votos: <strong>$totalvotes</strong></div>"); 
    
    
    
    
    
}else{





//if the submit button was pressed
if($_POST['submit']){
    
    
//grab vars
    
$vote $_POST['vote'];
    
$refer $_POST['refer'];
    
        
    
//update numbers
    
$update_totalvotes "UPDATE poll SET totalvotes = totalvotes + 1";
    
$insert mysql_query($update_totalvotes);
    
    
$update_votes "UPDATE options SET votes = votes + 1 WHERE id = $vote";
    
$insert mysql_query($update_votes);
            
    
//add ip to stop multiple voting
    
$ip $_SERVER['REMOTE_ADDR'];
    
$addip mysql_query("INSERT INTO ips (ip)""VALUES ('$ip')"); 

    
    
//send the user back to thepage they were just viewing
echo " <script language='JavaScript' type='text/JavaScript'> ";
echo 
" location.href='index.php' ";
echo 
" </script> ";

    
    
        
}    
    
    
//display the form!
    
?><form action="<? $_SERVER['PHP_SELF']; ?>" method="post"><?
        
        
    $getcurrent 
mysql_query("select * from options ORDER by id");
    while(
$r=mysql_fetch_array($getcurrent)){
        
        
extract($r);
        
        
?><input type="radio" name="vote" value="<? echo($id); ?>" class="radiobutton" /> <? echo($field); ?><br /><?
        
    
}    
        
        
    
?>
    <input type="hidden" name="refer" value="<? echo $_SERVER['REQUEST_URI']; ?>" />
    <input type="submit" name="submit" value="Submit" /> 
    </form>
    <?    
    
    
    
}
        



?>
index.php:

Código PHP:
<?
include("Fpoll/poll.php");
echo 
$question;
?>
quiero mostrar la variable $question
  #4 (permalink)  
Antiguo 28/06/2007, 11:39
AlvaroG
Invitado
 
Mensajes: n/a
Puntos:
Re: Incluir una variable

Solamente estás definiendo $question en el if, no en el else.
Quizás vaya por ahí el problema....

Lo seguro es que si la variable se define dentro de poll.php, tenés que hacer el include antes de usarla.


Saludos.
  #5 (permalink)  
Antiguo 28/06/2007, 12:25
Borjimante
Invitado
 
Mensajes: n/a
Puntos:
Re: Incluir una variable

pero esque meto la variable question dentro del else y solo me muestra la pregunta debajo despues de votar no antes de votar, te dejo la página para que veas, aprovecho tambien para decir que solo puede votar uno y no se por que, una vez vote uno no vuelve a salir el icono de votar abajo:

http://www.antera-solutions.com/encuesta/ (vota y veras como no funciona)

codigo de poll.php:

Código PHP:
<?php 

// Author: PHPFront.com © 2005
// License: Free (GPL)
//
// Version: 1.0
//
// Created: 8.12.2005 
//
// More information and downloads 
// available at http://www.PHPFront.com
//
// #### poll.php ####



include("admincp/config.php");

$user_ip $_SERVER['REMOTE_ADDR'];

$ipquery mysql_query("SELECT * FROM ips WHERE ip='$user_ip'");
$select_banned mysql_num_rows($ipquery);

if(
$select_banned){

    
//display results
    
    
    
$poll mysql_fetch_array(mysql_query("select * from poll"));
    
$totalvotes $poll['totalvotes'];
    
$question $poll['question'];
            
    echo(
"<div class=poll>$question<br /><br />");
    
    
$get_questions mysql_query("select * from options");
    while(
$r=mysql_fetch_array($get_questions)){
    
    
        
extract($r);
        
$per $votes 100 $totalvotes;
        
$per floor($per);
        
        echo 
htmlspecialchars($field); 
        
?> <strong><? echo("$votes"); ?></strong><br />
        <div style="background-color: <? echo config(bg1); ?>;"><div style="color: <? echo config(text); ?>; font-size: <? echo config(size); ?>px; text-align: right;background-color: <? echo config(bg2); ?>; width: <? echo($per); ?>%;"><? echo("$per%"); ?></div></div>
        <?
            
    
}
    
    echo(
"<br />Total de votos: <strong>$totalvotes</strong></div>"); 
    
    
    
    
    
}else{


echo 
$question;


//if the submit button was pressed
if($_POST['submit']){
    
    
//grab vars
    
$vote $_POST['vote'];
    
$refer $_POST['refer'];
    
        
    
//update numbers
    
$update_totalvotes "UPDATE poll SET totalvotes = totalvotes + 1";
    
$insert mysql_query($update_totalvotes);
    
    
$update_votes "UPDATE options SET votes = votes + 1 WHERE id = $vote";
    
$insert mysql_query($update_votes);
            
    
//add ip to stop multiple voting
    
$ip $_SERVER['REMOTE_ADDR'];
    
$addip mysql_query("INSERT INTO ips (ip)""VALUES ('$ip')"); 

    
    
//send the user back to thepage they were just viewing
echo " <script language='JavaScript' type='text/JavaScript'> ";
echo 
" location.href='index.php' ";
echo 
" </script> ";

    
    
        
}    
    
    
//display the form!
    
?><form action="<? $_SERVER['PHP_SELF']; ?>" method="post"><?
        
        
    $getcurrent 
mysql_query("select * from options ORDER by id");
    while(
$r=mysql_fetch_array($getcurrent)){
        
        
extract($r);
        
        
?><input type="radio" name="vote" value="<? echo($id); ?>" class="radiobutton" /> <? echo($field); ?><br /><?
        
    
}    
        
        
    
?>
    <input type="hidden" name="refer" value="<? echo $_SERVER['REQUEST_URI']; ?>" />
    <input type="submit" name="submit" value="Submit" /> 
    </form>
    <?    
    
    
    
}
        



?>
  #6 (permalink)  
Antiguo 28/06/2007, 12:43
AlvaroG
Invitado
 
Mensajes: n/a
Puntos:
Re: Incluir una variable

Pregunta que quizás debí hacer antes: ¿$question existe antes de incluir poll.php?

Porque si existe, no hay problema en hacer el echo primero y luego incluir.
Pero si no existe, tenés un problema: poll.php define la variable y además te escribe el formulario
Entonces deberías hacer un "echo" de la variable antes de incluirlo... pero antes de incluirlo no hay variable $question...

Creo que no te quedará otra más que modificar poll.php para que imprima $question antes de crear el formulario (después de la línea que dice "display the form").

Y disculpá si me enredé y no pregunté antes ...


Saludos.
  #7 (permalink)  
Antiguo 28/06/2007, 14:02
Borjimante
Invitado
 
Mensajes: n/a
Puntos:
Re: Incluir una variable

He hecho lo que me dijiste y he puesto en el index.php:

Código PHP:
<?
echo $question;
include(
"Fpoll/poll.php");
?>
y en el poll.php

Código PHP:
    //display the form!

    echo $question; 
    ?><form action="<? $_SERVER['PHP_SELF']; ?>" method="post"><?
Sigue sin mostrarme la pregunta al principio y entonces los usuarios no sabran que hay que votar
  #8 (permalink)  
Antiguo 28/06/2007, 14:13
AlvaroG
Invitado
 
Mensajes: n/a
Puntos:
Re: Incluir una variable

Ese programa es realmente un asco... con todos los ánimos de ofender. El código es realmente malo.

No me contestaste la pregunta de si tenés $question antes de llamar al archivo, asumo que no. De todas formas,

probá mover estas líneas

$poll = mysql_fetch_array(mysql_query("select * from poll"));
$totalvotes = $poll['totalvotes'];
$question = $poll['question'];
echo("<div class=poll>$question<br /><br />");

fuera (arriba) del IF.


Saludos.
  #9 (permalink)  
Antiguo 28/06/2007, 14:30
Borjimante
Invitado
 
Mensajes: n/a
Puntos:
Re: Incluir una variable

se repite tres veces la pregunta que hago aora, perdona mi ignoranacia .

Entiendo que el código sea un asco pero si no sabes php tienes que intentar con scripts tan malos de este tipo
  #10 (permalink)  
Antiguo 28/06/2007, 19:07
AlvaroG
Invitado
 
Mensajes: n/a
Puntos:
Re: Incluir una variable

Pegalo cómo quedó ahora, acordate que escribí MOVER y no COPIAR...


Y no te preocupes que se soluciona


Saludos.
  #11 (permalink)  
Antiguo 29/06/2007, 02:40
Borjimante
Invitado
 
Mensajes: n/a
Puntos:
Re: Incluir una variable

asi a quedado el codigo:

Código PHP:
<?php 

// Author: PHPFront.com © 2005
// License: Free (GPL)
//
// Version: 1.0
//
// Created: 8.12.2005 
//
// More information and downloads 
// available at http://www.PHPFront.com
//
// #### poll.php ####



include("admincp/config.php");

$user_ip $_SERVER['REMOTE_ADDR'];

$ipquery mysql_query("SELECT * FROM ips WHERE ip='$user_ip'");
$select_banned mysql_num_rows($ipquery);

    echo(
"<div class=poll>$question<br /><br />");

$poll mysql_fetch_array(mysql_query("select * from poll")); 
$totalvotes $poll['totalvotes']; 
$question $poll['question']; 
echo(
"<div class=poll>$question<br /><br />");

if(
$select_banned){

    
//display results    
    
    
$get_questions mysql_query("select * from options");
    while(
$r=mysql_fetch_array($get_questions)){
    
    
        
extract($r);
        
$per $votes 100 $totalvotes;
        
$per floor($per);
        
        echo 
htmlspecialchars($field); 
        
?> <strong><? echo("$votes"); ?></strong><br />
        <div style="background-color: <? echo config(bg1); ?>;"><div style="color: <? echo config(text); ?>; font-size: <? echo config(size); ?>px; text-align: right;background-color: <? echo config(bg2); ?>; width: <? echo($per); ?>%;"><? echo("$per%"); ?></div></div>
        <?
            
    
}
    
    echo(
"<br />Total de votos: <strong>$totalvotes</strong></div>"); 
    
    
    
    
    
}else{


echo 
$question;


//if the submit button was pressed
if($_POST['submit']){
    
    
//grab vars
    
$vote $_POST['vote'];
    
$refer $_POST['refer'];
    
        
    
//update numbers
    
$update_totalvotes "UPDATE poll SET totalvotes = totalvotes + 1";
    
$insert mysql_query($update_totalvotes);
    
    
$update_votes "UPDATE options SET votes = votes + 1 WHERE id = $vote";
    
$insert mysql_query($update_votes);
            
    
//add ip to stop multiple voting
    
$ip $_SERVER['REMOTE_ADDR'];
    
$addip mysql_query("INSERT INTO ips (ip)""VALUES ('$ip')"); 

    
    
//send the user back to thepage they were just viewing
echo " <script language='JavaScript' type='text/JavaScript'> ";
echo 
" location.href='index.php' ";
echo 
" </script> ";

    
    
        
}    
    
    
//display the form!

    
echo $question
    
?><form action="<? $_SERVER['PHP_SELF']; ?>" method="post"><?
        
        
    $getcurrent 
mysql_query("select * from options ORDER by id");
    while(
$r=mysql_fetch_array($getcurrent)){
        
        
extract($r);
        
        
?><input type="radio" name="vote" value="<? echo($id); ?>" class="radiobutton" /> <? echo($field); ?><br /><?
        
    
}    
        
        
    
?>
    <input type="hidden" name="refer" value="<? echo $_SERVER['REQUEST_URI']; ?>" />
    <input type="submit" name="submit" value="Submit" /> 
    </form>
    <?    
    
    
    
}
        



?>
Un saludo y gracias por tu ayuda
  #12 (permalink)  
Antiguo 29/06/2007, 06:17
AlvaroG
Invitado
 
Mensajes: n/a
Puntos:
Re: Incluir una variable

Hay muchos "echo $question" por ahí :p

Eliminá el primero (el que quedó abajo de $select_banned = mysql_num_rows($ipquery); )
Y luego eliminá los que te quedaron como "echo $question" (sin el div) que son 2, uno inmediatamente después de //display the form y el otro inmediatamente después del else.

Una vez que borres esos 3, solo te va a quedar 1, y entonces debería quedar correctamente.


Saludos.
  #13 (permalink)  
Antiguo 29/06/2007, 07:57
Borjimante
Invitado
 
Mensajes: n/a
Puntos:
Re: Incluir una variable

todo bien. una ultima pregunta y estara perfecta, donde tengo que meter el codigo de codificación web en el poll.php para que se muestren las tildes , eñes, etc bien?
  #14 (permalink)  
Antiguo 29/06/2007, 08:48
AlvaroG
Invitado
 
Mensajes: n/a
Puntos:
Re: Incluir una variable

No necesariamente es en el poll.php, tené en cuenta que lo que importa es lo que recibe el navegador, por lo que te tenés que asegurar que en el HTML generado está todo correcto. Principalmente en la etiqueta META Content-Type, también te tenés que asegurar de que estás guardando los archivos PHP que generan texto con la codificación adecuada.

Fijate por acá....
http://www.forosdelweb.com/showpost....&postcount=180


Saludos.
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 02:25.