简体   繁体   English

设定变量的未定义变量错误

[英]Undefined variable error on a set variable

I am a newbie in PHP. 我是PHP的新手。 This is a gallery code. 这是图库代码。 Though I made the gallery to work, there is an error that shows up as "PHP Notice: Undefined variable: caption_array in /home/th/public_html/gallery.php on line 13" . 尽管我使画廊正常工作,但还是出现了一个错误,显示为"PHP Notice: Undefined variable: caption_array in /home/th/public_html/gallery.php on line 13" I checked and saw that caption_array is set but cannot figure out why this error keeps showing up. 我检查了一下,发现设置了caption_array ,但无法弄清楚为什么此错误持续出现。 Code below is where caption_array is used... any help or direction is appreciated. 下面的代码是使用caption_array地方。

// display previous and next links if more than one photo 
else if( $pcaption ) 
{
    mysql_query("UPDATE gallery_photos SET photo_caption =    
    REPLACE(photo_caption,'\\\','') ");
    $pcaption = str_replace("-", " ",$pcaption);
    $pcaption = str_replace("%27", "'",$pcaption);
    $pcaption = str_replace("\\", "",$pcaption);
    $result = mysql_query( "SELECT photo_caption, photo_description, photo_filename,photo_keywords FROM gallery_photos WHERE photo_caption='".addslashes($pcaption)."'" ); 
    list($photo_caption, $photo_description, $photo_filename, $photo_keywords) = mysql_fetch_array( $result ); 

    $nr = mysql_num_rows( $result ); 
    mysql_free_result( $result );     

    $p_caption = $photo_caption;
    $p_description = $photo_description;
    $p_keywords = $photo_keywords;

    //fill caption_array with sorted pids in current category 

    $result = mysql_query( "SELECT photo_caption FROM gallery_photos WHERE category_name='".addslashes($cname)."' " ); 

    $ct = mysql_num_rows( $result ); 

    while ($row = mysql_fetch_array($result)) {
        $row[0]= trim($row[0]);
        $row[0] = str_replace(" ","-",$row[0]);
        $row[0] = str_replace("'","%27",$row[0]);
        $caption_array[] = trim($row[0]); 
    }

    mysql_free_result( $result );

    if( empty($nr ) ) 
    { 
        $result_final = "\t<tr><td>***No Photo found*******</td></tr>\n"; 
    } 
    else 
    { 
        $category_name = $cname; 
        $cname = str_replace(" ", "-", $cname); 
        $result_final = "
            <div class=limagePage>
                <div class=llink>
                    <a href=/gallery.php>ALBUMS</a>
                    <span class=arrow>&gt;&gt</span>
                    <a href=/gallery.php?cname=$cname>$category_name</a>
                </div>
        ";

        // display previous and next links if more than one photo 

        if ($ct > 1) 
        { 
            $pcaption = trim($pcaption);
            $pcaption = str_replace(" ","-",$pcaption);
            $pcaption = str_replace("'","%27",$pcaption);
            $key = array_search($pcaption , $caption_array); 
            $prev = $key - 1; 
            if ($prev < 0) $prev = $ct - 1; 
            $next = $key + 1; 

            if ($next == $ct) $next = 0; 
            $total_count= count($caption_array);

            $result_final .= "<div class='prevnext'>"; 
            $result_final .= "<span class='prev'><a href=/gallery.php?cname=$cname&pcaption=".$caption_array[$next]."><img src=/photos/assets/left.png  border=0 ></a></span>"; 
            $result_final .= "<span class='next'><a href=/gallery.php?cname=$cname&pcaption=".$caption_array[$prev]."><img src=/photos/assets/right.png  border=0 ></a></span>"; 
            $result_final .= "</div>"; 
        }            
    }

   $cname = str_replace(" ", "-", $cname);

   $images_dir =str_replace(".","",$images_dir);

   $result_final .= "<div class=limage><table><tr><td><table class=image><tr>\n\t<td><a href=/gallery.php?cname=$cname&pcaption=".$caption_array[$next]."><img src='".$images_dir."/".$photo_filename."' border='0' alt='".$photo_keywords."' /></a>
        <div class=caption>".$photo_caption."</div> 
        <div class='excerpt'>".$photo_description."</div> 
        </td>                    
        </tr></table></td></tr></table><div class=underline></div></div>
        <!-- .limagePage --></div>  ";
}

Define it before use to avoid this warning 在使用前定义它以避免此警告

$caption_array = array();

Define above while ($row = mysql_fetch_array($result)) { ... 在上面定义while($ row = mysql_fetch_array($ result)){...

Add

$caption_array = array();

Before while loop 在while循环之前

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

There could be several reasons for your error. 您的错误可能有多种原因。 Let me walk you through. 让我来引导您。

  1. Check both variable names so they match, perhaps you made a typo 检查两个变量名是否匹配,也许您输入错了
  2. Check your include 's, you might miss the one where you define that variable 检查您的include ,您可能会错过定义该变量的那个
  3. Make sure you don't unset it at some point 确保您没有在某个时候取消设置
  4. Are you defining the variable in a function, or trying to access it from a function? 您是在函数中定义变量,还是试图从函数中访问变量? Make sure you use the global keyword. 确保使用global关键字。 See code below. 请参见下面的代码。

// Set variable
$var = 'test';

function func() {
    var_dump($var); // NULL, perhaps errors may occur

    global $var; // The magic trick
    var_dump($var); // "test"
}

func();

This goes both ways 这是双向的

function func() {
    global $var; // The magic trick
    $var = 'test'; // Set variable
}

var_dump($var); // NULL, perhaps errors may occur
func();
var_dump($var); // "test";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM