Jump to content

Barand

Moderators
  • Posts

    24,609
  • Joined

  • Last visited

  • Days Won

    832

Everything posted by Barand

  1. I'd do it when I extract the data from the database instead of reading the data into an array then trying to manipulate that. SELECT order_id , customer_name , product_name , MAX(length) as length , MAX(width) as width , SUM(height) as height , SUM(weight) as weight FROM whichever tables GROUP BY customer_name Job done
  2. $img (the second parameter in imagecopyresampled) is false because of the first error message - not a valid png file.
  3. You don't need a second query. Your first query is to list the blog and existing comments. Save the comment_id from this query and that goes in the hidden field in the comment form at the bottom <form method='post' action='' class='comments_form'> <input type='text' name='username' placeholder='your name... *' id='name'> <textarea name='comments' id='textarea' placeholder='your comment... *' cols='30' rows='6'></textarea> <input type='hidden' name=blog_id' value='$saved_comment_id'> <input type='submit' name='submit' id='post' value='post'> </form>
  4. OK, the weight would be the total but the length, width and height will not all be sum of the items. You would pack them end-to-end, side-by-side or stack them, so only one of the dimensions would be the sum. The others would be the maximum values.
  5. It is difficult to see why one might want to this, particularly as you have now lost the data that Joe Bloggs also ordered a mouse.
  6. In that case, have separate totals array and accumulate the totals for each user in that, Added lines 25, 47, 64 /***************************************************** * 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 $totals = array_fill_keys($names, 0); // create array to store totals $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 $totals[$n] += $a; // accumulate total } $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; } } $tableData .= "<tr><th colspan='3'>Totals</th><td>" . join('</td><td>',$totals) . "</td></tr>\n"; ?> <table border='1' style='border-collapse: collapse;'> <?=$tableHeads?> <?=$tableData?> </table>
  7. foreach ($qdata as $q => $ans) { if ($k > 0) $tableData .= '<tr>'; $tot = array_sum(array_slice($ans,1)); // sum array excluding points value $tableData .= "<td>$q</td><td>".join('</td><td>',$ans)."</td><td>$tot</td></tr>"; ++$k; }
  8. OK So now I have to change the database I set up to allow for numeric answers. Why don't you just ask the real problem from the start instead of going round the houses with the dummy questions and answers?
  9. The pseudocode I gave you will map directly on to your function. Try it yourself. If you get stuck you can have the spoonfed answer by clicking the spoiler
  10. array_sum $a = [1,2,3]; echo array_sum($a); //==> 6
  11. ON blog.content_id = article_comments.blog_id that is the correct join. That's why you added the foreign key. As for the duplication - that is how joins work. You get the data from A with matching data from B. The trick is to output the article data only when it changes. Without knowing exactly how you want to display the data I cannot offer much help other than this pseudocode. prev_blog_id = 0 while (get next result row) if (blog_id != prev_blog_id) output article data prev_blog_id = blog_id endif output comment data endwhile
  12. see this thread - similar problem with solution http://forums.phpfreaks.com/topic/297720-php-matrix-table/?do=findComment&comment=1518791
  13. Do you have any values in the comment table's blog_id field that do not match an id in the blog table?
  14. $x = simplexml_load_file('members.xml'); echo $x->member[0]->firstName, $x->member[0]->lastName;
  15. 1. Don't connect to the database in side the function. You don't want to be connecting every time the function is called (connecting is slow). Connect once when you open your script and pass the connection as a parameter to your function. 2. INSERT queries do not have WHERE clauses, you would write the id in the insert with the other data. Are you sure you don't mean to update an existing record?
  16. $data[0] is the first column, $data[1] the second and so on.
  17. Do you mean SELECT A.column_id , B.column_id FROM tableA as A INNER JOIN tableB as B ON B.column_id > A.column_id
  18. If you are planning to store or process those converted dates, use YYYY-MM-DD instead of YYYY/MM/DD as that is the standard format. $htmldate = '21.03.15'; $dt = DateTime::createFromFormat('d.m.y', $htmldate); echo $dt->format('Y-m-d'); //==> 2015-03-21
  19. Explain "doesn't work". What symptoms are you experiencing that leads you to that conclusion? Give us a clue.
  20. while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) { if ($row > 1 && substr($data[0],0,3) != 'JOB' ) continue; // add this // rest of you loop code here } Better method would be to extract just the data you need from your database.
  21. What exactly is your problem if you can get it to work?
  22. For your order status another option is to use an ENUM type field CREATE TABLE IF NOT EXISTS `status_sample` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(40) DEFAULT NULL, `status` enum('Not Done','Produced') DEFAULT 'Not Done', PRIMARY KEY (`id`) ); INSERT INTO status_sample (name, status) VALUES ('Peter', 1), ('Paul', 2), ('Mary', 1); SELECT * FROM test.status_sample; +----+-------+----------+ | id | name | status | +----+-------+----------+ | 1 | Peter | Not Done | | 2 | Paul | Produced | | 3 | Mary | Not Done | +----+-------+----------+
  23. Have your button link to another php file. That other php file should contain only code that queries the table and downloads the file. for example: mydownload.php <?php $mysqli = new mysqli(HOST,USERNAME,PASSWORD,'test'); $sql="SELECT uompschedulenumber, uompscheduleColor,jmaPartID,ompSalesOrderID, uomlSorP,serial,rail,panel,stile,upsdescription,itemtype,jmaPartShortDescription , uomlStyleGroup,status , Case when status=10 then 'Created' when status=3 then 'PreSanding' when status=4 then 'PrePaint' else '' end as Status1 FROM WIP_master where redtag='Y' and redtagclosed !='Y' and status=10 order by uompschedulenumber,uomlSorP,upsdescription,jmaPartID "; // call the download sql2csv($mysql, $sql, 'orderstatus.csv', 1); /***************************** download function ******************************/ function sql2csv($mysqli, $sql, $filename='', $headings=1) /** * Parameters * $mysqli - connection * $sql - the sql query to be executed * $filename - name of download file (default "download_yymmddhhii.csv") * $headings - 1 if fieldname headings required (default), 0 if not required */ { if (!$filename) $f = 'download_' . date('ymdhi') . '.csv'; else $f = $filename; $fp = fopen('php://output', 'w'); // so you can fputcsv to STDOUT if ($fp) { $res = $mysqli->query($sql); if ($res) { header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename="'.$f.'"'); header('Pragma: no-cache'); header('Expires: 0'); $row = $res->fetch_assoc(); if ($headings) { fputcsv($fp, array_keys($row)); } do { fputcsv($fp, $row); } while ($row = $res->fetch_assoc()); } else echo "Error in query"; fclose($fp); } }
×
×
  • 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.