Azerhud
Новичок
Всем доброго времени суток. ООП я только начал изучать. Возник вопрос можно ли ниже приведённый код перевести в ООП код, если да то как? Объясните пожалуйста.
Код1:
Код2:
Код1:
PHP:
<?
// Подключаемся к серверу MySQL
$hostname = 'localhost';
$username = 'root';
$password = '';
$db = mysql_connect($hostname, $username, $password)
or die('connect to database failed');
mysql_query('SET NAMES "utf8"', $db);
mysql_query("set character_set_connection=utf8");
mysql_query("set names utf8");
// Выбираем нужную БД
mysql_select_db('rss')
or die('db not found');
//////////////////////////////////////////////////////////
$url = 'ertre.xml'; //адрес RSS ленты
//Интерпретирует XML-файл в объект
$rss = simplexml_load_file($url);
//цикл для обхода всей RSS ленты и записи в базу
foreach ($rss->channel->item as $item) {
$sql="INSERT INTO `base` (`id`, `title`, `url`, `text`,`data_news`, `images`)
VALUES (NULL , '$item->title', '$item->link','$item->description','$item->pubDate', '$item->images')";
mysql_query($sql) or die(mysql_error ());
}
?>
PHP:
<?php
// Подключаемся к серверу MySQL
$hostname = 'localhost';
$username = 'root';
$password = '';
$db = mysql_connect($hostname, $username, $password)
or die('connect to database failed');
mysql_query('SET NAMES "utf8"', $db);
mysql_query("set character_set_connection=utf8");
mysql_query("set names utf8");
// Выбираем нужную БД
mysql_select_db('rss')
or die('db not found');
//////////////////////////////////////////////////////////
//Выводим данные в таблицу
echo '<table width="100%" border="2" align="center" cellpadding="1" cellspacing="0">
<tr>
<th align="center" valign="top" scope="col">ID</th>
<th align="center" valign="top" scope="col">ЗАГАЛОВОК</th>
<th align="center" valign="top" scope="col">URL</th>
<th align="center" valign="top" scope="col">ТЕКСТ</th>
<th align="center" valign="top" scope="col">ДАТА</th>
<th align="center" valign="top" scope="col">КАРТИНКА</th>
</tr>';
$result=mysql_query('SELECT * FROM `base`');// делаем выборку из таблицы
while($row=mysql_fetch_array($result))// берем результаты из каждой строки
{ echo
'<tr>
<td align="center" valign="top">'.$row['id'].'</td>
<td align="center" valign="top"><h3>'.$row['title'].'</h3></td>
<td align="center" valign="top"><a href="'.$row['url'].'">'.$row['url'].'</a></td>
<td align="center" valign="top">'.$row['text'].'</td>
<td align="center" valign="top">'.$row['data_news'].'</td>
<td align="center" valign="top">'.$row['image'].'</td>
</tr>';
}
?>