check if line has already been parsed and inserted into mysql database - javascript

I am parsing a whole javascript file (min_day.js) into arrays and then creating INSERT Statements; then executing them with a mysqli_multi_query to get the data into my database
this min_day.js file is updating irregularly (in most cases every 5 min) during the day but it still holds the data from the same day: that means always a new line with data is added on top of the old data
so i am going to configure a cronjob that runs every min to do the parsing and inserting mentioned in the first paragraph but:
now the problem is: how can i only parse and insert the data that has not already been parsed and inserted into the database? how can i check if the data has already been parsed and inserted?
i want to avoid having twice the same data in my database. i guess the solution could be something with using a timestamp... but i'm a beginner and don't know how
// min_day.js file
m[mi++]="24.11.14 08:30:00|196;124;132;55;540;601;45|194;112;123;53;538;606;45|457;350;120;149;570;541;45|452;336;114;146;566;544;46|428;323;107;145;569;541;45|409;325;114;137;572;541;45|38;50;11;407;10|0;0;0;251;14|0;0;0;253;14|11;8;73;0.0;3|16;9;74;0.0;5|13;7;74;0.0;3|16;8;75;0.0;4|18;8;74;0.0;6|0;0;0;310"
m[mi++]="24.11.14 08:25:00|151;106;104;39;539;594;45|147;90;102;37;538;589;45|355;273;96;111;564;540;44|351;259;94;109;566;534;46|348;280;87;110;563;539;45|331;269;97;103;569;536;45|28;38;8;377;10|0;0;0;228;14|0;0;0;239;14|10;8;73;0.0;2|14;8;74;0.0;4|11;7;74;0.0;2|13;8;75;0.0;3|15;8;74;0.0;4|0;0;0;303"
m[mi++]="24.11.14 08:20:00|110;84;85;27;535;586;45|113;74;82;26;533;586;44|283;229;81;83;564;539;44|282;213;76;81;564;536;45|283;223;73;82;566;539;44|266;232;81;76;566;540;45|19;30;0;394;10|0;0;0;230;14|0;0;0;228;14|6;8;73;0.0;1|9;8;74;0.0;3|6;7;74;0.0;1|9;7;75;0.0;2|11;7;74;0.0;3|0;0;0;279"
m[mi++]="24.11.14 08:15:00|94;82;80;18;535;594;44|93;70;76;17;534;599;44|264;215;76;59;558;534;43|262;198;74;58;560;534;45|260;211;66;58;564;537;44|248;208;76;54;560;534;44|17;28;0;394;10|0;0;0;229;14|0;0;0;228;14|6;8;73;0.0;1|9;8;74;0.0;2|5;7;74;0.0;1|8;7;75;0.0;1|10;7;74;0.0;2|0;0;0;281"
m[mi++]="24.11.14 08:10:00|45;47;64;0;556;573;44|62;50;58;9;543;587;43|190;156;59;38;561;528;43|188;148;57;36;557;523;44|189;158;51;37;561;526;44|179;163;61;34;560;521;44|1;11;0;454;10|0;0;0;216;14|0;0;0;213;14|4;8;73;0.0;0|7;8;74;0.0;1|3;7;74;0.0;0|4;7;75;0.0;0|7;7;74;0.0;1|0;0;0;197"
m[mi++]="24.11.14 08:05:00|49;54;55;5;519;551;43|52;44;51;4;517;553;43|151;139;49;22;499;477;42|147;126;47;20;496;466;44|132;120;39;21;530;501;43|130;136;50;19;493;466;43|0;0;0;353;7|0;0;0;206;14|0;0;0;203;14|3;8;73;0.0;0|6;8;74;0.0;0|2;7;74;0.0;0|3;7;75;0.0;0|5;7;74;0.0;0|0;0;0;121"
m[mi++]="24.11.14 08:00:00|36;50;47;1;520;524;43|36;42;48;0;521;531;42|118;112;44;10;470;446;42|116;106;40;9;473;448;44|114;114;37;10;477;452;43|104;120;44;9;471;447;43|0;0;0;0;0|0;0;0;196;14|0;0;0;192;14|3;8;73;0.0;0|6;8;74;0.0;0|1;7;74;0.0;0|2;7;75;0.0;0|5;7;74;0.0;0|0;0;0;118"
m[mi++]="24.11.14 07:56:00|0;12;15;0;641;641;42|0;1;16;0;641;640;42|75;82;33;5;470;446;42|72;83;33;3;473;448;43|74;83;29;5;477;452;42|66;91;36;4;471;447;43|0;0;0;0;0|0;0;0;0;0|0;0;0;0;0|1;8;73;0.0;0|4;8;74;0.0;0|1;7;74;0.0;0|1;7;75;0.0;0|3;7;74;0.0;0|0;0;0;115"
m[mi++]="24.11.14 07:50:00|0;0;0;0;0;0;0|0;0;0;0;0;0;0|0;19;9;0;586;567;41|0;10;3;0;584;564;42|0;10;0;0;590;570;42|0;19;9;0;584;566;42|0;0;0;0;0|0;0;0;0;0|0;0;0;0;0|1;8;73;0.0;0|3;8;74;0.0;0|0;7;74;0.0;0|0;7;75;0.0;0|1;7;74;0.0;0|0;0;0;0"
// php code to parse min_day.js and insert data into db
ini_set('auto_detect_line_endings', true);
$fileArray = file("min_day.js");
$fileArray = array_values(array_filter($fileArray, "trim"));
$arrayElements = count($fileArray) -1;
$SQL = "";
$x = 0;
while($x <= $arrayElements)
{
$SQL .= "INSERT INTO mydatabase (DatumUhrzeit, Pac_1, Pdc1_1, Pdc2_1, DaySum_1, Udc1_1, Udc2_1, Temp_1, Pac_2, Pdc1_2, Pdc2_2, DaySum_2, Udc1_2, Udc2_2, Temp_2, Pac_3, Pdc1_3, Pdc2_3, DaySum_3, Udc1_3, Udc2_3, Temp_3, Pac_4, Pdc1_4, Pdc2_4, DaySum_4, Udc1_4, Udc2_4, Temp_4, Pac_5, Pdc1_5, Pdc2_5, DaySum_5, Udc1_5, Udc2_5, Temp_5, Pac_6, Pdc1_6, Pdc2_6, DaySum_6, Udc1_6, Udc2_6, Temp_6, Pac_7, Pdc1_7, DaySum_7, Udc1_7, Temp_7, Pac_8, Pdc1_8, DaySum_8, Udc1_8, Temp_8, Pac_9, Pdc1_9, DaySum_9, Udc1_9, Temp_9, SolIrr_10, TmpMod_10, TmpAmb_10, Wind_10, DaySumIrr_10, SolIrr_11, TmpMod_11, TmpAmb_11, Wind_11, DaySumIrr_11, SolIrr_12, TmpMod_12, TmpAmb_12, Wind_12, DaySumIrr_12, SolIrr_13, TmpMod_13, TmpAmb_13, Wind_13, DaySumIrr_13, SolIrr_14, TmpMod_14, TmpAmb_14, Wind_14, DaySumIrr_14, Pac_15, Pdc1_15, DaySum_15, Udc_15) VALUES ";
$string = $fileArray[$x];
$string = str_ireplace("|", ";", $string);
$data=explode(";", substr($string,9, strlen($string)-11));
$SQL .= "('" . DateTime::createFromFormat('d.m.y H:i:s', $data[0])->format('Y-m-d H:i:s') . "', ";
for ($i=1; $i<= 85; $i++){
$data2 = explode(";", $data[$i]);
$SQL .= "'" . $data[$i] . "', ";
}
$SQL .= "'$data[86]'); <br>";
$x++;
}
// connecting to db and executing mysqli_multi_query....

