Problem with while loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Little Spy
Forum Commoner
Posts: 31
Joined: Thu Oct 10, 2002 8:18 pm
Contact:

Problem with while loop

Post by Little Spy »

i thnk there is something wrong with my loop here because it repeats the same thing for the number of items in the database and doesnt move the array counters to set it to the new data is just used the stuff from the first run through of the loop so its not moving up the array counters in $data for some reason

Code: Select all

$query = mysql_query("SELECT * FROM news ORDER BY date DESC");   


while ($data = mysql_fetch_array($query)) {
      // Convert From Unix Time
      $data['date'] = date($datestyle, $data['date']);
      $search = array('<DISPLAY: Subject>', '<DISPLAY: Date>', '<DISPLAY: Poster>', '<DISPLAY: Email>', '<DISPLAY: ComNum>', '<DISPLAY: Content>');
      $replace = array($data['subject'], $data['date'], $data['poster'], $data['email'], $data['comnum'], $data['newstxt']);
      $news_settings['newsstyle'] = str_replace($search, $replace, $news_settings['newsstyle']);
      echo $news_settings['newsstyle'];
   }
}
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

$news_settings['newsstyle'] = str_replace($search, $replace, $news_settings['newsstyle']);
no more '<DISPLAY: Subject>', .... in $news_settings['newsstyle'] after you assigned the result of replacing those patterns to it ;)
Just skip assigning the result to a variable and echo it directly

Code: Select all

echo str_replace($search, $replace, $news_settings['newsstyle']);
Little Spy
Forum Commoner
Posts: 31
Joined: Thu Oct 10, 2002 8:18 pm
Contact:

Post by Little Spy »

just curious so i know for next time why does it work like this

thnx for helping me out btw works great
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

let's take a simple example

Code: Select all

<?php
$text = 'a';
$text = str_replace('a', 'b', $text);
// now $text=='b'
$text = str_replace('a', 'c', $text);
/*
the second str_replace does not find an [i]a[/i] in $text
since it already has been replaced by the first str_replace
and therefor ...
*/
// still $text=='b'
?>
Post Reply