Page 1 of 1

Problem with while loop

Posted: Fri Jan 17, 2003 9:39 pm
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'];
   }
}

Posted: Fri Jan 17, 2003 10:33 pm
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']);

Posted: Sat Jan 18, 2003 10:53 am
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

Posted: Sat Jan 18, 2003 12:15 pm
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'
?>