I am extracting some parts from a html webpage using DOM+php and trying to send the results to other pages as $_SESSION variables and eventually to update a mysql DB.
example of the HTML webpage code:
<html>
<body>
<div id="title">some title </div>
<div id="city">some city</div>
<div id="country">some country</div>
<div id="company">some company</div>
<div id="text">some text</div>
<body>
<html>
This is the code that I a using to get the data and is working...I can echo the $var:
<?php session_start(); ?>
---- some HTML---
<?
include('simple_html_dom.php');
$file = 'webpage.html';
$html = new simple_html_dom();
$html->load_file($file);
$title = $html->getElementById('title');
$city = $html->getElementById('city');
$country = $html->getElementById('country');
$company = $html->getElementById('company');
$text= $html->getElementById('text');
echo '<b>'.$title.'</b>';
$_SESSION['title'] = $title;
echo '<b>'.$city.'</b>';
$_SESSION['city'] = $city;
echo '<b>'.$country.'</b>';
..............
?>
My problem is that I can't send this $var ($title,$city, ...) to any other php page using $_SESSION... and I get this error:
Catchable fatal error: Object of class __PHP_Incomplete_Class could not be converted to string
According to the PHP docs, sessions can only contain data that can be serialized.
When PHP shuts down, it will automatically take the contents of the $_SESSION superglobal, serialize it, and send it for storage using the session save handler.
Source: http://php.net/manual/en/session.examples.basic.php
It looks like DOM elements unfortunately cannot be serialized and therefore cannot be stored in the session correctly.
UPDATE: It looks like you can fix this by casting the DOM elements as a string:
$_SESSION['title'] = (string)$title;
$_SESSION['city'] = (string)$city;
Related
Inside the source code of the url I want to scrape I can see this:
<div class="price">
22.96€
</div>
And this is the php file that I created but it´s not returning any value:
<?php
$data_scraped = file_get_contents('https://www.example.com/es/busquedas/?type%5B%5D=steam&query=motogp+20');
$the_start = explode('<div class="price">',$data_scraped);
$the_end = explode('</div>',$the_start[1]);
echo $the_end[0];
?>
Any help would be really appreciated
Your question looks similar to this one which makes use of simple_html_dom for web scraping in PHP.
Restructuring your code for usage with simple_html_dom:
<?php
require 'simple_html_dom.php';
$html = file_get_html('https://www.example.com/es/busquedas/?type%5B%5D=steam&query=motogp+20');
$price = $html->find('div[class=price]');
echo $price->plaintext."<br>";
?>
I currently have a php which echo my html template.
However in that HTML template there is another echo which calls from another php script.
Just wondering how do I do that? Because once I echo my html template the other it doesn't seems to echo my content from the other php script.
HTML TEMPLATE
<php? $html = '<span>name:<?php echo $name; ?></span><span>email:<?php echo $email; ?></span>' ?>
CONTACT TEMPLATE
<php? $name = "hello world"; $email = "hello#world.com"; ?>
I can see what you're trying to do, and it's a simple error. You can't escape php like that whilst inside setting a variable.
Also, I must add that you are declaring php incorrectly.
This is preferred
<?php
not
<php?
So make sure for your contact template you use the correct tag.
Also to include a file you have to call it/require it.
Back to the original question - Here is your method
<php? $html = '<span>name:<?php echo $name; ?></span><span>email:<?php echo $email; ?></span>' ?>
Here is the correct method
<?php
require('contact.php');
$html = '<span>name:'.$name.'</span><span>email:'.$email.'</span>';
echo $html;
?>
First I created the variable. And when doing so I insert the existing variables by escaping the php. Only once this final variable is created do I echo it.
Hope this helps you on your way.
Try to use include. The include statement includes and evaluates the specified file, in this case - your template.
Just Concatenation
<?
$html = '<span>name:'.$name.'</span><span>email:'.$email.'</span>';
?>
Change the tags from <php? ?> to <?php ?> in your script
I'm using CodeIgniter and I have a PHP MySQL Statement in a model:
function getAllDevices() {
$query = $this->db->query("SELECT * FROM Device_tbl ORDER BY Manufacturer");
return $query->result();
}
I then pass this through my controller to my view using:
$this->load->model("get_device");
$data['results'] = $this->get_device->getAllDevices();
And output this into a div using the following PHP:
<div class="hold-cont">
<div class="holder">
<div class="image-hold"><img class="image-icon" src="<?php echo base_url(); ?>assets/images/devices/<?php echo $row->Image; ?>"></div>
</div>
<div class="device-name devicename-txt">
<?php $mod = $row->Model;
$model = str_replace(" ","-",$mod);
?><?php echo($row->Manufacturer. ' ' .$row->Model); ?><br>
</div>
</div><?php } ?>
This currently works fine, however I have now incorporated a searchbox into my page which uses the jQuery function autocomplete and uses the JSON Array of $results
What I need to do is "convert" the PHP into Javascript which will enable the page to be populated using the JSON Array. I then need to be able to use this to pass the selection from the search box into a query which will change the content on the page without reloading it.
How can I go about this? I previously mashed together a way of doing it using the ajax function in jQuery, however this requires a URL which contained a PHP MySql statement which I cannot do now. The idea behind doing it like this is so it only runs 1 SQL Query when the page loads and I cannot change this method.
There is nothing to stop PHP "writing" Javascript.
You would need to build up a suitably formatted Javascript object either manually or by using the json_encode method of PHP (which essentially turns a PHP object into it's JSON notation). Assuming your PHP object is in $row then something like:
<code>
<script language="text/javascript">
var jsObject = <?php json_encode($row); ?>
</script>
</code>
Would give you a Javascript object in jsObject containing keys and values corresponding to the properties of the PHP $row object that you can then work with from Javascript.
json_encode();
json_decode();
Turns PHP arrays into a JSON array and back.
http://php.net/manual/en/function.json-encode.php
They work great.
Your ajax page:
$query = mysql_query("SELECT * FROM my_table WHERE my_field LIKE '%$input%'");
while ($row = mysql_fetch_assoc($query)) {
$json = array();
$json['value'] = $row['id'];
$json['name'] = $row['username'];
$json['image'] = $row['user_photo'];
$data[] = $json;
}
header("Content-type: application/json");
echo json_encode($data);
and fetch this echoed json encoded data in your javascript
this is just a rough code, avoid using mysql function.
<?php
$conn = oci_connect('usr', 'pass', 'host');
$instance_status="command1";
$spacecheck="command2";
$log_apply="command3";
$command=$_GET['name'];
echo $command;
$stid = oci_parse($conn, $command);
--some code--
?>
My HTML Page:
<html>
<title>Status Check</title>
<body>
<b>Spacecheck</b>
<b>Log Application Status</b>
<b>Database Status</b>
</body>
</html>
The above is my code, I intend to assign to $command, the value from the href variable through $_GET. But, when I test this code, $command is not being assigned the value of the variable from $_GET, rather the name of the variable is simply assigned to $command.
Eg, If I click on this:
Spacecheck
This should assign the VALUE of $spacecheck to $command, which is not happening. $command returns '$spacecheck'
How do I do this variable assignment?
You are simply writing $spacecheck. What you need to do is jump inside PHP tags and echo the variable values. Like so:
Spacecheck
or use the php echo shortcut:
Spacecheck
See the difference?
Good luck.
try going the other way around, I mean print from php:
<?php
echo '<b>Spacecheck</b>';
echo '<b>Log Application Status</b>';
echo '<b>Database Status</b>';
?>
You must use PHP open and close tags in order to place PHP code in your HTML page among other things. Try this link:
http://www.php.net/manual/en/tutorial.firstpage.php
Your HTML actually contains the literal string '$spacecheck' in the URL. $variables are only parsed in sections between tags, not in plain HTML.
Try this in your HTML file (which should be called .php) instead:
<?php
$spacecheck = 'foobar'; // (some dummy values)
$log_apply = 'nope';
$instance_status = 'idle';
print("<html>
<title>Status Check</title>
<body>
<b><a href='oraData.php?name=$spacecheck'>Spacecheck</a></b>
<b><a href='oraData.php?name=$log_apply'>Log Application Status</a></b>
<b><a href='oraData.php?name=$instance_status'>Database Status</a></b>
</body>
</html>");
?>
I have seen the following thread but it's a bit beyond me...
How can I change the <title> tag dynamically in php based on the URL values
Basically, I have a page index.php (no php in it just named to future proof - maybe now!). It contains numerous lightbox style galleries which can be triggered from an external link by a variable in the URL - e.g. index.php?open=true2, index.php?open=true3, etc.
I would like the index.php title tag - to include existing static data + append additional words based on the URL variable - e.g. if URL open=true2 add "car gallery", if URL open=true3 add "cat gallery", if URL has no variable append nothing to title.
Can anyone assist? I have been searching but either missed the point of posts or it hasn't been covered (to my amateaur level).
Many thanks. Paul.
At the top of your php script put this:
<?php
# define your titles
$titles = array('true2' => 'Car Gallery', 'true3' => 'Cat Gallery');
# if the 'open' var is set then get the appropriate title from the $titles array
# otherwise set to empty string.
$title = (isset($_GET['open']) ? ' - '.$titles[$_GET['open']] : '');
?>
And then use this to include your custom title:
<title>Pauls Great Site<?php echo htmlentities($title); ?></title>
<title>Your Static Stuff <?php echo $your_dyamic_stuff;?></title>
<?php
if( array_key_exists('open', $_GET) ){
$title = $_GET['open'];
}else{
$title = '';
}
?>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
The content of the document......
</body>
</html>
http://www.w3schools.com/TAGS/tag_title.asp
http://php.net/manual/en/reserved.variables.get.php
PHP can fetch information from the URL querystring (www.yoursite.com?page=1&cat=dog etc). You need to fetch that information, make sure it's not malicious, and then you could insert it into the title. Here's a simple example - for your application, make sure you sanitise the data and check it isn't malicious:
<?php
$open = "";
// check querystring exists
if (isset($_GET['open'])) {
// if it does, assign it to variable
$open = $_GET['open'];
}
?>
<html><head><title>This is the title: <?php $open ?></title></head>
PHP has lots of functions for escaping data that might contain nasty stuff - if you look up htmlspecialchars and htmlentities you should find information that will help.
Some of the other answers are open to abuse try this instead:
<?php
if(array_key_exists('open', $_GET)){
$title = $_GET['open'];
} else {
$title = '';
}
$title = strip_tags($title);
?>
<html>
<head>
<title><?php echo htmlentities($title); ?></title>
</head>
<body>
<p>The content of the document......</p>
</body>
</html>
Otherwise as #Ben has mentioned. Define you titles in your PHP first to prevent people from being able to directly inject text into your HTML.