Can this be done? (html + database)

Can this be done? (html + database)

Author
Discussion

Dave_ST220

Original Poster:

10,294 posts

205 months

Wednesday 23rd April 2014
quotequote all
I've got a database that contains prices for a wesbite, the prices are displayed on the page via some javascript (we paste a line of JS on the page for the pricing & stock info). The price renders OK in the browser but if you view source code the price is not there, just the JS that then gives the price (makes sense). What I'm now looking at doing is implementing schema org data by way of meta tags. What I'd like to know is can this be done via the existing database? ie, can I just add a line of code to the html pages like <meta itemprop="price" content="£10.00"> where the £10.00 part would be pulled from the DB?? (so there would be soemthing like <?php echo $price; ?> in the inverted commas of the content=)

??

or is it time to start again??

TIA smile


durbster

10,270 posts

222 months

Wednesday 23rd April 2014
quotequote all
If I've understood correctly then that's something you would usually do with PHP rather than Javascript. PHP is run on the server before the page is served to the browser, whereas Javascript runs as the page is loading which is why Javascript source can be viewed in the browser (but PHP cannot).

Dave_ST220

Original Poster:

10,294 posts

205 months

Wednesday 23rd April 2014
quotequote all
The problem I have is all pages are html rather than php. I guess I could look at changing all pages then using an htaccess rule to cover the change from html to php extensions??

Dave_ST220

Original Poster:

10,294 posts

205 months

Wednesday 23rd April 2014
quotequote all
Just thought, I'm sure the bots used to see this info anyway as Google used to display them in the SERP's. Is there a good tool that shows a page as bots see it? Ta

WinstonWolf

72,857 posts

239 months

Wednesday 23rd April 2014
quotequote all
Dave_ST220 said:
The problem I have is all pages are html rather than php. I guess I could look at changing all pages then using an htaccess rule to cover the change from html to php extensions??
Not really, the PHP runs on the server then dishes up the HTML.

You'd be better off doing it 'correctly'.

A basic db driven PHP site isn't much more difficult to build than an HTML one.

cornet

1,469 posts

158 months

Wednesday 23rd April 2014
quotequote all
When you say "a database" what do you mean ?

I'l guessing this isn't a database in the normal sense of the word (MySQL for example) but maybe a json/csv file containing all the prices

Also I'm not sure what the problem is you're trying to solve, is it for google shopping search or similar - depending on what the problem is there might be other ways to solve this rather than PHP smile

Dave_ST220

Original Poster:

10,294 posts

205 months

Thursday 24th April 2014
quotequote all
WinstonWolf said:
Dave_ST220 said:
The problem I have is all pages are html rather than php. I guess I could look at changing all pages then using an htaccess rule to cover the change from html to php extensions??
Not really, the PHP runs on the server then dishes up the HTML.

You'd be better off doing it 'correctly'.

A basic db driven PHP site isn't much more difficult to build than an HTML one.
The site is already built & has been for some years wink

Dave_ST220

Original Poster:

10,294 posts

205 months

Thursday 24th April 2014
quotequote all
cornet said:
When you say "a database" what do you mean ?

I'l guessing this isn't a database in the normal sense of the word (MySQL for example) but maybe a json/csv file containing all the prices

Also I'm not sure what the problem is you're trying to solve, is it for google shopping search or similar - depending on what the problem is there might be other ways to solve this rather than PHP smile
It's a MySQL database.

WinstonWolf

72,857 posts

239 months

Thursday 24th April 2014
quotequote all
Dave_ST220 said:
WinstonWolf said:
Dave_ST220 said:
The problem I have is all pages are html rather than php. I guess I could look at changing all pages then using an htaccess rule to cover the change from html to php extensions??
Not really, the PHP runs on the server then dishes up the HTML.

You'd be better off doing it 'correctly'.

A basic db driven PHP site isn't much more difficult to build than an HTML one.
The site is already built & has been for some years wink
In that case yes, you just need to PHP echo the db values rather than put them in using js.

Dave_ST220

Original Poster:

10,294 posts

205 months

Thursday 24th April 2014
quotequote all
Can you give some example code? & that would work on a static html page? Thanks