You can setup proper primary keys and use the insert on duplicate...update syntax:
--Say A and B together was the primary key.
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
-- Row will be inserted with A=1, B=2, C=3
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
-- Row where A=1, B=2 will be updated to C=4
Second insert would not give an error but would rather result in the value of C being updated on the already existing row.
In your case, you can update the timestamp:
INSERT INTO table (a,b,ts) VALUES (1,2,current_timestamp)
ON DUPLICATE KEY UPDATE ts=current_timestamp;
To create a primary key on a table that already exists:
alter table tablename add primary key(field1, field2);
To create a primary key on a new table:
create table newtable ( x int, y int, z varchar(50), primary key(x,y) );

Related

Selecting a specific column in a option box using Javascript

I'm using the following php code to create a select option box:
$TypeLU = mysqli_query($mysqli, "SELECT * FROM LookupList");
while($Row = mysqli_fetch_array($TypeLU)) {
$TypeOptions = $TypeOptions."<option>$Row[1] $Row[2]</option>";
}
In HTML it gets displayed as a list with 2 columns. If I select and item from the list the value would be the concat of both $Row[1] and $Row[2] which is fine for display purposes, but I want to be able to 'extract' for example only $Row[1] as being the 'bound' value which I can then use as refrence.
So in pure Javascript I want to be able to get the value of just $Row[1] for example:
var x = document.getElementById("selectbox").value;
// So x must be only $Row[1] and not the concat of $Row[1] $Row[2]
Thanks
Use:
$TypeOptions = $TypeOptions."<option value='$Row[1]'>$Row[2]</option>";
You may also want to consider escaping the variables in case they contain any < or " or other special HTML characters. See htmlspecialchars.
Full example with escaping:
$TypeLU = mysqli_query($mysqli, "SELECT * FROM LookupList");
while($Row = mysqli_fetch_array($TypeLU))
{
$Row[1] = htmlspecialchars($Row[1]);
$Row[2] = htmlspecialchars($Row[2]);
$TypeOptions = $TypeOptions."<option value='$Row[1]'>$Row[2]</option>";
}

