string to function

Status
Niet open voor verdere reacties.

tuxido devito

Gebruiker
Lid geworden
8 apr 2007
Berichten
71
Ik heb via een php functie een string gegenereerd met daarin een functie. Hoe zorg ik ervoor dat de functie in de string word uitgevoert?

voorbeeld

$output='echo "hoi";';

nu wil ik dat echo "hoi"; word uitgevoert. hoe doe ik dit?
 
Ik heb via een php functie een string gegenereerd met daarin een functie. Hoe zorg ik ervoor dat de functie in de string word uitgevoert?

voorbeeld

$output='echo "hoi";';

nu wil ik dat echo "hoi"; word uitgevoert. hoe doe ik dit?

Ik snap het niet helemaal, maar je kan evel gebruiken.

eval($output);

geeft als output:

hoi
 
okay ik heb nu dit:

Code:
<?php
//calculate the id
function calcid($table,$field,$reqid)
{
$query="SELECT $field FROM $table WHERE 1 ORDER BY $field DESC";
$result=mysql_query($query);

if(!mysql_affected_rows()==0)
{
$output="$reqid=mysql_result($result,0,$field); $reqid++;";
$output=str_replace('$reqid',$reqid,$output);
eval($output);
}
}
?>


ik gebruik het als module om een id van een tabel uit te rekenen het zit geintergreerd in een andere pagina die tot nu toe alleen een testrun onderaan de pagina laatzien, dit resulteerd in een error.

Parse error: syntax error, unexpected $end in /opt/lampp/htdocs/site/modules/calcid.php(12) : eval()'d code on line 1

de pagina waar het in geintergreerd zit ziet er zo uit:

Code:
<?php
include('modules/db_connect.php');
include('modules/calcid.php');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"><title>Digi Art</title>
<style type="text/css">
.menuitem:hover {
  background-image: url(images/hover.png);
  background-repeat: repeat-y;
}

a {
  color: #000000;
}

a:hover {
  color: gray;
}


</style></head>
<body style="background-color: gray;"><table style="text-align: left; width: 800px; height: 788px;" border="0" cellpadding="0" cellspacing="0"> <tbody><tr><td style="width: 560px;" colspan="3" rowspan="1"><img style="width: 800px; height: 106px;" alt="logo" src="images/banner.png"></td> </tr><tr> <td style="width: 560px; height: 8px;" colspan="3" rowspan="1"></td> </tr> <tr align="right"> <td colspan="3" rowspan="1" style="background-image: url(images/bar.png); background-repeat: no-repeat; width: 560px; height: 27px;"><span style="font-family: monospace;"><a href="index.php?showpage=login">Login</a> | <a href="index.php?showpage=register">Register</a></span></td></tr><tr><td style="height: 8px; width: 560px;" colspan="3" rowspan="1"></td> </tr> <tr><td class="" style="background-image: url(images/mtop.png); background-repeat: no-repeat; width: 220px; text-align: center; height: 24px;"><span style="font-weight: bold; font-size: 0.6em;">:: Menu ::</span></td>
<td colspan="1" rowspan="3" style="height: 24px; width: 8px;"></td> <td style="background-image: url(images/btop.png); background-repeat: no-repeat; text-align: center; height: 24px; width: 560px;"><span style="font-weight: bold; font-size: 0.6em;">:: Content ::</span></td>
</tr> <tr> <td style="background-image: url(images/mbody.png); background-repeat: repeat-y; width: 220px; height: 539px; text-align: left; vertical-align: top;"><!--Begin menu --><table style="text-align: left; width: 100%;" border="0" cellpadding="0" cellspacing="0"><tbody><tr align="center"><td style="height: 8px;"><span style="font-weight: bold;"></span><span style="font-family: monospace;">::&nbsp;&nbsp;<span style="font-weight: bold;">navigation</span>&nbsp;::</span></td></tr><tr class="menuitem" style="font-family: monospace;">
<td class="menuitem" style="width: 100%; text-align: center;"><a href="index.php?showpage=home">:: Home &nbsp; &nbsp; &nbsp; &nbsp;::</a></td>
</tr><tr align="center"><td class="menuitem"><a href="index.php?showpage=about"><span style="font-family: monospace;">::
About &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;::</span></a></td></tr><tr align="center"><td class="menuitem"><a href="index.php?showpage=news"><span style="font-family: monospace;">::
News&nbsp; &nbsp;
&nbsp; &nbsp; ::</span></a></td></tr><tr align="center"><td class="menuitem"><a href="index.php?showpage=articles"><span style="font-family: monospace;">::
Articles &nbsp;
&nbsp;::</span></a></td></tr><tr><td style="height: 16px;"></td></tr><tr style="font-family: monospace;" align="center"><td><span style="font-weight: bold;"></span>:: &nbsp;
&nbsp;<span style="font-weight: bold;">items</span> &nbsp; &nbsp;::</td></tr><tr style="font-family: monospace;" align="center"><td class="menuitem"><a href="index.php?showpage=3dmodels">::
3d
Models &nbsp; ::</a></td></tr><tr style="font-family: monospace;" align="center"><td class="menuitem"><a href="index.php?showpage=comics">::
Comics
&nbsp; &nbsp; &nbsp;::</a></td></tr><tr style="font-family: monospace;" align="center"><td class="menuitem"><a href="index.php?showpage=drawings">::
Drawings
&nbsp; &nbsp;::</a></td></tr><tr style="font-family: monospace;" align="center"><td class="menuitem"><a href="index.php?showpage=games">::
Games&nbsp;
&nbsp; &nbsp; &nbsp;::</a></td></tr><tr style="font-family: monospace;" align="center"><td class="menuitem"><a href="index.php?showpage=wallpapers">::
Wallpapers
&nbsp;::</a></td></tr><tr style="font-family: monospace;" align="center"><td class="menuitem"><a href="index.php?showpage=other">::
Other&nbsp;
&nbsp; &nbsp; &nbsp;::</a></td></tr>
</tbody></table></td><td style="background-image: url(images/bbody.png); background-repeat: repeat-y; height: 539px; width: 560px;"></td></tr><tr><td style="background-image: url(images/mbottom.png); background-repeat: no-repeat; height: 24px; width: 220px;"></td><td style="background-image: url(images/bbottom.png); background-repeat: no-repeat; height: 24px; width: 560px;"></td></tr><tr><td colspan="3" rowspan="1" style="height: 18px; width: 560px;"></td> </tr><tr><td style="background-image: url(images/bar.png); background-repeat: no-repeat; width: 560px; height: 27px; text-align: center;" colspan="3" rowspan="1"><small><small>Website
design ? 2007 Sven van de Scheur</small></small></td>
</tr> </tbody></table><br></body></html>

