gpt4 book ai didi

php - 如何从不同文件和数组变量中的 mysql_fetch_assoc() 访问连续行?

转载 作者:行者123 更新时间:2023-11-29 23:49:38 26 4
gpt4 key购买 nike

我正在尝试从城市的地点/地点表中实现一个简单的“列出”功能。

为此,我创建了一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com