php,如何从while循环之外的fetced数组中获取值?

时间:2015-04-15 17:09:02

标签: php while-loop fetch isset

这是我的代码。我想在while循环之外获取获取的值。 我怎么能这样做?

while($myRow = $result->fetch_array())
    {   
        echo "<tr>";
        echo "<td>"; ?><a href='./display_images2.php?showrealsize'> <img src="<?php echo $myRow["image"];  ?>" height="100" width="100"> </a> <?php echo "</td>"; 
        echo "<td style='display: block;'>"; echo $myRow["name"]; echo "</td>";
        echo "</tr>";

    }




//THIS CODE DOESNT WORK>>
    if(isset($_GET['showrealsize'])){
        echo  "<img src=" ?> <?php echo $myRow['image']; ?> <?php echo ">";
        } 

2 个答案:

答案 0 :(得分:0)

为了做到这一点,需要捕获数组中while循环中的值,并在需要时在数组外部使用它。

例如:

$capture_val = array();
while($myRow = mysql_fetch_array($mysql_query))
  $capture_val[] = ""; //store the value you want to 
                       //store in this array to use later 
                       //outside of the array
}

print_r($capture_val);

关键是使用数组来捕获while循环中的值。

答案 1 :(得分:0)

要扩展unixmiah的答案,您可以使用如下例所示的数组:

// create the array
$arr = array();

while($myRow = $result->fetch_array())
{   
    echo "<tr>";
    echo "<td>"; ?><a href='./display_images2.php?showrealsize'> <img src="<?php echo $myRow["image"];  ?>" height="100" width="100"> </a> <?php echo "</td>"; 
    echo "<td style='display: block;'>"; echo $myRow["name"]; echo "</td>";
    echo "</tr>";

    $arr[$myRow['id']] = $myRow['some_value']; // adds to $arr with the row ID as a key to the element.

}

然后循环遍历数组echo - img标记,如果这是您要在此处实现的目标。