- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从城市的地点/地点表中实现一个简单的“列出”功能。
为此,我创建了一个 Place 类并从“get”函数返回一个 mysql_fetch_assoc()
数组。该代码位于名为“placeops.php”的 PHP 文件中,仅包含 OO 逻辑。
现在转到另一个名为“displayplaces.php”的文件,我在这里调用相同的“get”函数将 mysql_fetch_assoc()
数组获取到另一个变量中。然后我使用 next()
和 current()
函数来检索记录。
这行不通。如果我使用 current() 获取特定的列/属性值,它会显示“current() 需要数组,字符串已传递”错误。通过不使用 current()
,我只能访问第一个元素,如预期的那样。我明白为什么会出现这个错误。但有什么解决办法吗?我必须使用面向对象的方法。代码如下:
//This is the class and the function I use to return the mysql array. The file name is
// placeops.php
<?php
//include '../../../myclass.php';
@mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("wom") or die(mysql_error());
global $plcname, $plcaddr, $plccity, $plcstate, $plcpic, $plcrating, $plclat, $plclong, $plccat, $plcid;
class Place
{
private $pname;
private $paddr;
private $pcity;
private $pstate;
private $plat;
private $plong;
private $prating;
function getAllPlaces()
{
$plcqry = "SELECT * FROM place,state,city where place.st_id = state.st_id AND place.ct_id = city.ct_id";
$rows = mysql_query($plcqry);
return mysql_fetch_assoc($rows);
/*
while($res = mysql_fetch_assoc($rows))
{
$GLOBALS["plcname"] = $res["plc_nm"];
$GLOBALS["plcstate"] = $res["st_nm"];
$GLOBALS["plccity"] = $res["ct_nm"];
echo $GLOBALS["plcname"]." -- ".$GLOBALS["plccity"]." -- ".$GLOBALS["plcstate"]."<br><br>";
}
* */
}
function getPlace($name, $city, $state)
{
$this->pname = $name;
$this->pcity = $city;
$this->pstate = $state;
$row = mysql_query($plcqry);
$plcqry = "SELECT place.plc_nm, place.plc_photo, place.plc_addr, place.ct_id, place.plc_lat, place.plc_long, city.ct_nm, state.st_nm FROM place,state,city WHERE place.plc_nm = '".$this->pname."' AND place.st_id = state.st_id AND state.st_nm = '".$this->pstate."' AND place.ct_id = city.ct_id AND city.ct_nm = '".$this->pcity."'";
@$row = mysql_query($plcqry);
$res = mysql_fetch_assoc($row);
$GLOBALS["plcname"] = $res["plc_nm"];
$GLOBALS["plcaddr"] = $res["plc_addr"];
$GLOBALS["plccity"] = $res["ct_nm"];
$GLOBALS["plcstate"] = $res["st_nm"];
$GLOBALS["plcpic"] = $res["plc_photo"];
$GLOBALS["plclat"] = $res["plc_lat"];
$GLOBALS["plclong"] = $res["plc_long"];
//echo "".$GLOBALS["plcname"]."<br>";// <img src=\"".$GLOBALS["plcpic"]."\"/>";
}
function ratePlace($name,$city,$state,$rating)
{
$this->pname = $name;
$this->pcity = $city;
$this->pstate = $state;
$this->prating = $rating;
$plcqry = "SELECT place.plc_id, place.plc_rating FROM place,state,city WHERE place.plc_nm = '".$this->pname."' AND place.st_id = state.st_id AND state.st_nm = '".$this->pstate."' AND place.ct_id = city.ct_id AND city.ct_nm = '".$this->pcity."'";
$row = mysql_query($plcqry);
$res = mysql_fetch_assoc($row);
$GLOBALS["plcrating"] = $res["plc_rating"];
$GLOBALS["plcid"] = $res["plc_id"];
$rating += $GLOBALS["plcrating"];
$ratingqry = "UPDATE place SET plc_rating = ".$this->prating." WHERE plc_id = ".$GLOBALS["plcid"]."";
mysql_query($ratingqry);
$plcqry = "SELECT plc_rating FROM place WHERE plc_id = ".$GLOBALS["plcid"]."";
$row2 = mysql_query($plcqry);
$res2 = mysql_fetch_assoc($row2);
$GLOBALS["plcrating"] = $res2["plc_rating"];
}
function getPlaceByRegion($city, $state)
{
$this->pcity = $city;
$this->pstate = $state;
$plcqry = "SELECT place.plc_id, place.plc_nm, state.st_nm, city.ct_nm FROM place,state,city WHERE place.st_id = state.st_id AND state.st_nm = '".$this->pstate."' AND place.ct_id = city.ct_id AND city.ct_nm = '".$this->pcity."'";
$rows = mysql_query($plcqry);
while($res = mysql_fetch_assoc($rows))
{
$GLOBALS["plcname"] = $res["plc_nm"];
$GLOBALS["plcstate"] = $res["st_nm"];
$GLOBALS["plccity"] = $res["ct_nm"];
//echo $GLOBALS["plcname"]." -- ".$GLOBALS["plccity"]." -- ".$GLOBALS["plcstate"]."<br><br>";
}
}
}
?>
//And this is the other part of the code, which I use in another PHP file that
//contains the HTML code ("view layer"). File name is "displayplaces.php"
<?php
include './placeops.php';
$obj = new Place;
$res = $obj->getAllPlaces();
while (next($res))
{
//expected string parameter errors for all these assignments
$GLOBALS["plcname"] = current($res["plc_nm"]);
$GLOBALS["plcstate"] = current($res["st_nm"]);
$GLOBALS["plccity"] = current($res["ct_nm"]);
$GLOBALS["plcrating"] = current($res["plc_rating"]);
$GLOBALS["plclat"] = current($res["plc_lat"]);
echo "<tr>
<td>" . $GLOBALS["plcname"] . "</td>
<td>" . $GLOBALS["plcrating"] . "</td>
<td>" . $GLOBALS["plccity"] . "</td>
<td>" . $GLOBALS["plcstate"] . "</td>
<td>" . $GLOBALS["plclat"] . "</td>
</tr>";
}
?>
目前表中有14条记录。我知道我可以通过在“displayplaces.php”文件本身中编写 sql 处理代码来解决这个问题。但功能和显示逻辑是否可以保持不同呢?可以在 $res 数组中使用额外的索引吗?或者我是否必须在显示内容的同一个 PHP 页面中编写获取代码?
最佳答案
这里使用对象存在一些问题。首先,这实际上并不是面向对象的。您可以在不使用全局变量的情况下完成此操作 - 对象应在内部包含其数据。
我希望名为 Place
的类仅包含一个地点,但您提供了一种名为 getPlaces
的方法,该方法似乎不符合该目的。我建议您创建另一个名为 PlaceSet
的类(或类似的复数形式),其中包含数组中的多个 Place
对象。
类似这样的事情:
class Place {
// I've stored some example properties here, you can
// change these if you wish
protected $id;
protected $name;
protected $state;
protected $city;
// I've used a constructor here, but you could use a setter function
// instead, so that a Place can have its properties changed during its
// lifetime
public function __construct($id, $name, $state, $city) {
$this->id = $id;
$this->name = $name;
$this->state = $state;
$this->city = $city;
}
public function ratePlace($rating) {
// You can access $this->id to set a rating
}
// Offer getters in this format
public function getName() {
return $this->name;
}
}
class PlaceSet {
// This function just returns an array of Places, but you could
// also store that result in this class if you wish, so as to
// reuse it without having to rerun the query every time
public function getAllPlaces() {
// Word-wrap your long lines!
$plcqry = "
SELECT *
FROM place,state,city
WHERE
place.st_id = state.st_id
AND place.ct_id = city.ct_id
";
// @todo This legacy library needs to be swapped out
$rows = mysql_query($plcqry);
$places = array();
while($res = mysql_fetch_assoc($rows))
{
$places[] = new $Place(
$res["plc_id"]
$res["plc_nm"],
$res["st_nm"],
$res["ct_nm"]
);
}
return $places;
}
// Maybe this should be getPlacesByRegion?
//
// Always declare your access level
public function getPlaceByRegion($city, $state)
{
// Add code here
}
}
您可以通过以下方式访问完整的地点列表:
<?php
// Do your connection here
include './placeops.php';
$placeSet = new PlaceSet();
$places = $placeSet->getAllPlaces();
foreach ($places as $place) {
// Add these getters into the Place class
echo "<tr>
<td>" . $place->getName() . "</td>
<td>" . $place->getRating() . "</td>
<td>" . $place->getCity() . "</td>
<td>" . $place->getState() . "</td>
<td>" . $place->getLat() . "</td>
</tr>";
}
?>
关于php - 如何从不同文件和数组变量中的 mysql_fetch_assoc() 访问连续行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25709701/
我有以下代码: $sortorder = $_GET['sort']; switch($sortorder) { case "modulea": $result2 = db_query
这是我在 php 中的查询代码: $query3 = mysql_query("SELECT * FROM area_of_work") or die('Invalid query:'. mysql_
我的 fetch_assoc 返回重复的行。似乎它自己倍增。我的表中有 4 个输入,它返回 16 个。 这是我的代码....请帮助我。我想我循环错了。 Customer D
我正在尝试使用 mysql_fetch_assoc() 从我的数据库中检索我的结果,但是当我回显以下内容(纯白屏幕)时,我似乎没有得到任何结果: $email_address = $_POST['em
对不起,我是 MeekroDB 库的新手,我想知道如何在登录过程中检查行; $query = DB::query("SELECT * FROM users WHERE username=%s",$us
我不明白为什么 echo 不显示值。在phpmyadmin sow查询中查询返回值就可以了。请帮忙 $query="SELECT `doctor_name` , `doctor_secondname`
这次我遇到了 mysql_fetch_assoc() 的问题,使用它后我在屏幕上看不到任何内容。它应该在屏幕上打印一堆记录,但我只有一个屏幕。 所以,这是我的代码: $selected="SELECT
我正在尝试显示在某个发布页面中发表的所有评论。所有变量调用都可以正常工作(包括函数 timeago),但它只显示第一个注释。 $com_sql = "SELECT comments.com_id,
我有这个代码: function get_team_a($id_team){ $result = mysql_query("SELECT * FROM `table1` WHERE `id`
我正在尝试运行 SQL 查询,然后在表中显示结果。这是我正在使用的代码 $sql = "SELECT * FROM mt.o_details WHERE cusid = '$cusid[$j]'
在我的 php 代码中,我有 2 个循环,它们运行正常,除了 1 个 if 语句,我无法替换以下函数。 (1)虽然这段代码很好并且在第一个循环中运行 if(isset($_GET['Filter'])
我目前正在尝试传递电影的类型,但出现以下错误: Notice: Undefined index: id_genre in --- on line 34 Notice: Undefined index:
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我的查询中有多个表 $query = mysql_query("SELECT * FROM reservation r, users u ...") while( $fetch = mysql_fet
我正在开发一个项目,其中使用 mysql_fetch_assoc 返回关联数组。 但是,我很困惑为什么它会返回TRUE 我查看了 PHP 手册页,它应该返回的唯一 bool 值是 FALSE,并且只有
我有一个关于在 while 循环中将自定义函数应用于 mysql_fetch_assoc 的问题。请参阅下面的代码。 //current php page $query = mysql_query("
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: Warning: mysql_fetch_array(): supplied argument is not a v
我正在尝试使用表单发布将行添加到 mySQL 表中。每行都有一个主值,我称之为 quoteID。当提交一个新表单时,它应该将自己添加为表中的一行,并且 quoteID 比之前的 quoteID 大 1
我在使用 mysql_fetch_assoc 脚本时遇到了一些问题,希望您能提供帮助。 背景:我正在使用典型的 MySQL 查询检索我网站的一部分的数据,并从我的主表中回显来自各个字段等的结果,其结构
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: php warning mysql_fetch_assoc 我只是在我的网站上实现一个简单的部分,它只是从
我是一名优秀的程序员,十分优秀!