Parse Php in Mysql output - php

i have a website with a Mysql database that has html in each row (yes, i know this is not ideal). The problem is that the programmer simply copied and pasted duplicated text in many rows. I therefore have to manually edit the html in many rows if i need to update the text. I wanted to includ a 'include' or 'require' function and store the generic html in an external file but it doesnt seem to parse in the mysql output (only shows the static html). Therefore, how can i include reference to an external file in the header html of the mysql rows that contains html? That is, i need to parse php included in a row of the mysql database and not from a php page calling to the database. I tried to the eval function but still it could not retrieve a external php file.
Another alternative is that the duplicated text is actually another row in the database. Therefore, i'm not sure how i can make a page dynamically pull multiple rows from the database in a specific order?
What a mess. Any ideas?
Cheers

You could parse each rows when getting them.
preg_match_all ("#include\((.+)\)#", $row, $result);
foreach ($result[1] as $value) {
include ($value);
}
Or something like that. :)
( http://php.net/manual/fr/function.preg-match-all.php )

Related

Looping mysql Table Values on a CSS Table

I have a table with date/time, temperature and humidity data stored on a mysql database.
I have a ugly looking but working HTML one so I made a new CSS table using templates and wanted to use the looping in PHP (as in the HTML) to fetch data to the table using the table designers format. (Some datas are manually in as in the sample table photo)
I want to automate that but my PHP script has a problem and does not load the page. shows error.
Im very new to LAMP environment actually and unsure if I can use class variables inside the PHP script without changing anything.
My table looks like this without the python script: https://imgur.com/a/WmpHzMZ
The code is here: https://codeshare.io/amvPYw
(I cannot add code here, it says bad formatting everytime)
Html element tags are close like </tr>. See your end line in while loop.

How to insert "<?php include("Login.php");?>" in a MySQL database?

I'm trying to realize a dynamic website for exercise. All contents are stored in the database:
I included html code(FORMS) as DB content with no ploblem, but when i try to insert php code as DB content nothing happens. The field type for Content is TEXT.
I tryed various ways :
INSERT INTO TableContent(Name_Content, Content, ID_Menu) values ("Amministration Area", "<?php include('LogIn.php')", 5);
INSERT INTO TableContent(Name_Content, Content, ID_Menu) values ("Amministration Area", "<?php include(\"LogIn.php\")", 5);
In the website page i see Amministrazion Area, but not what Login.php does.
I thought i made a mistake in Login.php page code so i changed it in a simply echo ("Hi"); but nothing appears anyway. I see only the text Amministration Area.
How can i solve it ?
It's just text! Text in a database. Nothing happens when you just insert text into a database and read from it. You have to actually get something to execute that text as PHP code. That doesn't happen automatically, thankfully! In PHP, the way to execute arbitrary strings as code is by using eval. But this is overall a very bad idea; storing all PHP code in a database is not making anything easier, on the contrary it makes everything more difficult to work with and more prone to exploits if you execute arbitrary code. I'd advice you to stop doing that and go back to PHP code in .php files, not databases.
PHP see <?php include("LogIn.php");? as a string, when you look in the source code of your file, you'll probably see it. It doesn't show up on screen because it starts with a <.
To execute the code instead of handling it as a string, you can use the eval() function.
You however don't want every row of your database be seen as PHP code, so you'll probably need to add a extra column that defines what the row is, i.e. a string, php code. Or you make an extra table where you store your includes.

Xpath Table Within Table

I am having a bit of a problem of scraping a table-heavy page with DOMXpath.
The layout is really ugly, meaning I am trying to get content out of a table within a table within a table.
Using Firebug FirePath I am getting for the table element the following path:
html/body/table/tbody/tr[3]/td/table[1]/tbody/tr[2]/td[1]/table[1]/tbody/tr[3]/td[4]
Now, after endless experimenting I found out, that with a stand alone table, I need to remove the "tbody" tag to make it work. But this doesn't seem to be enough for tables within tables.
So my question is how do I best get content out of tables within tables within tables?
I uploaded the file which I am trying to scrape here:1
i have gone through with the same problem as yours scrapping a source of complicated and not well formatted html where i want to get the values in a table inside another tables..
i came with the approach of eyeing the part that i want to get with some series of function like this:
function parse_html() {//gets a specific part of the table i chose to extract the contents
$query = $xpath->query('//tr[#data-eventid]/#data-eventid'); //gets the table i want
$this->parse_table();
}
function parse_table() {//
$query = $xpath->query('//tr[#data-eventid="405412"]/td[#class="impact"]/span[#title]/#title');...etc//extracts the content of the table
$this->parseEvaluate();
}
function parseEvaluate(){
...verifying values if correct
}
just giving the idea..
How about:
//*[contains(text(),"GRABME")]
I know that's probably not what you want, but you get the idea. Identify a pattern and use that pattern to construct the xpath.

Current date field in array read by PHP

I have an array made up of dated events that I list on a webpage using PHP to generate the page that shows a date sorted table. I am trying to insert the PHP time() function, in a specific format that works for me, into one specific record of the text (CSV) array so that every time someone opens this page, this one specific record always reads with the current date in the format that I need. I have tried many variations of
<?php print ((time() + '39681000000')/1000000); ?>
Both with and without the PHP open and close tags, and many other variations, but with no success. The record just shows a blank field with this function in it with the tags and just shows the code without the tags. The PHP function works fine stand alone, but will not work when inserted in the record field. It does not seem that I can insert any PHP function, let alone this one, into the record field and have it work, so it does not seem to be a problem with how this function is written.
I have also tried to declare this as a Global variable at the top of the page and then call it in this field, but that does not work. I have tried the now() function with no joy either.
Thanks,
Stan...
In that case a string replace might be the best option (like mentioned above by drew). Try to remove the PHP code from the CSV file and replace it with something like ${DATE}, then put something like this in your while loop that imports the CSV file right after you set $line:
$line = str_replace('${DATE}', (time() + 39681000000/1000000), $line);

Sorting a list in PHP

Alright, so I've just (almost) finished my first little php script. Basically all it does is handling forms. It writes whatever the user put in to the fields of the form to a text file, then I include that text file in the index of the little page I have set up.
So, currently it writes to the beginning of the text file (but doesn't overwrite previous data). But, several users wants this list to be alphabetically sorted instead. So what I want to do is make whatever they submit fall into the list in alphabetical order.
The thing here is also that all I use in the text file are divs. The list is basically 'divided' into 3 parts. 'Title', 'Link', and 'Poster'. All I have done is positioned them with css.
So, can I sort this list (the titles, in this case) alphabetically and still have the 'link' and 'poster' information assigned the way they already are, but just with the titles sorted?
It don't use databases at all on my site, so there is no database handling at all used in this script (mainly because I'm not experienced at all in this).
I would also suggest storing the data in the file as either XML or JSON. PHP can sort the records easily and the sorting will be preserved in the file when the data is read back in.
For example
file_put_contents("/tmp/datafile",json_encode($recordset));
and when reading the file back in
$recordset = json_decode(file_get_contents("/tmp/datafile"));
edit
but seriously if you have customers and are charging them for your experience and time, use a database, there are many out there (a few mentioned already)
MySQL
sqlite
PostgreSQL
Oracle
MSSQL
If you really don't want to use a database, even a one-file database (such as sqlite), you can group the three fields using a separator such as _ and sort this single-field list
If this is a small project for some friends or something and the file shouldn't ever have more than maybe a few hundred lines, the functions you're looking for are "file" and "usort".
If you make sure each row is on its' own line, you can load the lines into an array with "file". You can sort the array using a function to compare items with the "usort" function.

Categories