<?php
calcid('Users','id','$id');
include('modules/db_disconnect.php');
echo $id;
?>

wat doe ik fout?
 
Laatst bewerkt:
Probeer dit eens:

Functie:
PHP:
<?php
//calculate the id
function calcid($table,$field)
{
$query="SELECT $field FROM $table WHERE 1 ORDER BY $field DESC";
$result=mysql_query($query);

if(!mysql_affected_rows()==0)
{

	$reqid = mysql_result($result,0,$field);
	$reqid++;
	return $reqid;
}
}
?>

Bestand:

PHP:
<?php
include('modules/db_connect.php');
include('modules/calcid.php');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"><title>Digi Art</title>
<style type="text/css">
.menuitem:hover {
  background-image: url(images/hover.png);
  background-repeat: repeat-y;
}

a {
  color: #000000;
}

a:hover {
  color: gray;
}


</style></head>
<body style="background-color: gray;"><table style="text-align: left; width: 800px; height: 788px;" border="0" cellpadding="0" cellspacing="0"> <tbody><tr><td style="width: 560px;" colspan="3" rowspan="1"><img style="width: 800px; height: 106px;" alt="logo" src="images/banner.png"></td> </tr><tr> <td style="width: 560px; height: 8px;" colspan="3" rowspan="1"></td> </tr> <tr align="right"> <td colspan="3" rowspan="1" style="background-image: url(images/bar.png); background-repeat: no-repeat; width: 560px; height: 27px;"><span style="font-family: monospace;"><a href="index.php?showpage=login">Login</a> | <a href="index.php?showpage=register">Register</a></span></td></tr><tr><td style="height: 8px; width: 560px;" colspan="3" rowspan="1"></td> </tr> <tr><td class="" style="background-image: url(images/mtop.png); background-repeat: no-repeat; width: 220px; text-align: center; height: 24px;"><span style="font-weight: bold; font-size: 0.6em;">:: Menu ::</span></td>
<td colspan="1" rowspan="3" style="height: 24px; width: 8px;"></td> <td style="background-image: url(images/btop.png); background-repeat: no-repeat; text-align: center; height: 24px; width: 560px;"><span style="font-weight: bold; font-size: 0.6em;">:: Content ::</span></td>
</tr> <tr> <td style="background-image: url(images/mbody.png); background-repeat: repeat-y; width: 220px; height: 539px; text-align: left; vertical-align: top;"><!--Begin menu --><table style="text-align: left; width: 100%;" border="0" cellpadding="0" cellspacing="0"><tbody><tr align="center"><td style="height: 8px;"><span style="font-weight: bold;"></span><span style="font-family: monospace;">::&nbsp;&nbsp;<span style="font-weight: bold;">navigation</span>&nbsp;::</span></td></tr><tr class="menuitem" style="font-family: monospace;">
<td class="menuitem" style="width: 100%; text-align: center;"><a href="index.php?showpage=home">:: Home &nbsp; &nbsp; &nbsp; &nbsp;::</a></td>
</tr><tr align="center"><td class="menuitem"><a href="index.php?showpage=about"><span style="font-family: monospace;">::
About &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;::</span></a></td></tr><tr align="center"><td class="menuitem"><a href="index.php?showpage=news"><span style="font-family: monospace;">::
News&nbsp; &nbsp;
&nbsp; &nbsp; ::</span></a></td></tr><tr align="center"><td class="menuitem"><a href="index.php?showpage=articles"><span style="font-family: monospace;">::
Articles &nbsp;
&nbsp;::</span></a></td></tr><tr><td style="height: 16px;"></td></tr><tr style="font-family: monospace;" align="center"><td><span style="font-weight: bold;"></span>:: &nbsp;
&nbsp;<span style="font-weight: bold;">items</span> &nbsp; &nbsp;::</td></tr><tr style="font-family: monospace;" align="center"><td class="menuitem"><a href="index.php?showpage=3dmodels">::
3d
Models &nbsp; ::</a></td></tr><tr style="font-family: monospace;" align="center"><td class="menuitem"><a href="index.php?showpage=comics">::
Comics
&nbsp; &nbsp; &nbsp;::</a></td></tr><tr style="font-family: monospace;" align="center"><td class="menuitem"><a href="index.php?showpage=drawings">::
Drawings
&nbsp; &nbsp;::</a></td></tr><tr style="font-family: monospace;" align="center"><td class="menuitem"><a href="index.php?showpage=games">::
Games&nbsp;
&nbsp; &nbsp; &nbsp;::</a></td></tr><tr style="font-family: monospace;" align="center"><td class="menuitem"><a href="index.php?showpage=wallpapers">::
Wallpapers
&nbsp;::</a></td></tr><tr style="font-family: monospace;" align="center"><td class="menuitem"><a href="index.php?showpage=other">::
Other&nbsp;
&nbsp; &nbsp; &nbsp;::</a></td></tr>
</tbody></table></td><td style="background-image: url(images/bbody.png); background-repeat: repeat-y; height: 539px; width: 560px;"></td></tr><tr><td style="background-image: url(images/mbottom.png); background-repeat: no-repeat; height: 24px; width: 220px;"></td><td style="background-image: url(images/bbottom.png); background-repeat: no-repeat; height: 24px; width: 560px;"></td></tr><tr><td colspan="3" rowspan="1" style="height: 18px; width: 560px;"></td> </tr><tr><td style="background-image: url(images/bar.png); background-repeat: no-repeat; width: 560px; height: 27px; text-align: center;" colspan="3" rowspan="1"><small><small>Website
design ? 2007 Sven van de Scheur</small></small></td>
</tr> </tbody></table><br></body></html>

<?php
$id = calcid('Users','id');
include('modules/db_disconnect.php');
echo $id;
?>
 
hier heb ik niets aan aangezien dit de mogelijkhijd weghaalt te bepalen in welke variable je de output wilt opslaan (3e argument functie)
 
hier heb ik niets aan aangezien dit de mogelijkhijd weghaalt te bepalen in welke variable je de output wilt opslaan (3e argument functie)

Dit kan je doen doormiddel van:

$variabele = functie($var1,$var2);

Dan wordt de output in $variabele opgeslagenm.
die kan je hernoemen naar wat je maar wilt.
 
Ben je van plan die functie aan te maken?

Ligt het dan aan mij of kun je dan niet net zo goed mysql_num_rows gebruiken? :rolleyes:
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan