-
Posts
24,607 -
Joined
-
Last visited
-
Days Won
831
Everything posted by Barand
-
Should be <?php /***************************************************** * first you need to get the column headings * and create array to store responses for * each question ******************************************************/ $sql = "SELECT DISTINCT name FROM kelvin ORDER BY name"; $names = array(); $res = $db->query($sql); while ($row = $res->fetch_assoc()) { $names[] = $row['name']; } $tableHeads = "<tr><th>Category</th><th>Question</th><th>Points</th><th>" . join('</th><th>', $names) . "</th></tr>\n"; $newArray = array_fill_keys($names,''); // create blank array $newArray=array_merge(array('points'=>''), $newArray); // add points to the start of array for each question /***************************************************** * then you process the table data * storing the answers for each name in the array. * indexed by categor, question, name ******************************************************/ $sql = "SELECT category, question, points, name, answer FROM kelvin ORDER BY category, question"; $qarray = $newArray; // new array to store answers to question $currq = ''; // store the current question $currc = ''; // store the current category $data = array(); $res = $db->query($sql); while (list($c,$q,$p,$n,$a) = $res->fetch_row()) { if (!isset($data[$c][$q])) { $data[$c][$q] = $newArray; } $data[$c][$q][$n] = $a; // store the answer by name $data[$c][$q]['points'] = $p; // store the points for the question } $tableData = ''; /************************************************** * loop through the data array and output the table ***************************************************/ foreach ($data as $cat => $qdata) { $kq = count($qdata); // how many question rows? $tableData .= "<tr><td rowspan='$kq'>$cat</td> "; $k=0; foreach ($qdata as $q => $ans) { if ($k > 0) $tableData .= '<tr>'; $tableData .= "<td>$q</td><td>".join('</td><td>',$ans)."</td></tr>"; ++$k; } } ?> <table border='1' style='border-collapse: collapse;'> <?=$tableHeads?> <?=$tableData?> </table> You should not be repeating data like question text, category name and points in every answer record. You should normalize your data so this information is held only once in the correct table. Key values (ids) are the only things that should be repeated in different tables to link the data together. +------------------+ +--------------+ | category | | user | +------------------+ +--------------+ | category_id (PK) |----+ +---| user_id (PK) | | cat_name | | | | name | +------------------+ | | +--------------+ | | | +--------------+ | | | question | | | +--------------+ | | | q_id (PK) |----+ | | | questiontext | | | | | points | | | +----<| category_id | | | +--------------+ | | | +-------------+ | | | answer | | | +-------------+ | | | ans_id (PK) | | | | answertext | | | | user_id |>----+ +----<| q_id | +-------------+
-
You will find line 6 immediately before line 7, and by "closing )" i mean ) while($aGame = mysql_fetch_assoc($rQuery) ) ^ | this is missing
-
Missing a closing ) at the end of line 6
-
My method, using strpos, produces The preg_replace method takes twice as long and produces
-
No, you are using Points as a key when it should be part of the data belonging to a question.
-
Slight change required to the WHERE and the LEFT JOIN condition SELECT m.meta_key ,m.meta_value ,c.ID ,c.post_date ,c.post_title ,c.post_name ,c.post_content ,c.post_author ,u.object_id ,u.term_taxonomy_id FROM wp_posts c INNER JOIN wp_term_relationships u ON c.ID=u.object_id LEFT JOIN wp_postmeta m ON m.post_id=c.ID AND m.meta_key='priority' WHERE u.term_taxonomy_id='$taxonomy' AND post_status='publish' ORDER BY meta_value DESC
-
Search following the id links for matching ids
-
like this? $str = '<div class="text"> <mofish id="testing" type="testing" title="true" /> </div>'; $replace = 'whatever'; $p1 = strpos($str, '<mofish'); $p2 = strpos($str, '/>', $p1); $str = substr_replace($str, $replace, $p1+8, $p2-($p1+9));
-
perhaps +---------------+ | family_tree | +---------------+ | family_id | | firstname | | surname | | gender | | spouse_id | | mother_id | | father_id | +---------------+
-
Something like this will do it. (It isn't pretty, but then neither is the wp_usermeta table). SELECT u.id , firstname.meta_value , lastname.meta_value , city.meta_value FROM wp_users u INNER JOIN ( SELECT u.id , m.meta_value FROM wp_users u LEFT JOIN wp_usermeta m ON u.ID=m.user_id AND m.meta_key = 'firstname' ) firstname USING (id) INNER JOIN ( SELECT u.id , m.meta_value FROM wp_users u LEFT JOIN wp_usermeta m ON u.ID=m.user_id AND m.meta_key = 'lastname' ) lastname USING (id) INNER JOIN ( SELECT u.id , m.meta_value FROM wp_users u LEFT JOIN wp_usermeta m ON u.ID=m.user_id AND m.meta_key = 'city' ) city USING (id) ORDER BY u.id;
-
Sounds like a LEFT JOIN is required for the meta table SELECT m.meta_key ,m.meta_value ,c.ID ,c.post_date ,c.post_title ,c.post_name ,c.post_content ,c.post_author ,u.object_id ,u.term_taxonomy_id FROM wp_posts c INNER JOIN wp_term_relationships u ON c.ID=u.object_id LEFT JOIN wp_postmeta m ON m.post_id=c.ID WHERE u.term_taxonomy_id='$taxonomy' AND m.meta_key='priority' AND post_status='publish' ORDER BY meta_value DESC
-
you need while (list($c, $q, $n, $a) = sqlsrv_fetch_array($stmt2, SQLSRV_FETCH_NUMERIC)) {
-
try <?php $id = $_GET["id"]; settype($id, "integer"); $resultado=mysql_query("SELECT * FROM orcamen WHERE id = $id"); $orcrow=mysql_fetch_object($resultado); $query = mysql_query( "SELECT * FROM tbl_orderdetail WHERE order_id=$id"); ?> <html> <head> <title>Sample</title> <script type='text/javascript' src='jquery-1.10.2.js'></script> <script type="text/javascript"> $(function(){ $('#add').click(function(){ addnewrow(); }); $('body').delegate('.remove','click',function(){ $(this).parent().parent().remove(); }); $('.detail').delegate('.quantity,.price,.discount','keyup',function(){ var tr = $(this).parent().parent(); var qty = tr.find('.quantity').val(); var price = tr.find('.price').val(); var dis = tr.find('.discount').val(); var amt = (qty * price) - (qty * price * dis)/100; tr.find('.amount').val(amt); total(); }); }); function total() { var t = 0; $('.amount').each(function(i,e) { var amt = $(this).val()-0; t += amt; }); $('.total').html(t); } function addnewrow() { var n = ($('.detail tr').length-0)+1; var tr = '<tr>'+ '<td class="no">' + n + '</td>'+ '<td><input type="text" class="form-control quantity" name="quantity[]"></td>'+ '<td><input type="text" class="form-control productname" name="productname[]"></td>'+ '<td><input type="text" class="form-control price" name="price[]"></td>'+ '<td><input type="text" class="form-control discount" name="discount[]"></td>'+ '<td><input type="text" class="form-control amount" name="amount[]"></td>'+ '<td><a href="#" class="remove">Excluir</td>'+ '</tr>'; $('.detail').append(tr); } </script> </head> <body> <form method="post" action="salva_orc.php"> <input type="hidden" name="id" id="id" value="<?php echo $id;?>" /> <table border='1'> <thead> <tr> <th>No</th> <th>Qtde</th> <th>Descrição</th> <th>Unitário</th> <th>Desc.(%)</th> <th>Valor</th> <th><input type="button" value="+" id="add" class="btn btn-primary"></th> </tr> </thead> <tbody id="orderdetail" class="detail"> <?php while ($result = mysql_fetch_object($query)){ echo <<<ROW <tr> <td width="2%" class="no">1</td> <td width="10%"><input type="text" class="form-control quantity" name="quantity[$result->id]" value="$result->quantity"></td> <td width="60%"><input type="text" class="form-control productname" name="productname[$result->id]" value="$result->product_name"></td> <td width="8%"><input type="text" class="form-control price" name="price[$result->id]" value="$result->price"></td> <td width="4%"><input type="text" class="form-control discount" name="discount[$result->id]" value="$result->discount"></td> <td width="10%"><input type="text" class="form-control amount" name="amount[$result->id]" value="$result->amount"></td> <td width="6%"><a href="#" class="remove">Excluir</a></td> </tr> ROW; } ?> </tbody> </table> <input type='submit' name='btnSubmit' value='Submit'> </form> </body> </html>
-
BTW, these lines are redundant in the revised method and can be removed $qarray = $newArray; // new array to store answers to question $currq = ''; // store the current question $currc = ''; // store the current category
-
With list() you need a numerically indexed array, hence my use of fetch_row()
-
As the structure gets a little more complicated I opted for an alternative method, storing the query results in a multi-dimensional array indexed by category, question, name. <?php /***************************************************** * first you need to get the column headings * and create array to store responses for * each question ******************************************************/ $sql = "SELECT DISTINCT name FROM kelvin ORDER BY name"; $names = array(); $res = $db->query($sql); while ($row = $res->fetch_assoc()) { $names[] = $row['name']; } $tableHeads = "<tr><th>Category</th><th>Question</th><th>" . join('</th><th>', $names) . "</th></tr>\n"; $newArray = array_fill_keys($names,''); // create blank array /***************************************************** * then you process the table data * storing the answers for each name in the array. * indexed by categor, question, name ******************************************************/ $sql = "SELECT category, question, name, answer FROM kelvin ORDER BY category, question"; $qarray = $newArray; // new array to store answers to question $currq = ''; // store the current question $currc = ''; // store the current category $data = array(); $res = $db->query($sql); while (list($c,$q,$n,$a) = $res->fetch_row()) { if (!isset($data[$c][$q])) { $data[$c][$q] = $newArray; } $data[$c][$q][$n] = $a; // store the answer by name } $tableData = ''; /************************************************** * loop through the data array and output the table ***************************************************/ foreach ($data as $cat => $qdata) { $kq = count($qdata); // how many question rows? $tableData .= "<tr><td rowspan='$kq'>$cat</td> "; $k=0; foreach ($qdata as $q => $ans) { if ($k > 0) $tableData .= '<tr>'; $tableData .= "<td>$q</td><td>".join('</td><td>',$ans)."</td></tr>"; ++$k; } } ?> <table border='1' style='border-collapse: collapse;'> <?=$tableHeads?> <?=$tableData?> </table>
-
No, No, No! For the last time (I'll type it slowly), NOT the order id You need the id of the individual order detail record name="quantity[$result->id]"
-
The time-limit for editing the post will have expired by now. Post your current code
-
do you now have name="quantity[$result->id]" and same for the other order detail fields?
-
You need to be using tbl_orderdetail.id as the indexes for the field names
-
Add a hidden field to your form containing the id of whatever they are commenting on. Save this id, comment and user_id to a comments table. The id will allow you to relate the comment to the original.
-
NOT the order_id. That would give the fields for every detail record the same id. You want the id from the order detail records
-
SELECT statement, Multiple WHERE, with requirements
Barand replied to BuildMyWeb's topic in MySQL Help
Separate tables will be a PIA when you need to query for results across regions. EG How many submissions have we had in the last three months? -
At the moment you have name='productname[]'. I am suggesting you change that to name='productname[$det_id]' where $det_id is the id of the order detail record. Do this for all fields to tie them to the same record. That will be the key of the POSTed data foreach ($_POST['quantity'] as $ord_det_id => $quantity) {
-
This will do it using CSS $sql = "SELECT timeslot , client , TIMESTAMPDIFF(HOUR, timeslot, NOW()) as elapsed FROM timeslots"; $res = $db->query($sql); $rows = ''; while (list($ts, $cl, $el) = $res->fetch_row()) { $class = $el >= 20 ? 'class="blink"' : ''; $rows .= "<tr><td>$cl</td><td $class>$ts</td></tr>\n"; } ?> <html> <head> <title>Example Flashing</title> <style type='text/css'> /* The animation code */ @keyframes flash { from {background-color: red;} to {background-color: white;} } /* The element to apply the animation to */ td.blink { width: 100px; height: 100px; background-color: white; animation: flash 1s infinite; } </style> </head> <body> <table border="1"> <tr><th>Client</th><th>Time</th></tr> <?=$rows?> </table> </body> </html>