Jump to content

[SOLVED] While loop only uses last record


Gorkfu

Recommended Posts

<?php
$sSqlWrk = "SELECT `name` FROM `ingreds` ORDER BY `name` ASC";
$result = mysqli_query($sSqlWrk,$conn) or die("Failed to execute query at line " . __LINE__ . ": " . mysqli_error($conn) . '<br>SQL:' . $sSqlWrk);

// Text to Search Through
$oldtext = str_replace(chr(10), "<br />", $x_ingreds);

while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {

//Read & Print each Row's Data
$wordd = sprintf("%s", $row[name]);

// Words to Search For
$oldwords = ''.$wordd.'';

// Replace Words with Links
$newwords = '<a href="view.php?name='.$wordd.'">'.$wordd.'</a>';

}

// New Text to Display
$newtext = str_replace($oldwords , $newwords , $oldtext);
echo $newtext;

@mysqli_free_result($result);
?>

 

The problem with this code is, once the while loop has finished the $newtext variable only picks up $oldwords and $newwords from the last record. I also tried setting this up with a do then while loop but got the same results.

 

For example:

$old text will grab: "that there this"

$oldwords and $newwords will look in db for current words, if I put the words "that there this" in the db, the while loop only returns the word "this" with a link. If i insert the $newtext variable into the while loop, it will just display that there this 3 times, each with one word linked. I hope this doesn't sound confusing  :-\

 

How could I make this show every word found with a link and not just the last?

Link to comment
https://forums.phpfreaks.com/topic/49707-solved-while-loop-only-uses-last-record/
Share on other sites

You are overwriting your variables within each loop. You need to add them to an array or something similar to use within your str_replace() call:

<?php
$oldwords = array();
$newwords = array();

while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
  //Read & Print each Row's Data
  $wordd = sprintf("%s", $row[name]);

  // Words to Search For
  $oldwords[] = ''.$wordd.'';

  // Replace Words with Links
  $newwords[] = '<a href="view.php?name='.$wordd.'">'.$wordd.'</a>';
}

// New Text to Display
$newtext = str_replace($oldwords , $newwords , $oldtext);
echo $newtext;
?>

 

Good luck.

You are overwriting your variables within each loop. You need to add them to an array or something similar to use within your str_replace() call:

 

That's what I was thinking, I'm having a hard time rapping my brain around it, on how I would go about doing this, maybe some sort of foreach array?

 

or put this bit of code inside the loop

 

That won't work, it'll just show the newtext itself multiple times, for every word found which is not what I want, just as I explained at the end of my original post.

 

Thanks for the advice,  :)

Ok, lol. Ignore my prior post, obsidian's suggestion worked. I was so out of it from lack of sleep, I didn't even notice the code included with his post. ::) Thanks obsidian.

 

LOL... happens to all of us sometimes... Also, take careful note of Barand's post. If you move just the str_replace() line within the loop and then echo after the loop, I think you'll end up with the same result.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.