geeknout Posted November 11, 2011 Share Posted November 11, 2011 Hey Guys, I can get the data from sqlsrv but I can't figure out how to format similar to a crystal report.. i.e. something like: Find a specific customer number Print out the number, plus the customer name then: find all the rows that have the customer number and print them. I can get all the information obviously, but I'm not good enough with PHP yet to find similar to the above. Here is how I pull my data out if your curious: while( $obj = sqlsrv_fetch_object( $stmt)) { $reqshipdt = date("d M Y", strtotime($obj->req_ship_dt)); $popstdt = date("d M Y", strtotime($obj->start_dt)); $reqshipdt1 = date(strtotime($obj->req_ship_dt)); $popstdt1 = date(strtotime($obj->start_dt)); $datediff = ($reqshipdt1 - $popstdt1); $days = round($datediff / 86400); //gives how many days echo "<tr>"; echo "<td>". $obj->item_no."</td>"; echo "<td>".$obj->item_desc_1."</td>"; echo "<td>".$obj->ord_no."</td>"; echo "<td>".$obj->qty_ordered."</td>"; echo "<td>".$obj->mfg_ord_no."</td>"; echo "<td>".$reqshipdt."</td>"; echo "<td>".$obj->oe_po_no."</td>"; echo "<td>".$obj->prod_cat."</td>"; echo "<td>".$popstdt."</td>"; echo "<td>".$days." Day(s)</td>"; echo "<td>".$obj->planning_lead_tm."</td>"; echo "</tr>"; } Hope this makes sense on what I'm trying to shoot for. Thanks Guys and Gals. Quote Link to comment https://forums.phpfreaks.com/topic/250955-pull-specific-data-from-sql-and-group-it-best-way-i-can-think-to-put-it/ Share on other sites More sharing options...
geeknout Posted November 13, 2011 Author Share Posted November 13, 2011 Ok not sure why this was moved to SQL Server since its a PHP question, but hey whatever... I'm getting close can someone elaborate: <?php include("includes/dbconn.php"); if ($conn === 'false') { echo "Ooops!"; } else { $query = "SELECT * from customers"; $result = mysql_query($query) or die(mysql_error()); while($row=mysql_fetch_assoc($result)) { echo "<br />"; echo $row['customerNumber']; //displays all the customer numbers foreach($row as $cusno) { // This doesnt seem to be running... $query2 = "SELECT * FROM orders WHERE customerNumber = '%s'"; mysql_real_escape_string($cusno); $result2 = mysql_query($query2) or die(mysql_error()); while($row2=mysql_fetch_object($result2)) { echo "<br />"; echo "Got this far."; //not getting this far just here to see if I can as I run my page. } } } } mysql_close($conn); unset($row); ?> Quote Link to comment https://forums.phpfreaks.com/topic/250955-pull-specific-data-from-sql-and-group-it-best-way-i-can-think-to-put-it/#findComment-1287748 Share on other sites More sharing options...
kicken Posted November 14, 2011 Share Posted November 14, 2011 Do one query that returns the data you need, then group it in php for display. ex a query like select * from customers inner join orders on customers.customerNumber=orders.customerNumber order by customers.customerNumber then process it like: $customers=array(); while ($row=/*fetch row*/){ $custNo = $row['customerNumber']; if (!isset($customers[$custNo])){ $customers[$custNo]=array( 'name' => $row['customerName'], /* .. other customer info fields */ 'orders' => array() ); } $customers[$custNo]['orders'][] = $row; } print_r($customers); Quote Link to comment https://forums.phpfreaks.com/topic/250955-pull-specific-data-from-sql-and-group-it-best-way-i-can-think-to-put-it/#findComment-1288065 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.