Jump to content

[SOLVED] if-else or switch inside a vprintf()


Fearpig

Recommended Posts

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.

Link to comment
Share on other sites

<?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)

Link to comment
Share on other sites

;D 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]];

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>';
?>

Link to comment
Share on other sites

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 ?

 

Link to comment
Share on other sites

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>";
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.