Foros del Web » Programando para Internet » PHP »

Agregar Variable a function

Estas en el tema de Agregar Variable a function en el foro de PHP en Foros del Web. Buenas amigos, mi problema es el siguiente tengo el siguiente codigo de una galeria, funciona bien cuando en esta parte para abrir el directorio se ...
  #1 (permalink)  
Antiguo 12/02/2012, 23:26
Avatar de mazaku  
Fecha de Ingreso: septiembre-2009
Ubicación: Veracruz
Mensajes: 104
Antigüedad: 14 años, 7 meses
Puntos: 0
Pregunta Agregar Variable a function

Buenas amigos, mi problema es el siguiente tengo el siguiente codigo de una galeria, funciona bien cuando en esta parte para abrir el directorio se le escribe manualmente el destino

Código PHP:
{
    if (
$dir = @opendir("images/")) 
    { 
pero cuando le quiero pasar una ruta a traves de una variable la galeria no funciona, no da error pero no muestra nada. Lo trato de poner asi

Código PHP:
<?php 
$direct 
="".$reg['c_path']."";//  
function loadPics($pics)

{
    if (
$dir = @opendir("$direct/")) 
    {
pero no muestra nada.

hice la prueba al final mostrar la variable
Código PHP:
<?php echo "$direct"?>
y si muestra la ruta que saca de la bd. Mi problema es por que no funciona entonces en la parte de function loadPics que es donde debe ir la ruta.

les dejo el codigo completo para ver si me podrian hechar una mano, gracias y saludos a todos

galeria.php
Código PHP:
<?php 
session_start
();
include (
'config.php');
$c_capitulo $_GET['capitulo']; // Esto recoje los datos de la variable id en la url.
$registros=mysql_query("SELECT * FROM capitulos WHERE c_capitulo='$c_capitulo'",  
           
$db_link) or
  die(
"Problemas en el select:".mysql_error());
while (
$reg=mysql_fetch_array($registros))
{

 
 
?>
<?php 
$direct 
="".$reg['c_path']."";//  
function loadPics($pics)

{
    if (
$dir = @opendir("$direct/")) 
    {
        while(
$file readdir($dir)) 
        {
            if(
ereg("\.jpg|\.gif|\.bmp|\.pnc$"$file))
                
array_push($pics$file);
        }  
        
        
closedir($dir);
    }
    
    
sort($pics);
    return 
$pics;
}

function 
listPics($pics)
{
    while(list(
$num$pic) = each($pics))
    {
        echo 
"<option value='".$PHP_SELF."?c=$num'>".getPicName($num$pics)."</option>\n";
    }
}

function 
showPic($c$pics)
{
    
$pic $pics[$c];
    echo 
"<!-- $c -->\n";
    echo 
"<img src=\"$direct/$pic\">";
}

function 
getPrev($c$pics)
{
    
$len count($pics);
    
$ret 0;
    if(
$len 1)
    {
        if(
$c == 0)
            
$ret $len 1;
        else
            
$ret $c 1;
    }
    else
    {
        
$ret $c;
    }
    return 
$ret;
}

function 
getNext($c$pics)
{
    
$len count($pics);
    
$ret 0;
    if(
$len 1)
    {
        if(
$c == $len -1)
            
$ret 0;
        else
            
$ret $c 1;
    }
    else
    {
        
$ret $c;
    }
    return 
$ret;
}

function 
getPicName($c$pics)
{
    return 
preg_replace("/\.jpg$|\.gif$|\.png$/i"""$pics[$c]);
}

$pics = array();
$pics loadPics($pics);

if(!isset(
$_REQUEST['c'])) $_REQUEST['c'] = 0;

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>"<?=getPicName($_REQUEST['c'], $pics)?>"</title>

<style type="text/css">

body {
    background: #212020;
    font: 11px/16px arial, helvetica, sans-serif;
    color: #363333;
    margin: 0;
    padding: 0;
    }
table, tr, td {
    font: 11px/16px arial, helvetica, sans-serif;
    color: #363333;
    border-collapse: collapse;
    border: none;
    margin: 0;
    padding: 0;
    }
    
#header { background: url(images/backgroundHeader.gif) top repeat-x; }

#title {
    font-size: 24px;
    color: #fff;
    padding: 30px;
    margin-top: 2px;
    }
#dropdownNav {
    background: #212020;
    float: right;
    font-weight: bold;
    color: #959595;
    border-left: 1px solid #2b2a2a;
    padding: 30px 30px 25px 30px;
    }
    
.navigation {
    background: #959595 url(images/backgroundNavigation.gif) top repeat-x;
    border-top: 2px solid #b6b6b6;
    border-bottom: 2px solid #b6b6b6;
    width: 100%;
    }
.navLeft {
    text-align: left;
    padding: 10px 20px;
    width: 50%;
    font-weight: bold;
    font-size: 12px;
    }
.navRight {
    text-align: right;
    padding: 10px 20px;
    width: 50%;
    font-weight: bold;
    font-size: 12px;
    }

#container {
    background: #fff;
    margin: 0;
    padding: 0;
    width: 100%;
    }
#index {
    background: #e7e7e7;
    vertical-align: top;
    padding: 0;
    border-right: 1px dashed #d7dee2;
    white-space: nowrap;
    width: 200px;
    }
#index ul {
    margin: 0;
    padding: 0;
    }
