yathrakaaran
yathrakaaran

Reputation: 189

Undefined variable error on a set variable

I am a newbie in 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". I checked and saw that caption_array is set but cannot figure out why this error keeps showing up. Code below is where caption_array is used... any help or direction is appreciated.

// 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>  ";
}

Upvotes: 0

Views: 307

Answers (3)

Tim S.
Tim S.

Reputation: 13843

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
  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. 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";

Upvotes: 1

MD SHAHIDUL ISLAM
MD SHAHIDUL ISLAM

Reputation: 14523

Add

$caption_array = array();

Before while loop

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

Upvotes: 1

Farid Movsumov
Farid Movsumov

Reputation: 12725

Define it before use to avoid this warning

$caption_array = array();

Define above while ($row = mysql_fetch_array($result)) { ...

Upvotes: 1

Related Questions