Forum Moderators: coopster

Message Too Old, No Replies

How to make a variable made in a function usable in another function

         

Mokdeabar

7:35 pm on Mar 30, 2009 (gmt 0)

10+ Year Member



Here are the functions:

<?php

$link = mysql_connect("mysql15.example.com","xx","xx");
$db = mysql_selectdb("mdmfitness1", $link);
$path = "images/cap/";
$query = "SELECT link FROM Image ORDER BY RAND() LIMIT 1";
$result = mysql_query ($query);
$row = mysql_fetch_assoc($result);
$filename = $row['link'];
$myname = $_SESSION['myusername'];

function displaypic()
{
global $db;
global $link;
global $path;
global $filename;
if ($link)
{
mysql_selectdb("mdmfitness1",$link);
echo '<img src="'.$path.$filename.'" width="450">';
}
else
{
echo "Can't connect to the database!";
}
}

function addCaption()
{
global $db;
global $link;
global $filename;
global $myname;
$caption = ($_POST['caption']);
$query = "INSERT INTO Captions VALUES('$id','$filename','$myname','$caption')";
mysql_query($query);
echo "<meta http-equiv='refresh' content='0;URL=index.php'>";
}

function displayCaptions()
{
global $db;
global $link;
global $filename;
$query = "SELECT caption FROM Captions WHERE img_id=$filename";
$result = mysql_query ($query);

if ($link)
{
mysql_selectdb("mdmfitness1",$link);
echo "<div width='470'>$result</div>";
}
else
{
echo "Can't connect to the database!";
}
}

?>

I need the $filename to be the same in each function... and also, for some reason, the $myname isn't being posted in the MySQL table when required to. As it is right now, the comments are assigned to a random picture, not to the one being displayed.

The 3 functions are being called up in 3 separate places on a page...

also, I keep getting an error when the displayCaptions function is called up, as follows:

Fatal error: Cannot redeclare displaypic() (previously declared in /home/fhlinux144/m/example.com/user/htdocs/capsmak/capsmak.php:14) in /home/fhlinux144/m/example.com/user/htdocs/capsmak/capsmak.php on line 27

I don't understand why it's bringing this error, as I can't see how it is being redefined...

There's probably just something really simple wrong in the coding, but I can't see it for some reason... I'm learning as I'm making so I was hoping some of you guys would have an idea about what's wrong

[edited by: eelixduppy at 8:20 pm (utc) on Mar. 30, 2009]
[edit reason] removed specifics [/edit]

darrenG

8:34 am on Mar 31, 2009 (gmt 0)

10+ Year Member



I would start by declaring the (global) variables outside of functions, then go from there

[edited by: darrenG at 8:35 am (utc) on Mar. 31, 2009]

Mokdeabar

11:29 am on Mar 31, 2009 (gmt 0)

10+ Year Member



ok... that makes sense, but I'm unsure of how to declare a variable that's inside a function as global unless that's what I've done already...

see, I made the $filename outside of the functions... but it changes its value each time any of the functions recall it... I need it to bring a new value each the page loads, but to have the same value for each of the functions

[edited by: Mokdeabar at 12:00 pm (utc) on Mar. 31, 2009]