#index ul li {
    list-style: none;
    font-weight: bold;
    border-bottom: 1px dashed #d5d5d5;
    }
    
#design {
    vertical-align: top;
    padding: 20px;
    text-align: center;
    }
    
p, h1 { margin-top: 0; }

a:link, a:active, a:visited {
    color: #004f86;
    text-decoration: none;
    }
a:hover {
    color: #0072c1;
    text-decoration: none;
    }

.navigation a:link, .navigation a:active, .navigation a:visited {
    color: #fff;
    text-decoration: none;
    }
.navigation a:hover {
    color: #ddd;
    text-decoration: none;
    }
    
#index a:link, #index a:active, #index a:visited {
    background: #f3f3f3;
    text-decoration: none;
    padding: 5px 10px;
    display: block;
    }

#index a:hover {
    background: #f9f9f9;
    text-decoration: none;
    padding: 5px 10px;
    display: block;
    }
    
img { border: none; }

#design img {
    padding: 4px;
    border: 1px dashed #d7dee2;
    }
    
#footer {
    padding: 20px;
    color: #858585;
    font-size: 11px;
    }
#footer a:link, #footer a:active, #footer a:visited {
    color: #858585;
    text-decoration: none;
    }
#footer a:hover { color: #a1a1a1;}

form {
    margin: 0;
    padding: 0;
    }
select {
    font: 11px/16px arial, helvetica, sans-serif;
    color: #363333;
    margin: 0 0 0 5px;
    padding: 0;
    }
    
</style>

<script language="JavaScript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
//-->
</script>

</head>

<body>

<div id="header">

    <div id="dropdownNav">
        <form name="menu">
        Chose Design: 
          <select name="jumpMenu" onChange="MM_jumpMenu('parent',this,0)">
              <option><?=getPicName($_REQUEST['c'], $pics)?></option>
            <? listPics($pics); ?>
          </select>
        </form>
    </div>

    <div id="title"><?=getPicName($_REQUEST['c'], $pics)?></div>
    
</div>

<table class="navigation">
    <tr>
        <td class="navLeft"><a href=<?=$PHP_SELF?>?c=<?=getPrev($_REQUEST['c'], $pics)?>>&laquo; previous design</a></td>
        <td class="navRight"><a href=<?=$PHP_SELF?>?c=<?=getNext($_REQUEST['c'], $pics)?>>next design &raquo;</a></td>
    </tr>
</table>

<table id="container">
    <tr>
        <td id="design">

            <a href=<?=$PHP_SELF?>?c=<?=getNext($_REQUEST['c'], $pics)?>>
            <?
                
if(isset($_REQUEST['c']))
                
showPic($_REQUEST['c'], $pics);
            
?>
            </a>
            
        </td>
    </tr>
</table>

<table class="navigation">
    <tr>
        <td class="navLeft"><a href=<?=$PHP_SELF?>?c=<?=getPrev($_REQUEST['c'], $pics)?>>&laquo; previous design</a></td>
        <td class="navRight"><a href=<?=$PHP_SELF?>?c=<?=getNext($_REQUEST['c'], $pics)?>>next design &raquo;</a></td>
    </tr>
</table>

<div id="footer"><a href="http://imgview.com/">IMGView Photo Viewer</a></div>
<?php echo "$direct"?>
<?php 
}
?>
</body>
</html>
  #2 (permalink)  
