i have the function get_path_dis() and i want to send the resultant to javascript and alert result. while opening the page the alert doesn't show. can someone tell me where am i mistaken
function get_path_dis($array){
//print_r($array);
$total = count($array);
$current = $array[0]['distance'];
$loop=0;
//get shortest distance
for($loop=1;$loop<$total;$loop++){
$next = $array[($loop)]['distance'];
if ($next<$current){
$current = $next;
//print_r('<pre>');
}
}
//shortest path array;
for($i=0;$i<$total;$i++){
if ($current==$array[$i]['distance'])
{
$xmlConv = $array[$i];
}
}
$x = json_encode($xmlConv);
?>
function get_path_dis($array){
//print_r($array);
$total = count($array);
$current = $array[0]['distance'];
$loop=0;
//get shortest distance
for($loop=1;$loop<$total;$loop++){
$next = $array[($loop)]['distance'];
if ($next<$current){
$current = $next;
//print_r('<pre>');
}
}
//shortest path array;
for($i=0;$i<$total;$i++){
if ($current==$array[$i]['distance'])
{
$xmlConv = $array[$i];
}
}
$x = json_encode($xmlConv);
?>
<script type="Javascript">
<!--
var
$json_val= "<?php echo($x);?>";
alert($json_val);
//-->
</script>
Needs to be encapsulated in php start and end tags.
alert('<?php echo json_val; ?>');
Related
I m trying to parse a post related statement from a forum site using PHP dom parser. It works when we insert individual url's of the page, but when we try to apply a while loop logic it kinda prints only one page multiple times..
my code as goes::
<?php
set_time_limit(3600);
$i = 1;
$e = 839304-$i;
while(true){
require_once('dom/simple_html_dom.php');
$html =file_get_html('http://www.usmleforum.com/files/forum/2017/1/'.$e.'.php');
foreach ($html->find("tr") as $row) {
$element = $row->find('td.Text2',0);
if ($element == null) { continue; }
$textNode = array_filter($element->nodes, function ($n) {
return $n->nodetype == 3; //Text node type, like in jQuery
});
if (!empty($textNode)) {
$text = current($textNode);
echo $text."<br>";
}
}
$i++;
}
?>
as the result indicates, it only prints the statement from page 839303, but it prints it multiple times and still loads on.. so its clear that this code is skipping the $i++ line somehow and runs again...
Any help is appreciated...
Insert $e inside while loop will fix the problem. but it is an infine loop. so try to give an exit condition for while loop like: while($i < 100) or something else.
<?php
set_time_limit(3600);
$i = 0;
while($i < 10){
$e = 839303 + $i;
require_once('dom/simple_html_dom.php');
$html =file_get_html('http://www.usmleforum.com/files/forum/2017/1/'.$e.'.php');
foreach ($html->find("tr") as $row) {
$element = $row->find('td.Text2',0);
if ($element == null) { continue; }
$textNode = array_filter($element->nodes, function ($n) {
return $n->nodetype == 3; //Text node type, like in jQuery
});
if (!empty($textNode)) {
$text = current($textNode);
echo $text."<br>";
}
}
$i++;
}
?>
Getting Notice: Undefined offset: 25 in
C:\wamp\www\finalProjectDemo\search.php on line 32
I'm trying to read in from a file and search for a specific name and address within that for output. I know a database would be best. This is for a class assignment I'm giving out that's specifically set to work this way. I believe I almost have it all, but am just getting this problem. Fairly new to PHP.
I have this code:
<html>
<body>
<?php
// read lines into array
// search array for string
// get 7 lines from there.
$i = 0;
$fileName = "addresses.txt";
$readFile = fopen($fileName, 'r');
$readByLineArray = array();
// Get search string from submission
$searchFirstName = $_POST['searchFirstName'];
$searchLastName = $_POST['searchLastName'];
$searchFirstNameSuccess = 0;
$searchLastNameSuccess = 0;
while (!feof($readFile))
{
$readByLineArray[$i] = fgets($readFile);
//echo "$readByLineArray[$i] read from array position $i";
//echo "<br />";
$i++;
}
fclose($readFile);
$arrLength = count($readByLineArray);
$currentArrayPosition = 0;
for ($x=0;$x<=$arrLength;$x++){
if ($searchFirstName == $readByLineArray[$x])
{
$searchFirstNameSuccess = 1;
$x++;
if ($searchLastName == $readByLineArray[$x])
{
$searchLastNameSuccess = 1;
$currentArrayPosition = $x - 1;
} else {
$searchFirstNameSuccess = 0;
}
}
}
for ($y=0;$y<=7;$y++){
echo "$readByLineArray[$currentArrayPosition]<br />";
$currentArrayPosition++;
}
?>
</body>
</html>
Thanks for all your help!
Ben---
Try foreach :-
foreach ($readByLineArray as $temp){
if ($searchFirstName == $temp)
{
$searchFirstNameSuccess = 1;
$x++;
if ($searchLastName == $temp)
{
$searchLastNameSuccess = 1;
} else {
$searchFirstNameSuccess = 0;
}
}
}
Change your for loop like this..
for ($x=0;$x<$arrLength;$x++){ //<--- Should be < and not <=
Say if your array count is 3 , so the array elements keys are arranged as 0,1,2. When you put <= in the looping as condition , your code will check for an non-existent key with an index of 3 which will thrown an Undefined Offset notice.
EDIT :
The easier way....
<html>
<body>
<?php
$fileName = "addresses.txt";
// Get search string from submission
$searchFirstName = $_POST['searchFirstName'];
$searchLastName = $_POST['searchLastName'];
$searchFirstNameSuccess = 0;
$searchLastNameSuccess = 0;
foreach(file($fileName) as $recno=>$records)
{
if(stripos($records,$searchFirstName)!==false && stripos($records,$searchLastName)!==false)
{
$searchFirstNameSuccess = 1;
$searchLastNameSuccess = 1;
echo "Match Found at Position : $recno";
break;
}
}
?>
</body>
</html>
i used php implode method to an array inside javascript.Yesterday it was working fine but now am getting SyntaxError: syntax error <br/> in firebug.
I closed the php tag correctly but not sure why is this coming.
function create() {
var sTop = Math.floor(Math.random() * (windowHeight));
<?php
for ($i = 1; $ i <= 28; $i++) {
if(${'h'.$i} != NULL) {
$sel[] = ${'h'.$i};
}
}
$format= implode('","', $sel); ?>
var selectedImg = new Array("<?php echo $format; ?>");
}
I am getting above the
var selectedImg = new Array("<?php echo $format; ?>");
when I viewed the JavaScript in FireBug.Can anyone help?
A fix ..
function create() {
var sTop = Math.floor(Math.random() * (windowHeight));
<?php
$sel = array();
for ($i = 1; $ i <= 28; $i++) {
if(${'h'.$i} != NULL) {
$sel[] = ${'h'.$i};
}
}
$format = !empty($sel) ? implode('","', $sel) : ""; ?>
var selectedImg = new Array("<?php echo $format; ?>");
}
Other method with json encode
function create() {
var sTop = Math.floor(Math.random() * (windowHeight));
<?php
$sel = array();
for ($i = 1; $ i <= 28; $i++) {
if(${'h'.$i} != NULL) {
$sel[] = ${'h'.$i};
}
}
?>
var selectedImg = <?php echo json_encode($sel); ?>;
}
I believe this has something to do with my $paramList and $paramList1... The do-while loop is executing but nothing is being reported for those two variables. I declared them as undefined arrays then set them equal to the number of objects and scripts. Did I do something wrong? Any suggestions to try?
Thanks.
PHP:
<?php
$savefile = 'testing1.html';
ob_start();
function emu_getallheaders()
{
foreach($_SERVER as $name => $value)
if(substr($name, 0, 5) == 'HTTP_')
$headers[str_replace('X-Secondlife-', 'X-SecondLife-', str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5))))))] = $value;
return $headers;
}
$headers = emu_getallheaders(); // Just replace any use of `apache_request_headers()` with `emu_getallheaders()`.
$objectName = $headers["X-SecondLife-Object-Name"];
$objectKey = $headers["X-SecondLife-Object-Key"];
$ownerKey = $headers["X-SecondLife-Owner-Key"];
$ownerName = $headers["X-SecondLife-Owner-Name"];
$region = $headers["X-SecondLife-Region"];
$sensorNum = $_POST["sensorNum"];
$numOfObjects = $_POST["numOfObjects"];
$numOfScripts = $_POST["numOfScripts"];
$maxSensors = 4;
$paramList = array();
$paramList1 = array();
$paramList[$sensorNum] = $numOfObjects;
$paramList1[$sensorNum] = $numOfScripts;
$j = 1;
echo <<<EOT
<html>
<CENTER>
<H1>
<b>Heat Map<br><br></b>
</H1>
EOT;
do{
echo <<<EOT
<CENTER>
<H1>
<b>Sensor $j <br></b>
</H1>
</CENTER>
<font size="5" face="arial" color="red">
<CENTER>
Total number of objects running:
$paramList[$j]
<br>
Total number of scripts running:
$paramList1[$j]
<br>
</font>
</CENTER>
EOT;
$j = ($j + 1);
}while($j <= $maxSensors);
<<<EOT
</body>
</html>
EOT;
$thePara = ob_get_contents();
file_put_contents('testing1.html', $thePara);
?>
And this is my Second Life script in case someone wants to see:
//Get the number of objects and scripts in a given area
key requestid;
integer typeConst;
integer objects;
integer scripts;
integer two = 2;
webPage()
{
requestid = llHTTPRequest("http://www.wbi-icc.com/students/SL/thisisatest.php",
[HTTP_METHOD, "POST",
HTTP_MIMETYPE, "application/x-www-form-urlencoded"],
"numOfScripts=" + (string)scripts
+ "&sensorNum=" + (string)two);
}
default
{
state_entry()
{
}
//Activate on a touch
touch_start(integer total_number)
{
typeConst = 0;
llSensor("", NULL_KEY, SCRIPTED, 20.0, PI);
}
//Use the sensors to keep a count of the total objects and scripts
sensor(integer detected)
{
if(typeConst == 0)
{
llOwnerSay("There are "+(string)detected
+" scripted objects in range.");
typeConst = 1;
llSensor("", NULL_KEY, PASSIVE, 20.0, PI);
}
else if(typeConst == 1)
{
llOwnerSay("There are "+(string)detected
+" objects in range.");
}
}
no_sensor()
{
if(typeConst == 0)
{
llOwnerSay("There are no scripted objects in range.");
typeConst = 1;
llSensor("", NULL_KEY, PASSIVE, 20.0, PI);
}
else
{
llOwnerSay("No objects in range.");
}
}
}
Controller
for ($x = 1; $x <= $numb; $x++)
{
echo $quanoutput = $this->input->post('quanoutput');
$barcodeoutput = $this->input->post('barcodeoutput');
$productsoutput = $this->input->post('productsoutput');
$outward_date=$this->input->post('outward_date');
$stock=$this->input->post('stock');
$warehouse_id =$this->input->post('warehouse_id');
$request_id =$this->input->post('request_id');
$warehouse=$this->input->post('warehouse');
$buyprice = $this->input->post('buyprice');
if ($productsoutput=='undefined'){
//$flag3 = $this->cartmodel->cartInsert($barcodeoutput,$quanoutput,$buyprice,$stock,$warehouse,$warehouse_id,$request_id,$outward_date);
} else {
$flag3 = $this->cartmodel->cartInsert($barcodeoutput,$quanoutput,$buyprice,$stock,$warehouse,$warehouse_id,$request_id,$outward_date);
}
}
Try starting your for loops at 0. (ie. j=0) and change the <= to just <.