Помогите поправить код

Статус
В этой теме нельзя размещать новые ответы.

Scaltro

Гуру форума
Регистрация
15 Сен 2007
Сообщения
1.148
Реакции
79
Код курса валют с сайта центробанка:
Код:
<?php 

$ch = curl_init(); 
if (!$ch) { 
    die("Couldn't initialize a cURL handle"); 
} 

// Ставим переменные CURL 
$ret = curl_setopt($ch, CURLOPT_URL, "http://export.rbc.ru/free/cb.0/free.fcgi?period=DAILY&tickers=null&lastdays=5&separator=%3B&data_format=BROWSER"); 
$ret = curl_setopt($ch, CURLOPT_HEADER, 0); 
$ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$ret = curl_setopt($ch, CURLOPT_TIMEOUT, 5); 
$ret = curl_setopt($ch, CURLOPT_MUTE, 5); 

// Запускаем 
$ret = curl_exec($ch); 

   curl_close($ch); // ...и закрываем 

$ret = explode ("\n", $ret); 

foreach ($ret as $key => $value) { 
   $temp = explode (";", $value); 
   $result[$temp[0]][] = $temp[1]."|".$temp[5]; 
} 

if (!$result['USD'][0]) $error = 1; 

function result_output($result) { 

echo "<table cellpadding=0 cellspacing=2><tr><td colspan=2 align=right style='font-family: Courier New, Arial, Times New Roman; font-size: 11px;'>"; 

$count = count($result['USD']); 
$temp2 = explode ("|", $result['USD'][$count-1]); 
$temp2 = explode ("-", $temp2[0]); 
$date = $temp2[2].".".$temp2[1].".".$temp2[0]; 
echo "<i>"; 
echo $date; 
echo " / "; 
$temp2 = explode ("|", $result['USD'][$count-2]); 
$temp2 = explode ("-", $temp2[0]); 
$date = $temp2[2].".".$temp2[1].".".$temp2[0]; 
echo $date; 

$rate1 = explode ("|", $result['USD'][$count-1]); 
$rate1 = $rate1[1]; 
$rate2 = explode ("|", $result['USD'][$count-2]); 
$rate2 = $rate2[1]; 

echo "</i></td></tr><tr><td><img src='img/ico/pur_z.gif' width='16' height='16' hspace='4'></td><td style='font-family: Courier New, Arial, Times New Roman; font-size: 11px;'>"; 
echo "<b>USD</b>: ".str_pad($rate1, 7, "0"); 
echo " / "; 
echo str_pad($rate2, 7, "0"); 

$temp = round($rate1 - $rate2, 4); 
if ($temp > 0) { 
   $temp = "+".$temp; 
   echo " <img src='img/ico/arrow_g.gif' width='10' height='10'> "; 
   } elseif ($temp == 0) { 
   echo " <img src='img/ico/arrow_g.gif' width='10' height='10'> "; 
   $temp = "0.0"; 
   } else { 
   echo " <img src='img/ico/arrow_r.gif' width='10' height='10'> "; 
} 
$temp = str_pad($temp, 7, "0"); 
echo "<i>".$temp."</i>"; 

$rate1 = explode ("|", $result['EUR'][$count-1]); 
$rate1 = $rate1[1]; 
$rate2 = explode ("|", $result['EUR'][$count-2]); 
$rate2 = $rate2[1]; 

echo "</td></tr><tr><td><img src='img/ico/pur_e.gif' width='16' height='16' hspace='4'></td><td style='font-family: Courier New, Arial, Times New Roman; font-size: 11px;'>"; 
echo "<b>EUR</b>: ".str_pad($rate1, 7, "0"); 
echo " / "; 
echo str_pad($rate2, 7, "0"); 

$temp = round($rate1 - $rate2, 4); 
if ($temp > 0) { 
   $temp = "+".$temp; 
   echo " <img src='img/ico/arrow_g.gif' width='10' height='10'> "; 
   } elseif ($temp == 0) { 
   echo " <img src='img/ico/arrow_g.gif' width='10' height='10'> "; 
   $temp = "0.0"; 
   } else { 
   echo " <img src='img/ico/arrow_r.gif' width='10' height='10'> "; 
} 
$temp = str_pad($temp, 7, "0"); 
echo "<i>".$temp."</i>"; 

$rate1 = explode ("|", $result['UAH'][$count-1]); 
$rate1 = $rate1[1]; 
$rate2 = explode ("|", $result['UAH'][$count-2]); 
$rate2 = $rate2[1]; 

echo "</td></tr><tr><td><img src='img/ico/pur_u.gif' width='16' height='16' hspace='4'></td><td style='font-family: Courier New, Arial, Times New Roman; font-size: 11px;'>"; 
echo "<b>UAH</b>: ".str_pad($rate1, 7, "0"); 
echo " / "; 
echo str_pad($rate2, 7, "0"); 

$temp = round($rate1 - $rate2, 4); 
if ($temp > 0) { 
   $temp = "+".$temp; 
   echo " <img src='img/ico/arrow_g.gif' width='10' height='10'> "; 
   } elseif ($temp == 0) { 
   echo " <img src='img/ico/arrow_g.gif' width='10' height='10'> "; 
   $temp = "0.0"; 
   } else { 
   echo " <img src='img/ico/arrow_r.gif' width='10' height='10'> "; 
} 
$temp = str_pad($temp, 7, "0"); 
echo "<i>".$temp."</i></td></tr></table>"; 

} 

