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条)

报告相同问题?

悬赏问题

  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