所以我正在使用这个示例代码output6.php
我想做的是,如果 empire_name 返回 null,则跳过相关行
任何想法或建议将不胜感激
谢谢!
<!DOCTYPE html>
<html>
<body>
<?php
echo "<table style='border: solid 1px black;'>";
echo "<tr>
<th>Empire Name</th>
<th>Win?</th>
<th>Building 1</th>
<th>Building 2</th>
<th>Building 3</th>
<th>Building 4</th>
<th>Building 5</th>
<th>Building 6</th>
<th>Building 7</th>
<th>Building 8</th>
<th>Building 9</th>
</tr>";
class TableRows extends RecursiveIteratorIterator {
function __construct($it) {
parent::__construct($it, self::LEAVES_ONLY);
}
function current() {
return "<td style='width: 150px; border: 1px solid black;'>" . parent::current(). "</td>";
}
function beginChildren() {
echo "<tr>";
}
function endChildren() {
echo "</tr>" . "\n";
}
}
$servername = "xxx";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT empire_name, win, building_1, building_2, building_3, building_4, building_5, building_6, building_7, building_8, building_9 FROM ft_form_2 ");
$stmt->execute();
// set the resulting array to associative
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";
?>
</body>
</html>
我试着查找这个,但我不知道要查找什么
谢谢!
我目前的成绩 null = blank rows, image link
我认为这很接近... PDO and IS NOT NULL Function 但是我不确定如何在当前代码中实现(我尝试了一些变体)
不同的变体 - 相同的结果 (output3.php) 更接近所需的最终结果
<?php
define("DB_HOST", "xxx"); // Using Constants
define("DB_USER", "xxx");
define("DB_PASS", "xxx");
define("DB_NAME", "xxx");
try { // << using Try/Catch() to catch errors!
$dbc = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset-utf8",DB_USER,DB_PASS);
}catch(PDOException $e){ echo $e->getMessage();}
if($dbc <> true){
die("<p>There was an error</p>");
}
$print = ""; // assign an empty string
$stmt = $dbc->query("SELECT * FROM ft_form_2"); // fetch data
$stmt->setFetchMode(PDO::FETCH_OBJ);
if($stmt->execute() <> 0)
{
$print .= '<table border="1px">';
$print .= '<tr><th>Empire_Name</th>';
$print .= '<th>Win</th>';
$print .= '<th>Building_1</th>';
$print .= '<th>Building_2</th>';
$print .= '<th>Building_3</th>';
$print .= '<th>Building_4</th>';
$print .= '<th>Building_5</th>';
$print .= '<th>Building_6</th>';
$print .= '<th>Building_7</th>';
$print .= '<th>Building_8</th>';
$print .= '<th>Building_9</th> </tr>';
while($ft_form_2 = $stmt->fetch()) // loop and display data
{
$print .= '<tr>';
$print .= "<td>{$ft_form_2->empire_name}</td>";
$print .= "<td>{$ft_form_2->win}</td>";
$print .= "<td>{$ft_form_2->building_1} <br> {$ft_form_2->building_1_notes} </td>";
$print .= "<td>{$ft_form_2->building_2} <br> {$ft_form_2->building_2_notes} </td>";
$print .= "<td>{$ft_form_2->building_3} <br> {$ft_form_2->building_3_notes} </td>";
$print .= "<td>{$ft_form_2->building_4} <br> {$ft_form_2->building_4_notes} </td>";
$print .= "<td>{$ft_form_2->building_5} <br> {$ft_form_2->building_5_notes} </td>";
$print .= "<td>{$ft_form_2->building_6} <br> {$ft_form_2->building_6_notes} </td>";
$print .= "<td>{$ft_form_2->building_7} <br> {$ft_form_2->building_7_notes} </td>";
$print .= "<td>{$ft_form_2->building_8} <br> {$ft_form_2->building_8_notes} </td>";
$print .= "<td>{$ft_form_2->building_9} <br> {$ft_form_2->building_9_notes} </td>";
$print .= '</tr>';
}
$print .= "</table>";
echo $print;
}
?>
最佳答案
按照您在问题中提到的使用 IS NOT NULL 的逻辑。你走在正确的道路上。仅从数据库中返回您要查找的内容比获取所有内容并在 PHP 中过滤更有效。
你的声明在这里
$stmt = $conn->prepare("SELECT empire_name, win, building_1, building_2, building_3, building_4, building_5, building_6, building_7, building_8, building_9 FROM ft_form_2 ");
你可以在末尾添加一个where条件
$stmt = $conn->prepare("SELECT empire_name, win, building_1, building_2, building_3, building_4, building_5, building_6, building_7, building_8, building_9 FROM ft_form_2 WHERE empire_name IS NOT NULL ");
然后您的数据库将进行过滤。这同样适用于您的第二个示例。
$stmt = $dbc->query("SELECT * FROM ft_form_2 WHERE empire_name IS NOT NULL");
关于PHP PDO,排除具有空字段值的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42450973/
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr
我几天前在我的rubyonrails2.3.2上安装了Sphinx和Thinking-Sphinx,基本搜索效果很好。这意味着,没有任何条件。现在,我想用一些条件过滤搜索。我有公告模型,索引如下所示:define_indexdoindexestitle,:as=>:title,:sortable=>trueindexesdescription,:as=>:description,:sortable=>trueend也许我错了,但我注意到只有当我将:sortable=>true语法添加到这些属性时,我才能将它们用作搜索条件。否则它找不到任何东西。现在,我还在使用acts_as_tag
这是一个例子:s="abcd+subtext@example.com"s.match(/+[^@]*/)Result=>"+subtext"问题是,我不想在其中包含“+”。我希望结果是“潜台词”,没有+ 最佳答案 您可以在正则表达式中使用括号来创建匹配组:s="abcd+subtext@example.com"s=~/\+([^@]*)/&&$1=>"subtext" 关于ruby-正则表达式-排除一个字符,我们在StackOverflow上找到一个类似的问题:
假设您编写了一个类Sup,我决定将其扩展为SubSup。我不仅需要了解你发布的接口(interface),还需要了解你的私有(private)字段。见证这次失败:classSupdefinitialize@privateField="fromsup"enddefgetXreturn@privateFieldendendclassSub问题是,解决这个问题的正确方法是什么?看起来子类应该能够使用它想要的任何字段而不会弄乱父类(superclass)。编辑:equivalentexampleinJava返回"fromSup",这也是它应该产生的答案。 最佳答案
我使用rails3.1+rspec和factorygirl。我对必填字段(validates_presence_of)的验证工作正常。我如何让测试将该事实用作“成功”而不是“失败”规范是:describe"Addanindustrywithnoname"docontext"Unabletocreatearecordwhenthenameisblank"dosubjectdoind=Factory.create(:industry_name_blank)endit{shouldbe_invalid}endend但是我失败了:Failures:1)Addanindustrywithnona
我正在尝试按Rails相关模型中的字段进行排序。我研究的所有解决方案都没有解决如果相关模型被另一个参数过滤?元素模型classItem相关模型:classPriority我正在使用where子句检索项目:@items=Item.where('company_id=?andapproved=?',@company.id,true).all我需要按相关表格中的“位置”列进行排序。问题在于,在优先级模型中,一个项目可能会被多家公司列出。因此,这些职位取决于他们拥有的company_id。当我显示项目时,它是针对一个公司的,按公司内的职位排序。完成此任务的正确方法是什么?感谢您的帮助。PS-我
我想知道我应该引用什么异常名称。我的日期无效。我检查了文档,但找不到。BeginDate.new(day,month,year)Rescueexceptionnamestatements 最佳答案 我认为您正在寻找ArgumentError.使用irb:>Date.new(2,-200,3)ArgumentError:invaliddatefrom(irb):11:in`new'from(irb):11所以beginDate.new(2,-200,3)rescueArgumentError#yourlogicend
我想用sunspot重现以下原始solr查询q=exact_term_text:fooORterm_textv:foo*ORalternate_text:bar*但我无法通过标准的太阳黑子界面理解这是否可能以及如何实现,因为看起来:fulltext方法似乎不接受多个文本/搜索字段参数我不知道将什么参数作为第一个参数传递给fulltext,就好像我通过了"foo"或"bar"结果不匹配如果我传递一个空参数,我得到一个q=*:*范围过滤器(例如with(:term).starting_with('foo*')(顾名思义)作为过滤器查询应用,因此不参与评分。似乎可以手动编写字符串(或者可能使