if (!$error) result_output($result); 

?>
Вставляю его в mod_php в joostina и при отображении выдаёт:
Код:
Notice: Undefined offset: 1 in /tmp/htmlZohOty on line 22 

Notice: Undefined offset: 5 in /tmp/htmlZohOty on line 22 

Notice: Undefined variable: error in /tmp/htmlZohOty on line 114
И потом только таблицу курсов, помогите избавиться от этих ошибочек :bc:
 
По последней ошибке:
Надо вставить в самом начале
$error=0;

А две первых похоже указывают на строку
$ret = curl_exec($ch);
Можно попробовать записать ее так:
$ret = @curl_exec($ch);
 
После этих манипуляций вылезло следующее:
Код:
Notice: Undefined offset: 1 in /tmp/html32JHab on line 17
Notice: Undefined offset: 5 in /tmp/html32JHab on line 17
 
Точно строку не могу определить, т.к. не приведен код всей страницы.
Попробуйте еще
$ret = @explode ("\n", $ret);
 
Блиня, теперь:
Код:
Notice: Undefined offset: 1 in /tmp/htmlYSpaPx on line 17
Notice: Undefined offset: 5 in /tmp/htmlYSpaPx on line 17
:confused:
 
На Notice можно и не обращать-то внимания) Легче их просто отключить. Но если Вам принципиально...

Проблема, видимо, в этой строке
PHP:
$result[$temp[0]][] = $temp[1]."|".$temp[5];

Ошибка возникает, когда Вы обращаетесь к несуществующему элементу массива.
Попробуйте перед это строчкой добавить
PHP:
if (count($temp) >5)
 
В таком случае курсы вобще не отображаются и выдаёт такую строку:
Код:
Notice: Undefined index: USD in /tmp/htmlahZnrO on line 20
 
Покажи код, который получился
 
После всех вышеперечисленных манипуляций получилось следующее:
Код:
<?php   
$error=0;
$ch = curl_init(); 
if (!$ch) { 
    die("Couldn't initialize a cURL handle"); 
} 
// Ставим переменные CURL 
$ret = curl_setopt($ch, CURLOPT_URL, "http://export.rbc.ru/free/cb.0/free.fcgi?period=DAILY&tickers=null&lastdays=5&separator=%3B&data_format=BROWSER"); 
$ret = curl_setopt($ch, CURLOPT_HEADER, 0); 
$ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$ret = curl_setopt($ch, CURLOPT_TIMEOUT, 5); 
$ret = curl_setopt($ch, CURLOPT_MUTE, 5); 
// Запускаем 
$ret = @curl_exec($ch); 
   curl_close($ch); // ...и закрываем 