Antiguo 13/02/2012, 01:05
Avatar de repara2  
Fecha de Ingreso: septiembre-2010
Ubicación: München
Mensajes: 2.445
Antigüedad: 13 años, 7 meses
Puntos: 331
Respuesta: Agregar Variable a function

Dentro de la función la variable no está definida:

Código PHP:
Ver original
  1. $direct ="".$reg['c_path']."";//  
  2. function loadPics($pics)
  3.  
  4. {
  5.     if ($dir = @opendir("$direct/"))
  6.     {

Código PHP:
Ver original
  1. $direct ="".$reg['c_path']."";//  
  2. function loadPics($pics)
  3.  
  4. {
  5.    global $direct;
  6.     if ($dir = @opendir("$direct/"))
  7.     {
__________________
Fere libenter homines, id quod volunt, credunt.
  #3 (permalink)  
Antiguo 13/02/2012, 10:08
Avatar de mazaku  
Fecha de Ingreso: septiembre-2009
Ubicación: Veracruz
Mensajes: 104
Antigüedad: 14 años, 7 meses
Puntos: 0
Respuesta: Agregar Variable a function

muchas gracias repara2 me ha funcionado bien, ahora me han surgido otros inconvenientes. podrias decirme de que forma podrian arreglarse por favor

1.- que en el select en vez de mostrar los nombres de las imagenes se muestren por numeracion ejemplo en vez de imagen1, imagen 2... etc. me aparezca 1,2,3...etc

2.- como arreglar para que se muestren en orden por ejemplo si tengo las imagenes de la 1 a la 10, en vez de ser 1,2,3....10, me aparecen asi 1,10,2,3,....9

saludos y muchas gracias por su ayuda.
  #4 (permalink)  
Antiguo 13/02/2012, 12:38
Avatar de repara2  
Fecha de Ingreso: septiembre-2010
Ubicación: München
Mensajes: 2.445
Antigüedad: 13 años, 7 meses
Puntos: 331
Respuesta: Agregar Variable a function

1.- que en el select en vez de mostrar los nombres de las imagenes se muestren por numeracion ejemplo en vez de imagen1, imagen 2... etc. me aparezca 1,2,3...etc
En principio no veo tu select ni tu tabla, pero si tiene un campo id o similar puedes utilizarlo

2.- como arreglar para que se muestren en orden por ejemplo si tengo las imagenes de la 1 a la 10, en vez de ser 1,2,3....10, me aparecen asi 1,10,2,3,....9

Utiliza las funciones de ordenamiento de arrays de php. No sé a qué array te refieres pero sort() y usort() te pueden servir.

Saludos
__________________
Fere libenter homines, id quod volunt, credunt.
  #5 (permalink)  
Antiguo 13/02/2012, 16:05
Avatar de mazaku  
Fecha de Ingreso: septiembre-2009
Ubicación: Veracruz
Mensajes: 104
Antigüedad: 14 años, 7 meses
Puntos: 0
Respuesta: Agregar Variable a function

pues si maneja sort

Código PHP:
function loadPics($pics)

{
    if (
$dir = @opendir("$direct/")) 
    {
        while(
$file readdir($dir)) 
        {
            if(
ereg("\.jpg|\.gif|\.bmp|\.pnc$"$file))
                
array_push($pics$file);
        }  
        
        
closedir($dir);
    }
    
    
sort($pics);
    return 
$pics;

pero me muestra las imagenes no en orden sino asi de 1,10,2,3,4,5,6...etc
  #6 (permalink)  
Antiguo 14/02/2012, 23:20
Avatar de mazaku  
Fecha de Ingreso: septiembre-2009
Ubicación: Veracruz
Mensajes: 104
Antigüedad: 14 años, 7 meses
Puntos: 0
Respuesta: Agregar Variable a function

buenas al parecer ya las muestra ordenas las imagenes pero tengo otro problema y es que como hago para que al llegar al final de las imagenes ya no tenga para darle siguiente y ya no me vuelva a regresar a la primera ni al principio tampoco al darle atras me muestre nada anterior.

creo es en esta parte pero no se que hacerle para que no muestre ni antes ni despues de la primera y ultima imagen.

Código PHP:
function getPrev($c$pics)
{
    
$len count($pics);
    
$ret 0;
    if(
$len 1)
    {
        if(
$c == 0)
            
$ret $len 1;
        else
            
$ret $c 1;
    }
    else
    {
        
$ret $c;
    }
    return 
$ret;
}

function 
getNext($c$pics)
{
    
$len count($pics);
    
$ret 0;
    if(
$len 1)
    {
        if(
$c == $len -1)
            
$ret 0;
        else
            
$ret $c 1;
    }
    else
    {
        
$ret $c;
    }
    return 
$ret;

  #7 (permalink)  
Antiguo 15/02/2012, 01:46
Avatar de repara2  
Fecha de Ingreso: septiembre-2010
Ubicación: München
Mensajes: 2.445
Antigüedad: 13 años, 7 meses
Puntos: 331
Respuesta: Agregar Variable a function

Comprueba los valores devueltos por cada función, si getPrev devuelve 0 entonces no muestras el link.
Postea los datos de muestra para ver cómo funciona.
__________________
Fere libenter homines, id quod volunt, credunt.
  #8 (permalink)  
Antiguo 15/02/2012, 18:10
Avatar de mazaku  
Fecha de Ingreso: septiembre-2009
Ubicación: Veracruz
Mensajes: 104
Antigüedad: 14 años, 7 meses
Puntos: 0
Respuesta: Agregar Variable a function

esto es lo que muestra

Código HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>"VK47-01"</title>

<style type="text/css">

body {
	background: #212020;
	font: 11px/16px arial, helvetica, sans-serif;
	color: #363333;
	margin: 0;
	padding: 0;
	}
table, tr, td {
	font: 11px/16px arial, helvetica, sans-serif;
	color: #363333;
	border-collapse: collapse;
	border: none;
	margin: 0;
	padding: 0;
	}
	
#header { background: url(images/backgroundHeader.gif) top repeat-x; }

#title {
	font-size: 24px;
	color: #fff;
	padding: 30px;
	margin-top: 2px;
	}
#dropdownNav {
	background: #212020;
	float: right;
	font-weight: bold;
	color: #959595;
	border-left: 1px solid #2b2a2a;
	padding: 30px 30px 25px 30px;
	}
	
.navigation {
	background: #959595 url(images/backgroundNavigation.gif) top repeat-x;
	border-top: 2px solid #b6b6b6;
	border-bottom: 2px solid #b6b6b6;
	width: 100%;
	}
.navLeft {
	text-align: left;
	padding: 10px 20px;
	width: 50%;
	font-weight: bold;
	font-size: 12px;
	}
.navRight {
	text-align: right;
	padding: 10px 20px;
	width: 50%;
	font-weight: bold;
	font-size: 12px;
	}

#container {
	background: #fff;
	margin: 0;
	padding: 0;
	width: 100%;
	}
#index {
	background: #e7e7e7;
	vertical-align: top;
	padding: 0;
	border-right: 1px dashed #d7dee2;
	white-space: nowrap;
	width: 200px;
	}
#index ul {
	margin: 0;
	padding: 0;
	}
#index ul li {
	list-style: none;
	font-weight: bold;
	border-bottom: 1px dashed #d5d5d5;
	}
	
