好的我想在这里学习我是菜鸟,基本上,这是我当前的( server.php )代码,
<?php
/**
* Sample php server script for a wookmark integration
*
* @author Sebastian Helzle <sebastian@helzle.net>
*/
/**
* Basic class which provides all functions to retrieve and paginate pictures
*/
class PictureDatabase {
/**
* @var array $data
*/
protected $data;
/**
* @var int $itemsPerPage
*/
protected $itemsPerPage;
function __construct($data, $itemsPerPage) {
$this->data = $data;
$this->itemsPerPage = $itemsPerPage;
}
/**
* Returns the pictures of the given page or an empty array if page doesn't exist
* @param int $page
* @return array
*/
public function getPage($page=1) {
if ($page > 0 && $page <= $this->getNumberOfPages()) {
$startOffset = ($page - 1) * $this->itemsPerPage;
return array_slice($this->data, $startOffset, $this->itemsPerPage);
}
return array();
}
/**
* Returns the maximum number of pages
* @return int
*/
public function getNumberOfPages() {
return ceil(count($this->data) / $this->itemsPerPage);
}
}
// Our data source
$data = include ('xyz.php');
// Make data array a bit bigger to have more pages
for ($i=0; $i<3; $i++) {
$data = array_merge($data, $data);
}
// Create instance of picture database with 10 items per page and our data as source
$pictureDatabase = new PictureDatabase($data, 10);
$result = array(
'success' => TRUE,
'message' => 'Retrieved pictures',
'data' => array()
);
$callback = isset($_REQUEST['callback']) ? $_REQUEST['callback'] : false;
// Get requested page number from request and return error message if parameter is not a number
$page = 1;
try {
$page = intval($_REQUEST['page']);
} catch (Exception $e) {
$result['success'] = FALSE;
$result['message'] = 'Parameter page is not a number';
}
// Get data from database
$result['data'] = $pictureDatabase->getPage($page);
if (count($result['data']) == 0 || $page >= $pictureDatabase->getNumberOfPages()) {
$result['success'] = TRUE;
$result['message'] = 'No more pictures';
}
// Encode data as json or jsonp and return it
if ($callback) {
header('Content-Type: application/javascript');
echo $callback.'('.json_encode($result).')';
} else {
header('Content-Type: application/json');
echo json_encode($result);
}
好吧现在在// datasource下,原来是
$data = array (
array (
'id' => "1",
'title' => "First image",
'url' => "http://www.example.org/1",
'width' => "200",
'height' => "283",
'image' => "/images/image_1_big.jpg",
'preview' => "/images/image_1.jpg"
),
array (
'id' => "2",
'title' => "Second image",
'url' => "http://www.example.org/2",
'width' => "200",
'height' => "300",
'image' => "/images/image_2_big.jpg",
'preview' => "/images/image_2.jpg"
)
);
现在我在这里很少有人的帮助下得到另一个( xyz.php )文件从sql打印出来,
<?php
// Our data source
$conn = mysql_connect("", "", "");
if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}
if (!mysql_select_db("")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}
//= Query ========================//
$sql = mysql_query("select id, title, url, width, height, image, preview from mmf_content");
//= Closed while ====================//
/*everytime it fetches the row, adds it to array...*/
$foo = "array (\n";
while ($row=mysql_fetch_assoc($sql)){
$foo.= "array (\n";
foreach ($row as $key => $value){
$foo .= "'{$key}' => \"{$value}\",\n";
}
$foo = substr($foo,0,strlen($foo)-2)."\n";//removes the comma at the end
$foo .="),\n";
}
$foo = substr($foo,0,strlen($foo)-2)."\n";//removes the comma at the end
$foo .= ');';
echo '<pre>'.$foo.'</pre>';
?>
这是打印输出,
array (
array (
'id' => "1",
'title' => "First image",
'url' => "http://www.example.org/1",
'width' => "200",
'height' => "283",
'image' => "/images/image_1_big.jpg",
'preview' => "/images/image_1.jpg"
),
array (
'id' => "2",
'title' => "Second image",
'url' => "http://www.example.org/2",
'width' => "200",
'height' => "300",
'image' => "/images/image_2_big.jpg",
'preview' => "/images/image_2.jpg"
)
);
现在当前在server.php中
// Our data source
$data = include ('xyz.php');
它不起作用,但如果我手动放置打印输出它可以很好地工作。
所以如何让$ data =打印输出xyz.php
答案 0 :(得分:0)
更改
$foo = "array (\n";
while ($row=mysql_fetch_assoc($sql)){
$foo.= "array (\n";
foreach ($row as $key => $value){
$foo .= "'{$key}' => \"{$value}\",\n";
}
$foo = substr($foo,0,strlen($foo)-2)."\n";//removes the comma at the end
$foo .="),\n";
}
$foo = substr($foo,0,strlen($foo)-2)."\n";//removes the comma at the end
$foo .= ');';
echo '<pre>'.$foo.'</pre>';
到
$foo = array();
while ($row=mysql_fetch_assoc($sql)){
$foo[]= $row;
}
return $foo;
答案 1 :(得分:0)
如果您包含xyz.php,您将可以访问内部的所有内容,因此无需返回任何内容。把它放在xyz.php中:
$data = array();
while ($row=mysql_fetch_assoc($sql)){
$data[]= $row;
}
然后在server.php内部,您将能够访问$ data。