-
Posts
24,604 -
Joined
-
Last visited
-
Days Won
830
Everything posted by Barand
-
Try these links https://www.php.net/substr https://www.php.net/strlen https://www.php.net/str_replace https://www.php.net/isset https://www.php.net/intval
-
You want us to help you to pass off a piece of code that you found as your work so you can submit it as your homework assignment?
-
That was not stated in your question. Deal with it. I've given you the basics - work on it. If you know which pets are in there, you stand a chance. If you don't, you are going to need another way to identify the text portions you want.
-
No loop required $text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus cat lectus malesuada libero, sit amet commodo magna eros quis urna. "; $srch = 'cat'; $p1 = strpos($text, $srch, 0); // find position of 'cat' if ($p1===false) die("'$srch' not found"); echo "$srch found at pos $p1<br>"; $p2 = strpos($text, '.', $p1); // find position of '.' following 'cat' echo substr($text, $p1, $p2-$p1+1); // get text beteen those points.
-
If you can use fixed value range offsets for gmp and ewt (in this example they are gmp ± 0.4, ewt ± 9 ) then extra tables not required mysql> SELECT gpm, ewt -> FROM uniluxmodel -> WHERE gpm BETWEEN 1.75 - 0.4 AND 1.75 + 0.4 -> AND ewt BETWEEN 35 - 9 AND 35 + 9; +------+------+ | gpm | ewt | +------+------+ | 1.50 | 30 | | 1.50 | 40 | | 2.00 | 30 | | 2.00 | 40 | +------+------+
-
As you have told us nothing about the file or the portion you want from it, how do you expect us to help you? Why is your data stored in a php file, especially as you apparently need to use it elsewhere?
-
Plan D, staying with ranges but setting the lo/hi of range to extend from the value below to the value above gpm_range ewt_range; +------+--------+--------+ +-----+--------+--------+ | gpm | gpm_lo | gpm_hi | | ewt | ewt_lo | ewt_hi | +------+--------+--------+ +-----+--------+--------+ | 1.00 | 0.00 | 1.25 | | 30 | 0 | 40 | | 1.25 | 1.00 | 1.50 | | 40 | 30 | 50 | | 1.50 | 1.25 | 2.00 | | 50 | 40 | 60 | | 2.00 | 1.50 | 3.00 | | 60 | 50 | 70 | +------+--------+--------+ | 70 | 60 | 80 | | 80 | 70 | 90 | +-----+--------+--------+ then SELECT DISTINCT gpm.gpm , ewt.ewt FROM ( SELECT u.gpm FROM uniluxmodel u JOIN ( SELECT gpm FROM gpm_range WHERE 1.75 BETWEEN gpm_lo AND gpm_hi ) g USING (gpm) ) gpm JOIN ( SELECT u.ewt FROM uniluxmodel u JOIN ( SELECT ewt FROM ewt_range WHERE 35 BETWEEN ewt_lo AND ewt_hi ) e USING (ewt) ) ewt; +------+------+ | gpm | ewt | +------+------+ | 1.50 | 30 | | 2.00 | 30 | | 1.50 | 40 | | 2.00 | 40 | +------+------+
-
Perhaps if you referenced the manual occasionally you would know exactly what it returns and why it's the wrong function. readfile() file_get_contents() file()
-
-
If the user enters a gpm value between 1.37 and 1.74, then the record with a gpm of 1.5 is selected. If the user didn't enter 1.5 it is flagged with an * as not being an exact match. gpm_range +------+--------+--------+ | gpm | gpm_lo | gpm_hi | +------+--------+--------+ | 1.00 | 0.01 | 1.11 | | 1.25 | 1.12 | 1.36 | | 1.50 | 1.37 | 1.74 | | 2.00 | 1.75 | 2.99 | +------+--------+--------+ Same with other values, the record with closest matches being returned.
-
No, you really don't. That is path to disaster in a relational database where entities are related by their ids. As a rule, don't change, delete or re-use ids.
-
It looks like your $AFC/$NFC array keys are not 0-based, as they need to be, so $AFC[0] and $NFC[0] do not exist. (It should be throwing out messages to that effect - are you reporting your errors?) Try $AFC = array_values($AFC); $NFC = array_values($NFC); just before the loop to update the matrix table.
-
I used your "create table" this time then ran exactly the same code again $AFC = [3,6,5,9,7,8,4,2,0,1]; $NFC = [3,8,5,7,6,4,9,1,2,0]; $query = $db->prepare("UPDATE VNSB21_squares_matrix SET nfc = ? , afc = ? WHERE square = ? "); $query->bind_param('iii', $n, $a, $i); for ($i=0; $i<100; $i++) { $a = $AFC[intdiv($i, 10)]; $n = $NFC[$i%10]; $query->execute(); } mysql> select * from vnsb21_squares_matrix; +--------+------+------+------+ +--------+------+------+------+ +--------+------+------+------+ | SQUARE | NAME | NFC | AFC | | SQUARE | NAME | NFC | AFC | | SQUARE | NAME | NFC | AFC | +--------+------+------+------+ +--------+------+------+------+ +--------+------+------+------+ | 00 | A | 3 | 3 | | 34 | AI | 6 | 9 | | 68 | BQ | 2 | 4 | | 01 | B | 8 | 3 | | 35 | AJ | 4 | 9 | | 69 | BR | 0 | 4 | | 02 | C | 5 | 3 | | 36 | AK | 9 | 9 | | 70 | BS | 3 | 2 | | 03 | D | 7 | 3 | | 37 | AL | 1 | 9 | | 71 | BT | 8 | 2 | | 04 | E | 6 | 3 | | 38 | AM | 2 | 9 | | 72 | BU | 5 | 2 | | 05 | F | 4 | 3 | | 39 | AN | 0 | 9 | | 73 | BV | 7 | 2 | | 06 | G | 9 | 3 | | 40 | AO | 3 | 7 | | 74 | BW | 6 | 2 | | 07 | H | 1 | 3 | | 41 | AP | 8 | 7 | | 75 | BX | 4 | 2 | | 08 | I | 2 | 3 | | 42 | AQ | 5 | 7 | | 76 | BY | 9 | 2 | | 09 | J | 0 | 3 | | 43 | AR | 7 | 7 | | 77 | BZ | 1 | 2 | | 10 | K | 3 | 6 | | 44 | AS | 6 | 7 | | 78 | CA | 2 | 2 | | 11 | L | 8 | 6 | | 45 | AT | 4 | 7 | | 79 | CB | 0 | 2 | | 12 | M | 5 | 6 | | 46 | AU | 9 | 7 | | 80 | CC | 3 | 0 | | 13 | N | 7 | 6 | | 47 | AV | 1 | 7 | | 81 | CD | 8 | 0 | | 14 | O | 6 | 6 | | 48 | AW | 2 | 7 | | 82 | CE | 5 | 0 | | 15 | P | 4 | 6 | | 49 | AX | 0 | 7 | | 83 | CF | 7 | 0 | | 16 | Q | 9 | 6 | | 50 | AY | 3 | 8 | | 84 | CG | 6 | 0 | | 17 | R | 1 | 6 | | 51 | AZ | 8 | 8 | | 85 | CH | 4 | 0 | | 18 | S | 2 | 6 | | 52 | BA | 5 | 8 | | 86 | CI | 9 | 0 | | 19 | T | 0 | 6 | | 53 | BB | 7 | 8 | | 87 | CJ | 1 | 0 | | 20 | U | 3 | 5 | | 54 | BC | 6 | 8 | | 88 | CK | 2 | 0 | | 21 | V | 8 | 5 | | 55 | BD | 4 | 8 | | 89 | CL | 0 | 0 | | 22 | W | 5 | 5 | | 56 | BE | 9 | 8 | | 90 | CM | 3 | 1 | | 23 | X | 7 | 5 | | 57 | BF | 1 | 8 | | 91 | CN | 8 | 1 | | 24 | Y | 6 | 5 | | 58 | BG | 2 | 8 | | 92 | CO | 5 | 1 | | 25 | Z | 4 | 5 | | 59 | BH | 0 | 8 | | 93 | CP | 7 | 1 | | 26 | AA | 9 | 5 | | 60 | BI | 3 | 4 | | 94 | CQ | 6 | 1 | | 27 | AB | 1 | 5 | | 61 | BJ | 8 | 4 | | 95 | CR | 4 | 1 | | 28 | AC | 2 | 5 | | 62 | BK | 5 | 4 | | 96 | CS | 9 | 1 | | 29 | AD | 0 | 5 | | 63 | BL | 7 | 4 | | 97 | CT | 1 | 1 | | 30 | AE | 3 | 9 | | 64 | BM | 6 | 4 | | 98 | CU | 2 | 1 | | 31 | AF | 8 | 9 | | 65 | BN | 4 | 4 | | 99 | CV | 0 | 1 | | 32 | AG | 5 | 9 | | 66 | BO | 9 | 4 | +--------+------+------+------+ | 33 | AH | 7 | 9 | | 67 | BP | 1 | 4 | +--------+------+------+------+ +--------+------+------+------+ and I still couldn't get your messed up results. However, just in case your local set up isn't as tolerant as mine when it comes to matching "00" with 0, then try $AFC = [3,6,5,9,7,8,4,2,0,1]; $NFC = [3,8,5,7,6,4,9,1,2,0]; $query = $db->prepare("UPDATE VNSB21_squares_matrix SET nfc = ? , afc = ? WHERE square = ? "); $query->bind_param('iis', $n, $a, $s); for ($i=0; $i<100; $i++) { $a = $AFC[intdiv($i, 10)]; $n = $NFC[$i%10]; $s = sprintf('%02d', $i); $query->execute(); } PS - What is in your $AFC and $NFC arrays?
-
I don't know what your matrix table looks like but when I create one CREATE TABLE `squares_matrix` ( `square` int(11) NOT NULL, `name` varchar(20) DEFAULT NULL, `nfc` int(11) DEFAULT NULL, `afc` int(11) DEFAULT NULL, PRIMARY KEY (`square`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ... generate 100 rows, then run that same code I get mysql> select * from squares_matrix; +--------+------+------+------+ +--------+------+------+------+ +--------+------+------+------+ | square | name | nfc | afc | | square | name | nfc | afc | | square | name | nfc | afc | +--------+------+------+------+ +--------+------+------+------+ +--------+------+------+------+ | 0 | A | 3 | 3 | | 35 | AJ | 4 | 9 | | 69 | BR | 0 | 4 | | 1 | B | 8 | 3 | | 36 | AK | 9 | 9 | | 70 | BS | 3 | 2 | | 2 | C | 5 | 3 | | 37 | AL | 1 | 9 | | 71 | BT | 8 | 2 | | 3 | D | 7 | 3 | | 38 | AM | 2 | 9 | | 72 | BU | 5 | 2 | | 4 | E | 6 | 3 | | 39 | AN | 0 | 9 | | 73 | BV | 7 | 2 | | 5 | F | 4 | 3 | | 40 | AO | 3 | 7 | | 74 | BW | 6 | 2 | | 6 | G | 9 | 3 | | 41 | AP | 8 | 7 | | 75 | BX | 4 | 2 | | 7 | H | 1 | 3 | | 42 | AQ | 5 | 7 | | 76 | BY | 9 | 2 | | 8 | I | 2 | 3 | | 43 | AR | 7 | 7 | | 77 | BZ | 1 | 2 | | 9 | J | 0 | 3 | | 44 | AS | 6 | 7 | | 78 | CA | 2 | 2 | | 10 | K | 3 | 6 | | 45 | AT | 4 | 7 | | 79 | CB | 0 | 2 | | 11 | L | 8 | 6 | | 46 | AU | 9 | 7 | | 80 | CC | 3 | 0 | | 12 | M | 5 | 6 | | 47 | AV | 1 | 7 | | 81 | CD | 8 | 0 | | 13 | N | 7 | 6 | | 48 | AW | 2 | 7 | | 82 | CE | 5 | 0 | | 14 | O | 6 | 6 | | 49 | AX | 0 | 7 | | 83 | CF | 7 | 0 | | 15 | P | 4 | 6 | | 50 | AY | 3 | 8 | | 84 | CG | 6 | 0 | | 16 | Q | 9 | 6 | | 51 | AZ | 8 | 8 | | 85 | CH | 4 | 0 | | 17 | R | 1 | 6 | | 52 | BA | 5 | 8 | | 86 | CI | 9 | 0 | | 18 | S | 2 | 6 | | 53 | BB | 7 | 8 | | 87 | CJ | 1 | 0 | | 19 | T | 0 | 6 | | 54 | BC | 6 | 8 | | 88 | CK | 2 | 0 | | 20 | U | 3 | 5 | | 55 | BD | 4 | 8 | | 89 | CL | 0 | 0 | | 21 | V | 8 | 5 | | 56 | BE | 9 | 8 | | 90 | CM | 3 | 1 | | 22 | W | 5 | 5 | | 57 | BF | 1 | 8 | | 91 | CN | 8 | 1 | | 23 | X | 7 | 5 | | 58 | BG | 2 | 8 | | 92 | CO | 5 | 1 | | 24 | Y | 6 | 5 | | 59 | BH | 0 | 8 | | 93 | CP | 7 | 1 | | 25 | Z | 4 | 5 | | 60 | BI | 3 | 4 | | 94 | CQ | 6 | 1 | | 26 | AA | 9 | 5 | | 61 | BJ | 8 | 4 | | 95 | CR | 4 | 1 | | 27 | AB | 1 | 5 | | 62 | BK | 5 | 4 | | 96 | CS | 9 | 1 | | 28 | AC | 2 | 5 | | 63 | BL | 7 | 4 | | 97 | CT | 1 | 1 | | 29 | AD | 0 | 5 | | 64 | BM | 6 | 4 | | 98 | CU | 2 | 1 | | 30 | AE | 3 | 9 | | 65 | BN | 4 | 4 | | 99 | CV | 0 | 1 | | 31 | AF | 8 | 9 | | 66 | BO | 9 | 4 | +--------+------+------+------+ | 32 | AG | 5 | 9 | | 67 | BP | 1 | 4 | | 33 | AH | 7 | 9 | | 68 | BQ | 2 | 4 | | 34 | AI | 6 | 9 | +--------+------+------+------+ +--------+------+------+------+
-
When you do an UPDATE query, the changes are applied to all records in the table unless you specify which record(s) is to be updated. I am guessing that the last pair of numbers in your two arrays are NFC 4, AFC 0 Your query needs a WHERE clause adding $query = $connection->prepare("UPDATE VNSB21_squares_matrix SET NFC = ? , AFC = ? WHERE square = ? "); $query->bind_param('iii', $n, $a, $i); for ($i=0; $i<100; $i++) { $a = $AFC[intdiv($i, 10)]; $n = $NFC[$i%10]; $query->execute(); }
-
Plan C - add a couple of "range" tables gpm_range ewt_range +------+--------+--------+ +-----+--------+--------+ | gpm | gpm_lo | gpm_hi | | ewt | ewt_lo | ewt_hi | +------+--------+--------+ +-----+--------+--------+ | 1.00 | 0.01 | 1.11 | | 30 | 1 | 34 | | 1.25 | 1.12 | 1.36 | | 40 | 35 | 44 | | 1.50 | 1.37 | 1.74 | | 50 | 45 | 54 | | 2.00 | 1.75 | 2.99 | | 60 | 55 | 64 | +------+--------+--------+ | 70 | 65 | 74 | | 80 | 75 | 85 | +-----+--------+--------+ code $sql = "SELECT CASE u.gpm WHEN ? THEN u.gpm ELSE concat(u.gpm, '*') END as gpm , CASE u.ewt WHEN ? THEN u.ewt ELSE concat(u.ewt, '*') END as ewt FROM uniluxmodel u JOIN gpm_range g USING (gpm) JOIN ewt_range e USING (ewt) WHERE ? BETWEEN g.gpm_lo AND g.gpm_hi AND ? BETWEEN e.ewt_lo AND e.ewt_hi "; $stmt = $db->prepare($sql); $gpm = 1.2; $ewt = 40; $stmt->execute( [ $gpm, $ewt, $gpm, $ewt ] ); // +-------+-----+ // | gpm | ewt | // +-------+-----+ // | 1.25* | 40 | * denotes not exact match // +-------+-----+ $gpm = 1.5; $ewt = 45; $stmt->execute( [ $gpm, $ewt, $gpm, $ewt ] ); // +------+-----+ // | gpm | ewt | // +------+-----+ // | 1.50 | 50* | // +------+-----+ $gpm = 2.0; $ewt = 60; $stmt->execute( [ $gpm, $ewt, $gpm, $ewt ] ); // +------+-----+ // | gpm | ewt | // +------+-----+ // | 2.00 | 60 | // +------+-----+ $gpm = 2.2; $ewt = 90; $stmt->execute( [ $gpm, $ewt, $gpm, $ewt ] ); // NO RECORDS FOUND
-
Here's a way... $sql = "SELECT DISTINCT gpm, ewt FROM ( SELECT id, gpm FROM uniluxmodel WHERE gpm = ? ) gpm1 LEFT JOIN ( SELECT id, ewt FROM uniluxmodel WHERE ewt = ? ) ewt1 USING (id) UNION SELECT gpm, ewt FROM ( SELECT id, ewt FROM uniluxmodel WHERE ewt = ? ) ewt2 LEFT JOIN ( SELECT id, gpm FROM uniluxmodel WHERE gpm = ? ) gpm2 USING (id) ORDER BY gpm IS NULL, ewt IS NULL LIMIT 1 "; $stmt = $db->prepare($sql); $gpm = 1.2; $ewt = 40; $stmt->execute( [ $gpm, $ewt, $ewt, $gpm ] ); // +-----+-----+ // | gpm | ewt | // +-----+-----+ // | | 40 | // +-----+-----+ $gpm = 1.5; $ewt = 45; $stmt->execute( [ $gpm, $ewt, $ewt, $gpm ] ); // +------+-----+ // | gpm | ewt | // +------+-----+ // | 1.50 | | // +------+-----+ $gpm = 2.0; $ewt = 60; $stmt->execute( [ $gpm, $ewt, $ewt, $gpm ] ); // +------+-----+ // | gpm | ewt | // +------+-----+ // | 2.00 | 60 | // +------+-----+ $gpm = 2.2; $ewt = 61; $stmt->execute( [ $gpm, $ewt, $ewt, $gpm ] ); // NO RECORDS FOUND
-
Avoid using "global". Instead, pass the $employees array as an argument to functions. EG echo getTotalSalaries($employees); function getTotalSalries($emps) { return array_sum(array_column($emps, 'salary')); }
-
OK - hard-coding your $afc and $nfc arrays instead of randomly generating them $afc = [6,0,9,4,2,3,8,5,7,1]; $nfc = [2,9,1,4,8,7,0,3,5,6]; //$nfc = range(0,9); //$afc = range(0,9); //shuffle($nfc); //shuffle($afc); echo '<pre>Numbers table<br>'; echo "| NFC | AFC |<br>"; for ($i=0; $i<10; $i++) { printf( '| %d | %d |<br>', $nfc[$i], $afc[$i]); } echo '<hr>Matrix table<br>'; for ($i=0; $i<100; $i++) { $a = $afc[intdiv($i, 10)]; $n = $nfc[$i%10]; printf('| %02d | %d, %d<br>', $i, $n, $a); } gives this, Numbers table | NFC | AFC | | 2 | 6 | | 9 | 0 | | 1 | 9 | | 4 | 4 | | 8 | 2 | | 7 | 3 | | 0 | 8 | | 3 | 5 | | 5 | 7 | | 6 | 1 | Matrix table | 00 | 2, 6 | 01 | 9, 6 | 02 | 1, 6 | 03 | 4, 6 | 04 | 8, 6 | 05 | 7, 6 | 06 | 0, 6 | 07 | 3, 6 | 08 | 5, 6 | 09 | 6, 6 | 10 | 2, 0 | 11 | 9, 0 | 12 | 1, 0 | 13 | 4, 0 | 14 | 8, 0 | 15 | 7, 0 | 16 | 0, 0 | 17 | 3, 0 | 18 | 5, 0 | 19 | 6, 0 | 20 | 2, 9 | 21 | 9, 9 | 22 | 1, 9 | 23 | 4, 9 | 24 | 8, 9 | 25 | 7, 9 | 26 | 0, 9 | 27 | 3, 9 | 28 | 5, 9 | 29 | 6, 9 | 30 | 2, 4 | 31 | 9, 4 | 32 | 1, 4 | 33 | 4, 4 | 34 | 8, 4 | 35 | 7, 4 | 36 | 0, 4 | 37 | 3, 4 | 38 | 5, 4 | 39 | 6, 4 | 40 | 2, 2 | 41 | 9, 2 | 42 | 1, 2 | 43 | 4, 2 | 44 | 8, 2 | 45 | 7, 2 | 46 | 0, 2 | 47 | 3, 2 | 48 | 5, 2 | 49 | 6, 2 | 50 | 2, 3 | 51 | 9, 3 | 52 | 1, 3 | 53 | 4, 3 | 54 | 8, 3 | 55 | 7, 3 | 56 | 0, 3 | 57 | 3, 3 | 58 | 5, 3 | 59 | 6, 3 | 60 | 2, 8 | 61 | 9, 8 | 62 | 1, 8 | 63 | 4, 8 | 64 | 8, 8 | 65 | 7, 8 | 66 | 0, 8 | 67 | 3, 8 | 68 | 5, 8 | 69 | 6, 8 | 70 | 2, 5 | 71 | 9, 5 | 72 | 1, 5 | 73 | 4, 5 | 74 | 8, 5 | 75 | 7, 5 | 76 | 0, 5 | 77 | 3, 5 | 78 | 5, 5 | 79 | 6, 5 | 80 | 2, 7 | 81 | 9, 7 | 82 | 1, 7 | 83 | 4, 7 | 84 | 8, 7 | 85 | 7, 7 | 86 | 0, 7 | 87 | 3, 7 | 88 | 5, 7 | 89 | 6, 7 | 90 | 2, 1 | 91 | 9, 1 | 92 | 1, 1 | 93 | 4, 1 | 94 | 8, 1 | 95 | 7, 1 | 96 | 0, 1 | 97 | 3, 1 | 98 | 5, 1 | 99 | 6, 1 so which bit didn't I get? Did you even try running the code and looking at the output
-
I wish. I have been programming since 1968 and I'm still learning. If you want to produce web pages, learn the basics of HTML/CSS first. PHP is mainly used to create web pages dynamically and you'll have problems if you don't know how the correct output should look. When you start with PHP, familiarize yourself with php.net - the online manual there is an essential resource. Beware of tutorials and videos out there on the net - a lot of them are poor quality and outdated. One of the better ones is phpdelusions.net. And, of course, if get stuck, there's always PHPFreaks.
-
Something like this? $nfc = range(0,9); $afc = range(0,9); shuffle($nfc); shuffle($afc); echo '<pre>Numbers table<br>'; echo "| NFC | AFC |<br>"; for ($i=0; $i<10; $i++) { printf( '| %d | %d |<br>', $nfc[$i], $afc[$i]); } echo '<hr>Matrix table<br>'; for ($i=0; $i<100; $i++) { $a = $afc[intdiv($i, 10)]; $n = $nfc[$i%10]; printf('| %02d | %d, %d<br>', $i, $n, $a); }
-
You are outputting 7 data cells (indexes 0 - 6) $cells = [$brandname, $storename, $r['checkin_time'], $r['checkout_time'], $r['robot_num'], $r['date'], $r['total']]; foreach ($cells as $k => $v) { $this->Cell($this->widths[$k], 5, $v, 0, 0, 0, 0, $this->aligns[$k]); } In the class constructor, only 6 widths and alignments (0 - 5) are specified, so index 6 is missing for those arrays $this->widths = [25,65,30,20,20,20]; $this->aligns = ['L','L','L','R','R','R'];