Too Too Many Variables - What to do?
01-05-2011, 09:43 AM
|
Too Too Many Variables - What to do?
|
Posts: 128
Name: Chris Kummelstedt
|
Hello Forum!
Thanks for looking at my post!
I am using Drupal (a CMS, but you probably knew that) and when you make templates there you use the following code to display the content of a field.
Code:
$node->field_sa1[0]['view'];
The problem is that I am using them in a function, like this:
Code:
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $sa1)?></td>
and for some reason, the function does not play ball if I use the normal call function so I defined each field as a variable as such:
Code:
$sa1 = $node->field_sa1[0]['view'];
To get thing to work, the problem is that it takes a bloody year to load the page after I did that. Thoughts, suggestions? They would be massively appreciated, as would a nod in the right direction.
To illustrate the problem I included the whole thing below;
Code:
<?php
$sa1 = $node->field_sa1[0]['view'];
$sa2 = $node->field_sa2[0]['view'];
$sa3 = $node->field_sa3[0]['view'];
$sa4 = $node->field_sa4[0]['view'];
$sa5 = $node->field_sa5[0]['view'];
$sa6 = $node->field_sa6[0]['view'];
$sa7 = $node->field_sa7[0]['view'];
$sa8 = $node->field_sa8[0]['view'];
$sa9 = $node->field_sa9[0]['view'];
$sa10 = $node->field_sa10[0]['view'];
$sa11 = $node->field_sa11[0]['view'];
$sa12 = $node->field_sa12[0]['view'];
$na1 = $node->field_na1[0]['view'];
$na2 = $node->field_na2[0]['view'];
$na3 = $node->field_na3[0]['view'];
$na4 = $node->field_na4[0]['view'];
$na5 = $node->field_na5[0]['view'];
$na6 = $node->field_na6[0]['view'];
$na7 = $node->field_na7[0]['view'];
$na8 = $node->field_na8[0]['view'];
$na9 = $node->field_na9[0]['view'];
$na10 = $node->field_na10[0]['view'];
$na11 = $node->field_na11[0]['view'];
$na12 = $node->field_na12[0]['view'];
$ha1 = $node->field_ha1[0]['view'];
$ha2 = $node->field_ha2[0]['view'];
$ha3 = $node->field_ha3[0]['view'];
$ha4 = $node->field_ha4[0]['view'];
$ha5 = $node->field_ha5[0]['view'];
$ha6 = $node->field_ha6[0]['view'];
$ha7 = $node->field_ha7[0]['view'];
$ha8 = $node->field_ha8[0]['view'];
$ha9 = $node->field_ha9[0]['view'];
$ha10 = $node->field_ha10[0]['view'];
$ha11 = $node->field_ha11[0]['view'];
$ha12 = $node->field_ha12[0]['view'];
$pa1 = $node->field_pa1[0]['view'];
$pa2 = $node->field_pa2[0]['view'];
$pa3 = $node->field_pa3[0]['view'];
$pa4 = $node->field_pa4[0]['view'];
$pa5 = $node->field_pa5[0]['view'];
$pa6 = $node->field_pa6[0]['view'];
$pa7 = $node->field_pa7[0]['view'];
$pa8 = $node->field_pa8[0]['view'];
$pa9 = $node->field_pa9[0]['view'];
$pa10 = $node->field_pa10[0]['view'];
$pa11 = $node->field_pa11[0]['view'];
$pa12 = $node->field_pa12[0]['view'];
?>
<tr class='tr_school_3'>
<td class='td_school_1'><strong>Duration</strong></td>
<td class='td_school_y'><strong>None</strong></td>
<td class='td_school_y'><strong>Shared</strong></td>
<td class='td_school_y'><strong>Home Stay</strong></td>
<td class='td_school_y'><strong>Private</strong></td>
</tr>
<tr class='tr_school_2'>
<td class='td_school_x'>1 Month</td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $na1)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $sa1)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $ha1)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $pa1)?></td>
</tr>
<tr class='tr_school_3'>
<td class='td_school_1'>2 Months</td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $na2)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $sa2)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $ha2)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $pa2)?></td>
</tr>
<tr class='tr_school_2'>
<td class='td_school_x'>3 Months</td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $na3)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $sa3)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $ha3)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $pa3)?></td>
</tr>
<tr class='tr_school_3'>
<td class='td_school_1'>4 Months</td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $na4)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $sa4)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $ha4)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $pa4)?></td>
</tr>
<tr class='tr_school_2'>
<td class='td_school_x'>5 Months</td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $na5)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $sa5)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $ha5)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $pa5)?></td>
</tr>
<tr class='tr_school_3'>
<td class='td_school_1'>6 Months</td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $na6)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $sa6)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $ha6)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $pa6)?></td>
</tr>
<tr class='tr_school_2'>
<td class='td_school_x'>7 Months</td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $na7)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $sa7)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $ha7)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $pa7)?></td>
</tr>
<tr class='tr_school_3'>
<td class='td_school_1'>8 Months</td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $na8)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $sa8)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $ha8)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $pa8)?></td>
</tr>
<tr class='tr_school_2'>
<td class='td_school_x'>9 Months</td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $na9)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $sa9)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $ha9)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $pa9)?></td>
</tr>
<tr class='tr_school_3'>
<td class='td_school_1'>10 Months</td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $na10)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $sa10)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $ha10)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $pa10)?></td>
</tr>
<tr class='tr_school_2'>
<td class='td_school_x'>11 Months</td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $na11)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $sa11)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $ha11)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $pa11)?></td>
</tr>
<tr class='tr_school_3'>
<td class='td_school_1'>12 Months</td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $na12)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $sa12)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $ha12)?></td>
<td class='td_school_y'><?php echo display_price($cff , $currency_to, $pa12)?></td>
</tr>
</table>
and here is the function itself:
Code:
<?php
function display_price ($currency_from, $currency_to,$figure ) {
$url = "http://quote.yahoo.com/d/quotes.csv?s="
. $currency_from . $currency_to . "=X"
. "&f=l1&e=.csv";
$open = fopen($url, "r");
$exchange_rate = fread($open, 2000);
fclose($open);
if ($exchange_rate == "0.00")
$exchange_rate = "1";
else
$error = "sorry, we could not determine your location and display the prices in your local area,";
if (empty($figure))
return "<img src='http://www.chinese-language-schools.org/sites/all/themes/alphorn/images/green/0.png' style='height:1em;' title='Sorry, the school does not offer this option.'></img>";
else
return round($exchange_rate*$figure, -1) . ' ' . $currency_to;
}
?>
|
|
|
|
01-05-2011, 07:07 PM
|
Re: Too Too Many Variables - What to do?
|
Posts: 377
Name: Rasim
|
Assigning variables and such do not take years to load. I think that the reason why it takes so long is because you open another URL every time you call a function such as ( http://quote.yahoo.com/d/quotes.csv?...=X&f=l1&e=.csv) . Since you call a function around 50 times, at 1 sec per url request it would take 50 seconds to load. Just to debug, place a current time, including seconds to see how long it takes to process.
echo $current_time;
$url = "http://quote.yahoo.com/d/quotes.csv?s="
. $currency_from . $currency_to . "=X"
. "&f=l1&e=.csv";
$open = fopen($url, "r");
$exchange_rate = fread($open, 2000);
fclose($open);
echo $current_time;
Thats how you can test if this is your main problem, which I think it is... 
|
|
|
|
01-06-2011, 04:29 PM
|
Re: Too Too Many Variables - What to do?
|
Posts: 128
Name: Chris Kummelstedt
|
Thanks, your input was spot on. The page loads like a charm (I redid the function to not include the bit that calls up Yahoo each time.)
Unluckily for me, in the meantime, I managed to break the original function though
I made this test page to try to resolve the matter myself, but having failed for about 2 hours and run out of ideas, I wanted to come back and see if the forum had any feedback.
The function is supposed to display a picture of an X when there is no value and display the value multiplied by the exchange rate if there is a value. It displays the image fine but only displays a 0 when a value exists.
This bit is just so i could test it once without calling on database.
Code:
<?php $pa9 = 2000; ?>
<?php
$cff = CNY;
?>
This bit works (it provides the conversion rate). I can tell it works because when I echo $exchange_rate below it spits back the right answer.
Code:
<?php
$url = "http://quote.yahoo.com/d/quotes.csv?s="
. $cff . $currency_to . "=X"
. "&f=l1&e=.csv";
$open = fopen($url, "r");
$exchange_rate = fread($open, 2000);
fclose($open);
if ($exchange_rate == "0.00")
$exchange_rate = "1";
else
$error = "sorry, we could not determine your location and display the prices in your local area,";
?>
This is the broken part, specifically the bold.
Code:
<?php
function display_price($figure)
{
if (empty($figure))
return "<img src='http://www.chinese-language-schools.org/sites/all/themes/alphorn/images/green/0.png' style='height:1em;' title='Sorry, the school does not offer this option.'></img>";
else
return ($exchange_rate*$figure);}
?>
The bits in italics work, the final bit (the function when the value is not nothing) does not.
Code:
Echo pa9: <?php echo $pa9; ?><br/>
Echo Exchange rate: <?php echo $exchange_rate; ?><br/>
Multiply Pa9 with exchange rate: <?php echo ($pa9*$exchange_rate); ?><br/>
Function (display_price if 0): <?php echo display_price (0); ?> <br/>
Function return (display_price): <?php echo display_price($pa9); ?> <br/>
As the multiplication (<?php echo ($pa9*$exchange_rate); ?>) works just fine I have been playing around with echo, print and return, in hope that this is where the problem lies, but having exhausted the possibilites (I have very little idea of what they actually do, especially the difference between print and echo) I just can't think of what else to do.
Would literally love your feedback,
Naturally no code is being sought specifically, I am not asking for someone to do my work, just a nod/hint in the right direction would be helpful and v. appreciated.
Last edited by Chris_123K; 01-06-2011 at 04:32 PM..
|
|
|
|
01-06-2011, 05:39 PM
|
Re: Too Too Many Variables - What to do?
|
Posts: 377
Name: Rasim
|
It looks like you are passing only 1 value to the function but use 2 inside.
<?php
function display_price($figure)
{
if (empty($figure))
return "<img src='http://www.chinese-language-schools.org/sites/all/themes/alphorn/images/green/0.png' style='height:1em;' title='Sorry, the school does not offer this option.'></img>";
else
return ($exchange_rate*$figure);}
?>
You cannot use the variables defined outside the function inside unless they are global or passed. So you need to change it to:
function display_price($figure, $exchange_rate = 0) { ... } and when calling the function, pass the $exchange_rate along with $figure...
|
|
|
|
01-06-2011, 06:51 PM
|
Re: Too Too Many Variables - What to do?
|
Posts: 128
Name: Chris Kummelstedt
|
thanks alot sir, worked perfectly
|
|
|
|
01-07-2011, 02:02 PM
|
Re: Too Too Many Variables - What to do?
|
Posts: 128
Name: Chris Kummelstedt
|
thanks, really. so happy it works 
|
|
|
|
|
« Reply to Too Too Many Variables - What to do?
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|