Is there a way to link spreadsheet information into my HTML file?

I have a CSV file with 3 columns and 223 rows. The columns go from items (A column), description (B column), to type (C column). Each row contains information about each item. I want to insert data from all this spreadsheet into my HTML file. Along with this, I want to save time by not copy and pasting 233 sets of information manually.
Originally the information I needed was on a website, however, I found a suggestion to use a webscraper to get all the information I needed. I did this by using python and now I have all the information in the spreadsheet.
The template I would like to follow is shown (using spreadsheet terms). If I were manually doing this the next line of code would look this but with a B instead of an A (A1->B1)
<p class="item-title">(A1 in spreadsheet)</p>
<p class="w-itemid">ItemID: N/A</p>
<p>• (A2 in spreadsheet)</p>
<ul>
<p>Type: (A3 in spreadsheet</p>
<p>Item Pool: N/A</p>
</ul>
I want the solution to be able to link the information from my spreadsheet into my HTML without spending an immense amount of time copy and pasting 233 items. I do not mind using Javascript, jQuery, or PHP as long as it helps me finish this task.
you can try this solution:
$row = 1;
$columnArray = [];
$resultArray = [];
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle)) !== FALSE) {
if($row == 1){
$columnArray = $data;
}else{
$tmpArray = [];
for($i=0;$i<count($columnArray);$i++){
$tmpArray[$columnArray[$i]] = $data[$i];
}
$resultArray[] = $tmpArray;
}
$row++;
}
fclose($handle);
}
I'm assuming that the first row of your CSV holds the column names. At the end you will have $resultArray associative array holding all your data.
UPDATE
If your CSV does not contain field names at the first row you can use this code:
$row = 1;
$resultArray = [];
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle)) !== FALSE) {
$resultArray[] = $data;
$row++;
}
fclose($handle);
}
In this case $resultArray will hold the data as a normal nested array and you can show the info by iterating trough it:
if(count($resultArray)>0){
for($i=0;$i<count($resultArray);$i++){
echo '<p>Item: '.$resultArray[$i][0].'</p>';
echo '<p>Description: '.$resultArray[$i][1].'</p>';
echo '<p>Type: '.$resultArray[$i][2].'</p>';
}
}

Text not marking up

I am writing a page which allows a tournament director (TD) to add players to an event. When the TD goes to search for a player I want the search results to appear on keyup below the search input (without needing to refresh the page).
At this stage I have 2 php pages and 1 js page and have the live search happening, but the output is not getting marked up. I have written a piece of code to convert a PHP $_SESSION[''] array to a javascript array, but my brain is frying and I'm not sure where to go from here.
I effectively want the processed version of this code as output (presuming the SQL returns only 1 player named John Smith)
<tr></td> 123 </td><td> John Smith </td></tr>
My main php page has:
<table border='1px'>
<tr><th colspan='6'> <?php echo ($eName . " - " . $vn); ?></th></tr>
<tr><th>Player ID</th>
<th>Player Name</th>
<th>Place</th>
<th>Points</th>
<th>Cash</th>
<th>Ticket?</th></tr>
<tr><td colspan='3'>Search <input type='text' id='newsearch'</input>
</table>
<br>
<div id="newsearch-data"> </div>
<script>
var players = [];
</script>
<?php
//Makes php array into js array
foreach ($_SESSION['players'] as $id){
echo "<script> players.push('" . $id . "') </script>";
}
?>
<script src="http://code.jquery.com/jquery-1.8.0.min.js"> </script>
<script src="global.js"> </script>
js page:
$('input#newsearch').on('keyup', function(){
var newsearch = $('input#newsearch').val();
if ($.trim(newsearch)!= "") {
$.post('newsearch.php', {newsearch: newsearch}, function(data) {
$('div#newsearch-data').text(data);
});
}
});
And 2nd php page
<?php
session_start();
unset($_SESSION['players']);
if (isset($_POST['newsearch']) === true && empty($_POST['newsearch'] === false)){
require 'dbconnect.php';
$term = $_POST['newsearch'];
$terms = "%" . $term . "%";
$_SESSION['players'] = array();
$query = ("SELECT * FROM players WHERE Username LIKE '$terms'");
$run_query = mysqli_query($dbcon, $query);
while ($dbsearch = mysqli_fetch_assoc($run_query))
{
array_push ($_SESSION['players'],$dbsearch['PlayerID']);
echo "<tr><td>" . $dbsearch['PlayerID'] . "</tr></td>";
}}?
How can I output a new row on a html table rather than raw html code?
replace $('div#newsearch-data').text(data);
with $('div#newsearch-data').html(data);
$.text() escapes the input string and you should use it when you know the input is a string, and that you want to preset it "as is". If the input includes actual HTML that you want to inject into the DOM, $.html() is your friend :-)
You need .html() to render output as a HTML rather than .text(), because .text() will output the result as a raw data, then try this solution instead :
$('div#newsearch-data').html(data);
Live example - credited to #Peter Bailey for live example.
References
http://api.jquery.com/html/ - .html()
http://api.jquery.com/text/ - .text()