$ret = @explode ("\n", $ret); 
foreach ($ret as $key => $value) { 
   $temp = explode (";", $value);
   if (count($temp) >5) 
   $result[$temp[0]][] = $temp[1]."|".$temp[5]; 
} 
if (!$result['USD'][0]) $error = 1; 
function result_output($result) { 
echo "<table cellpadding=0 cellspacing=2><tr><td colspan=2 align=right style='font-family: Courier New, Arial, Times New Roman; font-size: 11px;'>"; 
$count = count($result['USD']); 
$temp2 = explode ("|", $result['USD'][$count-1]); 
$temp2 = explode ("-", $temp2[0]); 
$date = $temp2[2].".".$temp2[1].".".$temp2[0]; 
echo "<i>"; 
echo $date; 
echo " / "; 
$temp2 = explode ("|", $result['USD'][$count-2]); 
$temp2 = explode ("-", $temp2[0]); 
$date = $temp2[2].".".$temp2[1].".".$temp2[0]; 
echo $date; 
$rate1 = explode ("|", $result['USD'][$count-1]); 
$rate1 = $rate1[1]; 
$rate2 = explode ("|", $result['USD'][$count-2]); 
$rate2 = $rate2[1]; 
echo "</i></td></tr><tr><td><img src='img/ico/pur_z.gif' width='16' height='16' hspace='4'></td><td style='font-family: Courier New, Arial, Times New Roman; font-size: 11px;'>"; 
echo "<b>USD</b>: ".str_pad($rate1, 7, "0"); 
echo " / "; 
echo str_pad($rate2, 7, "0"); 
$temp = round($rate1 - $rate2, 4); 
if ($temp > 0) { 
   $temp = "+".$temp; 
   echo " <img src='img/ico/arrow_g.gif' width='10' height='10'> "; 
   } elseif ($temp == 0) { 
   echo " <img src='img/ico/arrow_g.gif' width='10' height='10'> "; 
   $temp = "0.0"; 
   } else { 
   echo " <img src='img/ico/arrow_r.gif' width='10' height='10'> "; 
} 
$temp = str_pad($temp, 7, "0"); 
echo "<i>".$temp."</i>"; 
$rate1 = explode ("|", $result['EUR'][$count-1]); 
$rate1 = $rate1[1]; 
$rate2 = explode ("|", $result['EUR'][$count-2]); 
$rate2 = $rate2[1]; 
echo "</td></tr><tr><td><img src='img/ico/pur_e.gif' width='16' height='16' hspace='4'></td><td style='font-family: Courier New, Arial, Times New Roman; font-size: 11px;'>"; 
echo "<b>EUR</b>: ".str_pad($rate1, 7, "0"); 
echo " / "; 
echo str_pad($rate2, 7, "0"); 
$temp = round($rate1 - $rate2, 4); 
if ($temp > 0) { 
   $temp = "+".$temp; 
   echo " <img src='img/ico/arrow_g.gif' width='10' height='10'> "; 
   } elseif ($temp == 0) { 
   echo " <img src='img/ico/arrow_g.gif' width='10' height='10'> "; 
   $temp = "0.0"; 
   } else { 
   echo " <img src='img/ico/arrow_r.gif' width='10' height='10'> "; 
} 
$temp = str_pad($temp, 7, "0"); 
echo "<i>".$temp."</i>"; 
$rate1 = explode ("|", $result['UAH'][$count-1]); 
$rate1 = $rate1[1]; 
$rate2 = explode ("|", $result['UAH'][$count-2]); 
$rate2 = $rate2[1]; 
echo "</td></tr><tr><td><img src='img/ico/pur_u.gif' width='16' height='16' hspace='4'></td><td style='font-family: Courier New, Arial, Times New Roman; font-size: 11px;'>"; 
echo "<b>UAH</b>: ".str_pad($rate1, 7, "0"); 
echo " / "; 
echo str_pad($rate2, 7, "0"); 
$temp = round($rate1 - $rate2, 4); 
if ($temp > 0) { 
   $temp = "+".$temp; 
   echo " <img src='img/ico/arrow_g.gif' width='10' height='10'> "; 
   } elseif ($temp == 0) { 
   echo " <img src='img/ico/arrow_g.gif' width='10' height='10'> "; 
   $temp = "0.0"; 
   } else { 
   echo " <img src='img/ico/arrow_r.gif' width='10' height='10'> "; 
} 
$temp = str_pad($temp, 7, "0"); 
echo "<i>".$temp."</i></td></tr></table>"; 
} 
if (!$error) result_output($result); 
?>
 
Хм.. У меня работает, выдает такое
Код:
24.03.2010 / 23.03.2010
USD: 29.4707 / 29.3389 +0.1318
EUR: 39.8238 / 39.6339 +0.1899
UAH: 37.0934 / 36.8556 +0.2378
 
Статус
В этой теме нельзя размещать новые ответы.
Назад
Сверху