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

时间:2013-09-12 08:16:17

标签: php

我是PHP的新手。这是一个图库代码。虽然我让图库工作,但有一个错误显示为"PHP Notice: Undefined variable: caption_array in /home/th/public_html/gallery.php on line 13"。我查看并看到caption_array已设置但无法弄清楚为什么此错误不断出现。下面的代码是使用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>  ";
}

3 个答案:

答案 0 :(得分:1)

在使用前定义它以避免此警告

$caption_array = array();

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

答案 1 :(得分:1)

添加

$caption_array = array();

while while循环

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

答案 2 :(得分:1)

您的错误可能有多种原因。让我带你走过。

  1. 检查两个变量名称,使它们匹配,也许你输错了
  2. 检查您的include,您可能会错过定义该变量的那个
  3. 确保您不会在某个时候取消设置
  4. 您是在函数中定义变量,还是尝试从函数中访问它?请务必使用global关键字。请参阅以下代码。

  5. // Set variable
    $var = 'test';
    
    function func() {
        var_dump($var); // NULL, perhaps errors may occur
    
        global $var; // The magic trick
        var_dump($var); // "test"
    }
    
    func();
    

    这是双向的

    function func() {
        global $var; // The magic trick
        $var = 'test'; // Set variable
    }
    
    var_dump($var); // NULL, perhaps errors may occur
    func();
    var_dump($var); // "test";