array issue

array issue

am 27.11.2007 17:42:56 von Earl Clare

------=_NextPart_000_0013_01C830E2.47CB7F40
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit

Guys really in need of some assistance here.

I am reading an xml file and want to loop through the entire file and
display the information.

Unfortunately I am only showing 1 item. how can I go about showing all news
item?

I've tried array and loop however I am not versed with any of them.

Thanks in advance





eg.

------------------------------------------------------------ -------------



title 1

Description 1





title 2

Description 2





title 3

Description 3



------------------------------------------------------------ -------------



My current code

+++++++++++++++++++++++++++++++++

function getNews()

{

$file = NEWS_FILE.".xml";

if(!file_exists($file) || filemtime($file) <
time() - 43200) {

$this->data =
@file_get_contents("http://feeds.mydomain.com/dailynews");

$fp = @fopen($file, 'w');

@fwrite($fp, $this->data);

@fclose($fd);

}

else $this->data =
@file_get_contents($file);

if(strlen($this->data) <= 0) return;



// get the location

$attr = explode('<', $this->tag('item'));


$return['title'] = $attr[1];

$return['title'] = substr($return['title'],
6);



$return['description'] = $attr[7];

$return['description'] =
substr($return['description'], 2);



return $return;

}



function view_news()

{



$currentNews = newsfeed::getNews();



$NEWS=
''.$currentNews['title'].'
'.$currentNews['description']
...'
';



return $NEWS;



}



function tag($tag, $skip=0)

{

$start = -1;

for($i = 0; $i <= $skip; $i++)

$start = strpos($this->data,
"<{$tag}", $start + 1);

if($start === false) return false;

$start += strlen($tag) + 1;

$end = strpos($this->data, "",
$start);

if($end === false)

$end = strpos($this->data,
'/>', $start);

return trim(substr($this->data, $start, $end
- $start));

}


------=_NextPart_000_0013_01C830E2.47CB7F40--

Re: array issue

am 27.11.2007 19:33:48 von Jason Gerfen

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Clare Media wrote:
> Guys really in need of some assistance here.
>
> I am reading an xml file and want to loop through the entire file and
> display the information.
>
> Unfortunately I am only showing 1 item. how can I go about showing all news
> item?
>
> I've tried array and loop however I am not versed with any of them.
>
> Thanks in advance
>
>
>
>
>
> eg.
>
> ------------------------------------------------------------ -------------
>
>
>
> title 1
>
> Description 1
>
>

>
>
>
> title 2
>
> Description 2
>
>

>
>
>
> title 3
>
> Description 3
>
>

>
> ------------------------------------------------------------ -------------
>
>
>
> My current code
>
> +++++++++++++++++++++++++++++++++
>
> function getNews()
>
> {
>
> $file = NEWS_FILE.".xml";
>
> if(!file_exists($file) || filemtime($file) <
> time() - 43200) {
>
> $this->data =
> @file_get_contents("http://feeds.mydomain.com/dailynews");
>
> $fp = @fopen($file, 'w');
>
> @fwrite($fp, $this->data);
>
> @fclose($fd);
>
> }
>
> else $this->data =
> @file_get_contents($file);
>
> if(strlen($this->data) <= 0) return;
>
>
Count the elements in $this->data = @file_get_contents( $file );
If more then one then loop else use the code below:

ex:
if( count( $this->data = @file_get_contents( $file ) < 1 ) ) {
foreach( $this->data as $key => $value ) {
// show your titles etc.
}
} else {
>
> // get the location
>
> $attr = explode('<', $this->tag('item'));
>
>
> $return['title'] = $attr[1];
>
> $return['title'] = substr($return['title'],
> 6);
>
>
>
> $return['description'] = $attr[7];
>
> $return['description'] =
> substr($return['description'], 2);
>
>
>
> return $return;
>
> }
>
>
>
> function view_news()
>
> {
>
>
>
> $currentNews = newsfeed::getNews();
>
>
>
> $NEWS=
> ''.$currentNews['title'].'
'.$currentNews['description']
> ..'
';
>
>
>
> return $NEWS;
>
>
>
> }
>
>
>
> function tag($tag, $skip=0)
>
> {
>
> $start = -1;
>
> for($i = 0; $i <= $skip; $i++)
>
> $start = strpos($this->data,
> "<{$tag}", $start + 1);
>
> if($start === false) return false;
>
> $start += strlen($tag) + 1;
>
> $end = strpos($this->data, "",
> $start);
>
> if($end === false)
>
> $end = strpos($this->data,
> '/>', $start);
>
> return trim(substr($this->data, $start, $end
> - $start));
>
> }
>
>


- --
Jason Gerfen

"I practice my religion
while stepping on your
toes..."
~The Ditty Bops
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFHTGMM5vk8bwKVAaIRAhAjAJ9FklveFH1PORVl0HC9nCb+klgcUACe Oren
RgXSIP0bl/bt9rI6g9a/6Uk=
=y9XX
-----END PGP SIGNATURE-----

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php