Jump to content

Barand

Moderators
  • Posts

    24,563
  • Joined

  • Last visited

  • Days Won

    822

Everything posted by Barand

  1. Use ... WHERE %d IN (BizCat1, BixCat2, BizCat3) AND LiveOnWeb = 1 ...
  2. You should be normalizing your data and not storing data in db tables as you would in a spreadsheet.
  3. If you are that curious, why don't you time how long it takes to get a few hundred items from your array. Then convert the array to a string and time how long it takes doing it that way. $t1 = microtime(true); // perform the data extraction here $t2 = microtime(true); $timeTaken = $t2 - $t1;
  4. Here is how I would do it $sql = "SELECT ID_student , name , Age , Grade , ID_infractions , infractions_text , ID_Actions , Actions_text FROM view1 ORDER BY ID_student,ID_infractions,ID_Actions"; $res = $db->query($sql); $currid = 0; $currName = $currAge = $currGrade = ''; $data = array(); $heads = "<tr><th>Name</th><th>Age</th><th>Grade</th><th>Infractions</th><th>Actions</th></tr>"; $tdata = ''; while (list($sid,$name,$age,$grd,$infid,$inftxt,$actid,$acttxt) = $res->fetch_row()) { // CHECK FOR CHANGE IN ID if ($sid != $currid) { if ($currid) { // OUTPUT DATA FOR STUDENT WHEN WE GET A NEW ID $tdata .= "<tr valign='top'><td>$currName</td><td>$currAge</td><td>$currGrade</td><td>"; foreach ($data['infracts'] as $k=>$v) { $tdata .= "$k $v<br>"; } $tdata .= "</td><td>"; foreach ($data['actions'] as $k=>$v) { $tdata .= "$k $v<br>"; } $tdata .= "</td></tr>\n"; } // STORE DATA FOR CURRENT STUDENT $currid = $sid; $currName = $name; $currAge = $age; $currGrade = $grd; $data = array(); } // STORE INFRACTION AND ACTION DATA $data['actions'][$actid] = $acttxt; $data['infracts'][$infid] = $inftxt; } // DON'T FORGET TO OUTPUT THE LAST STUDENT // THAT IS STORED IN THE DATA $tdata .= "<tr><td>$currName</td><td>$currAge</td><td>$currGrade</td><td>"; foreach ($data['infracts'] as $k=>$v) { $tdata .= "$k $v<br>"; } $tdata .= "</td><td>"; foreach ($data['actions'] as $k=>$v) { $tdata .= "$k $v<br>"; } $tdata .= "</td></tr>\n"; ?> <html> <body> <table border='1'> <?= $heads?> <?= $tdata?> </table> </body> </html> Giving
  5. The "ENT_Quotes" constant should be uppercase - ENT_QUOTES
  6. in which case ... and (keywords like '%$skey%' or bname like '%skey%') and category like '%Mycategory%'";
  7. Makes no difference. If I search php.net manual for "mysqli_query" I get the page shown below. Same goes for a search for "mysqli::query"
  8. Or you can do it in the query mysql> SELECT * FROM user; +--------+------------+-----------+ | iduser | first_name | last_name | +--------+------------+-----------+ | 1 | Stella | NULL | | 2 | Amanda | Brown | | 3 | Kevin | Green | | 4 | John | Wilson | | 5 | Helen | NULL | | 6 | Pete | Doone | +--------+------------+-----------+ mysql> SELECT iduser -> , first_name -> , IFNULL(last_name, '(No data)') as lastname -> FROM user; +--------+------------+-----------+ | iduser | first_name | lastname | +--------+------------+-----------+ | 1 | Stella | (No data) | | 2 | Amanda | Brown | | 3 | Kevin | Green | | 4 | John | Wilson | | 5 | Helen | (No data) | | 6 | Pete | Doone | +--------+------------+-----------+
  9. As you have it now the name would be in $info[0], uom in $info[1] and description in $info[2]. I'd do it something like this function items($item_id) { $details = array(); $result = mysql_query("select name, uom, description from items where item_id=".$item_id."") or die (mysql_error()); while($row = mysql_fetch_assoc($result)) { $details[] = $row; } return $details; } $info = items($id); echo $info['name']; echo $info['uom']; echo $info['description']; If you are having to stripslashes then your handling of inputs to the database is wrong.
  10. It is there in the php manual http://php.net/manual/en/mysqli.query.php Search for "mysqli" then click on the links for the individual methods. As to why a search for "mysqli_query" doesn't work is an issue you would have to take up with php.net
  11. So what is the query you are actually executing? $query = mysql_query($query) or die (mysql_error()); echo $query; // what does this output?
  12. I suggest you read the pseudocode again (reply #9) Setting the total to zero is done before the loop begins. Inside the loop calculate the value for the points accumulate into the total After the loop echo the accumulated total
  13. pseudocode version: $total_points = 0; // DEFINE the variable begin loop $points = whatever; // get points value $total_points += $points; // accumulate points total endloop echo $total_points;
  14. Define a total variable, say $total_points then accumulate the points values into that. $total_points += $points;
  15. You can use "fall through" in a switch statement switch ($diff) { case 0: case 1: case 2: case 3: case 4: case 5: $point = 6; break; case 6: case 7: case 8: case 9: case 10: $point = 4; break; default: $point = 1; } echo $point;
  16. $arr1 = [ [4,5], [7,9,5], [1,2] ]; $arr2 = ['A', 'B', 'C']; $arr3 = ['A' => 123, 'B'=> 456]; $simple = 'xyz'; $txt = "Print simple var like $simple or $arr2[2] or $arr3[B], but complex var needs curlies as in {$arr1[1][2]}"; echo $txt; /**** OUTPUT **** Print simple var like xyz or C or 456, but complex var needs curlies as in 5 */ NOTE: if using $arr3['B'] on its own e.g. echo $arr3['B']; the quotes around 'B' are required (it will work but inefficiently as PHP will first search for a defined constant 'B' and interpret it as a string when one is not found), but not if it is inside a double-quoted string. If you use "$arr3['B']" inside double quotes then it too needs curlies. Simple vars may also need curlies in some cases to prevent ambiguity in the var name e.g. $day = 26; echo "Today is the {$day}th of November"; As a rule, I use curlies around all array and object expressions but not around simple variables (unless required as in the last example)
  17. Because you are using a LEFT join to the grade table then put the condition in the ON clause SELECT s.studentid , s.lastname , c.classname , g.grade , g.period FROM students s INNER JOIN classes c USING (studentid) LEFT JOIN grades g ON s.studentid = g.studentid AND g.period = 3 ORDER BY s.lastname
  18. Insert into the invoice table before the while loop (exclude invoice id field) $query1 = "INSERT into sales_invoice (order_id, customer_id, date_invoiced) VALUES ( ".$order_id.", ".$customer_id.", NOW())"; Use $invoice_id = mysql_insert_id() to get the new id and use that when you insert the invoice lines
  19. Checking for an error message would've revealed that syntax error
  20. Then that was your problem
  21. That is weird! Nether "conversation" nor "members" is a reserved word so the `` should be unnecessary A=B is the same B=A That new query has a definite syntax error - WHERE (c.cid=$q orc.originalid=$q)
  22. You're welcome. As well as making your queries more understandable in terms of both function and structure, those tips will make your queries more efficient.
  23. Where are $username and $password defined in admin.php? Why don't you just store the user level in the session and check that in admin.php instead of re-querying the db?
  24. Then your query $reference = mysql_query("SELECT * FROM contacts"); has failed. Check to see what mysql_error() contains. NOTE: you should not be using mysql_ functions, they are deprecated. Use mysqli_ or PDO instead.
  25. It would seem that the query called by $this->_db->get('users', array($field, '=', $user)); failed and $data is, therefore, not a valid object.
×
×
  • 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.