Web Hosting Talk







View Full Version : help with function


vision3
09-11-2004, 03:13 PM
is there any way to make this work?

while ($qry = mysql_fetch_array($result, MYSQL_ASSOC))
{

$name = $qry['name'];

function $name()
{
global $site, $DB;

echo 'test';
}

}

Im trying to make it so that it will make the same function for all the names i have on the data base. Any advice is welcome :)

PaddysPlace
09-11-2004, 03:49 PM
What is the final goal for this script? There might be a better way :p

Regards,
Patrick

vision3
09-11-2004, 03:55 PM
I have my index page to do a page break. I have a form where I add html into my database with the page name. I want to be able to automatically when I add a new page into my database it would create a new page break and add new function for it like the one I have above.

I hope this makes sense

jorisvdb
09-12-2004, 10:03 AM
What error do you recieve?

while ($qry = mysql_fetch_array($result))
{

$name = $qry['name'];

function name() // function $functionname() is incorrect, $ is for a variable
{
global $site, $DB;

echo 'test';
}

}

nothing will happen when you run this script because you don't run the function name()

vision3
09-12-2004, 10:09 AM
well this is what i actualy have:


$tableA = "SELECT * FROM pages";
$result = mysql_query($tableA) or die("Query failed : " . mysql_error());
while ($qry = mysql_fetch_array($result, MYSQL_ASSOC))
{

$name = $qry['name'];
function $name()
{
global $site, $DB;
$table = "SELECT * FROM pages WHERE name='$name' ";
$result = mysql_query($table) or die("Query failed : " . mysql_error());
$qry = mysql_fetch_array($result, MYSQL_ASSOC);
$html = $qry['html'];
$name = $qry['name'];
$body = stripslashes($html);

require ('templates/main.html');

}

The messge i get is


Parse error: parse error, unexpected T_VARIABLE, expecting T_STRING in /home/user/public_html/index.php on line 45

zeonite
09-12-2004, 10:10 AM
What is the goal for it?

<?php

function name($var)
{
global $site, $DB;

echo $var;
}

while ($qry = mysql_fetch_array($result, MYSQL_ASSOC))
{

$name = $qry['name'];

name($name); // output: $qry['name'];

}

?>

I don't think there is a way to do it like you were doing.
Check PHP manual for $$var or ${$var}
If i'm not mistaken...
$var = "John";
${$var} = "Smith";
echo $var; // output: "John"
echo $John; // output: "Smith";

But I'm not sure.

Hope it helps.

vision3
09-12-2004, 10:21 AM
what im trying to do is create a php page thats does a break. i whant to be able to loop the breack example:


switch ($id){

while ($qry = mysql_fetch_array($result, MYSQL_ASSOC))
{
$name = $qry['name'];

case '$name':
$name();
break;




}
default:
home();
break;

}


This way i dont have to keep adding a new break manualy when it will automaticly will do it once i enter the name of it in the database.

I also want it to also automaticly add a function for each case break, example:


while ($qry = mysql_fetch_array($result, MYSQL_ASSOC))
{

$name = $qry['name'];
function $name()
{
global $site, $DB;
$table = "SELECT * FROM pages WHERE name='$name' ";
$result = mysql_query($table) or die("Query failed : " . mysql_error());
$qry = mysql_fetch_array($result, MYSQL_ASSOC);
$html = $qry['html'];
$name = $qry['name'];
$body = stripslashes($html);

require ('templates/main.html');

}

Burhan
09-12-2004, 11:22 AM
This will not work the way you want because the function won't be in scope.

You should rethink your logic -- because creating dynamic code on the fly is rarely a good idea.

You should instead consider a class, or store such meta information in the database itself.

Or use a templating system.

vision3
09-12-2004, 11:32 AM
Thanks for the advice ill do just that.

vision3
09-12-2004, 11:49 AM
Welli got it to work but in a differrent way.

Case break:

$id = $_GET['page'];
$name = $_GET['name'];
$site = new Page;
switch ($id){


case 'pages':
pages($name);
break;

default:
home();
break;



}



Function:

function pages($name)
{
global $site, $DB;
$table = "SELECT * FROM pages WHERE name='$name'";
$result = mysql_query($table) or die("Query failed : " . mysql_error());
$qry = mysql_fetch_array($result, MYSQL_ASSOC);
$html = $qry['html'];
$name = $qry['name'];
$body = stripslashes($html);

require ('templates/main.html');
}