Currently, I created a system that uses daypilot calendar. For example, when I clicked a date, it will show the list of activities on that selected date.
Below code is successful display the list:
<?php
require_once '../../../config/configPDO.php';
$Fac_ID = strtolower($_GET['Fac_ID']);
$Room_Desc = strtolower($_GET['Room_Desc']);
$stmt = $conn->prepare("SELECT * FROM booking LEFT JOIN room ON booking.Room_ID = room.Room_ID
WHERE room.Fac_ID = '$Fac_ID' AND room.Room_Desc = '$Room_Desc' AND Book_Status <> 'Reject'");
$stmt->execute();
$result = $stmt->fetchAll();
class Event {}
$events = array();
foreach($result as $row) {
$e = new Event();
$e->id = $row['Book_No'];
$e->text = "Meeting Name: ". $row['Meeting_Description']. " || " ."Requested by: ".$row['Requested_by']. " || " ."Location: ".$row['Fac_ID']. ", " .$row['Room_Desc'];
$e->start = $row['StartTime'];
$e->end = $row['EndTime'];
$events[] = $e;
}
header('Content-Type: application/json');
echo json_encode($events);
?>
But, when I change the code (Because want to retrieve json data), the data doesn't display at calendar.
Below is the code:
<?php
require_once '../../../config/configPDO.php';
$Fac_ID = strtolower($_GET['Fac_ID']);
$Room_Desc = strtolower($_GET['Room_Desc']);
//retrieve json
$url = "http://172.20.0.45/TGWebService/TGWebService.asmx/displayPABooking?Fac_ID=$Fac_ID&Room_Desc=$Room_Desc&Room_ID=&Book_No=&Book_Date_From=&Book_Date_To=";
$data = file_get_contents($url); //line 10
$json = json_decode($data);
$result = $json->bookingList; //line 12
class Event {}
$events = array(); //
foreach($result as $row) { //line 17
$e = new Event();
$e->id = $row->bookNo;
$e->text = "Meeting Name: ".$row->desc. " || " ."Requested by: ".$row->requestedBy." || " ."Location: ".$row->facID.", " .$row->roomName;
$e->start = $row->startTime;
$e->end = $row->endTime;
$events[] = $e;
}
header('Content-Type: application/json');
echo json_encode($events);
?>
And, This is the error that I found at console:
PHP Warning: file_get_contents(http://172.20.0.45/TGWebService/TGWebService.asmx/displayPABooking?Fac_ID=TGT&Room_Desc=LEVEL 1 &Room_ID=&Book_No=&Book_Date_From=&Book_Date_To=): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
in C:\inetpub\wwwroot\ebooking\pages\manual_booking\backend\backend_events.php on line 10
PHP Notice: Trying to get property 'bookingList' of non-object in C:\inetpub\wwwroot\ebooking\pages\manual_booking\backend\backend_events.php on line 12
PHP Warning: Invalid argument supplied for foreach() in C:\inetpub\wwwroot\ebooking\pages\manual_booking\backend\backend_events.php on line 17
Can I know where I need to change? Thanks
This is probably because of the space in "Level 1". Try urlencoding your $_GET parameters:
$url = "http://172.20.0.45/TGWebService/TGWebService.asmx/displayPABooking?Fac_ID=" . urlencode($Fac_ID) . "&Room_Desc=" . urlencode($Room_Desc) . "&Room_ID=&Book_No=&Book_Date_From=&Book_Date_To=";
If you want to make the variable substitution more explicit, you could use strtr:
$template = "http://172.20.0.45/TGWebService/TGWebService.asmx/displayPABooking?Fac_ID={Fac_ID}&Room_Desc={Room_Desc}&Room_ID=&Book_No=&Book_Date_From=&Book_Date_To=";
$url = strtr($template, array(
'{Fac_ID}' => $Fac_ID,
'{Room_Desc}' => $Room_Desc
));
Related
I have a code below
<?php
/* Error display */
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('memory_limit', '512M');
/* Requires */
require 'conn.php';
/* Parameters (DIM) */
$param_customer = $_POST['param_customer'];
$param_user = $_POST['param_user'];
/* Others */
$param_email = $_POST['email'];
$file_dump_area = "../general_sync/";
/* Array */
$jsonData = array();
$arr_result = array();
/******************************** Download customer *********************************/
$cur_filename = $file_dump_area . removeCharEmail($param_email) . "_" . $param_customer . ".csv";
$cur_file = fopen($cur_filename, "w");
$cur_sql = "CALL android_getCustomer('" .$param_email. "')";
$cur_result = mysqli_query($con,$cur_sql);
if ($cur_file && $cur_result) {
while ($row = $cur_result->fetch_array(MYSQLI_NUM)) {
fputcsv($cur_file, array_values($row));
}
array_push($arr_result, array('done_process' => "done_cus"));
}
fclose($cur_file);
/******************************** Download user *********************************/
$cur_filename = $file_dump_area . removeCharEmail($param_email) . "_" . $param_customer . "1.csv";
$cur_file = fopen($cur_filename, "w");
$cur_sql = "CALL android_getCustomer('" .$param_email. "')";
$cur_result = mysqli_query($con,$cur_sql);
if ($cur_file && $cur_result) {
while ($row = $cur_result->fetch_array(MYSQLI_NUM)) {
fputcsv($cur_file, array_values($row));
}
array_push($arr_result, array('done_process' => "done_user"));
}
fclose($cur_file);
$jsonData = array("received"=>$arr_result);
echo json_encode($jsonData,JSON_PRETTY_PRINT);
function removeCharEmail($val) {
$new_val1 = str_replace(".", "", $val);
$new_val2 = str_replace("#", "", $new_val1);
return $new_val2;
}
?>
The target output of that code is to create 2 csv which is it does but the problem is the 2nd csv has no data although the query shows some it does not write. I tried to copy the 1st line of codes. it does create the file but it didnt write
Whats the problem?
Updated with help of Mr. Barmar
i got this error Commands out of sync; you can't run this command now
The stored procedure is apparently returning two result sets. You need to fetch the next result set before you can start another query. Add:
$cur_result->close();
$con->next_result();
After each loop that fetches the results. See https://stackoverflow.com/a/14561639/1491895 for more details.
I am unable to export my mysql table to XML format using PHP, I receive the following error when I run the below script. I have removed the connection bit, I have gone through already errors but was not able resolve this issue.
Catchable fatal error: Argument 1 passed to DOMNode::appendChild() must be an instance of DOMNode, null given,
Script:
<?php
$query = "SELECT id, name, plate, datetime , time , image_name FROM EarlsdonMSIN_anpr_vega";
$camArray = array();
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
array_push($camArray, $row);
}
if(count($camArray)){
createXMLfile($camArray);
}
/* free result set */
$result->free();
}
/* close connection */
$mysqli->close();
function createXMLfile($camArray){
$filePath = '/cam.xml';
$dom = new DOMDocument('1.0', 'utf-8');
$root = $dom->createElement('cam');
for($i=0; $i<count($camArray); $i++){
$camId = $camArray[$i]['id'];
$camName = $camArray[$i]['name'];
$camplate = $camArray[$i]['plate'];
$camdatetime = $camArray[$i]['datetime'];
$camtime = $camArray[$i]['time'];
$camimagename = $camArray[$i]['image_name'];
$cam = $dom->createElement('cam');
$cam->setAttribute('id', $camId);
$name = $dom->createElement('name', camName);
$cam->appendChild($name);
$plate = $dom->createElement('plate', $camplate);
$cam->appendChild($plate);
$datetime = $dom->createElement('datetime', $camdatetime);
$cam->appendChild($datetime);
$time = $dom->createElement('time', $camtime);
$cam->appendChild($time);
$imagename = $dom->createElement('image_name', $camimagename); $cam->appendChild($image_name);
$root->appendChild($cam);
}
$dom->appendChild($root);
$dom->save($filePath);
}
This question already has answers here:
How to store values from foreach loop into an array?
(9 answers)
Closed 1 year ago.
Basically I want to make a download button for my project that will produce different csv files (one csv file per table) in memory and zip before download. It works fine but the problem is that I am only getting one row (the last result) on each mysql_fetch_array where it is supposed to return rows depending on how many are stored in the database. This code is depreciated, sorry for that.
Here is my code:
<?php
require("../includes/connection.php");
require("../includes/myLib.php");
//get the ID that is passed
$ID = $_REQUEST['q'];
$ID = xdec($ID);
//All queries to fetch data
$query = mysql_query("SELECT * FROM `ge2`.`projects` WHERE `projects`.`proj_id`='$ID'");
$query2 = mysql_query("SELECT * FROM `ge2`.`attributes` WHERE `attributes`.`proj_id`='$ID'");
$query3 = mysql_query("SELECT * FROM `ge2`.`category` WHERE `category`.`proj_id`='$ID'");
$query4 = mysql_query("SELECT * FROM `ge2`.`multipletarget` WHERE `multipletarget`.`proj_id`='$ID'");
$query5 = mysql_query("SELECT * FROM `ge2`.`data_cut` WHERE `data_cut`.`proj_id`='$ID'");
$query6 = mysql_query("SELECT * FROM `ge2`.`raw` WHERE `raw`.`proj_id`='$ID'");
//getting all array
while ($row = mysql_fetch_array($query)) {
$proj_alias = $row['proj_alias'];
$proj_id = $row['proj_id'];
$date_added = $row['date_added'];
}
while ($row1 = mysql_fetch_array($query2)) {
$attrib_param_id = $row1['param_id'];
$attrib_proj_id = $row1['proj_id'];
$attrib_cat_id = $row1['cat_id'];
$attrib_val_id = $row1['val_id'];
$attrib_name = $row1['name'];
$attrib_isCust = $row1['isCust'];
}
while ($row2 = mysql_fetch_array($query3)) {
$category_cat_id = $row2['cat_id'];
$category_name = $row2['name'];
$category_proj_id = $row2['proj_id'];
$category_desc = $row2['desc'];
}
while ($row3 = mysql_fetch_array($query4)) {
$multipletarget_id = $row3['id'];
$multipletarget_proj_id = $row3['proj_id'];
$multipletarget_mtarget1 = $row3['mtarget1'];
$multipletarget_mtarget2 = $row3['mtarget2'];
}
while ($row4 = mysql_fetch_array($query5)) {
$data_cut_id = $row4['id'];
$data_cut_proj_id = $row4['proj_id'];
$data_cut_name = $row4['name'];
$data_cut_param = $row4['param'];
$data_cut_lvl = $row4['lvl'];
$data_cut_val = $row4['val'];
}
while ($row5 = mysql_fetch_array($query6)) {
$raw_id = $row5['raw_id'];
$raw_proj_id = $row5['proj_id'];
$raw_p_id = $row5['p_id'];
$raw_url = $row5['url'];
$raw_ip = $row5['ip'];
$raw_pos = $row5['pos'];
$raw_datetaken = $row5['datetaken'];
$raw_used = $row5['used'];
$raw_fdc_id = $row5['fdc_id'];
$raw_dq = $row5['dq'];
}
// some data to be used in the csv files
$records = array(
$proj_alias, $proj_id, $date_added
);
$records2 = array(
$attrib_param_id, $attrib_proj_id, $attrib_cat_id, $attrib_val_id, $attrib_name, $attrib_isCust
);
$records3 = array(
$category_cat_id, $category_name, $category_proj_id, $category_desc
);
$records4 = array(
$multipletarget_id, $multipletarget_proj_id, $multipletarget_mtarget1, $multipletarget_mtarget2
);
$records5 = array(
$data_cut_id, $data_cut_proj_id, $data_cut_name, $data_cut_param,$data_cut_lvl,$data_cut_val
);
$records6 = array(
$raw_id, $raw_proj_id, $raw_p_id, $raw_url,$raw_ip,$raw_pos,$raw_datetaken,$raw_used,$raw_fdc_id,$raw_dq
);
//making an array to be used in loop
$set = array($records,$records2,$records3,$records4,$records5,$records6);
//names to be named for each csv file
$names = array('projects', 'attributes', 'category', 'multipletarget', 'data_cut', 'raw');
// create zip file
$zipname = $proj_alias;
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
// loop to create csv files
$n = 0;
foreach ($set as $setk => $sets) {
$n += 1;
$fd = fopen('php://temp/maxmemory:1048576', 'w');
if (false === $fd) {
die('Failed to create temporary file');
}
fputcsv($fd, $sets);
// return to the start of the stream
rewind($fd);
// add the in-memory file to the archive, giving a name
$zip->addFromString('BrainLink-' . $proj_alias . "-" . $names[$setk] . '.csv', stream_get_contents($fd));
//close the file
fclose($fd);
}
// close the archive
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname.'.zip');
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
// remove the zip archive
// you could also use the temp file method above for this.
unlink($zipname);
?>
Thanks in advance.
Well, it seems that you're independently iterating over all query results and overwriting the variables over and over again. So in the end, you have only last table results to work with.
You might try using JOIN OR UNION SELECT in MySQL to get all the items in one big query result row:
$query = mysql_query('SELECT proj.*, attrs.*
FROM `projects` AS proj
JOIN `attributes` AS attrs ON (attrs.project_id=proj.project_id)
<..more tables to join in the same manner..>
WHERE proj.`proj_id`= ' . $projectId);
And then, you'll just have to iterate only over a single query resource.
while ($row = mysql_fetch_array($query)) {
//your code here
}
Note, that if tables being JOIN'ed have the same column names, they will "overwrite" each other and you'll have to rename them yourself "on the fly".
Like so:
SELECT proj.field, proj.another_field, proj.conflicting_field_name AS unique_field_name
I did not read all of your code, but in each while loop, you only save last record. It should be something like this.
while($row = mysql_fetch_array($query)){
$proj_alias[] = $row['proj_alias'];
$proj_id[] = $row['proj_id'];
$date_added[] = $row['date_added'];
}
and the others like above.
I have this code that access a api and gets the data and outputs it on my site, but I'm having a problem with the arrays and etc. I was wondering how can I access that data needed.
My problem is in:
foreach($entrieszz as $statSummaryzz)
{ $gamemodematchhistoryfellowPlayerslolsearch = $statSummaryzz->fellowPlayers->teamId; }
and its giving me error: Trying to get property of non-object
The data im trying to access:
Trying to get games->fellowPlayers->teamId and also games->fellowPlayers->championId
This is what I have:
<?php
$apiKey = 'e9044828-20e3-46cc-9eb5-545949299803';
$summonerName = 'tamini';
$new = rawurlencode($summonerName);
$news = str_replace(' ', '', $summonerName);
$str = strtolower($news);
// get the basic summoner info
$result = file_get_contents('https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/' . $new . '?api_key=' . $apiKey);
$summoner = json_decode($result)->$str;
$id = $summoner->id;
// var_dump($summoner);
?>
<?php
$clawzzeyu = file_get_contents('https://euw.api.pvp.net/api/lol/euw/v1.3/game/by-summoner/' . $id . '/recent?api_key=' . $apiKey);
$gazazzeyu = json_decode($clawzzeyu);
$entrieszz = $gazazzeyu->games;
usort($entrieszz, function($accc,$bccc){
return $bccc->createDate-$accc->createDate;
});
foreach($entrieszz as $statSummaryzz){
$gamemodematchhistoryfellowPlayerslolsearch = $statSummaryzz->fellowPlayers->teamId;
}
?>
In your json sample there isn't teamId but only team
could be need
foreach($entrieszz as $statSummaryzz){
$gamemodematchhistoryfellowPlayerslolsearch = $statSummaryzz->fellowPlayers->team;
}
For some reason this works flawlesly on my out of the box WAMP yet when uploaded to web server it drops the following error:
Severity: Warning
Message: DOMDocument::load(): I/O warning : failed to load external
entity
"/mounted-storage/home155/sub004/sc81961-YHHB/* * * * * *.com/dev/
2012-10-04 03:15:03
4 2012-10-04 03:25:16
2012-09-17 19:25:11
3
0
1
1
2012-10-04 04:08:18 "
Filename: models/fuel_model.php
Line Number: 74
It's pointing to the applications root folder.
Code:
function get_pos($id)
{
$get = $this->db->query(
"SELECT * FROM holding WHERE id=1"
);
$stream = $get->row_array();
$addr = 'http://api.eveonline.com/corp/StarbaseDetail.xml.aspx?keyID='.$stream['apiid'].
'&vCode='.$stream['apikey'] . '&itemID=' . $id;
$link = new DOMDocument();
$get_cache = $this->db->query(
"SELECT * FROM cache_poses WHERE posid=".$id
);
$cache = $get_cache->row_array();
$posted = strtotime(date("Y-m-d H:i:s"));
if(!empty($cache))
{
$posted = strtotime($cache['cachedon']);
}
$current = strtotime(date("Y-m-d H:i:s"));
$mins = $posted - $current;
$expiry = $mins % 60;
if(empty($cache))
{
$link->load($addr);
$this->db->query("INSERT INTO cache_poses (data, posid) VALUES (" . $this->db->escape($link->saveXML()) . "," . $id .")");
} elseif(!empty($cache) && ($expiry >= 360)) {
$this->db->query("DELETE FROM cache_poses WHERE posid=".$id);
$link->load($addr);
$this->db->query("INSERT INTO cache_poses (data, posid) VALUES (" . $this->db->escape($link->saveXML()) . "," . $id . ")");
} elseif(!empty($cache) && ($expiry < 360)) {
$link->load($cache['data']);
}
$supply = $link->getElementsByTagName('row');
$supplies = array();
foreach ($supply as $fuel) {
$block = $fuel->attributes;
$put = $this->db->query('SELECT name FROM items WHERE typeid='.$block->item(0)->nodeValue);
$lck = $put->row_array();
$supplies[$lck['name']] = $block->item(1)->nodeValue;
}
$info = $this->implode_with_keys($supplies);
return $info;
}
Can you post the XML? It is referencing a DTD that must be accessible on your local dev machine but not on your live server. If the DTD and the entities it defines are not important to your XML, you can suppress the warning, and likely get a performance increase for not loading an extra resource, by disabling the loading of enternal entities before calling DOMDocument::load().
$link->resolveExternals = FALSE;
$link->load($cache['data']);
http://www.php.net/manual/en/class.domdocument.php#domdocument.props.resolveexternals
By the way, DOMDocument::load() accepts a file path. If your $cache['data'] is really a string of XML in the database, you need to use DOMDocument::loadXML().