how to display only 4 result in each page using html & sqlite

This is me once again as I got some error in previous question in the code. So asking this question once again.
This is code from phonegap app from index html page. I don't know how to get only 4 result from database at each page when a sqlite query processed?
Also I want to add next page button. When clicking on this button next 4 result from database should come. This is code.
function querySuccess(tx, results){
var len = results.rows.length;
var output = '';
for (var i=0; i<len; i++){
output = output + '<li id="' + results.rows.item(i).id + '">' + results.rows.item(i).list_action + '</li>';
}
messageElement.html('<p>There are ' + len + ' items in your list:</p>');
listElement.html('<ul>' + output + '</ul>');
}
Dividng your solution into 3 phases
Phase 1: Using LIMIT Clause you can limit number of rows to be displayed
SELECT expressions FROM tables WHERE conditions ORDER BY expression [ ASC | DESC ] LIMIT number_rows OFFSET offset_value;
For Example:
SELECT employee_id, last_name, first_name
FROM employees
WHERE favorite_website = 'divyashah.in'
ORDER BY employee_id DESC
LIMIT 4;
Phase 2:
As the code provided by you is not efficient to explain but still... Now for your Next button
1) Fetch number of rows from your database table (mysql_num_rows).
2) Store that number in a Variable say 'a'.
3) Divide it(a) with 4 and store it in variable 'b'.
4) Use if > if(b!=0) {display next button} else {no need to display}
Phase 3:
This will fetch your first four rows i.e. from 0 to 4.
<?PHP
$fetch = mysql_query("SELECT * FROM table LIMIT 0, 4")or
die(mysql_error());
?>
Now how can you make the next page show the next 4 records?
you simply have to store the value of the starting row in a variable and pass it in the URL as a GET variable. Also have to check if there was a value already passed or not so we can set a default value in case it wasn't (zero to start from first row):
<?PHP
//check if the starting row variable was passed in URL or not
if (!isset($_GET['startrow']) or !is_numeric($_GET['startrow'])) {
//we give the value of the starting row to 0 because nothing was found in URL
$startrow = 0;
//otherwise we take the value from the URL
} else {
$startrow = (int)$_GET['startrow'];
}
?>
Now your query should have this new variable ($startrow) in the LIMIT clause
<?PHP
//this part goes after the checking of the $_GET var
$fetch = mysql_query("SELECT * FROM table LIMIT $startrow, 4")or
die(mysql_error());
?>
Now to see the next 4 records you should have a link which will add 4 to $startrow so you can view the next 4 records
<?PHP
//now this is the link..
echo 'Next';
?>

How to use explode function to delimit and display different values

