-
Posts
24,609 -
Joined
-
Last visited
-
Days Won
832
Everything posted by Barand
-
Merging orders in an array, then removing the excess orders
Barand replied to blmg2009's topic in PHP Coding Help
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 -
Upload resize and convert images turn black help
Barand replied to cobusbo's topic in PHP Coding Help
$img (the second parameter in imagecopyresampled) is false because of the first error message - not a valid png file. -
Identifying and connecting ID's between tables
Barand replied to VanityCrush's topic in PHP Coding Help
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> -
Merging orders in an array, then removing the excess orders
Barand replied to blmg2009's topic in PHP Coding Help
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. -
Merging orders in an array, then removing the excess orders
Barand replied to blmg2009's topic in PHP Coding Help
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. -
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>
-
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; }
-
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?
-
array_sum $a = [1,2,3]; echo array_sum($a); //==> 6
-
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
-
see this thread - similar problem with solution http://forums.phpfreaks.com/topic/297720-php-matrix-table/?do=findComment&comment=1518791
-
$x = simplexml_load_file('members.xml'); echo $x->member[0]->firstName, $x->member[0]->lastName;
-
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?
-
Creating an array from a selected column based based on order
Barand replied to bambinou1980's topic in PHP Coding Help
json_encode -
Explain "doesn't work". What symptoms are you experiencing that leads you to that conclusion? Give us a clue.
-
What exactly is your problem if you can get it to work?
-
mysqli_real_escape_string alternative for decimal ?
Barand replied to bambinou1980's topic in PHP Coding Help
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 | +----+-------+----------+ -
need to add a the option to export data to a csv file
Barand replied to kat35601's topic in PHP Coding Help
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); } } -
mysqli_real_escape_string alternative for decimal ?
Barand replied to bambinou1980's topic in PHP Coding Help