#design {
	vertical-align: top;
	padding: 20px;
	text-align: center;
	}
	
p, h1 { margin-top: 0; }

a:link, a:active, a:visited {
	color: #004f86;
	text-decoration: none;
	}
a:hover {
	color: #0072c1;
	text-decoration: none;
	}

.navigation a:link, .navigation a:active, .navigation a:visited {
	color: #fff;
	text-decoration: none;
	}
.navigation a:hover {
	color: #ddd;
	text-decoration: none;
	}
	
#index a:link, #index a:active, #index a:visited {
	background: #f3f3f3;
	text-decoration: none;
	padding: 5px 10px;
	display: block;
	}

#index a:hover {
	background: #f9f9f9;
	text-decoration: none;
	padding: 5px 10px;
	display: block;
	}
	
img { border: none; }

#design img {
	padding: 4px;
	border: 1px dashed #d7dee2;
	}
	
#footer {
	padding: 20px;
	color: #858585;
	font-size: 11px;
	}
#footer a:link, #footer a:active, #footer a:visited {
	color: #858585;
	text-decoration: none;
	}
#footer a:hover { color: #a1a1a1;}

form {
	margin: 0;
	padding: 0;
	}
select {
	font: 11px/16px arial, helvetica, sans-serif;
	color: #363333;
	margin: 0 0 0 5px;
	padding: 0;
	}
	
