我有一个 mysql 查询,它作为使用 PHP 输出 CSV 文件的基础。我有一个 foreach 语句,使表中的每一列也是 CSV 输出中的一列。
foreach($rowr as $name => $value)
我的问题是我需要在作为列输出时省略查询中的一列(Img AS source
)。
我目前正在使用 MLSNumber 值执行此操作
foreach($rowr as $name => $value)
{
if($name !='MLSNumber'){
$csv_output .= $name . "\t";
}
我尝试像这样更新这一行:$name !='MLSNumber' || $name !='source'
但当我这样做时它会破坏代码并最终将两者都显示为列。如何省略这两列?
$values = @mysqli_query($connect,"SELECT MLSNumber AS MLSNumber, ListAgentMLSID AS agent_ids, Matrix_Unique_ID AS mls, City AS city, PostalCode AS zip, REPLACE(PropertyInformation, '\r\n', '') AS description, SubCondoName AS neighborhood, ListPrice AS price, VirtualTourURL AS virtual_tour_1, Bedrooms AS bedrooms, BathsTotal AS bathrooms_full, ApproxLivingArea AS living_area, Img AS source FROM table-name WHERE ListOfficeName LIKE 'Company%' AND ListPrice >= 500000 AND PhotoCount >=4 ORDER BY CreatedDate DESC");
$row = 0;
while ($rowr = mysqli_fetch_assoc($values))
{
if ($row == 0)
{
$csv_output .= "state\t";
$csv_output .= "country_code\t";
$csv_output .= "email\t";
foreach($rowr as $name => $value)
{
if($name !='MLSNumber'){
$csv_output .= $name . "\t";
}
}
$csv_output .= "image_filename_1\timage_filename_2\timage_filename_3\timage_filename_4\timage_filename_5\tlink_url_1\tlink_text_1";
$csv_output .= "\n";
}
$row++;
$csv_output .= "FL\t";
$csv_output .= "US\t";
$csv_output .= "lreInquiry@my-url-link\t";
foreach($rowr as $name => $value)
{
if($name !='MLSNumber'){
if($name=='mls') {$jpg_name=$value;}
if($name=='source') {$source_link=$value;}
$csv_output .= $value."\t";
}
}
{
$csv_output .= "http://www.my-url-link/feeds/".$source_link."/rets_images/".$jpg_name."_1.jpg\t http://www.my-url-link/feeds/".$source_link."/rets_images/".$jpg_name."_5.jpg\t http://www.my-url-link/feeds/".$source_link."/rets_images/".$jpg_name."_2.jpg\t http://www.my-url-link/feeds/".$source_link."/rets_images/".$jpg_name."_3.jpg\t http://www.my-url-link/feeds/".$source_link."/rets_images/".$jpg_name."_4.jpg";
}
foreach($rowr as $name => $value)
{
if($name=='MLSNumber') {$home_name=$value;}
}
{
$csv_output .= "\thttp://www.my-url-link/search-results/?fullinfo=".$home_name."\tMore Details ";
}
$csv_output .= "\n";
}
最佳答案
问:有没有办法调整 foreach
语句以排除 source
列?
答:不,没有对 foreach
进行任何调整来排除元素。
不清楚为什么要进行该特定调整,因为 foreach 循环的主体包含对 source
列(结果集中的列的名称,其中包含Img
列。)
if($name=='source') {$source_link=$value;}
如果我们想从 CSV 中的行中省略该列,为什么我们不直接跳过这一行的执行:
$csv_output .= $value."\t";
我们要排除的列名称。
例如,我们可以将这一行替换为如下内容:
if( $name!='source' ) {
$csv_output .= $value."\t";
}
<小时/>
(再次)回答提出的问题:
不,没有对 foreach
语句进行任何调整来省略或跳过某些元素。
关于php - 在 PHP 变量中使用 MySQL 查询,但在 CSV 输出中省略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49657847/