-
Posts
24,563 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
Combine results from multiple members who have like categories using OR
Barand replied to HalRau's topic in PHP Coding Help
Use ... WHERE %d IN (BizCat1, BixCat2, BizCat3) AND LiveOnWeb = 1 ... -
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;
-
need to Amendment the Code: it Duplicate records
Barand replied to x-man1's topic in PHP Coding Help
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 -
Help with pulling/displaying data from table?
Barand replied to Sherlocked's topic in PHP Coding Help
The "ENT_Quotes" constant should be uppercase - ENT_QUOTES -
in which case ... and (keywords like '%$skey%' or bname like '%skey%') and category like '%Mycategory%'";
-
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"
-
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 | +--------+------------+-----------+
-
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.
-
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
-
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
-
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;
-
Define a total variable, say $total_points then accumulate the points values into that. $total_points += $points;
-
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;
-
Newbie question about single quotes, double quotes, and {}
Barand replied to LazerOrca's topic in PHP Coding Help
$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) -
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
-
inserting multiple data with a same id in database
Barand replied to mythri's topic in PHP Coding Help
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 -
Checking for an error message would've revealed that syntax error
-
Then that was your problem
-
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)
-
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.
-
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?
-
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.
-
It would seem that the query called by $this->_db->get('users', array($field, '=', $user)); failed and $data is, therefore, not a valid object.