Fearpig Posted November 7, 2007 Share Posted November 7, 2007 Hello, Would someone have a quick look at my code and see if you can suggest a solution! The code below pulls all the data from the database correctly but I want to change what is displayed. Usually I would just use an if-else or a switch but the field i want to change is inside a vprintf(). Here's the code I'm using at the moment.... <?php while (odbc_fetch_into($result, $data)) { vprintf (" <form action='PAC_Level_2.php' method='post'><td align='center'> <input name='Year' type='hidden' value='$Year'> <input name='id' type='hidden' value='$id'> <input name='PAC' type='hidden' value='%s'> <input name='Submit' type='submit' value='%s' </td></form>$data); } ?> ...and here's an example of the switch... switch (%s){ case "A": echo "Domestic"; case "B": echo "Ancillaries"; case "N": echo "Spares"; }; Can anyone suggest a way to combine them? Thank you. Quote Link to comment Share on other sites More sharing options...
Barand Posted November 7, 2007 Share Posted November 7, 2007 <?php $pacs = array ( 'A' => "Domestic", 'B' => "Ancillaries", 'N' => "Spares" ); $result = odbc_exec($conn, "SELECT year, id, PAC FROM mytable"); while (odbc_fetch_into($result, $data)) { /** * $data has 3 elenents, 1 for each hidden field. * Now add 4th to appear in the button value */ $data[] = $pacs[$data[2]]; vprintf ( " <form action='PAC_Level_2.php' method='post'><td align='center'> <input name='Year' type='hidden' value='%s'> <input name='id' type='hidden' value='%s'> <input name='PAC' type='hidden' value='%s'> <input name='Submit' type='submit' value='%s'> </td></form>", $data); } ?> (I'd go for 1 form with several submit buttons with different values) Quote Link to comment Share on other sites More sharing options...
Fearpig Posted November 7, 2007 Author Share Posted November 7, 2007 Hi Barand... Its you helping me AGAIN!! I'm actually using this in the code you helped me with yesterday. It's shown below in full but I'm getting the following error: Error: Notice: Undefined index: 42.0000 in D:\Intranet v3\Sales\Individual_Area_Summary_by_PAC_Code.php on line 47 I'm guessing that it is trying to resolve the array value but with the wrong variable as 42 is the jan_quantity value? <?php $sql_month="SELECT Pac1, Pac1_v2, SUM(CASE month WHEN '1' THEN SumQuantity ELSE 0 END) AS jan_quantity, SUM(CASE month WHEN '1' THEN SumPrice ELSE 0 END) AS jan_price, SUM(CASE month WHEN '2' THEN SumQuantity ELSE 0 END) AS feb_quantity, SUM(CASE month WHEN '2' THEN SumPrice ELSE 0 END) AS feb_price, SUM(CASE month WHEN '3' THEN SumQuantity ELSE 0 END) AS mar_quantity, SUM(CASE month WHEN '3' THEN SumPrice ELSE 0 END) AS mar_price, SUM(CASE month WHEN '4' THEN SumQuantity ELSE 0 END) AS apr_quantity, SUM(CASE month WHEN '4' THEN SumPrice ELSE 0 END) AS apr_price, SUM(CASE month WHEN '5' THEN SumQuantity ELSE 0 END) AS may_quantity, SUM(CASE month WHEN '5' THEN SumPrice ELSE 0 END) AS may_price, SUM(CASE month WHEN '6' THEN SumQuantity ELSE 0 END) AS jun_quantity, SUM(CASE month WHEN '6' THEN SumPrice ELSE 0 END) AS jun_price, SUM(CASE month WHEN '7' THEN SumQuantity ELSE 0 END) AS jul_quantity, SUM(CASE month WHEN '7' THEN SumPrice ELSE 0 END) AS jul_price, SUM(CASE month WHEN '8' THEN SumQuantity ELSE 0 END) AS aug_quantity, SUM(CASE month WHEN '8' THEN SumPrice ELSE 0 END) AS aug_price, SUM(CASE month WHEN '9' THEN SumQuantity ELSE 0 END) AS sep_quantity, SUM(CASE month WHEN '9' THEN SumPrice ELSE 0 END) AS sep_price, SUM(CASE month WHEN '10' THEN SumQuantity ELSE 0 END) AS oct_quantity, SUM(CASE month WHEN '10' THEN SumPrice ELSE 0 END) AS oct_price, SUM(CASE month WHEN '11' THEN SumQuantity ELSE 0 END) AS nov_quantity, SUM(CASE month WHEN '11' THEN SumPrice ELSE 0 END) AS nov_price, SUM(CASE month WHEN '12' THEN SumQuantity ELSE 0 END) AS dec_quantity, SUM(CASE month WHEN '12' THEN SumPrice ELSE 0 END) AS dec_price, SUM(SumQuantity) as ytdqty, SUM(SumPrice) as ytdprice FROM qry_Sales_by_PAC_Code WHERE Year = '$Year' AND Area = '$id' GROUP BY Pac1, Pac1_v2 ORDER BY Pac1"; $ButtonName = array ( 'A' => "Domestic", 'B' => "Ancillaries", 'N' => "Spares" ); $result = odbc_exec($conn, $sql_month); echo "<Table border='0' cellpadding='4'><tr class='TitleText_White' bgcolor=#FF6600><th>Area: $id</th><th>Jan</th><th>Feb</th><th>Mar</th><th>Apr</th><th>May</th><th>June</th><th>July</th><th>Aug</th><th>Sept</th><th>Oct</th><th>Nov</th><th>Dec</th><th>Y.T.D.</th></tr>"; while (odbc_fetch_into($result, $data)) { $data[] = $ButtonName[$data[2]]; vprintf ("<tr bgcolor='#FFE0CC' class='Body2' align='right'> <form action='PAC_Level_2.php' method='post'><td align='center'> <input name='Year' type='hidden' value='$Year'> <input name='id' type='hidden' value='$id'> <input name='PAC' type='hidden' value='%s'> <input name='Submit' type='submit' value='%s'> </td></form> <td>%0.2f<br>£%0.2f</td><td>%0.2f<br>£%0.2f</td><td>%0.2f<br>£%0.2f</td><td>%0.2f<br>£%0.2f</td> <td>%0.2f<br>£%0.2f</td><td>%0.2f<br>£%0.2f</td><td>%0.2f<br>£%0.2f</td><td>%0.2f<br>£%0.2f</td> <td>%0.2f<br>£%0.2f</td><td>%0.2f<br>£%0.2f</td><td>%0.2f<br>£%0.2f</td><td>%0.2f<br>£%0.2f</td> <td>%0.2f<br>£%0.2f</td></tr>", $data); } echo '</table>'; ?> Could you also explain what this line does : $data[] = $ButtonName[$data[2]]; Quote Link to comment Share on other sites More sharing options...
Fearpig Posted November 7, 2007 Author Share Posted November 7, 2007 OK... you can ignore some of the last post! I've figured out what this does: $data[] = $ButtonName[$data[2]]; But all of the lines are coming back as "Domestic" rather that whatever the array should be to match the PAC Code. Here's my code so far: <?php $sql_month="SELECT Pac1, Pac1_v2, SUM(CASE month WHEN '1' THEN SumQuantity ELSE 0 END) AS jan_quantity, SUM(CASE month WHEN '1' THEN SumPrice ELSE 0 END) AS jan_price, SUM(CASE month WHEN '2' THEN SumQuantity ELSE 0 END) AS feb_quantity, SUM(CASE month WHEN '2' THEN SumPrice ELSE 0 END) AS feb_price, SUM(CASE month WHEN '3' THEN SumQuantity ELSE 0 END) AS mar_quantity, SUM(CASE month WHEN '3' THEN SumPrice ELSE 0 END) AS mar_price, SUM(CASE month WHEN '4' THEN SumQuantity ELSE 0 END) AS apr_quantity, SUM(CASE month WHEN '4' THEN SumPrice ELSE 0 END) AS apr_price, SUM(CASE month WHEN '5' THEN SumQuantity ELSE 0 END) AS may_quantity, SUM(CASE month WHEN '5' THEN SumPrice ELSE 0 END) AS may_price, SUM(CASE month WHEN '6' THEN SumQuantity ELSE 0 END) AS jun_quantity, SUM(CASE month WHEN '6' THEN SumPrice ELSE 0 END) AS jun_price, SUM(CASE month WHEN '7' THEN SumQuantity ELSE 0 END) AS jul_quantity, SUM(CASE month WHEN '7' THEN SumPrice ELSE 0 END) AS jul_price, SUM(CASE month WHEN '8' THEN SumQuantity ELSE 0 END) AS aug_quantity, SUM(CASE month WHEN '8' THEN SumPrice ELSE 0 END) AS aug_price, SUM(CASE month WHEN '9' THEN SumQuantity ELSE 0 END) AS sep_quantity, SUM(CASE month WHEN '9' THEN SumPrice ELSE 0 END) AS sep_price, SUM(CASE month WHEN '10' THEN SumQuantity ELSE 0 END) AS oct_quantity, SUM(CASE month WHEN '10' THEN SumPrice ELSE 0 END) AS oct_price, SUM(CASE month WHEN '11' THEN SumQuantity ELSE 0 END) AS nov_quantity, SUM(CASE month WHEN '11' THEN SumPrice ELSE 0 END) AS nov_price, SUM(CASE month WHEN '12' THEN SumQuantity ELSE 0 END) AS dec_quantity, SUM(CASE month WHEN '12' THEN SumPrice ELSE 0 END) AS dec_price, SUM(SumQuantity) as ytdqty, SUM(SumPrice) as ytdprice FROM qry_Sales_by_PAC_Code WHERE Year = '$Year' AND Area = '$id' GROUP BY Pac1, Pac1_v2 ORDER BY Pac1"; $result = odbc_exec($conn, $sql_month); echo "<Table border='0' cellpadding='4'><tr class='TitleText_White' bgcolor=#FF6600><th>Area: $id</th><th>Jan</th><th>Feb</th><th>Mar</th><th>Apr</th><th>May</th><th>June</th><th>July</th><th>Aug</th><th>Sept</th><th>Oct</th><th>Nov</th><th>Dec</th><th>Y.T.D.</th><th></th></tr>"; while (odbc_fetch_into($result, $data)) { $ButtonName = array ( 'A' => "Domestic", 'B' => "Ancillaries", 'N' => "Spares" ); $data[] = $ButtonName[$data[1]]; vprintf ("<tr bgcolor='#FFE0CC' class='Body2' align='right'> <form action='PAC_Level_2.php' method='post'><td align='center'> <input name='Year' type='hidden' value='$Year'> <input name='id' type='hidden' value='$id'> <input name='PAC' type='hidden' value='%s'> <input name='Submit' type='submit' value='%s'> </td></form> <td>%0.2f<br>£%0.2f</td><td>%0.2f<br>£%0.2f</td><td>%0.2f<br>£%0.2f</td><td>%0.2f<br>£%0.2f</td> <td>%0.2f<br>£%0.2f</td><td>%0.2f<br>£%0.2f</td><td>%0.2f<br>£%0.2f</td><td>%0.2f<br>£%0.2f</td> <td>%0.2f<br>£%0.2f</td><td>%0.2f<br>£%0.2f</td><td>%0.2f<br>£%0.2f</td><td>%0.2f<br>£%0.2f</td> <td>%0.2f<br>£%0.2f</td><td>%s</td></tr>", $data); } echo '</table>'; ?> Can anyone see why it isn't renewing with each line? Cheers. Quote Link to comment Share on other sites More sharing options...
Barand Posted November 7, 2007 Share Posted November 7, 2007 In the example I posted earlier today, I was adding an extra element to the $data array (hence $data[] ). In this cas you need to change an existing data element to "Domestic", say. try $data[1] = $ButtonName[$data[1]]; to convert $data[1] from "N" to "Spares" Quote Link to comment Share on other sites More sharing options...
Fearpig Posted November 8, 2007 Author Share Posted November 8, 2007 Hi Barand, I've followed your code and got the array to print a differnet value for each row of the table but I've come up against yet another issue!! The code below all works without errors but I want the value from the array to appear as the name of the button, at the moment the value is in its own cell at the end of the row. How do I move the code around so that the array shows up as the name on the submit button? <?php $ButtonName = array ( 'A' => "Domestic", 'B' => "Ancillaries", 'N' => "Spares" ); $sql_month="SELECT Pac1, Pac1_v2, SUM(CASE month WHEN '1' THEN SumQuantity ELSE 0 END) AS jan_quantity, SUM(CASE month WHEN '1' THEN SumPrice ELSE 0 END) AS jan_price, SUM(CASE month WHEN '2' THEN SumQuantity ELSE 0 END) AS feb_quantity, SUM(CASE month WHEN '2' THEN SumPrice ELSE 0 END) AS feb_price, SUM(CASE month WHEN '3' THEN SumQuantity ELSE 0 END) AS mar_quantity, SUM(CASE month WHEN '3' THEN SumPrice ELSE 0 END) AS mar_price, SUM(CASE month WHEN '4' THEN SumQuantity ELSE 0 END) AS apr_quantity, SUM(CASE month WHEN '4' THEN SumPrice ELSE 0 END) AS apr_price, SUM(CASE month WHEN '5' THEN SumQuantity ELSE 0 END) AS may_quantity, SUM(CASE month WHEN '5' THEN SumPrice ELSE 0 END) AS may_price, SUM(CASE month WHEN '6' THEN SumQuantity ELSE 0 END) AS jun_quantity, SUM(CASE month WHEN '6' THEN SumPrice ELSE 0 END) AS jun_price, SUM(CASE month WHEN '7' THEN SumQuantity ELSE 0 END) AS jul_quantity, SUM(CASE month WHEN '7' THEN SumPrice ELSE 0 END) AS jul_price, SUM(CASE month WHEN '8' THEN SumQuantity ELSE 0 END) AS aug_quantity, SUM(CASE month WHEN '8' THEN SumPrice ELSE 0 END) AS aug_price, SUM(CASE month WHEN '9' THEN SumQuantity ELSE 0 END) AS sep_quantity, SUM(CASE month WHEN '9' THEN SumPrice ELSE 0 END) AS sep_price, SUM(CASE month WHEN '10' THEN SumQuantity ELSE 0 END) AS oct_quantity, SUM(CASE month WHEN '10' THEN SumPrice ELSE 0 END) AS oct_price, SUM(CASE month WHEN '11' THEN SumQuantity ELSE 0 END) AS nov_quantity, SUM(CASE month WHEN '11' THEN SumPrice ELSE 0 END) AS nov_price, SUM(CASE month WHEN '12' THEN SumQuantity ELSE 0 END) AS dec_quantity, SUM(CASE month WHEN '12' THEN SumPrice ELSE 0 END) AS dec_price, SUM(SumQuantity) as ytdqty, SUM(SumPrice) as ytdprice FROM qry_Sales_by_PAC_Code WHERE Year = '$Year' AND Area = '$id' GROUP BY Pac1, Pac1_v2 ORDER BY Pac1"; $result = odbc_exec($conn, $sql_month); echo "<Table border='0' cellpadding='4'><tr class='TitleText_White' bgcolor=#FF6600><th>Area: $id</th><th>Jan</th><th>Feb</th><th>Mar</th><th>Apr</th><th>May</th><th>June</th><th>July</th><th>Aug</th><th>Sept</th><th>Oct</th><th>Nov</th><th>Dec</th><th>Y.T.D.</th><th></th></tr>"; while (odbc_fetch_into($result, $data)) { $data['A'] = $ButtonName[$data[0]]; vprintf ("<tr bgcolor='#FFE0CC' class='Body2' align='right'> <form action='PAC_Level_2.php' method='post'><td align='center'> <input name='Year' type='hidden' value='$Year'> <input name='id' type='hidden' value='$id'> <input name='PAC' type='hidden' value='%s'> <input name='Submit' type='submit' value='%s'> </td></form> <td>%0.2f<br>£%0.2f</td><td>%0.2f<br>£%0.2f</td><td>%0.2f<br>£%0.2f</td><td>%0.2f<br>£%0.2f</td> <td>%0.2f<br>£%0.2f</td><td>%0.2f<br>£%0.2f</td><td>%0.2f<br>£%0.2f</td><td>%0.2f<br>£%0.2f</td> <td>%0.2f<br>£%0.2f</td><td>%0.2f<br>£%0.2f</td><td>%0.2f<br>£%0.2f</td><td>%0.2f<br>£%0.2f</td> <td>%0.2f<br>£%0.2f</td><td>%s</td></tr>", $data); } echo '</table>'; ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted November 8, 2007 Share Posted November 8, 2007 $data['A'] = $ButtonName[$data[0]]; $data[1] = $ButtonName[$data[0]]; Quote Link to comment Share on other sites More sharing options...
Fearpig Posted November 8, 2007 Author Share Posted November 8, 2007 Sorry Barand... Did you mean just replace the 'A' with 1? If so I now get the following error: Warning: vprintf() [function.vprintf]: Too few arguments in D:\Intranet v3\Sales\Individual_Area_Summary_by_PAC_Code.php on line 64 As it stood with the 'A' value the names printed out correctly but I haven't been able to reorganise the output so that the array value appears as the button name. Is there any way to declare all of the values as variables as oppose to using %s and %0.2f ? Quote Link to comment Share on other sites More sharing options...
Barand Posted November 8, 2007 Share Posted November 8, 2007 I meant to add the second line so the array element holding pac1_v2 (which looks as though it is going into the button name but is otherwise unused in the output) shoud be overwritten with the button name. Quote Link to comment Share on other sites More sharing options...
Fearpig Posted November 8, 2007 Author Share Posted November 8, 2007 Right then..... Sorted it but incase anyone was following this here's what I did: I was getting in a real mess with this so I transferred the query to the MS SQL server (which runs it quicker anyway!) and now I can just handle all of the values as variables. Thanks to Barand (yet again!) for all his help, it would have taken years without him and a couple of days with his help! Here's my final code: <?php $sql_by_PAC = "SELECT * FROM qry_Sales_by_PAC_Code_qry2 WHERE Year = '$Year' AND Area = '$id' ORDER BY Pac1"; $result_by_PAC=odbc_exec($conn,$sql_by_PAC); if (!$result_by_PAC) {exit("Error in SQL");} echo "<Table border='0' cellpadding='4'><tr class='TitleText_White' bgcolor=#FF6600><th>Area: $id</th><th>Jan</th><th>Feb</th><th>Mar</th><th>Apr</th><th>May</th><th>June</th><th>July</th><th>Aug</th><th>Sept</th><th>Oct</th><th>Nov</th><th>Dec</th><th>Y.T.D.</th></tr>"; while (odbc_fetch_row($result_by_PAC)) { $PAC=odbc_result($result_by_PAC,"Pac1"); $jan_quantity=round(odbc_result($result_by_PAC,"jan_quantity")); $jan_price=number_format(odbc_result($result_by_PAC,"jan_price"), 2, '.', ','); $feb_quantity=round(odbc_result($result_by_PAC,"feb_quantity")); $feb_price=number_format(odbc_result($result_by_PAC,"feb_price"), 2, '.', ','); $mar_quantity=round(odbc_result($result_by_PAC,"mar_quantity")); $mar_price=number_format(odbc_result($result_by_PAC,"mar_price"), 2, '.', ','); $apr_quantity=round(odbc_result($result_by_PAC,"apr_quantity")); $apr_price=number_format(odbc_result($result_by_PAC,"apr_price"), 2, '.', ','); $may_quantity=round(odbc_result($result_by_PAC,"may_quantity")); $may_price=number_format(odbc_result($result_by_PAC,"may_price"), 2, '.', ','); $jun_quantity=round(odbc_result($result_by_PAC,"jun_quantity")); $jun_price=number_format(odbc_result($result_by_PAC,"jun_price"), 2, '.', ','); $jul_quantity=round(odbc_result($result_by_PAC,"jul_quantity")); $jul_price=number_format(odbc_result($result_by_PAC,"jul_price"), 2, '.', ','); $aug_quantity=round(odbc_result($result_by_PAC,"aug_quantity")); $aug_price=number_format(odbc_result($result_by_PAC,"aug_price"), 2, '.', ','); $sep_quantity=round(odbc_result($result_by_PAC,"sep_quantity")); $sep_price=number_format(odbc_result($result_by_PAC,"sep_price"), 2, '.', ','); $oct_quantity=round(odbc_result($result_by_PAC,"oct_quantity")); $oct_price=number_format(odbc_result($result_by_PAC,"oct_price"), 2, '.', ','); $nov_quantity=round(odbc_result($result_by_PAC,"jan_quantity")); $nov_price=number_format(odbc_result($result_by_PAC,"jan_price"), 2, '.', ','); $dec_quantity=round(odbc_result($result_by_PAC,"dec_quantity")); $dec_price=number_format(odbc_result($result_by_PAC,"dec_price"), 2, '.', ','); $YTD_quantity=round(odbc_result($result_by_PAC,"ytdqty")); $YTD_price=number_format(odbc_result($result_by_PAC,"ytdprice"), 2, '.', ','); if($PAC == 'A'){$PAC_Desc = 'Domestic';} elseif($PAC == 'B'){$PAC_Desc = 'Ancillaries';} elseif($PAC == 'N'){$PAC_Desc = 'Spares';} else{$PAC_Desc = 'Unknown';} echo "<tr bgcolor='#FFE0CC' class='Body2' align='right'> <form action='PAC_Level_2.php' method='post'><td align='center'> <input name='Year' type='hidden' value='$Year'> <input name='id' type='hidden' value='$id'> <input name='PAC' type='hidden' value='$PAC'> <input name='Submit' type='submit' value='$PAC_Desc'> </td></form> <td>$jan_quantity<br>£$jan_price</td> <td>$feb_quantity<br>£$feb_price</td> <td>$mar_quantity<br>£$mar_price</td> <td>$apr_quantity<br>£$apr_price</td> <td>$may_quantity<br>£$may_price</td> <td>$jun_quantity<br>£$jun_price</td> <td>$jul_quantity<br>£$jul_price</td> <td>$aug_quantity<br>£$aug_price</td> <td>$sep_quantity<br>£$sep_price</td> <td>$oct_quantity<br>£$oct_price</td> <td>$nov_quantity<br>£$nov_price</td> <td>$dec_quantity<br>£$dec_price</td> <td>$YTD_quantity<br>£$YTD_price</td>"; } echo "</table>"; ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted November 8, 2007 Share Posted November 8, 2007 I can't help wondering why you are using ODBC and not http://www.php.net/manual/en/ref.mssql.php Quote Link to comment 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.