-
Posts
24,566 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
You have a foreach () loop in your code which process the array $files You need to put that sort code before that loop
-
Then, as @benanamen said, truncate the tables. That will remove all the data and reset the auto-inc to 1. It's also far faster the a delete query. And to answer your question... SQL file (atest.sql) TRUNCATE TABLE player TRUNCATE TABLE roster PHP foreach (file('atest.sql') as $sql) { $conn->query($sql); }
-
It doesn't look like the sort of query you would ever need to run more than once, so why store it at all?
-
If the files data is coming from DB query, do the sort in the query, otherwise use a custom sort function. uasort($files, function($a, $b) { return $b['time'] <=> $a['time]; } ); (For ascending sort, switch a and b in the return)
-
Unfortunately (for you) both those are things you will have to check for yourself.
-
I can't be definite because there does not not appear to be an end } to your while loop, but could it be because you are sending the mail inside a loop? I'm guessing, therefore, that isn't your actual code, so not much we can do.
-
generate the calendar. make an AJAX request which returns a JSON encoded array of your events from the database. process the array, adding the events and colours to the calendar
-
Allowed memory exhausted...tried to allocate
Barand replied to ScorpioTiger's topic in PHP Installation and Configuration
Could be your exit condition isn't being met sometimes. In the above, call recurse(30) instead of recurse(0) so that the level is never 20. -
Allowed memory exhausted...tried to allocate
Barand replied to ScorpioTiger's topic in PHP Installation and Configuration
It can happen very quickly... recurse(0); function recurse($level) { if ($level == 20) return; // then comment out this line and try again recurse($level+1); } -
Try echo nl2br(htmlentities(file_get_contents('FILE'))); or <div style='white-space: pre; font-family: monospace;'> <?=htmlentities(file_get_contents('FILE'))?> </div>
-
Allowed memory exhausted...tried to allocate
Barand replied to ScorpioTiger's topic in PHP Installation and Configuration
The most common cause I've experienced is a recursive process with no working exit strategy - infinite recursion. -
Best way to traverse an array is with foreach(). $fileArr = []; $files = glob("$masterdir/*.jpg"); foreach ($files as $fname) { $t = filemtime($fname); $fileArr{$fname} = $t; } arsort($fileArr); foreach (array_slice($fileArr, 0, 7) as $thisName => $thisTime) { // iterate through first 7 files echo "$thisName — " . date("d M y", $thisTime) . '<br>'; }
- 1 reply
-
- 1
-
forget the for() loop. while ($row = $result->fetch_row() ) { echo $row[0]; . . . }
-
It's maybe due to you having "InSped" in your code intead of "inSped". If not, can you repost the json data returned from your cUrl request. The original data that you posted had no mention of inSped
-
$dataPoints[] = array( $user->User, $user->Amount ); becomes if ($user->userObject->inSped) { $dataPoints[] = array( $user->User, $user->Amount ); }
-
Yes - only add to the $dataPoints array if the entry matches your criteria.
-
With Ajax, opening a PHP function on the same page without JQuery.
Barand replied to JoshEir's topic in PHP Coding Help
There's an example here of processing an ajax request in the same page -
Please define the following terms my url there
-
I tried Googling charts.js to have a look at their API documentation. What I found was nothing like the formats that you appear to be using. I did manage to get a chart produced using google.visualization api (if that helps) <?php $getdata = ' { "result": [ { "ID": 1, "Users": [ { "UserObject": { "UserName": "User1", "-": { "ID": 1 }, "0": "0" }, "User": "User1", "Amount": 10 }, { "UserObject": { "UserName": "User2", "-": { "ID": 1 }, "0": "0" }, "User": "User2", "Amount": 20 }, { "UserObject": { "UserName": "User3", "-": { "ID": 1 }, "0": "0" }, "User": "User3", "Amount": 15 } ], "Reached": false, "IsActive": true } ], "error": false, "version": 1 } '; $data = json_decode($getdata); $users = $data->result[0]->Users; $dataPoints = array( ['User', 'Amount'] ); foreach($users as $user): $dataPoints[] = array( $user->User, $user->Amount ); endforeach; $jdata = json_encode($dataPoints); ?> <!DOCTYPE HTML> <html> <head> <title>Test</title> <meta http-equiv="content-language" content="en"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script> <script type='text/javascript'> $().ready(function() { google.charts.load('current', {'packages':['corechart']}); google.charts.setOnLoadCallback(drawChart); // Draw the chart and set the chart values function drawChart() { var dataArray = JSON.parse($("#chart-values").val()) var data = google.visualization.arrayToDataTable(dataArray); // Optional; add a title and set the width and height of the chart var options = { 'title':'User Amounts', 'width':550, 'height':400, 'slices': { 0: {'color':'#2ecc71' }, 1: {'color':'#3498db' }, 2: {'color':'#95a5a6' }, 3: {'color':'#9b59b6' }, 4: {'color':'#f1c40f' }, 5: {'color':'#e74c3c' }, 6: {'color':'#34495e' } } }; // Display the chart inside the <div> element with id="piechart" var chart = new google.visualization.PieChart(document.getElementById('myChart')); chart.draw(data, options); } }) </script> <style> .container { width: 80%; margin: 15px auto; } </style> </head> <body> <input type='hidden' id='chart-values' value='<?=$jdata?>'> <div class="container"> <h2>Pie Chart Demo</h2> <div id="myChart"></div> </div> </body> </html>
-
Trying to get data from form with Repeatable fields into MySQL...
Barand replied to Jim R's topic in MySQL Help
One option would be to take your approach - totally ignore the book then every day get on the phone to you saying "Hey, get your arse down to the court and coach these kids for me". And those needs would be what? Define "entirely". If the combination of (firstName, LastName) is defined as unique then it will only allow one record. If you have "Jon Doe" on the team it won't allow another "John Doe" to be added. With IGNORE it ignores the second John Doe and continues adding the rest of the players' records. Without the ignore the query would fail with an error. -
Trying to get data from form with Repeatable fields into MySQL...
Barand replied to Jim R's topic in MySQL Help
If you use the ignore modifier, ignorable errors that occur while executing the insert statement are ignored. For example, without IGNORE, a row that duplicates an existing unique index or primary key value in the table causes a duplicate-key error and the statement is aborted. With IGNORE, the row is discarded and no error occurs. -
Javascript runs on the client, PHP on the server, so you can't incorporate PHP code inside JS. What you can do is submit an AJAX request from JS to the server, process it with PHP and send back the results as a response. In JQuery, look at $.ajax() $.get() $.post() Here's a very basic example <?php if (isset($_GET['ajax'])) { // I like to tell my script it's reciving AJAX $x = $_GET['x'] ?? 0; exit("$x squared is " . ($x**2)); // when process an AJAX request, anything that would normally be sent to the screen } // is sent back in a response message // rest of php code here ?> <html> <head> <title>Example</title> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script> <script type='text/javascript'> $().ready( function() { $("#btnSub").click( function() { $.get ( "", // target script is "self" {"ajax":1, "x":$("#x").val() }, // data to send function(resp) { // process response $("#result").html(resp); }, "TEXT" // response type ) }) }) </script> </head> <body> Input a number <input type='text' id='x' value='0'> <br> <button id='btnSub'>Get Square</button> <hr> <div id='result'></div> </body> </html>
- 1 reply
-
- 1
-
Trying to get data from form with Repeatable fields into MySQL...
Barand replied to Jim R's topic in MySQL Help
Just like it says in the manual. The query is a multiple insert like this, which insert 3 records with a single insert query, and is many times faster than several single record inserts. INSERT INTO tableX (colA, colB) VALUES (1, 'A'), (2, 'B'), (3, 'C'); Because it's a prepared query it looks a little different though - the $holders would be the array [ "(?,?)","(?,?)", "(?,?)" ] and the $vals contain [ 1, 'A', 2, 'B', 3, 'C' ] (ie a value for each of the placeholders) -
Trying to get data from form with Repeatable fields into MySQL...
Barand replied to Jim R's topic in MySQL Help
I decided to get it working using your method and, as a comparison, created a separate pair of tables (player2, roster2) into which I add the data using my method. I timed the processing when adding 2, 10 and 20 records to the roster. Data results were identical in both cases. Processing times (as expected) were different. The code... <?php if ($_SERVER['REQUEST_METHOD']=='POST') { $schoolID = $_POST['school']; $varsity = $_POST['varsity']; $season = $_POST['season']; ## ## Jimr mysqli code ## $jimt1 = microtime(1); $player = $con->prepare("INSERT INTO player (schoolID,nameFirst,nameLast,feet,inches,grade,position) VALUES (?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE feet = VALUES(feet), inches = VALUES(inches), grade = VALUES(grade), position = VALUES(position) "); $player->bind_param('issiiis', $schoolID,$fname,$lname,$feet,$inches,$grade,$position); $pid = $con->prepare("SELECT id FROM player WHERE nameFirst = ? AND nameLast = ? AND schoolID = ?"); $pid->bind_param('ssi',$fname,$lname,$schoolID); $roster = $con->prepare("INSERT INTO roster (schoolID, playerID, uniform, varsity, season) VALUES (?,?,?,?,?) "); $roster->bind_param('sissi', $schoolID, $playerID, $uniform, $varsity, $season); foreach ($_POST['uniform'] as $k => $uniform) { $fname = $_POST['nameFirst'][$k]; $lname = $_POST['nameLast'][$k]; $feet = $_POST['feet'][$k]; $inches = $_POST['inches'][$k]; $grade = $_POST['grade'][$k]; $position = $_POST['position'][$k]; $player->execute(); $pid->execute(); $pid->bind_result($playerID); $pid->fetch(); $pid->reset(); $roster->execute(); } $jimt2 = microtime(1); printf("\nJIMR time to process: %0.3f seconds\n\n", $jimt2 - $jimt1); ## ## My PDO method ## $t1 = microtime(1); $db->exec("CREATE TEMPORARY TABLE temp_csv ( id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, schoolid int(11) DEFAULT NULL, varsity varchar(10) DEFAULT NULL, season int, uniform varchar(45) DEFAULT NULL, nameFirst varchar(45) DEFAULT NULL, nameLast varchar(45) DEFAULT NULL, feet tinyint(4) DEFAULT NULL, inches tinyint(4) DEFAULT NULL, grade int(11) DEFAULT NULL, position varchar(15) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 "); $sql = "INSERT INTO temp_csv (schoolid, varsity, season, uniform, nameFirst, nameLast, feet, inches, grade, position) VALUES \n"; $holders = $vals = []; foreach ($_POST['uniform'] as $k => $uniform) { $fname = $_POST['nameFirst'][$k]; $lname = $_POST['nameLast'][$k]; $feet = $_POST['feet'][$k]; $inches = $_POST['inches'][$k]; $grade = $_POST['grade'][$k]; $position = $_POST['position'][$k]; $holders[] = "(?,?,?,?,?,?,?,?,?,?)"; array_push($vals, $schoolID, $varsity, $season, $uniform, $fname, $lname, $feet, $inches, $grade, $position); } $stmtc = $db->prepare($sql . join(',', $holders)); $stmtc->execute($vals); $db->exec("INSERT IGNORE INTO player2 (schoolID,nameFirst,nameLast,feet,inches,grade,position) SELECT schoolid , nameFirst , nameLast , feet , inches , grade , position FROM temp_csv "); $db->exec("INSERT IGNORE INTO roster2 (schoolID, playerID, uniform, varsity, season) SELECT t.schoolid , p.id , t.uniform , t.varsity , t.season FROM temp_csv t JOIN player2 p ON t.nameFirst = p.nameFirst AND t.nameLast = p.nameLast; "); $t2 = microtime(1); printf("\nMy time to process: %0.3f seconds\n\n", $t2 - $t1); } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="content-language" content="en"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.1/css/all.css"> <title>Example</title> <meta name="creation-date" content="11/23/2020"> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script> <script type='text/javascript'> $().ready( function() { $("#add-player").click( function(e) { e.preventDefault() $("#myRepeatingFields").append('<div class="entry input-group col-xs-3">' + '<input class="form-control" name="uniform[]" type="text" placeholder="Uni #" /> ' + '<input class="form-control" name="nameFirst[]" type="text" placeholder="First Name" /> ' + '<input class="form-control" name="nameLast[]" type="text" placeholder="Last Name" /> ' + '<input class="form-control" name="feet[]" type="text" placeholder="Feet" /> ' + '<input class="form-control" name="inches[]" type="text" placeholder="Inches" /> ' + '<input class="form-control" name="grade[]" type="text" placeholder="Grade" /> ' + '<input class="form-control" name="position[]" type="text" placeholder="Position" />' + '</div>'); }) }) </script> </head> <body> <form action="" method="post"> <fieldset> <legend>Roster</legend> School ID <input type="text" name="school"><br> Varsity <input type="text" name="varsity"><br> Season <input type="text" name="season"><br> </fieldset> <fieldset><br> <legend>Players</legend> <div id="myRepeatingFields"> <div class="entry input-group col-xs-3"> <input class="form-control" name="uniform[]" type="text" placeholder="Uni #" /> <input class="form-control" name="nameFirst[]" type="text" placeholder="First Name" /> <input class="form-control" name="nameLast[]" type="text" placeholder="Last Name" /> <input class="form-control" name="feet[]" type="text" placeholder="Feet" /> <input class="form-control" name="inches[]" type="text" placeholder="Inches" /> <input class="form-control" name="grade[]" type="text" placeholder="Grade" /> <input class="form-control" name="position[]" type="text" placeholder="Position" /> </div> </div> </fieldset><br> <button id='add-player' title='add new player entry'><i class='fas fa-plus-circle'></i></button> <input type="submit" value="Send Roster" name="submit"> </form> </body> </html> -
I use MySQL Workbench - it's free. I've also been known to us a And there's an SQL tutorial in my sig. which may help.