I’m making a random sentence generator for my English class. I’m close but because of my limited php and javascript knowledge I need to ask for help. I’m not bad at reading the code, I just get stuck writing it.
I want to use explode to break up a string of comma seperated values. The string is a mix of English and Spanish, on the .txt file they would seperated like:
The book, El libro
The man, El hombre
The woman, La mujer
etc.
I would like to break these two values into an array and display them in separate places on my web page.
I`m going to use a random text generator script that I found, it’s working great with no problems. I just need to modify it using explode to read, separate the values into an array, and be able to display the separate values of the array.
<?php
/* File, where the random text/quotes are stored one per line */
$settings['text_from_file'] = 'quotes.txt';
/*
How to display the text?
0 = raw mode: print the text as it is, when using RanTex as an include
1 = Javascript mode: when using Javascript to display the quote
*/
$settings['display_type'] = 1;
/* Allow on-the-fly settings override? 0 = NO, 1 = YES */
$settings['allow_otf'] = 1;
// Override type?
if ($settings['allow_otf'] && isset($_GET['type']))
{
$type = intval($_GET['type']);
}
else
{
$type = $settings['display_type'];
}
// Get a list of all text options
if ($settings['text_from_file'])
{
$settings['quotes'] = file($settings['text_from_file']);
}
// If we have any text choose a random one, otherwise show 'No text to choose from'
if (count($settings['quotes']))
{
$txt = $settings['quotes'][array_rand($settings['quotes'])];
}
else
{
$txt = 'No text to choose from';
}
// Output the image according to the selected type
if ($type)
{
// New lines will break Javascript, remove any and replace them with <br />
$txt = nl2br(trim($txt));
$txt = str_replace(array("\n","\r"),'',$txt);
// Set the correct MIME type
header("Content-type: text/javascript");
// Print the Javascript code
echo 'document.write(\''.addslashes($txt).'\')';
}
else
{
echo $txt;
}
?>
The script that displays the result:
<script type="text/javascript" src="rantex.php?type=1"></script>
Can someone please help me modify the rantex.php file so that I can use explode to separate the different comma separated values, and use a different script to call them in different places on my web page?
Thank you, and please excuse my noobness.
The following seems unnecessary, since file() will have already removed new line characters:
// New lines will break Javascript, remove any and replace them with <br />
$txt = nl2br(trim($txt));
$txt = str_replace(array("\n","\r"),'',$txt);
To break your line, you may instead use:
list($english, $spanish) = explode(', ', trim($txt));
It seems you are trying to use PHP to serve a static page with some random sentences, right? So why not use PHP to serve valid JSON, and handle to display logic on the client?
Heres a quick implementation.
// Get the data from the text file
$source = file_get_contents('./quotes.txt', true);
// Build an array (break on every line break)
$sentences = explode("\n", $source);
// Filter out empty values (if there is any)
$filtered = array_filter($sentences, function($item) {
return $item !== "";
});
// Build a hashmap of the array
$pairs = array_map(function($item) {
return ['sentence' => $item];
}, $filtered);
// Encode the hashmap to JSON, and return this to the client.
$json = json_encode($pairs);
Now you can let the client handle the rest, with some basic JavaScript.
// Return a random sentence from your list.
var random = sentences[Math.floor(Math.random() * sentences.length)];
// Finally display it
random.sentence
[edit]
You can get the JSON data to client in many ways, but if you don't want to use something like Ajax, you could simply just dump the contents on your webpage, then use JavaScript to update the random sentence, from the global window object.
// Inside your php page
<p>English: <span id="english"></span></p>
<p>Spanish: <span id="spanish"></span></p>
<script>
var sentences = <?= json_encode($pairs); ?>;
var random = sentences[Math.floor(Math.random() * sentences.length)];
var elspa = document.getElementById('spanish');
var eleng = document.getElementById('english');
elspa.innerText = random.sentence.split(',')[1];
eleng.innerText = random.sentence.split(',')[0];
</script>
Ok, so I have this figured out, I take 0 credit because I paid someone to do it. Special thanks to #stormpat for sending me in the right direction, if not for him I wouldn't have looked at this from a JSON point of view.
The .PHP file is like so:
<?php
$f_contents = file('quotes.txt');
$line = trim($f_contents[rand(0, count($f_contents) - 1)]);
$data = explode(',', $line);
$data['eng'] = $data[0];
$data['esp'] = $data[1];
echo json_encode($data);
?>
On the .HTML page in the header:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
(function ($) {
$(function()
{
function load_random_data() {
$.get('random_line.php', function(data) {
var data = $.parseJSON(data);
$('#random_english').text(data.eng);
$('#random_spanish').text(data.esp);
});
}
load_random_data();
$('#get_random').click(function(e){
e.preventDefault();
load_random_data();
});
});
})(jQuery);
</script>
This splits the different variables into classes, so to call them into my html page I call them by their class, for instance I wanted to drop the variable into a table cell so I gave the individual td cell a class:
<td id="random_spanish"></td>
<td id="random_english"></td>
Plus as a bonus the coder threw in a nifty button to refresh the json classes:
<input type="button" value="Get random" id="get_random" />
So now I don`t have to have my students refresh the whole web page, they can just hit the button and refresh the random variables.
Thanks again everyone!

Categories