Jump to content

echo question


Minase

Recommended Posts

hy there i have the following code

 

<?php
$elements = array (
                'test1:'   => $row->test1,
                'test2:'    => $row->test2,
                'test3:'  => $row->test3,
                'test4:'    => $row->test4,
                'test5:'  => $row->test5
		);
$max = max ($elements);

foreach ($elements as $el => $val)
{
     $val = isset($val) ? $val : 0;
     $max = isset($max) ? $max : 100;
     $maxw = 300;
     $barw = $max ? floor(($maxw-2) * $val / $max) : 0;
     $barh = $h - 2;
     $w = $barw+2;
     $h = 30;
}
?>
<tr><td>Test1</td><td><img src="image.gif" alt="" height="30" width="300"></td>
<?php 
if ( $row->Gold >= $costtest1 ) {
echo '<td><a href="upgrade.php?b=1" target="_top" class="headlines">costs ';
echo number_format($costtest1,0,",",".");
echo ' <img src="gold.gif" alt=gold align="absmiddle" border="0"></a></td></tr>';
} elseif ( $row->Gold < $costtest1 ) {
echo '<td>costs ';
echo number_format($costtest1,0,",",".");
echo ' <img src="gold.gif" alt=gold align="absmiddle" border="0"></td></tr>';
}
?>
<tr><td>Test2</td><td><img src="image.gif" alt="" height="30" width="300"></td>
<?php 
if ( $row->Gold >= $costtest2 ) {
echo '<td><a href="upgrade.php?b=2" target="_top" class="headlines">costs ';
echo number_format($costtest2,0,",",".");
echo ' <img src="gold.gif" alt=gold align="absmiddle" border="0"></a></td></tr>';
} elseif ( $row->Gold < $costtest2 ) {
echo '<td>costs ';
echo number_format($costtest2,0,",",".");
echo ' <img src="gold.gif" alt=gold align="absmiddle" border="0"></td></tr>';
}
?>

 

how can i use echo in foreach syntax ?

i mean its easy to put there something to echo,but i want for every array to have the IF syntax when it echo.how can i do that?

or can i put the $w; variable into 5 separate variables,each one with its value?

without using the loop and to be possible to use later in code?

thank you

Link to comment
https://forums.phpfreaks.com/topic/110979-echo-question/
Share on other sites

ok i do have this script

 

<?php
$elements = array (
                'test1:'   => $row->test1,
                'test2:'    => $row->test2,
                'test3:'  => $row->test3,
                'test4:'    => $row->test4,
                'test5:'  => $row->test5







);
$max = max ($elements);

foreach ($elements as $el => $val)
{
     $val = isset($val) ? $val : 0;
     $max = isset($max) ? $max : 100;
     $maxw = 300;
     $barw = $max ? floor(($maxw-2) * $val / $max) : 0;
     $barh = $h - 2;
     $w = $barw+2;
     $h = 30;
}
?>

in total they are 5 values (from arrays)

the $w variable hold the info that i need,but the problem is that i can echo all  5 values only when i echo into the foreach syntax.

if i want to echo $w variable in another place ,after the foreach end it return just the last value,i want to asign a variable for each value

example:

when i echo $w in foreach ,each $w to be put into a separate variable for using it later in script.

hope this make sense

Link to comment
https://forums.phpfreaks.com/topic/110979-echo-question/#findComment-569394
Share on other sites

Make a new array inside of the foreach with $w assigned at every iteration. 

I.E:

foreach ($elements as $el => $val)
{
     $val = isset($val) ? $val : 0;
     $max = isset($max) ? $max : 100;
     $maxw = 300;
     $barw = $max ? floor(($maxw-2) * $val / $max) : 0;
     $barh = $h - 2;
     $w = $barw+2;
     $h = 30;
     $width[] = $w;
}

 

I'm assuming $w meant width. =P  So now, $width[0] will contain the first width, $width[1] will contain the second one, etc.

Link to comment
https://forums.phpfreaks.com/topic/110979-echo-question/#findComment-569401
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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