duanran8648 2012-07-06 23:37
浏览 65
已采纳

如何使用while()和array()从$ _GET变量打印多个ID

I'm here today because i'm under a minor issue that i can't seem to solve and it's why it brings be here. The issue is that i'm trying to get multiple ID's from a $_GET variable to print out. I tried to use array and great, it works but the thing is it makes separate arrays with [0] and i need it to be as one in the same array not array() array() which is what its doing.

So i'm using while() with mysql_fetch_array to get the results off the database and it depends on the $_GET from the url in-order to grab the correct id's. So in the URL i pass in api.php?do=remove&id=1,4,7 it will only print out the first id and not the others as a name. As i said i tried with array() and this is what came out :

Array
(
    [0] => BzlXO.jpg
)
Array
(
    [0] => cHZTk.jpg
)
Array
(
    [0] => 9yset.jpg
)
Array
(
    [0] => Ss04V.jpg
)

i don't understand why its doing that as i need it to be in one array as in

Array (

   [0] => BzlXO.jpg,
   [1] => cHZTk.jpg,
   [2] => 9yset.jpg,
   [3] => Ss04V.jpg
)

So that way when i implode them it will show as text as in: "You removed image(s) "BzlXO.jpg,cHZTk.jpg,9yset.jpg,Ss04V.jpg" with something like

        $iName[imagename] = array($iName[imagename]);
        $name = implode(",", $iName[imagename]);

Here is my code:

This is the URL "api.php?do=remove&id=1,4,7"

      $ID = $_GET['id'];
    $query = "SELECT ID,imagename FROM uploads WHERE ID IN ({$ID}) AND username = '{$uploader}'";
    $result = mysql_query($query);

      while( $iName = mysql_fetch_array($result)){
          $querys = "DELETE FROM uploads WHERE ID IN ({$ID}) AND username = '{$uploader}'";
      $results = mysql_query($querys);

          if(!$results) {
       $api_message = 'Failed to get a Removal result';
       } else {

            $iName[imagename] = array($iName[imagename]);
            $name = implode(",", $iName[imagename]);

           $api_message = "You removed image(s) $name"; 

          }
}

The OUTPUT :

You removed image(s) BzlXO.jpg

but i need it to be:

The OUTPUT : You removed image(s) "BzlXO.jpg,cHZTk.jpg,9yset.jpg,Ss04V.jpg"

Any help with this will be much appreciated, if any more information is needed please let me know and i'll include Thanks

  • 写回答

3条回答 默认 最新

  • duanhao4156 2012-07-07 00:00
    关注

    In addition to the solution posted by raina77ow, there is also a problem with the control flow in that you are executing the DELETE FROM uploads WHERE ID IN (...) statement for each iteration of the while loop. This will delete all the records on the first iteration. You need to change it to something like:

    $ID = $_GET['id'];
    $names = array();
    $query = "SELECT ID,imagename FROM uploads WHERE ID IN ({$ID}) AND username = '{$uploader}'";
    $result = mysql_query($query);
    
      while( $iName = mysql_fetch_array($result)){
          $querys = "DELETE FROM uploads WHERE ID = {$iName['ID']} AND username = '{$uploader}'";
          $results = mysql_query($querys);
          if(!$results) {
           $api_message = 'Failed to get a Removal result';
          } else {
            $names[] = $iName['imagename']);
          }
      }
      $name = implode(",", $names);
      $api_message = "You removed image(s) $name"; 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号