</style>

<script language="JavaScript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
//-->
</script>

</head>

<body>

<div id="header">

	<div id="dropdownNav">
		<form name="menu">
		Chose Design: 
		  <select name="jumpMenu" onChange="MM_jumpMenu('parent',this,0)">
		  	<option>VK47-01</option>
			<option value='?capitulo=47&c=0'>VK47-01</option>
<option value='?capitulo=47&c=1'>VK47-02</option>
<option value='?capitulo=47&c=2'>VK47-03</option>
<option value='?capitulo=47&c=3'>VK47-04</option>
<option value='?capitulo=47&c=4'>VK47-05</option>
<option value='?capitulo=47&c=5'>VK47-06</option>
<option value='?capitulo=47&c=6'>VK47-08</option>
<option value='?capitulo=47&c=7'>VK47-10</option>
<option value='?capitulo=47&c=8'>VK47-11</option>
<option value='?capitulo=47&c=9'>VK47-13</option>
		  </select>
		</form>
	</div>

	<div id="title">VK47-01</div>
	
</div>

<table class="navigation">
	<tr>
		<td class="navLeft"><a href=?capitulo=47&c=9>&laquo; previous design</a></td>
		<td class="navRight"><a href=?capitulo=47&c=1>next design &raquo;</a></td>
	</tr>
</table>

<table id="container">
	<tr>
		<td id="design">

			<a href=?capitulo=47&c=1>
			<!-- 0 -->
<img src="c/nosferatu_fansub/maga-tsuki/47/3491//VK47-01.jpg"> </a>
			
		</td>
	</tr>
</table>

<table class="navigation">
	<tr>
		<td class="navLeft"><a href=?capitulo=47&c=9>&laquo; previous design</a></td>
		<td class="navRight"><a href=?capitulo=47&c=1>next design &raquo;</a></td>
	</tr>
</table>

<div id="footer"><a href="http://imgview.com/">IMGView Photo Viewer</a></div>
c/nosferatu_fansub/maga-tsuki/47/3491////
47<br><table border='1' cellpadding='2' bgcolor='#FFFFDF' bordercolor='#E8B900' align='center'><tr><td><font face='Arial' size='1' color='#000000'><b>PHP Error Message</b></font></td></tr></table><br />
<b>Fatal error</b>:  Cannot redeclare loadpics() (previously declared in /home/a8419646/public_html/galeria2.php:16) in <b>/home/a8419646/public_html/galeria2.php</b> on line <b>16</b><br />
<br><table border='1' cellpadding='2' bgcolor='#FFFFDF' bordercolor='#E8B900' align='center'><tr><td><div align='center'><a href='http://www.000webhost.com/'><font face='Arial' size='1' color='#000000'>Free Web Hosting</font></a></div></td></tr></table> 
de hecho no me habia dado cuenta antes pero me marca un error en esta parte

Código PHP:
<table border='1' cellpadding='2' bgcolor='#FFFFDF' bordercolor='#E8B900' align='center'><tr><td><font face='Arial' size='1' color='#000000'><b>PHP Error Message</b></font></td></tr></table><br />
<
b>Fatal error</b>:  Cannot redeclare loadpics() (previously declared in /home/a8419646/public_html/galeria2.php:16in <b>/home/a8419646/public_html/galeria2.php</bon line <b>16</b><br /> 
  #9 (permalink)  
Antiguo 16/02/2012, 00:33
Avatar de repara2  
Fecha de Ingreso: septiembre-2010
Ubicación: München
Mensajes: 2.445
Antigüedad: 13 años, 7 meses
Puntos: 331
Respuesta: Agregar Variable a function

El método loadpics() está dos veces en tu código.
Casi seguro que estás incluyendo algún archivo dos veces.
__________________
Fere libenter homines, id quod volunt, credunt.
  #10 (permalink)  
Antiguo 18/02/2012, 12:19
Avatar de mazaku  
Fecha de Ingreso: septiembre-2009
Ubicación: Veracruz
Mensajes: 104
Antigüedad: 14 años, 7 meses
Puntos: 0
Respuesta: Agregar Variable a function

disculpa pero no se como agregarle eso que me dices

Cita:
getPrev devuelve 0 entonces no muestras el link.
no se como se lo agregaria a la funcion.

Etiquetas: function, html, registro, sql, variables
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 19:02.