ETA, I tried this which I thought would work :-

<?php echo $price; ?>

But no joy frown I know nothing about PHP btw!!

Edited by Dave_ST220 on Thursday 24th April 08:47

WinstonWolf

72,857 posts

239 months

Thursday 24th April 2014
quotequote all
Is your file being parsed by php?

http://stackoverflow.com/questions/7181853/parse-h...

Discusses the why's and wherefores of how to do it.

What do you get in the source when you add the echo statement?

Dave_ST220

Original Poster:

10,294 posts

205 months

Thursday 24th April 2014
quotequote all
It isn't no, which I guess explains that! Quick read reveals this can be done in htaccess, is that the best & only method? Or can some code be added to the actual HTML pages in question? Thanks for your help so far!!! It's a great help smile

ETA, all I get in the source when it's added is exactly what I added! It doesn't show on the page in the browser, just in the source.

Edited by Dave_ST220 on Thursday 24th April 09:11

WinstonWolf

72,857 posts

239 months

Thursday 24th April 2014
quotequote all
I always write in PHP these days smile

You could write a very simple php page and call the existing file.

This will show you any errors, prove PHP is running via the echo statement, show your existing page then show you the price below the page. Bodgy as hell but it should get you going. If you don't see a value for price your DB connection isn't working.

Save the below as test.php and run it...


<?php
error_reporting(E_ALL);
echo 'PHP is running';
include ('./path_to_file/my_file.html');
echo 'price is ';
echo $price
?>

Dave_ST220

Original Poster:

10,294 posts

205 months

Thursday 24th April 2014
quotequote all
OK, I'll try that. When you say run it I assume just navigate to the URL?

The htaccess method I found was :-

AddType application/x-httpd-php .htm .html

Not tried that either yet though...

WinstonWolf

72,857 posts

239 months

Thursday 24th April 2014
quotequote all
Yup, just enter it in the address bar.

I don't normally parse .html but this *should* do it for one file in a .htaccess

<FilesMatch "^file_name\.html$">
AddType application/x-httpd-php .html
</FilesMatch>

Dave_ST220

Original Poster:

10,294 posts

205 months

Thursday 24th April 2014
quotequote all
Ok, done that but no price. This line :-

include ('./path_to_file/my_file.html');

Should be set to the file I'm working on? I don't get how that would work?

With regard to htaccess I need to do it on around 30 pages so I need a generic rule rather than per file really.

Dave_ST220

Original Poster:

10,294 posts

205 months

Thursday 24th April 2014
quotequote all
Tried the htaccess rule & that didn't work either. Sitting here thinking about it I'm guessing I need more code to tell it to actually look at the DB? On one of the pages that IS PHP I can see code like this :-

$sql = "SELECT id, name, menulinktext FROM categories WHERE id=".mysql_real_escape_string($_GET['id']);
$result = mysql_query($sql);

Shame, I thought it would have been simple but should have known better! All the info is sat there in a DB, I thought I could "tap" into it to provide this extra meta info frown

WinstonWolf

72,857 posts

239 months

Thursday 24th April 2014
quotequote all
One step at a time smile

Because the test file has a .php extension the PHP engine will parse it. If you include the .html that will have been processed. Sounds like $price isn't defined...

Normally it goes like this.

You have a connection string somewhere that connects to your database.

That runs, then a sql query executes and grabs data from the db.

You dump the data into an array and then echo out the values.

This article has enough info for you to hack together a php page that will connect to your database and produce an elementary table.

http://php.about.com/od/phpwithmysql/ss/mysql_php_...

There's a mixture of html and variables grabbed by PHP.

If you get *how* it works the rest should be plain(ish) sailing smile

Dave_ST220

Original Poster:

10,294 posts

205 months

Thursday 24th April 2014
quotequote all
Ok, thanks. I'll get reading smile I know there is a config.php file that seems to handle the DB connection (user name & PW etc). I think that is the route of my problems smile

WinstonWolf

72,857 posts

239 months

Thursday 24th April 2014
quotequote all
If you're able to display the values *somehow* the db connection must be working, might be time to pop some code up with pw etc removed!