从PHP数组中获取值并在选择href时显示它

从PHP数组中获取值并在选择href时显示它

问题描述:

I have the following php code below. I would like that when the user hits the Delete Now button - the picture value is displayed. Regardless of which any button I select the picture05 gets displayed.

I may have a problem getting the selected value from the foreach to the echo code below

<?php
$pic_names = array (
    '01' => 'picture01',
    '02' => 'picture02',
    '03' => 'picture03',
    '04' => 'picture04',
    '05' => 'picture05',
);

foreach($pic_names as $pic_key => $pic_value){
echo '<a href="?delete=';
echo $pic_value;
echo '">Delete Now!</a><br/>';
}

//Delete Images
if(isset($_GET['delete'])){
echo $pic_value;
}
?>

我在下面有以下php代码。 我希望当用户点击立即删除按钮时 - 显示图片值。 无论我选择图片05的任何按钮都显示出来。 p>

我可能无法将所选值从foreach获取到下面的echo代码 p>

 &lt;?php 
 $ pic_names = array(
'01'=&gt;'picture01',
'02'=&gt;'picture02',
'03'=&gt;'picture03'  ,
'04'=&gt;'picture04',
'05'=&gt;'picture05',
); 
 
foreach($ pic_names as $ pic_key =&gt; $ pic_value){
echo'&lt;  ; a href =“?delete ='; 
echo $ pic_value; 
echo'”&gt;立即删除!&lt; / a&gt;&lt; br /&gt;'; 
} 
 
 //删除图片
if  (isset($ _ GET ['delete'])){
echo $ pic_value; 
} 
?&gt; 
  code>  pre> 
  div>

Try this:

foreach($pic_names as $pic_key => $pic_value){
     echo '<a href="?delete='.$pic_value.'">Delete Now!</a><br/>';
}

In you code you make checking GET but echo another variable, try this:

if(isset($_GET['delete'])){
   echo $_GET['delete'];
} 

$pic_value contains the value of the last iteration of your foreach loop. use the value of $_GET['delete'].

if(isset($_GET['delete'])){
  echo $_GET['delete'];
}

You could drop your photo "keys" and use the natural keys from the array. Then show the image if $_GET['delete'] is defined. If not show your delete links.

<?php
    $pic_names = array(
        'picture01', 
        'picture02', 
        'picture03', 
        'picture04',
        'picture05',
    );

    if(isset($_GET['delete']) {
        $imgPath = "define/your/path/here/";
        echo '<img src="' . $imgPath . $pic_names[$_GET['delete']] . '">';
    } else {
        foreach($pic_names as $pic_key => $pic_value){
            echo '<a href="?delete=' . $key . '">Delete: ' . $pic_value . '</a><br />';
        }
    }
    ?>

Try some thing like this

foreach($pic_names as $pic_key => $pic_value){
$href = "";
$href = '<a href="?delete=';
$href.= $pic_value;
$href.= '">Delete Now!</a><br/>';
echo $href;
}

And then try

if(isset($_GET['delete'])){
  echo $_GET['delete'];
}

Your loop is fine, you just print the wrong variable. Try this: (I recommend you to expose keys rather than names)

$pic_names = array (
    '01' => 'picture01',
    '02' => 'picture02',
    '03' => 'picture03',
    '04' => 'picture04',
    '05' => 'picture05',
);

foreach($pic_names as $pic_key => $pic_value){
    print '<a href="?delete='.$pic_key.'">Delete Now!</a><br/>';
}

//Delete Images
if(isset($_GET['delete'])){
    print $pic_names[$_GET['delete']];
}