Reputation: 1844
i have this function for check news files from MySQL database:
function check_Files($id){
$files = sqlaccess::fetch("SELECT url FROM ".FILES." WHERE news_id=".$id." AND type = 'video' ");
if (count($files) > 0) {
foreach($files as $file){
$url = $file['url'];
return TRUE;
}
} else
{
return FALSE;
}
}
HTML :
if(check_Files($id) == TRUE){
echo $url;
}
But I see this error:
Notice: Undefined variable: url in ......
How do can I back/send $url
from function to html ?!
Upvotes: 0
Views: 77
Reputation: 152294
Instead of
return TRUE;
in your check_Files
function do:
return $url;
And use it in following way:
if (($url = check_Files($id)) !== false){
echo $url;
}
Or to pass an array of urls from database:
function check_Files($id){
$files = sqlaccess::fetch("SELECT url FROM ".FILES." WHERE news_id=".$id." AND type = 'video' ");
if (count($files) > 0) {
$urls = array();
foreach($files as $file){
$urls[] = $file['url'];
}
return $urls;
} else {
return FALSE;
}
}
if (($urls = check_Files($id)) !== false){
foreach ($urls as $url) {
echo $url;
}
}
The test can be simplified to:
if ($urls = check_Files($id)){}
Upvotes: 4
Reputation: 5078
function check_Files($id){
$files = sqlaccess::fetch("SELECT url FROM ".FILES." WHERE news_id=".$id." AND type = 'video' ");
if (count($files) > 0) {
foreach($files as $file){
$url = $file['url'];
return $url;
}
} else
{
return FALSE;
}
}
and in html
if (($url = check_Files($id)) != false){
echo $url;
}
Upvotes: 0
Reputation: 356
Is the function defined in the same file that is is being called ?? Because the error seems to be that you are trying to output a variable that has not be defined. A better approach would be
function check_Files($id){
$files = sqlaccess::fetch("SELECT url FROM ".FILES." WHERE news_id=".$id." AND type = 'video' ");
if (count($files) > 0) {
foreach($files as $file){
$url = $file['url'];
return $url
}
} else
{
return FALSE;
}
}
if(check_Files($id) != FALSE){
echo check_Files($id);
}
Upvotes: 0
Reputation: 250
Make $url global inside the function and define $url above the function then only you can get the $url;
$url="";
function check_Files($id){
global $url;
$files = sqlaccess::fetch("SELECT url FROM ".FILES." WHERE news_id=".$id." AND type = 'video' ");
if (count($files) > 0) {
foreach($files as $file){
$url = $file['url'];
return TRUE;
}
} else
{
return FALSE;
}
}
if(check_Files($id) == TRUE){
echo $url;
}
Check yourself and enjoy!
Upvotes: 0
Reputation: 33542
This is about variable scope. Inside the function, $url exists. Outside the function, it isn't around anymore. Your best bet is to return the URL instead of true/false:
function check_Files($id){
$files = sqlaccess::fetch("SELECT url FROM ".FILES." WHERE news_id=".$id." AND type = 'video' ");
if (count($files) > 0) {
foreach($files as $file){
$url = $file['url'];
return $url;
}
} else
{
return FALSE;
}
}
Then outside in your code, you can call it like so:
$link=check_Files($id);
echo $link;
Or even simply
echo check_Files($id);
Upvotes: 0