Jump to content

Barand

Moderators
  • Posts

    24,612
  • Joined

  • Last visited

  • Days Won

    834

Everything posted by Barand

  1. If you are going to insist on using "return" then put the code inside a function.
  2. You would also need to substitute "true" for "OK". I just used the string values so the result can be echoed.
  3. You could shorten it by using array_intersect(), thus removing the need for the nested foreach loops. But I think we've been here before.
  4. if you really must do it the long way $match_product1 = [9,10,11]; $match_product2 = [3,4,5,6,7,12]; $cart_prods = [3,4,9]; $result = 'ERROR'; foreach ($cart_prods as $id) { if (in_array($id, $match_product1)) { // we found a match in array1 so now check array2 foreach ($cart_prods as $id) { if (in_array($id, $match_product2)) { // match found so set result and exit the loops $result = 'OK'; break 2; } } } } echo $result;
  5. A couple of things wrong with that code. With array_intersect() there is no need for the foreach loop, is checks them all in one go. - See my function "return" is used to return the result of a function. Your return is not inside a function.
  6. this is the pseudocode for the function if any id is in both cart and group1 if any id is in both cart and group 2 return true else return false end if else return false end if
  7. Your next step is the PHP reference manual http://uk1.php.net/manual/en/function.array-intersect.php
  8. Is this what you mean? $group1 = [3,4,5,6,7,12]; $group2 = [9,10,11]; $cart_prods = [6,10]; echo checkCart($cart_prods, $group1, $group2) ? 'OK' : 'ERROR'; function checkCart($prods, $g1, $g2) { if (array_intersect($prods, $g1)) { return array_intersect($prods, $g2); } return false; }
  9. Sounds like a case for FULLTEXT INDEX
  10. Select date cells and right click Select "format cells" Choose "custom" enter "mm.dd.yy"
  11. Barand

    Website

    The most common use of PHP is to retrieve data from a database, textfile or RSS feed and display on the page. For example, if you have 1,000 employees then you don't want to create a separate static page for each employee's profile. Instead you ask the user to input an identifier (employee number, say) then retrieve and display the profile data for that employee. That way you only need one employee profile page.
  12. Don't run queries from inside a loop. Use a join to get all the data with a single query. data mysql> select * from cart; +---------+---------+---------+------+ | cart_id | ip | prod_id | qty | +---------+---------+---------+------+ | 1 | 123.321 | 1 | 2 | | 2 | 123.321 | 2 | 5 | | 3 | 123.321 | 3 | 2 | | 4 | 321.333 | 1 | 1 | | 5 | 321.333 | 3 | 3 | +---------+---------+---------+------+ mysql> select * from product; +---------+-------------+------------+ | prod_id | prod_title | prod_price | +---------+-------------+------------+ | 1 | Widget | 5.99 | | 2 | Gizmo | 10.99 | | 3 | Thingumajig | 19.99 | +---------+-------------+------------+ query SELECT prod_title , qty as quantity , prod_price as unitprice , prod_price * qty as price FROM product INNER JOIN cart USING (prod_id) WHERE ip = '123.321'; +-------------+----------+-----------+-------+ | prod_title | quantity | unitprice | price | +-------------+----------+-----------+-------+ | Widget | 2 | 5.99 | 11.98 | | Gizmo | 5 | 10.99 | 54.95 | | Thingumajig | 2 | 19.99 | 39.98 | +-------------+----------+-----------+-------+ Now you can loop through the single result set and accumulate the total with $totalPrice += $row['price'];
  13. So it looks like the exercise is to normalize the user_group, creating a separate table and then storing the id of the group in the user table instead of the name Original data mysql> SELECT * FROM clients; +----+----------+-----------------------+------------+----------------+ | id | uname | pass | user_group | email | +----+----------+-----------------------+------------+----------------+ | 1 | jonysrb | d5er56rPg12ebN8eh65lk | Admin | [email protected] | | 2 | peter99 | kl8HGbL93sbx7 | Users | [email protected] | | 3 | paul1999 | asdFG5VbF98z | Moderator | [email protected] | | 4 | maryram | kQ87FF75wT2v | Users | [email protected] | +----+----------+-----------------------+------------+----------------+ Step 1: Create the new tables CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(16) NOT NULL, `pwd` varchar(32) NOT NULL, `email` varchar(255) DEFAULT NULL, `group_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`), KEY `idx_users_group_id` (`group_id`) ) ; CREATE TABLE `user_group` ( `group_id` int(11) NOT NULL AUTO_INCREMENT, `user_group` varchar(45) DEFAULT NULL, PRIMARY KEY (`group_id`) ) ; Step 2a Check that spellings of the groups are consistent. (I once did an exercise like this on a large invoices table and some of the supplier names had half a dozen different spelling variations) SELECT DISTINCT user_group FROM clients ORDER BY user_group; Step 2b: populate the new groups table INSERT INTO user_group (user_group) SELECT DISTINCT user_group FROM clients ORDER BY user_group; Step 3: populate the new users table INSERT INTO users (id,username,pwd,email,group_id) SELECT c.id , c.uname , c.pass , c.email , g.group_id FROM clients c INNER JOIN user_group g USING (user_group); -- join on group name to get the new id New data mysql> SELECT * FROM user_group; +----------+------------+ | group_id | user_group | +----------+------------+ | 1 | Admin | | 2 | Moderator | | 3 | Users | +----------+------------+ mysql> select * from users; +----+----------+-----------------------+----------------+----------+ | id | username | pwd | email | group_id | +----+----------+-----------------------+----------------+----------+ | 1 | jonysrb | d5er56rPg12ebN8eh65lk | [email protected] | 1 | | 2 | peter99 | kl8HGbL93sbx7 | [email protected] | 3 | | 3 | paul1999 | asdFG5VbF98z | [email protected] | 2 | | 4 | maryram | kQ87FF75wT2v | [email protected] | 3 | +----+----------+-----------------------+----------------+----------+
  14. When you have all the product info in table "product", why would you want to duplicate that info into two other tables?
  15. This uses the above model to produce your table output <?php // PDO connection $db = pdoConnect('foo'); $sql = "SELECT course,certlevel FROM course ORDER BY certlevel,course_id"; $res = $db->query($sql); $courses = []; foreach ($res as $crs) { $courses[ $crs['certlevel'] ][] = $crs['course']; } // build table heading and empty table row array $thead = "<tr><th>Name</th>"; $newarray = []; foreach ($courses as $cert=>$crss) { $cclass = "L$cert"; $thead .= "<th class='$cclass'>" . join("</th><th class='$cclass'>",$crss) . "</th><th class='$cclass'>Certificate $cert</th>"; foreach ($crss as $cname) { $newarray[$cert][$cname] = 0; } } $thead .= "</tr>\n"; // get the student marks $sql = "SELECT s.name , c.course , c.certlevel , e.marks FROM student s LEFT JOIN enrolment e USING (student_id) LEFT JOIN course c USING (course_id) ORDER BY e.student_id, c.certlevel, e.course_id"; $res = $db->query($sql); $prevname=''; $tdata = ''; $studata = $newarray; foreach ($res as $row) { if ($row['name'] != $prevname) { if ($prevname) { $tdata .= "<tr><td>$prevname</td>"; foreach ($studata as $cert=>$marks) { $cclass = "L$cert"; $certres = min($marks) >= 50 ? "Entitle for Cert $cert" : 'Complete only'; foreach ($marks as $m) { $tdata .= "<td class='$cclass'>$m</td>"; } $tdata .= "<td class='cert$cclass'>$certres</td>"; } $tdata .= "</tr>\n"; } $studata = $newarray; $prevname = $row['name']; } $studata[$row['certlevel']][$row['course']] = $row['marks']; } // last student $tdata .= "<tr><td>$prevname</td>"; foreach ($studata as $cert=>$marks) { $cclass = "L$cert"; $certres = min($marks) >= 50 ? "Entitle for Cert $cert" : 'Complete only'; foreach ($marks as $m) { $tdata .= "<td class='$cclass'>$m</td>"; } $tdata .= "<td class='cert$cclass'>$certres</td>"; } $tdata .= "</tr>\n"; ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style type="text/css"> body { font-family: sans-serif; font-size: 10pt; } table { border-collapse: collapse; } td, th { border: 1px solid #888; padding: 3px; min-width: 40px; } td.L1 { background-color: #ccf; text-align: right; } td.L2 { background-color: #fcc; text-align: right; } td.certL1 { background-color: #ccf; font-weight: 600; } td.certL2 { background-color: #fcc; font-weight: 600; } th.L1 { background-color: #aaf; } th.L2 { background-color: #faa; } </style> </head> <body> <table> <thead> <?=$thead?> </thead> <tbody> <?=$tdata?> </tbody> </table> </body> </html> NOTE The pdo connect function (in an included file) is define("HOST",'localhost'); define("USERNAME",'*****************'); define("PASSWORD",'*****************'); function pdoConnect($dbname) { $db = new PDO("mysql:host=".HOST.";dbname=$dbname",USERNAME,PASSWORD); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); return $db; }
  16. Noooo! That is not the way to store data in a RDBMS. I mean like this, normalizing your data mysql> select * from course; +-----------+--------+-----------+ | course_id | course | certlevel | +-----------+--------+-----------+ | 1 | MS1 | 1 | | 2 | MS2 | 1 | | 3 | MS3 | 1 | | 4 | MS4 | 1 | | 5 | MS5 | 1 | | 6 | MS6 | 1 | | 7 | MS7 | 2 | | 8 | MS8 | 2 | | 9 | MS9 | 2 | | 10 | MS10 | 2 | | 11 | MS11 | 2 | | 12 | MS12 | 2 | +-----------+--------+-----------+ mysql> select * from student; +------------+------+ | student_id | name | +------------+------+ | 1 | foo | | 2 | lim | +------------+------+ mysql> select * from enrolment; +--------------+------------+-----------+-------+ | enrolment_id | student_id | course_id | marks | +--------------+------------+-----------+-------+ | 1 | 2 | 7 | 40 | | 2 | 2 | 8 | 50 | | 3 | 2 | 9 | 66 | | 4 | 2 | 10 | 76 | | 5 | 2 | 11 | 56 | | 6 | 2 | 12 | 55 | | 7 | 2 | 1 | 40 | | 8 | 2 | 2 | 50 | | 9 | 2 | 3 | 66 | | 10 | 2 | 4 | 76 | | 11 | 2 | 5 | 56 | | 12 | 2 | 6 | 55 | | 13 | 1 | 1 | 60 | | 14 | 1 | 2 | 60 | | 15 | 1 | 3 | 76 | | 16 | 1 | 4 | 76 | | 17 | 1 | 5 | 86 | | 18 | 1 | 6 | 75 | | 19 | 1 | 7 | 30 | | 20 | 1 | 8 | 30 | | 21 | 1 | 9 | 36 | | 22 | 1 | 10 | 36 | | 23 | 1 | 11 | 66 | | 24 | 1 | 12 | 65 | +--------------+------------+-----------+-------+ SELECT s.name , c.certlevel , c.course , e.marks FROM student s LEFT JOIN enrolment e USING (student_id) LEFT JOIN course c USING (course_id) ORDER BY e.student_id, c.certlevel, e.course_id; +------+-----------+--------+-------+ | name | certlevel | course | marks | +------+-----------+--------+-------+ | foo | 1 | MS1 | 60 | | foo | 1 | MS2 | 60 | | foo | 1 | MS3 | 76 | | foo | 1 | MS4 | 76 | | foo | 1 | MS5 | 86 | | foo | 1 | MS6 | 75 | | foo | 2 | MS7 | 30 | | foo | 2 | MS8 | 30 | | foo | 2 | MS9 | 36 | | foo | 2 | MS10 | 36 | | foo | 2 | MS11 | 66 | | foo | 2 | MS12 | 65 | | lim | 1 | MS1 | 40 | | lim | 1 | MS2 | 50 | | lim | 1 | MS3 | 66 | | lim | 1 | MS4 | 76 | | lim | 1 | MS5 | 56 | | lim | 1 | MS6 | 55 | | lim | 2 | MS7 | 40 | | lim | 2 | MS8 | 50 | | lim | 2 | MS9 | 66 | | lim | 2 | MS10 | 76 | | lim | 2 | MS11 | 56 | | lim | 2 | MS12 | 55 | +------+-----------+--------+-------+
  17. I would have expected your course data to contain a reference to the certificate level (1 or 2)
  18. or use the same tests as before but test the values in descending order. Currently, if the value is 3001 then that is > 1, so it goes no further.
  19. Your tables would be something like this. Although I am unsure about the advertising codes, which crop up everywhere in your model.
  20. On a serious note, you cannot run queries without establishing a connection to the database server. If including the file causes an error then you should be looking at the connection code and fixing errors there.
  21. This will stop that error // $executingFetchQuery = $mysqli->query("SELECT `StreamStatus` FROM streamdb WHERE 1"); // if($executingFetchQuery) // { // while($arr = $executingFetchQuery->fetch_assoc()) // { // $resultArr[] = $arr['StreamStatus'];//storing values into an array // } // } However, it may cause others
  22. It was working when it left the shop. Do you have error reporting turned on? Your call to sqlsrv_query() does not look right. You are not using query parameters so why try to provide them? RTFM
  23. try <?php // get data date range $date = new DateTime(); // now $dateto = $date->format('Y-m-01'); $datefrom = $date->sub(new DateInterval('P1Y'))->format('Y-m-01'); // minus 1 year // initialize array $dp = new DatePeriod($date, new DateInterval('P1M'),12); $data = []; foreach ($dp as $d) { $data[$d->format('F')] = [ 'reg' => 0, 'app' => 0 ]; } // get data and accumulate counts in array $sql = "SELECT registerdate , approvedate FROM datetest WHERE registerdate >= ? AND registerdate < ? ORDER BY registerdate"; $stmt = $pdo->prepare($sql); $stmt->execute( [$datefrom, $dateto] ); while ($row = $stmt->fetch()) { if ($row['registerdate']) { $rm = (new DateTime($row['registerdate']))->format('F'); $data[$rm]['reg']++; } if ($row['approvedate']) { $am = (new DateTime($row['approvedate']))->format('F'); $data[$am]['app']++; } } // assemble the output $tabledata = ''; foreach ($data as $month=>$vals) { $tabledata .= "<tr><td>$month</td><td>{$vals['reg']}</td><td>{$vals['app']}</td></tr>\n"; } ?> <table> <thead> <tr><th>Month</th><th>Registered</th><th>Approved</th></tr> </thead> <tbody> <?=$tabledata?> </tbody> </table> Note that this processes the previous year's data. Set your date range as required.
  24. You need to turn error reporting on. Preferably in your php.ini file but if not possible put this at top of the script error_reporting(E_ALL); ini_set('display_errors', 1); mysqli_query requires the connection as its first parameter. With error reporting on it would have told you. You also need exit; after a header redirect to stop other code in the page from executing. PS If error reporting is on, the redirect may be hiding the message.
  25. Read the data into an array, indexed by month. Store counts of registered and approved for each month. Output the table from the array.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.