简体   繁体   English

在php中显示表中的数据

[英]Display data from table in php

$sql=" SELECT * from `request_location` where requestid = '".$requestid."' ";
    $result = mysqli_query($con, $sql);
    if(mysqli_num_rows($result)>0)
        {
            echo "(";
            while($row = mysqli_fetch_assoc($result))
                {
                    echo  $row["location_tag"]; 
                    echo ",";
                }
            echo ")";   
        }

I have this above code that displays data in a format where the result is placed within round brackets and each term is separated by a comma 我有上面的代码,以一种格式显示数据,其中结果放在圆括号内,每个术语用逗号分隔

The result that i am getting is like this 我得到的结果是这样的

(a,b,c,)

Although it seems to be fine but i want that the comma should not appear after c as it is the last term like this 虽然看起来很好,但我希望逗号不应该出现在c之后,因为它是这样的最后一个术语

(a,b,c)

Can anyone please tell how to remove comma after the last element 任何人都可以告诉如何删除最后一个元素后的逗号

Try this use PHP str_replace :- 试试这个用PHP str_replace : -

$str='(a,b,c,)';
str_replace(",)",")",$str);

Demo 演示

or try this code :- 或尝试此代码: -

$var= "(";
while($row = mysqli_fetch_assoc($result))
{
$var.=  $row["location_tag"]; 
$var.= ",";
}
$var.= ")";  
echo str_replace(",)",")",$var);

Solution 1: 解决方案1:

You can do it using implode() function and array(). 你可以使用implode()函数和array()来完成它。

Logic behind it: 背后的逻辑:

1) Take a blank array. 1)取一个空白数组。

2) Fetch the results the same way as existing. 2)以与现有相同的方式获取结果。

3) Append results to the array. 3)将结果附加到数组。

4) After the loop ends implode() the array with a comma. 4)循环结束后用逗号结束implode()数组。

5) Add a prepending and post pending ( and ) to the result. 5)在结果中添加一个前置和后挂起()

sql=" SELECT * from `request_location` where requestid = '".$requestid."' ";
$result = mysqli_query($con, $sql);
$arr = array();
if(mysqli_num_rows($result)>0) {
  while($row = mysqli_fetch_assoc($result)) {
    $arr[] = ["location_tag"]; 
  }
}
echo ! empty($arr) ? '(' . implode(',', $arr) . ')' : '';

Solution 2: 解决方案2:

Use MYSQL's GROUP_CONCAT() (if you want to fetch only one field from database table). 使用MYSQL的GROUP_CONCAT() (如果只想从数据库表中获取一个字段)。

sql=" SELECT GROUP_CONCAT(location_tag) from `request_location` where requestid = '".$requestid."' ";
    $result = mysqli_query($con, $sql);
    $ids = '';
    if(mysqli_num_rows($result)>0) {
      while($row = mysqli_fetch_assoc($result)) {
        $ids = ["location_tag"]; 
      }
    }

echo ! empty($ids) ? '(' . $ids . ')' : '';

Use an IF condition and check the num_of_rows 使用IF条件并检查num_of_rows

$sql=" SELECT * from `request_location` where requestid = '".$requestid."' ";
$result = mysqli_query($con, $sql);
$count = mysqli_num_rows($result);
if($count>0)
    {
        echo "(";
        $i = 1;
        while($row = mysqli_fetch_assoc($result))
            {
                echo  $row["location_tag"];
                if($i<$count) {
                 echo ",";
                }
              $i++;

            }
        echo ")";   
    }

what about this 那这个呢

$sql= "SELECT * from request_location where requestid = '$requestid'";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result)>0)
    {
        $new = ''
        while($row = mysqli_fetch_assoc($result))
        {
            $new .=  $row["location_tag"].",";
        }            

        $data =  "(".$new.")"; // Output - (a,b,c,)
        $new_data = str_replace(",)", ")", $data);
        echo $new_data ; // Output - (a,b,c)

    }

For adding "," between two string you can use implode(",", array_variable) , like below: 要在两个字符串之间添加“,”,您可以使用implode(",", array_variable) ,如下所示:

$sql=" SELECT * from `request_location` where requestid = '".$requestid."' ";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result)>0)
    {
        while($row = mysqli_fetch_assoc($result))
            {
                $location_tag_arr[] = $row["location_tag"];
            }
        echo ")";   
    }

echo "(" . implode(",", $location_tag_arr) . ")";
//^output will be: (a,b,c)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM